Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/OverlayGroup.cc @ 10639

Last change on this file since 10639 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 7.3 KB
RevLine 
[1505]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:
[1604]23 *      Reto Grieder
[1505]24 *   Co-authors:
[1604]25 *      ...
[1505]26 *
27 */
28
[1623]29/**
30@file
31@brief Definition of the OverlayGroup class.
32*/
33
[1601]34#include "OverlayGroup.h"
[1505]35
[1588]36#include "core/CoreIncludes.h"
[1616]37#include "core/XMLPort.h"
[10624]38#include "core/command/ConsoleCommandIncludes.h"
[1604]39#include "OrxonoxOverlay.h"
[10624]40#include "gametypes/Gametype.h"
[1505]41
42namespace orxonox
43{
[9667]44    RegisterClass(OverlayGroup);
[1588]45
[7284]46    SetConsoleCommand("OverlayGroup", "toggleVisibility", &OverlayGroup::toggleVisibility);
[8309]47    SetConsoleCommand("OverlayGroup", "show", &OverlayGroup::show);
[7284]48    SetConsoleCommand("OverlayGroup", "scaleGroup",       &OverlayGroup::scaleGroup);
49    SetConsoleCommand("OverlayGroup", "scrollGroup",      &OverlayGroup::scrollGroup);
[1505]50
[9667]51    OverlayGroup::OverlayGroup(Context* context)
52        : BaseObject(context)
[1615]53    {
54        RegisterObject(OverlayGroup);
[2087]55
[2662]56        this->owner_ = 0;
57
[2087]58        setScale(Vector2(1.0, 1.0));
59        setScroll(Vector2(0.0, 0.0));
[1615]60    }
[1505]61
[2087]62    OverlayGroup::~OverlayGroup()
63    {
[10624]64        for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
[5929]65            (*it)->destroy();
[6054]66        this->hudElements_.clear();
[2087]67    }
68
[1623]69    /**
70    @brief
71        Loads the group and all its children OrxonoxOverlays.
72    @copydoc
73        BaseObject::XMLPort()
74    */
[7401]75    void OverlayGroup::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[1615]76    {
[7401]77        SUPER(OverlayGroup, XMLPort, xmlelement, mode);
[1588]78
[7401]79        XMLPortParam(OverlayGroup, "scale",  setScale,  getScale,  xmlelement, mode);
80        XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlelement, mode);
[1623]81        // loads all the child elements
[7401]82        XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlelement, mode);
[1615]83    }
[1505]84
[2890]85    //! Scales every element in the set.
[1615]86    void OverlayGroup::setScale(const Vector2& scale)
87    {
[10624]88        for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
[2890]89            (*it)->scale(scale / this->scale_);
[1615]90        this->scale_ = scale;
91    }
[1588]92
[2890]93    //! Scrolls every element in the set.
[1615]94    void OverlayGroup::setScroll(const Vector2& scroll)
95    {
[10624]96        for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
[2890]97            (*it)->scroll(scroll - this->scroll_);
[1615]98        this->scroll_ = scroll;
99    }
[1505]100
[1623]101    /**
102    @brief
[2890]103        Adds an element to the set (used when loading with XMLPort).
[1623]104    @remarks
105        The names of the OrxonoxOverlays have to be unique!
106    */
[1615]107    void OverlayGroup::addElement(OrxonoxOverlay* element)
[1564]108    {
[10624]109        hudElements_.insert(element);
[5980]110        element->setOverlayGroup( this );
[2890]111        if (this->owner_)
112            element->setOwner(this->owner_);
[1588]113    }
[1564]114
[3034]115    /**
[2911]116    @brief
117        Removes an element from the map.
[7401]118    @param element
119        A pointer to the element that is removed.
[2911]120    @return
121        Returns true if there was such an element to remove, false if not.
122    */
123    bool OverlayGroup::removeElement(OrxonoxOverlay* element)
124    {
[10624]125        if(this->hudElements_.erase(element) == 0)
[2911]126            return false;
127        return true;
128    }
129
[1623]130    //! Returns a different element as long as index < hudElements_.size().
[1615]131    OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
[1588]132    {
[1615]133        if (index < this->hudElements_.size())
134        {
[10624]135            std::set< StrongPtr<OrxonoxOverlay> >::const_iterator it = hudElements_.begin();
[1615]136            for (unsigned int i = 0; i != index; ++it, ++i)
137                ;
[10624]138            return *it;
[1615]139        }
140        else
141            return 0;
[1505]142    }
143
[1633]144    //! Changes the visibility of all elements
145    void OverlayGroup::changedVisibility()
146    {
[5980]147        SUPER( OverlayGroup, changedVisibility );
[6417]148
[10624]149        for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
[5980]150            (*it)->changedVisibility(); //inform all Child Overlays that our visibility has changed
[1633]151    }
[1614]152
[2890]153    void OverlayGroup::setOwner(BaseObject* owner)
[2662]154    {
155        this->owner_ = owner;
[1633]156
[10624]157        for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
[2890]158            (*it)->setOwner(owner);
[2662]159    }
160
[1623]161    //########### Console commands ############
162
163    /**
164    @brief
165        Hides/shows an overlay group by its name.
166    @param name
167        The name of the group defined BaseObject::setName() (usually done with the "name"
168        attribute in the xml file).
169    */
[1615]170    /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
[1614]171    {
[1747]172        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]173        {
174            if ((*it)->getName() == name)
[1625]175                (*it)->setVisible(!((*it)->isVisible()));
[1615]176        }
[1614]177    }
[8309]178   
179    /**
180    @brief
181        Shows an overlay group by its name.
182    @param name
183        The name of the group defined BaseObject::setName() (usually done with the "name" attribute in the xml file).
184    */
185    /*static*/ void OverlayGroup::show(const std::string& name)
186    {
187        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
188        {
189            if ((*it)->getName() == name)
190            {
191                if((*it)->isVisible())
192                    (*it)->changedVisibility();
193                else
194                    (*it)->setVisible(!((*it)->isVisible()));
195            }
196        }
197    }
[1505]198
[1623]199    /**
200    @brief
201        Scales an overlay group by its name.
202    @param name
203        The name of the group defined BaseObject::setName() (usually done with the "name"
204        attribute in the xml file).
[7401]205    @param scale
206        The scaling factor
[1623]207    */
[1615]208    /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale)
[1564]209    {
[1747]210        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]211        {
212            if ((*it)->getName() == name)
213                (*it)->scale(Vector2(scale, scale));
214        }
[1564]215    }
[1615]216
[1623]217    /**
218    @brief
219        Scrolls an overlay group by its name.
220    @param name
221        The name of the group defined BaseObject::setName() (usually done with the "name"
222        attribute in the xml file).
[7401]223    @param scroll
224        The relative translation of the overlay group
[1623]225    */
[1615]226    /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll)
227    {
[1747]228        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]229        {
230            if ((*it)->getName() == name)
231                (*it)->scroll(scroll);
232        }
233    }
[1505]234}
Note: See TracBrowser for help on using the repository browser.