Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6241 was 6057, checked in by rgrieder, 15 years ago

Moved an inline function to the source in OrxonoxOverlay.

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