Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3374 was 3327, checked in by rgrieder, 15 years ago

Merged all remaining revisions from core4 back to the trunk.

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