Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6180 was 6054, checked in by scheusso, 15 years ago

trying to solve a double free bug (replaced OrxonoxOverlay pointers with smart pointers)

  • Property svn:eol-style set to native
File size: 6.5 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
36#include "core/ConsoleCommand.h"
[1588]37#include "core/CoreIncludes.h"
[1616]38#include "core/XMLPort.h"
[1604]39#include "OrxonoxOverlay.h"
[1505]40
41namespace orxonox
42{
[1615]43    CreateFactory(OverlayGroup);
[1588]44
[1747]45    SetConsoleCommand(OverlayGroup, toggleVisibility, false).accessLevel(AccessLevel::User);
46    SetConsoleCommand(OverlayGroup, scaleGroup, false).accessLevel(AccessLevel::User);
47    SetConsoleCommand(OverlayGroup, scrollGroup, false).accessLevel(AccessLevel::User);
[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    */
[1615]73    void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode)
74    {
[1747]75        SUPER(OverlayGroup, XMLPort, xmlElement, mode);
[1588]76
[2087]77        XMLPortParam(OverlayGroup, "scale",  setScale,  getScale,  xmlElement, mode);
78        XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlElement, mode);
[1623]79        // loads all the child elements
[1854]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.
116    @param name
117        The name of the element that is removed.
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 );
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).
183    */
[1615]184    /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale)
[1564]185    {
[1747]186        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]187        {
188            if ((*it)->getName() == name)
189                (*it)->scale(Vector2(scale, scale));
190        }
[1564]191    }
[1615]192
[1623]193    /**
194    @brief
195        Scrolls an overlay group by its name.
196    @param name
197        The name of the group defined BaseObject::setName() (usually done with the "name"
198        attribute in the xml file).
199    */
[1615]200    /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll)
201    {
[1747]202        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
[1615]203        {
204            if ((*it)->getName() == name)
205                (*it)->scroll(scroll);
206        }
207    }
[1505]208}
Note: See TracBrowser for help on using the repository browser.