Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/overlays/OrxonoxOverlay.cc @ 2019

Last change on this file since 2019 was 2019, checked in by landauf, 16 years ago

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

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