Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/orxonox/overlays/OrxonoxOverlay.cc @ 7497

Last change on this file since 7497 was 7297, checked in by landauf, 14 years ago

fixed lots of Doxygen warnings

Note: Doxygen prints a warning if only a part of the parameters of a function are documented.

Added documentation for missing parameters (as good as I could), removed documentation of obsolete parameters and fixed names of renamed parameters.
Some parameters are tagged with "FIXME", please replace this with an appropriate documentation if you know what it does.

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