Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8059 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

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