Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/overlays/OverlayText.cc @ 2014

Last change on this file since 2014 was 1993, checked in by landauf, 17 years ago

added several new classes

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "OverlayText.h"
31
32#include <OgreOverlayManager.h>
33#include <OgrePanelOverlayElement.h>
34
35#include "util/String.h"
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38
39namespace orxonox
40{
41    CreateFactory(OverlayText);
42
43    OverlayText::OverlayText()
44        : text_(0)
45    {
46        RegisterObject(OverlayText);
47    }
48
49    OverlayText::~OverlayText()
50    {
51        if (this->text_)
52            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->text_);
53    }
54
55    void OverlayText::XMLPort(Element& xmlElement, XMLPort::Mode mode)
56    {
57        SUPER(OverlayText, XMLPort, xmlElement, mode);
58
59        if (mode == XMLPort::LoadObject)
60        {
61            this->text_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
62                .createOverlayElement("TextArea", "OverlayText_text_" + getUniqueNumberStr()));
63            this->text_->setCharHeight(1.0);
64
65            this->background_->addChild(this->text_);
66        }
67
68        XMLPortParam(OverlayText, "font",     setFont,     getFont,     xmlElement, mode).defaultValues("Monofur");
69        XMLPortParam(OverlayText, "colour",   setColour,   getColour,   xmlElement, mode).defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0));
70        XMLPortParam(OverlayText, "caption",  setCaption,  getCaption,  xmlElement, mode).defaultValues("");
71        XMLPortParam(OverlayText, "textSize", setTextSize, getTextSize, xmlElement, mode).defaultValues(1.0f);
72        XMLPortParamTemplate(OverlayText, "align", setAlignment, getAlignment, xmlElement, mode, const std::string&).defaultValues("left");
73    }
74
75    void OverlayText::setFont(const std::string& font)
76    {
77        if (this->text_ && font != "")
78            this->text_->setFontName(font);
79    }
80
81    const std::string& OverlayText::getFont() const
82    {
83        if (this->text_)
84            return this->text_->getFontName();
85        else
86            return blankString;
87    }
88
89    void OverlayText::setColour(const ColourValue& colour)
90    {
91        if (this->text_)
92            this->text_->setColour(colour);
93    }
94
95    const ColourValue& OverlayText::getColour() const
96    {
97        if (this->text_)
98            return this->text_->getColour();
99        else
100            return ColourValue::White;
101    }
102
103    void OverlayText::setAlignment(Ogre::TextAreaOverlayElement::Alignment alignment)
104    {
105        if (this->text_)
106            this->text_->setAlignment(alignment);
107    }
108
109    void OverlayText::setAlignment(const std::string& alignment)
110    {
111        if (alignment == "right")
112            this->setAlignment(Ogre::TextAreaOverlayElement::Right);
113        else if (alignment == "center")
114            this->setAlignment(Ogre::TextAreaOverlayElement::Center);
115        else // "left" and default
116            this->setAlignment(Ogre::TextAreaOverlayElement::Left);
117    }
118
119    std::string OverlayText::getAlignment() const
120    {
121        if (this->text_)
122        {
123            Ogre::TextAreaOverlayElement::Alignment alignment = this->text_->getAlignment();
124
125            switch (alignment)
126            {
127                case Ogre::TextAreaOverlayElement::Right:
128                    return "right";
129                case Ogre::TextAreaOverlayElement::Center:
130                    return "center";
131                case Ogre::TextAreaOverlayElement::Left:
132                default:;
133            }
134        }
135        return "left";
136    }
137
138    void OverlayText::setCaption(const std::string& caption)
139    {
140        this->caption_ = caption;
141        this->text_->setCaption(caption);
142    }
143
144    void OverlayText::sizeChanged()
145    {
146        if (!this->overlay_)
147            return;
148
149        if (this->rotState_ == Horizontal)
150            this->overlay_->setScale(size_.y * sizeCorrection_.y, size_.y * sizeCorrection_.y);
151        else if (this->rotState_ == Vertical)
152            this->overlay_->setScale(size_.y / (sizeCorrection_.y * sizeCorrection_.y), size_.y * sizeCorrection_.y);
153        else
154            this->overlay_->setScale(size_.y, size_.y);
155
156        positionChanged();
157    }
158}
Note: See TracBrowser for help on using the repository browser.