Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3057 was 2993, checked in by dafrick, 16 years ago

Small changes in QuestManager for the GUI. Added toggleVisibility command to OrxonoxOverlay.

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