Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/OrxonoxOverlay.cc @ 1627

Last change on this file since 1627 was 1627, checked in by rgrieder, 16 years ago

some adjustment to the default value setting in the overlay files

  • Property svn:eol-style set to native
File size: 11.0 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/**
30@file
31@brief Definition of the OrxonoxOverlay class.
32*/
33
34#include "OrxonoxStableHeaders.h"
35#include "OrxonoxOverlay.h"
36
37#include <cmath>
38#include <OgreOverlay.h>
39#include <OgreOverlayManager.h>
40#include <OgrePanelOverlayElement.h>
41#include "util/Convert.h"
42#include "util/String.h"
43#include "core/CoreIncludes.h"
44#include "core/XMLPort.h"
45#include "core/ConsoleCommand.h"
46#include "GraphicsEngine.h"
47
48namespace orxonox
49{
50    unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0;
51    std::map<std::string, OrxonoxOverlay*> OrxonoxOverlay::overlays_s;
52
53    SetConsoleCommand(OrxonoxOverlay, scaleOverlay, false).setAccessLevel(AccessLevel::User);
54    SetConsoleCommand(OrxonoxOverlay, scrollOverlay, false).setAccessLevel(AccessLevel::User);
55    SetConsoleCommand(OrxonoxOverlay, rotateOverlay, false).setAccessLevel(AccessLevel::User);
56
57    OrxonoxOverlay::OrxonoxOverlay()
58        : overlay_(0)
59        , background_(0)
60    {
61        RegisterObject(OrxonoxOverlay);
62    }
63
64    /**
65    @brief
66        Make sure everything gets removed/destroyed.
67    @remark
68        We assume that the Ogre::OverlayManager exists (there is an assert in Ogre for that!)
69    */
70    OrxonoxOverlay::~OrxonoxOverlay()
71    {
72        // erase ourself from the map with all overlays
73        std::map<std::string, OrxonoxOverlay*>::iterator it = overlays_s.find(this->getName());
74        if (it != overlays_s.end())
75            overlays_s.erase(it);
76
77        if (this->background_)
78            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
79        if (this->overlay_)
80            Ogre::OverlayManager::getSingleton().destroy(this->overlay_);
81    }
82
83    /**
84    @brief
85        Loads the OrxonoxOverlay.
86       
87        This has to be called before usage, otherwise strange behaviour is
88        guaranteed! (there should be no segfaults however).
89    @copydoc
90        BaseObject::XMLPort()
91    */
92    void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
93    {
94        BaseObject::XMLPort(xmlElement, mode);
95
96        if (mode == XMLPort::LoadObject)
97        {
98            // add this overlay to the static map of OrxonoxOverlays
99            if (overlays_s.find(this->getName()) != overlays_s.end())
100            {
101                COUT(1) << "Overlay names should be unique or you cannnot access them via console." << std::endl;
102            }
103            overlays_s[this->getName()] = this;
104
105            // create the Ogre::Overlay
106            overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_"
107                + convertToString(hudOverlayCounter_s++));
108
109            // create background panel (can be used to show any picture)
110            this->background_ = static_cast<Ogre::PanelOverlayElement*>(
111                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel",
112                "OrxonoxOverlay_background_" + convertToString(hudOverlayCounter_s++)));
113            this->overlay_->add2D(this->background_);
114
115            // We'll have to get the aspect ratio manually for the first time. Afterwards windowResized() gets
116            // called automatically by the GraphicsEngine.
117            this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
118                GraphicsEngine::getSingleton().getWindowHeight());
119
120            this->changedVisibility();
121
122            this->setSize(Vector2(0.5f, 0.5f));
123            this->setPickPoint(Vector2(0.5f, 0.5f));
124            this->setPosition(Vector2(0.5f, 0.5f));
125            this->setRotation(Degree(0.0f));
126            this->setAspectCorrection(true);
127            this->setBackgroundMaterial("");
128        }
129
130        XMLPortParam(OrxonoxOverlay, "size",      setSize,      getSize,      xmlElement, mode)
131            .defaultValues(Vector2(0.5f, 0.5f));
132        XMLPortParam(OrxonoxOverlay, "pickPoint", setPickPoint, getPickPoint, xmlElement, mode)
133            .defaultValues(Vector2(0.0f, 0.0f));
134        XMLPortParam(OrxonoxOverlay, "position",  setPosition,  getPosition,  xmlElement, mode)
135            .defaultValues(Vector2(0.0f, 0.0f));
136        XMLPortParam(OrxonoxOverlay, "rotation",  setRotation,  getRotation,  xmlElement, mode)
137            .defaultValues(0.0f);
138        XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection,   getAspectCorrection,   xmlElement, mode)
139            .defaultValues(true);
140        XMLPortParam(OrxonoxOverlay, "background",    setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode)
141            .defaultValues("");
142    }
143
144    //! Only sets the background material name if not ""
145    void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
146    {
147        if (this->background_ && material != "")
148            this->background_->setMaterialName(material);
149    }
150
151    //! Returns the the material name of the background
152    const std::string& OrxonoxOverlay::getBackgroundMaterial() const
153    {
154        if (this->background_)
155            return this->background_->getMaterialName();
156        else
157            return blankString;
158    }
159
160    //! Called by BaseObject when visibility has changed.
161    void OrxonoxOverlay::changedVisibility()
162    {
163        if (!this->overlay_)
164            return;
165
166        if (this->isVisible())
167            this->overlay_->show();
168        else
169            this->overlay_->hide();
170    }
171
172    /**
173    @brief
174        Called by the GraphicsEngine whenever the window size changes.
175        Calculates the aspect ratio only.
176    */
177    void OrxonoxOverlay::windowResized(int newWidth, int newHeight)
178    {
179        this->windowAspectRatio_ = newWidth/(float)newHeight;
180        this->sizeCorrectionChanged();
181    }
182
183    /**
184    @brief
185        Called whenever the rotation angle has changed.
186    */
187    void OrxonoxOverlay::angleChanged()
188    {
189        if (!this->overlay_)
190            return;
191
192        this->overlay_->setRotate(this->angle_);
193        this->sizeCorrectionChanged();
194    }
195
196    /**
197    @brief
198        Called whenever the aspect ratio or the angle has changed.
199        The method calculates a correction factor for the size to compensate
200        for aspect distortion if desired.
201    @remarks
202        This only works when the angle is about 0 or 90 degrees.
203    */
204    void OrxonoxOverlay::sizeCorrectionChanged()
205    {
206        if (this->bCorrectAspect_)
207        {
208            float angle = this->angle_.valueDegrees();
209            if (angle < 0.0)
210                angle = -angle;
211            angle -= 180.0 * (int)(angle / 180.0);
212
213            // take the reverse if angle is about 90 degrees
214            float tempAspect;
215            if (angle > 89.0 && angle < 91.0)
216                tempAspect = 1.0 / this->windowAspectRatio_;
217            else if (angle > 179 || angle < 1)
218                tempAspect = this->windowAspectRatio_;
219            else
220                tempAspect = 1.0;
221
222            // note: this is only an approximation that is mostly valid when the
223            // magnitude of the width is about the magnitude of the height.
224            // Correctly we would have to take the square root of width*height
225            this->sizeCorrection_.x = 2.0 / (tempAspect + 1.0);
226            this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x;
227        }
228        else
229        {
230            this->sizeCorrection_ = Vector2::UNIT_SCALE;
231        }
232
233        this->sizeChanged();
234    }
235
236    /**
237    @brief
238        Sets the overlay size using the actual corrected size.
239    */
240    void OrxonoxOverlay::sizeChanged()
241    {
242        if (!this->overlay_)
243            return;
244
245        this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
246        positionChanged();
247    }
248
249    /**
250    @brief
251        Determines the position of the overlay.
252        This works also well when a rotation angle is applied. The overlay always
253        gets aligned correctly as well as possible.
254    */
255    void OrxonoxOverlay::positionChanged()
256    {
257        if (!this->overlay_)
258            return;
259
260        // transform the angle to a range of 0 - pi/2 first.
261        float angle = this->angle_.valueRadians();
262        if (angle < 0.0)
263            angle = -angle;
264        angle -= Ogre::Math::PI * (int)(angle / (Ogre::Math::PI));
265        if (angle > Ogre::Math::PI * 0.5)
266            angle = Ogre::Math::PI - angle;
267       
268        // do some mathematical fiddling for a bounding box
269        Vector2 actualSize = size_ * sizeCorrection_;
270        float radius = actualSize.length();
271        float phi = atan(actualSize.y / actualSize.x);
272        Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi));
273
274        // calculate the scrolling offset
275        Vector2 scroll = (position_ - 0.5 - boundingBox * (pickPoint_ - 0.5)) * 2.0;
276        this->overlay_->setScroll(scroll.x, -scroll.y);
277    }
278
279
280    //########### Console commands ############
281
282    /**
283    @brief
284        Scales an overlay by its name.
285    @param name
286        The name of the overlay defined BaseObject::setName() (usually done with the "name"
287        attribute in the xml file).
288    */
289    /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale)
290    {
291        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
292        if (it != overlays_s.end())
293            (*it).second->scale(Vector2(scale, scale));
294    }
295
296    /**
297    @brief
298        Scrolls an overlay by its name.
299    @param name
300        The name of the overlay defined BaseObject::setName() (usually done with the "name"
301        attribute in the xml file).
302    */
303    /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll)
304    {
305        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
306        if (it != overlays_s.end())
307            (*it).second->scroll(scroll);
308    }
309
310    /**
311    @brief
312        Rotates an overlay by its name.
313    @param name
314        The name of the overlay defined BaseObject::setName() (usually done with the "name"
315        attribute in the xml file).
316    */
317    /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle)
318    {
319        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
320        if (it != overlays_s.end())
321            (*it).second->rotate(angle);
322    }
323}
Note: See TracBrowser for help on using the repository browser.