Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7329 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 12.9 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    */
309    /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale)
310    {
311        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
312        if (it != overlays_s.end())
313            it->second->scale(Vector2(scale, scale));
314    }
315
316    /**
317    @brief
318        Toggles the visibility of an Overlay by it's name.
319    @param name
320        The name of the overlay defined BaseObject::setName() (usually done with the "name"
321        attribute in the xml file).
322    */
323    /*static*/ void OrxonoxOverlay::toggleVisibility(const std::string& name)
324    {
325        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
326        if (it != overlays_s.end())
327        {
328            OrxonoxOverlay* overlay= it->second;
329            if(overlay->isVisible())
330            {
331                overlay->hide();
332                COUT(4) << "HIDE " << name << std::endl;
333            }
334            else
335            {
336                overlay->show();
337                COUT(4) << "SHOW " << name << std::endl;
338            }
339        }
340    }
341
342    /**
343    @brief
344        Scrolls an overlay by its name.
345    @param name
346        The name of the overlay defined BaseObject::setName() (usually done with the "name"
347        attribute in the xml file).
348    */
349    /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll)
350    {
351        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
352        if (it != overlays_s.end())
353            it->second->scroll(scroll);
354    }
355
356    /**
357    @brief
358        Rotates an overlay by its name.
359    @param name
360        The name of the overlay defined BaseObject::setName() (usually done with the "name"
361        attribute in the xml file).
362    */
363    /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle)
364    {
365        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
366        if (it != overlays_s.end())
367            it->second->rotate(angle);
368    }
369
370    void OrxonoxOverlay::setOverlayGroup(OverlayGroup* group)
371    {
372        if (group != this->group_)
373        {
374            if (this->group_)
375                this->group_->removeElement(this);
376            this->group_ = group;
377            this->changedOverlayGroup();
378        }
379    }
380
381    void OrxonoxOverlay::setBackgroundAlpha(float alpha) {
382        Ogre::MaterialPtr ptr = this->background_->getMaterial();
383        Ogre::TextureUnitState* tempTx = ptr->getTechnique(0)->getPass(0)->getTextureUnitState(0);
384        tempTx->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, alpha);
385    }
386}
Note: See TracBrowser for help on using the repository browser.