Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoKart_HS18/src/modules/overlays/OverlayText.cc @ 12412

Last change on this file since 12412 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

  • Property svn:eol-style set to native
File size: 5.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 "OverlayText.h"
30
31#if OGRE_VERSION >= 0x010900
32#   include <Overlay/OgreOverlayManager.h>
33#   include <Overlay/OgrePanelOverlayElement.h>
34#   include <Overlay/OgreTextAreaOverlayElement.h>
35#else
36#   include <OgreOverlayManager.h>
37#   include <OgrePanelOverlayElement.h>
38#   include <OgreTextAreaOverlayElement.h>
39#endif
40
41#include "util/StringUtils.h"
42#include "core/CoreIncludes.h"
43#include "core/XMLPort.h"
44
45
46namespace orxonox
47{
48    RegisterClass(OverlayText);
49
50    static_assert((int)Ogre::TextAreaOverlayElement::Left   == (int)OverlayText::Alignment::Left,   "check enum");
51    static_assert((int)Ogre::TextAreaOverlayElement::Center == (int)OverlayText::Alignment::Center, "check enum");
52    static_assert((int)Ogre::TextAreaOverlayElement::Right  == (int)OverlayText::Alignment::Right,  "check enum");
53
54    OverlayText::OverlayText(Context* context)
55        : OrxonoxOverlay(context)
56    {
57        RegisterObject(OverlayText);
58
59        this->text_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton()
60            .createOverlayElement("TextArea", "OverlayText_text_" + getUniqueNumberString()));
61        this->text_->setCharHeight(1.0);
62
63        this->setFont("Monofur");
64        this->setColour(ColourValue(1.0, 1.0, 1.0, 1.0));
65        this->setCaption("");
66        this->setTextSize(1.0f);
67        this->setAlignmentString("left");
68
69        this->background_->addChild(this->text_);
70    }
71
72    OverlayText::~OverlayText()
73    {
74        if (this->isInitialized())
75            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->text_);
76    }
77
78    void OverlayText::XMLPort(Element& xmlelement, XMLPort::Mode mode)
79    {
80        SUPER(OverlayText, XMLPort, xmlelement, mode);
81
82        XMLPortParam(OverlayText, "font",       setFont,            getFont,            xmlelement, mode);
83        XMLPortParam(OverlayText, "colour",     setColour,          getColour,          xmlelement, mode);
84        XMLPortParam(OverlayText, "caption",    setCaption,         getCaption,         xmlelement, mode);
85        XMLPortParam(OverlayText, "textsize",   setTextSize,        getTextSize,        xmlelement, mode);
86        XMLPortParam(OverlayText, "align",      setAlignmentString, getAlignmentString, xmlelement, mode);
87        XMLPortParam(OverlayText, "spacewidth", setSpaceWidth,      getSpaceWidth,      xmlelement, mode);
88    }
89
90    void OverlayText::setAlignmentString(const std::string& alignment)
91    {
92        if (alignment == "right")
93            this->setAlignment(OverlayText::Alignment::Right);
94        else if (alignment == "center")
95            this->setAlignment(OverlayText::Alignment::Center);
96        else // "left" and default
97            this->setAlignment(OverlayText::Alignment::Left);
98    }
99
100    std::string OverlayText::getAlignmentString() const
101    {
102        Ogre::TextAreaOverlayElement::Alignment alignment = this->text_->getAlignment();
103
104        switch (alignment)
105        {
106            case Ogre::TextAreaOverlayElement::Right:
107                return "right";
108            case Ogre::TextAreaOverlayElement::Center:
109                return "center";
110            case Ogre::TextAreaOverlayElement::Left:
111                return "left";
112            default:
113                assert(false); return "";
114        }
115    }
116
117    void OverlayText::sizeChanged()
118    {
119        if (this->rotState_ == RotationState::Horizontal)
120            this->overlay_->setScale(size_.y * sizeCorrection_.y, size_.y * sizeCorrection_.y);
121        else if (this->rotState_ == RotationState::Vertical)
122            this->overlay_->setScale(size_.y / (sizeCorrection_.y * sizeCorrection_.y), size_.y * sizeCorrection_.y);
123        else
124            this->overlay_->setScale(size_.y, size_.y);
125
126        positionChanged();
127    }
128
129    void OverlayText::setCaption(const std::string& caption)
130    {
131        this->text_->setCaption(caption);
132        this->changedCaption();
133    }
134    std::string OverlayText::getCaption() const
135    {
136        return this->text_->getCaption();
137    }
138
139    void OverlayText::setFont(const std::string& font)
140    {
141        if (!font.empty())
142            this->text_->setFontName(font);
143    }
144    const std::string& OverlayText::getFont() const
145    {
146        return this->text_->getFontName();
147    }
148
149    void OverlayText::setSpaceWidth(float width)
150    {
151        this->text_->setSpaceWidth(width);
152    }
153    float OverlayText::getSpaceWidth() const
154    {
155        return this->text_->getSpaceWidth();
156    }
157
158    void OverlayText::setColour(const ColourValue& colour)
159    {
160        this->text_->setColour(colour); this->changedColour();
161    }
162    const ColourValue& OverlayText::getColour() const
163    {
164        return this->text_->getColour();
165    }
166
167    void OverlayText::setAlignment(OverlayText::Alignment alignment)
168    {
169        this->text_->setAlignment(static_cast<Ogre::TextAreaOverlayElement::Alignment>(alignment));
170    }
171    OverlayText::Alignment OverlayText::getAlignment() const
172    {
173        return static_cast<OverlayText::Alignment>(this->text_->getAlignment());
174    }
175}
Note: See TracBrowser for help on using the repository browser.