Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added documentation to OverlayGroup
ready for merge

  • Property svn:eol-style set to native
File size: 11.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/**
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            // set some default values
99            this->windowAspectRatio_ = 1.0f;
100            this->bCorrectAspect_    = false;
101            this->size_              = Vector2(1.0f, 1.0f);
102            this->sizeCorrection_    = Vector2(1.0f, 1.0f);
103            this->position_          = Vector2(0.0f, 0.0f);
104            this->pickPoint_         = Vector2(0.0f, 0.0f);
105            this->angle_             = Radian(0.0f);
106
107            // add this overlay to the static map of OrxonoxOverlays
108            if (overlays_s.find(this->getName()) != overlays_s.end())
109            {
110                COUT(1) << "Overlay names should be unique or you cannnot access them via console." << std::endl;
111            }
112            overlays_s[this->getName()] = this;
113
114            // create the Ogre::Overlay
115            overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_"
116                + convertToString(hudOverlayCounter_s++));
117
118            // create background panel (can be used to show any picture)
119            this->background_ = static_cast<Ogre::PanelOverlayElement*>(
120                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel",
121                "OrxonoxOverlay_background_" + convertToString(hudOverlayCounter_s++)));
122            this->overlay_->add2D(this->background_);
123
124            // We'll have to get the aspect ratio for the first. Afterwards windowResized() gets
125            // called automatically by the GraphicsEngine.
126            this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
127                GraphicsEngine::getSingleton().getWindowHeight());
128        }
129
130        XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode);
131        XMLPortParam(OrxonoxOverlay, "size", setSize, getSize, xmlElement, mode);
132        XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode);
133        // see setPickPoint()
134        XMLPortParam(OrxonoxOverlay, "pickPoint", setPickPoint, getPickPoint, xmlElement, mode);
135        XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode);
136        XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode);
137
138        if (mode == XMLPort::LoadObject)
139        {
140            // call all the virtual 'setters'
141            this->changedVisibility();
142            this->angleChanged();
143            this->sizeCorrectionChanged();
144            this->sizeChanged();
145            // probably gets called 4 times (by all the methods), but sizeChanged() could be overwritten.
146            this->positionChanged();
147        }
148    }
149
150    //! Only sets the background material name if not ""
151    void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
152    {
153        if (this->background_ && material != "")
154            this->background_->setMaterialName(material);
155    }
156
157    //! Returns the the material name of the background
158    const std::string& OrxonoxOverlay::getBackgroundMaterial() const
159    {
160        if (this->background_)
161            return this->background_->getMaterialName();
162        else
163            return blankString;
164    }
165
166    //! Called by BaseObject when visibility has changed.
167    void OrxonoxOverlay::changedVisibility()
168    {
169        if (!this->overlay_)
170            return;
171
172        if (this->isVisible())
173            this->overlay_->show();
174        else
175            this->overlay_->hide();
176    }
177
178    /**
179    @brief
180        Called by the GraphicsEngine whenever the window size changes.
181        Calculates the aspect ratio only.
182    */
183    void OrxonoxOverlay::windowResized(int newWidth, int newHeight)
184    {
185        this->windowAspectRatio_ = newWidth/(float)newHeight;
186        this->sizeCorrectionChanged();
187    }
188
189    /**
190    @brief
191        Called whenever the rotation angle has changed.
192    */
193    void OrxonoxOverlay::angleChanged()
194    {
195        if (!this->overlay_)
196            return;
197
198        this->overlay_->setRotate(this->angle_);
199        this->sizeCorrectionChanged();
200    }
201
202    /**
203    @brief
204        Called whenever the aspect ratio or the angle has changed.
205        The method calculates a correction factor for the size to compensate
206        for aspect distortion if desired.
207    @remarks
208        This only works when the angle is about 0 or 90 degrees.
209    */
210    void OrxonoxOverlay::sizeCorrectionChanged()
211    {
212        if (this->bCorrectAspect_)
213        {
214            float angle = this->angle_.valueDegrees();
215            if (angle < 0.0)
216                angle = -angle;
217            angle -= 180.0 * (int)(angle / 180.0);
218
219            // take the reverse if angle is about 90 degrees
220            float tempAspect;
221            if (angle > 89.0 && angle < 91.0)
222                tempAspect = 1.0 / this->windowAspectRatio_;
223            else if (angle > 179 || angle < 1)
224                tempAspect = this->windowAspectRatio_;
225            else
226                tempAspect = 1.0;
227
228            // note: this is only an approximation that is mostly valid when the
229            // magnitude of the width is about the magnitude of the height.
230            // Correctly we would have to take the square root of width*height
231            this->sizeCorrection_.x = 2.0 / (tempAspect + 1.0);
232            this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x;
233        }
234        else
235        {
236            this->sizeCorrection_ = Vector2::UNIT_SCALE;
237        }
238
239        this->sizeChanged();
240    }
241
242    /**
243    @brief
244        Sets the overlay size using the actual corrected size.
245    */
246    void OrxonoxOverlay::sizeChanged()
247    {
248        if (!this->overlay_)
249            return;
250
251        this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
252        positionChanged();
253    }
254
255    /**
256    @brief
257        Determines the position of the overlay.
258        This works also well when a rotation angle is applied. The overlay always
259        gets aligned correctly as well as possible.
260    */
261    void OrxonoxOverlay::positionChanged()
262    {
263        if (!this->overlay_)
264            return;
265
266        // transform the angle to a range of 0 - pi/2 first.
267        float angle = this->angle_.valueRadians();
268        if (angle < 0.0)
269            angle = -angle;
270        angle -= Ogre::Math::PI * (int)(angle / (Ogre::Math::PI));
271        if (angle > Ogre::Math::PI * 0.5)
272            angle = Ogre::Math::PI - angle;
273       
274        // do some mathematical fiddling for a bounding box
275        Vector2 actualSize = size_ * sizeCorrection_;
276        float radius = actualSize.length();
277        float phi = atan(actualSize.y / actualSize.x);
278        Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi));
279
280        // calculate the scrolling offset
281        Vector2 scroll = (position_ - 0.5 - boundingBox * (pickPoint_ - 0.5)) * 2.0;
282        this->overlay_->setScroll(scroll.x, -scroll.y);
283    }
284
285
286    //########### Console commands ############
287
288    /**
289    @brief
290        Scales an overlay by its name.
291    @param name
292        The name of the overlay defined BaseObject::setName() (usually done with the "name"
293        attribute in the xml file).
294    */
295    /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale)
296    {
297        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
298        if (it != overlays_s.end())
299            (*it).second->scale(Vector2(scale, scale));
300    }
301
302    /**
303    @brief
304        Scrolls an overlay by its name.
305    @param name
306        The name of the overlay defined BaseObject::setName() (usually done with the "name"
307        attribute in the xml file).
308    */
309    /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll)
310    {
311        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
312        if (it != overlays_s.end())
313            (*it).second->scroll(scroll);
314    }
315
316    /**
317    @brief
318        Rotates an overlay by its name.
319    @param name
320        The name of the overlay defined BaseObject::setName() (usually done with the "name"
321        attribute in the xml file).
322    */
323    /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle)
324    {
325        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
326        if (it != overlays_s.end())
327            (*it).second->rotate(angle);
328    }
329}
Note: See TracBrowser for help on using the repository browser.