Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/revampbigships/src/orxonox/overlays/OrxonoxOverlay.cc @ 9700

Last change on this file since 9700 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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