Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12138 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

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