Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6330 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
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
[5980]50#include "OverlayGroup.h"
51
[1588]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_"
[3280]75            + multi_cast<std::string>(hudOverlayCounter_s++));
[2087]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",
[3280]80            "OrxonoxOverlay_background_" + multi_cast<std::string>(hudOverlayCounter_s++)));
[2087]81        this->overlay_->add2D(this->background_);
82
[2896]83        // Get aspect ratio from the render window. Later on, we get informed automatically
[3327]84        this->windowAspectRatio_ = static_cast<float>(this->getWindowWidth()) / this->getWindowHeight();
[2087]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));
[2662]93        setAspectCorrection(false);
[2087]94        setBackgroundMaterial("");
[1615]95    }
[1614]96
[1622]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    */
[1615]103    OrxonoxOverlay::~OrxonoxOverlay()
104    {
[2087]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);
[1622]111
112            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
113            Ogre::OverlayManager::getSingleton().destroy(this->overlay_);
[2087]114        }
[1615]115    }
116
[1622]117    /**
118    @brief
119        Loads the OrxonoxOverlay.
[1747]120
[1622]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    */
[1615]126    void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
[1590]127    {
[1747]128        SUPER(OrxonoxOverlay, XMLPort, xmlElement, mode);
[1588]129
[2087]130        XMLPortParam(OrxonoxOverlay, "size",      setSize,      getSize,      xmlElement, mode);
[2662]131        XMLPortParam(OrxonoxOverlay, "pickpoint", setPickPoint, getPickPoint, xmlElement, mode);
[2087]132        XMLPortParam(OrxonoxOverlay, "position",  setPosition,  getPosition,  xmlElement, mode);
133        XMLPortParam(OrxonoxOverlay, "rotation",  setRotation,  getRotation,  xmlElement, mode);
[2662]134        XMLPortParam(OrxonoxOverlay, "correctaspect", setAspectCorrection,   getAspectCorrection,   xmlElement, mode);
[2087]135        XMLPortParam(OrxonoxOverlay, "background",    setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode);
136    }
[1614]137
[2087]138    void OrxonoxOverlay::changedName()
139    {
[2662]140        SUPER(OrxonoxOverlay, changedName);
141
[2087]142        OrxonoxOverlay::overlays_s.erase(this->getOldName());
[1590]143
[2087]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;
[1588]146
[2087]147        OrxonoxOverlay::overlays_s[this->getName()] = this;
[1590]148    }
[1588]149
[1622]150    //! Only sets the background material name if not ""
[1615]151    void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
152    {
153        if (this->background_ && material != "")
154            this->background_->setMaterialName(material);
155    }
[1588]156
[1622]157    //! Returns the the material name of the background
[1615]158    const std::string& OrxonoxOverlay::getBackgroundMaterial() const
159    {
160        if (this->background_)
161            return this->background_->getMaterialName();
162        else
[2087]163            return BLANKSTRING;
[1615]164    }
[1614]165
[1622]166    //! Called by BaseObject when visibility has changed.
[1615]167    void OrxonoxOverlay::changedVisibility()
[1595]168    {
[5980]169        SUPER( OrxonoxOverlay, changedVisibility );
170       
[1622]171        if (!this->overlay_)
172            return;
173
[5980]174        // only set to visible if corresponding OverlayGroup is also visible
175        if (this->isVisible() && (!this->getOverlayGroup() || this->getOverlayGroup()->isVisible()) )
[1622]176            this->overlay_->show();
177        else
178            this->overlay_->hide();
[1595]179    }
[1588]180
[1622]181    /**
182    @brief
[2896]183        Called by the GraphicsManager whenever the window size changes.
[1622]184        Calculates the aspect ratio only.
185    */
[2896]186    void OrxonoxOverlay::windowResized(unsigned int newWidth, unsigned int newHeight)
[1615]187    {
[3301]188        this->windowAspectRatio_ = static_cast<float>(newWidth) / newHeight;
[1622]189        this->sizeCorrectionChanged();
[1615]190    }
[1590]191
[1622]192    /**
193    @brief
194        Called whenever the rotation angle has changed.
195    */
196    void OrxonoxOverlay::angleChanged()
[1595]197    {
[1622]198        if (!this->overlay_)
199            return;
200
201        this->overlay_->setRotate(this->angle_);
[1615]202        this->sizeCorrectionChanged();
[1595]203    }
[1615]204
[1622]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    */
[1615]213    void OrxonoxOverlay::sizeCorrectionChanged()
[1595]214    {
[1615]215        if (this->bCorrectAspect_)
216        {
217            float angle = this->angle_.valueDegrees();
[1618]218            if (angle < 0.0)
219                angle = -angle;
[3301]220            angle -= 180.0f * static_cast<int>(angle / 180.0);
[1618]221
[1622]222            // take the reverse if angle is about 90 degrees
[1615]223            float tempAspect;
[3196]224            if (angle > 89.0f && angle < 91.0f)
[1632]225            {
[1615]226                tempAspect = 1.0 / this->windowAspectRatio_;
[1632]227                rotState_ = Vertical;
228            }
[1618]229            else if (angle > 179 || angle < 1)
[1632]230            {
[1615]231                tempAspect = this->windowAspectRatio_;
[1632]232                rotState_ = Horizontal;
233            }
[1615]234            else
[1632]235            {
[3196]236                tempAspect = 1.0f;
[1632]237                rotState_ = Inbetween;
238            }
[1615]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
[3196]243            this->sizeCorrection_.x = 2.0f / (tempAspect + 1.0f);
[1615]244            this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x;
245        }
246        else
247        {
248            this->sizeCorrection_ = Vector2::UNIT_SCALE;
249        }
[1622]250
[1615]251        this->sizeChanged();
[1595]252    }
253
[1615]254    /**
[1622]255    @brief
256        Sets the overlay size using the actual corrected size.
[1615]257    */
258    void OrxonoxOverlay::sizeChanged()
259    {
[1622]260        if (!this->overlay_)
261            return;
262
[1615]263        this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
264        positionChanged();
265    }
[1595]266
[1615]267    /**
[1622]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.
[1615]272    */
[1622]273    void OrxonoxOverlay::positionChanged()
[1615]274    {
[1622]275        if (!this->overlay_)
276            return;
[1595]277
[1622]278        // transform the angle to a range of 0 - pi/2 first.
[1617]279        float angle = this->angle_.valueRadians();
280        if (angle < 0.0)
281            angle = -angle;
[3300]282        angle -= Ogre::Math::PI * static_cast<int>(angle / (Ogre::Math::PI));
[1615]283        if (angle > Ogre::Math::PI * 0.5)
284            angle = Ogre::Math::PI - angle;
[1747]285
[1622]286        // do some mathematical fiddling for a bounding box
[1615]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));
[1622]291
292        // calculate the scrolling offset
293        Vector2 scroll = (position_ - 0.5 - boundingBox * (pickPoint_ - 0.5)) * 2.0;
[1615]294        this->overlay_->setScroll(scroll.x, -scroll.y);
295    }
[1598]296
[1615]297
[1622]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"
[1623]305        attribute in the xml file).
[1622]306    */
[1615]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
[1622]314    /**
315    @brief
[2993]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
[1622]336        Scrolls an overlay by its name.
337    @param name
338        The name of the overlay defined BaseObject::setName() (usually done with the "name"
[1623]339        attribute in the xml file).
[1622]340    */
[1615]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
[1622]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"
[1623]353        attribute in the xml file).
[1622]354    */
[1615]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    }
[6057]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    }
[1588]372}
Note: See TracBrowser for help on using the repository browser.