Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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