Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/overlays/OrxonoxOverlay.cc @ 6336

Last change on this file since 6336 was 6310, checked in by wirthmi, 15 years ago

Added damage overlays

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