Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2848 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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