Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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