Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/overlays/OrxonoxOverlay.cc @ 1615

Last change on this file since 1615 was 1615, checked in by rgrieder, 16 years ago
  • added blankString to String so you can return ""; even if it's a const std::string&
  • fixed several bugs with aspect correct and margin alignment
  • added console commands for OrxonoxOverlays and OverlayGroups for rotate, scale and scroll (you can access the by name (from name=.. in xml file), e.g. "OrxonoxOverlay rotateOverlay SpeedBar 90)
  • converted everything in overlays/ to 4 spaces/tab ;)
  • removed all using namespace Ogre;
  • added background_ Panel to OrxonoxOverlay, since most of the derived classes can use that
  • should work now, but I'll have to test on a tardis box first
  • Property svn:eol-style set to native
File size: 8.2 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 "OrxonoxOverlay.h"
31
32#include <cmath>
33#include <OgreOverlayManager.h>
34#include <OgrePanelOverlayElement.h>
35#include "util/Convert.h"
36#include "util/String.h"
37#include "core/CoreIncludes.h"
38#include "GraphicsEngine.h"
39
40namespace orxonox
41{
42    unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0;
43    std::map<std::string, OrxonoxOverlay*> OrxonoxOverlay::overlays_s;
44
45    SetConsoleCommand(OrxonoxOverlay, scaleOverlay, false).setAccessLevel(AccessLevel::User);
46    SetConsoleCommand(OrxonoxOverlay, scrollOverlay, false).setAccessLevel(AccessLevel::User);
47    SetConsoleCommand(OrxonoxOverlay, rotateOverlay, false).setAccessLevel(AccessLevel::User);
48
49    OrxonoxOverlay::OrxonoxOverlay()
50        : overlay_(0)
51        , background_(0)
52        , windowAspectRatio_(1.0f)
53        , bCorrectAspect_(false)
54        , size_(1.0f, 1.0f)
55        , sizeCorrection_(1.0f, 1.0f)
56        , angle_(0.0f)
57        , position_(0.0f, 0.0f)
58        , origin_(0.0f, 0.0f)
59    {
60        RegisterObject(OrxonoxOverlay);
61    }
62
63    OrxonoxOverlay::~OrxonoxOverlay()
64    {
65        if (this->background_)
66            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
67
68        std::map<std::string, OrxonoxOverlay*>::iterator it = overlays_s.find(this->getName());
69        if (it != overlays_s.end())
70            overlays_s.erase(it);
71    }
72
73    void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
74    {
75        BaseObject::XMLPort(xmlElement, mode);
76
77        if (mode == XMLPort::LoadObject)
78        {
79            if (overlays_s.find(this->getName()) != overlays_s.end())
80            {
81                COUT(1) << "Overlay names should be unique or you cannnot access them via console." << std::endl;
82            }
83            overlays_s[this->getName()] = this;
84
85            overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_"
86                + convertToString(hudOverlayCounter_s++));
87
88            // create background
89            this->background_ = static_cast<Ogre::PanelOverlayElement*>(
90                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel",
91                "OrxonoxOverlay_background_" + convertToString(hudOverlayCounter_s++)));
92            this->overlay_->add2D(this->background_);
93
94            this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
95                GraphicsEngine::getSingleton().getWindowHeight());
96        }
97
98        XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode);
99        XMLPortParam(OrxonoxOverlay, "size", setSize, getUncorrectedSize, xmlElement, mode);
100        XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode);
101        XMLPortParam(OrxonoxOverlay, "origin", setOrigin, getOrigin, xmlElement, mode);
102        XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode);
103        XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode);
104
105        if (mode == XMLPort::LoadObject)
106        {
107            this->overlay_->show();
108            if (!this->isVisible())
109                this->overlay_->hide();
110
111            this->sizeChanged();
112            this->positionChanged();
113            this->angleChanged();
114        }
115    }
116
117    void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
118    {
119        if (this->background_ && material != "")
120            this->background_->setMaterialName(material);
121    }
122
123    const std::string& OrxonoxOverlay::getBackgroundMaterial() const
124    {
125        if (this->background_)
126            return this->background_->getMaterialName();
127        else
128            return blankString;
129    }
130
131    void OrxonoxOverlay::changedVisibility()
132    {
133        if (this->overlay_)
134        {
135            if (this->isVisible())
136                this->overlay_->show();
137            else
138                this->overlay_->hide();
139        }
140    }
141
142    void OrxonoxOverlay::windowResized(int newWidth, int newHeight)
143    {
144        this->windowAspectRatio_ = newWidth/(float)newHeight;
145
146        this->setAspectCorrection(this->bCorrectAspect_);
147    }
148
149    void OrxonoxOverlay::setAspectCorrection(bool val)
150    {
151        this->bCorrectAspect_ = val;
152        this->sizeCorrectionChanged();
153    }
154
155    void OrxonoxOverlay::sizeCorrectionChanged()
156    {
157        if (this->bCorrectAspect_)
158        {
159            float angle = this->angle_.valueDegrees();
160            float tempAspect;
161            if (angle > 89.0 && angle < 91.0 || angle > 269 && angle < 271)
162                tempAspect = 1.0 / this->windowAspectRatio_;
163            else if (angle > 359 && angle < 1 || angle > 179 && angle < 181)
164                tempAspect = this->windowAspectRatio_;
165            else
166                tempAspect = 1.0;
167
168            // note: this is only an approximation that is mostly valid when the
169            // magnitude of the width is about the magnitude of the height.
170            // Correctly we would have to take the square root of width*height
171            this->sizeCorrection_.x = 2.0 / (tempAspect + 1.0);
172            this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x;
173        }
174        else
175        {
176            this->sizeCorrection_ = Vector2::UNIT_SCALE;
177        }
178        this->sizeChanged();
179    }
180
181    /**
182    @remarks
183        This function can be overriden by any derivative.
184    */
185    void OrxonoxOverlay::sizeChanged()
186    {
187        this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
188        positionChanged();
189    }
190
191    /**
192    @remarks
193        This function can be overriden by any derivative.
194    */
195    void OrxonoxOverlay::angleChanged()
196    {
197        this->overlay_->setRotate(this->angle_);
198        this->sizeCorrectionChanged();
199    }
200
201    /**
202    @remarks
203        This function can be overriden by any derivative.
204    */
205    void OrxonoxOverlay::positionChanged()
206    {
207        float angle = abs(this->angle_.valueRadians());
208        angle -= Ogre::Math::PI * (int)(angle / (Ogre::Math::PI));
209        if (angle > Ogre::Math::PI * 0.5)
210            angle = Ogre::Math::PI - angle;
211        Vector2 actualSize = size_ * sizeCorrection_;
212        float radius = actualSize.length();
213        float phi = atan(actualSize.y / actualSize.x);
214        Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi));
215        Vector2 scroll = (position_ - 0.5 - boundingBox * (origin_ - 0.5)) * 2.0;
216        this->overlay_->setScroll(scroll.x, -scroll.y);
217    }
218
219
220    /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale)
221    {
222        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
223        if (it != overlays_s.end())
224            (*it).second->scale(Vector2(scale, scale));
225    }
226
227    /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll)
228    {
229        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
230        if (it != overlays_s.end())
231            (*it).second->scroll(scroll);
232    }
233
234    /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle)
235    {
236        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
237        if (it != overlays_s.end())
238            (*it).second->rotate(angle);
239    }
240}
Note: See TracBrowser for help on using the repository browser.