Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib2/src/orxonox/overlays/OverlayGroup.cc @ 8529

Last change on this file since 8529 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
Line 
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
29/**
30@file
31@brief Definition of the OverlayGroup class.
32*/
33
34#include "OverlayGroup.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "core/command/ConsoleCommand.h"
39#include "OrxonoxOverlay.h"
40
41namespace orxonox
42{
43    CreateFactory(OverlayGroup);
44
45    SetConsoleCommand("OverlayGroup", "toggleVisibility", &OverlayGroup::toggleVisibility);
46    SetConsoleCommand("OverlayGroup", "scaleGroup",       &OverlayGroup::scaleGroup);
47    SetConsoleCommand("OverlayGroup", "scrollGroup",      &OverlayGroup::scrollGroup);
48
49    OverlayGroup::OverlayGroup(BaseObject* creator)
50        : BaseObject(creator)
51    {
52        RegisterObject(OverlayGroup);
53
54        this->owner_ = 0;
55
56        setScale(Vector2(1.0, 1.0));
57        setScroll(Vector2(0.0, 0.0));
58    }
59
60    OverlayGroup::~OverlayGroup()
61    {
62        for (std::set< SmartPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
63            (*it)->destroy();
64        this->hudElements_.clear();
65    }
66
67    /**
68    @brief
69        Loads the group and all its children OrxonoxOverlays.
70    @copydoc
71        BaseObject::XMLPort()
72    */
73    void OverlayGroup::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(OverlayGroup, XMLPort, xmlelement, mode);
76
77        XMLPortParam(OverlayGroup, "scale",  setScale,  getScale,  xmlelement, mode);
78        XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlelement, mode);
79        // loads all the child elements
80        XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlelement, mode);
81    }
82
83    //! Scales every element in the set.
84    void OverlayGroup::setScale(const Vector2& scale)
85    {
86        for (std::set< SmartPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
87            (*it)->scale(scale / this->scale_);
88        this->scale_ = scale;
89    }
90
91    //! Scrolls every element in the set.
92    void OverlayGroup::setScroll(const Vector2& scroll)
93    {
94        for (std::set< SmartPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
95            (*it)->scroll(scroll - this->scroll_);
96        this->scroll_ = scroll;
97    }
98
99    /**
100    @brief
101        Adds an element to the set (used when loading with XMLPort).
102    @remarks
103        The names of the OrxonoxOverlays have to be unique!
104    */
105    void OverlayGroup::addElement(OrxonoxOverlay* element)
106    {
107        hudElements_.insert(SmartPtr<OrxonoxOverlay>(element));
108        element->setOverlayGroup( this );
109        if (this->owner_)
110            element->setOwner(this->owner_);
111    }
112
113    /**
114    @brief
115        Removes an element from the map.
116    @param element
117        A pointer to 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    {
123        if(this->hudElements_.erase(SmartPtr<OrxonoxOverlay>(element)) == 0)
124            return false;
125        return true;
126    }
127
128    //! Returns a different element as long as index < hudElements_.size().
129    OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
130    {
131        if (index < this->hudElements_.size())
132        {
133            std::set< SmartPtr<OrxonoxOverlay> >::const_iterator it = hudElements_.begin();
134            for (unsigned int i = 0; i != index; ++it, ++i)
135                ;
136            return it->get();
137        }
138        else
139            return 0;
140    }
141
142    //! Changes the visibility of all elements
143    void OverlayGroup::changedVisibility()
144    {
145        SUPER( OverlayGroup, changedVisibility );
146
147        for (std::set< SmartPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
148            (*it)->changedVisibility(); //inform all Child Overlays that our visibility has changed
149    }
150
151    void OverlayGroup::setOwner(BaseObject* owner)
152    {
153        this->owner_ = owner;
154
155        for (std::set< SmartPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
156            (*it)->setOwner(owner);
157    }
158
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    */
168    /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
169    {
170        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
171        {
172            if ((*it)->getName() == name)
173                (*it)->setVisible(!((*it)->isVisible()));
174        }
175    }
176
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    @param scale
184        The scaling factor
185    */
186    /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale)
187    {
188        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
189        {
190            if ((*it)->getName() == name)
191                (*it)->scale(Vector2(scale, scale));
192        }
193    }
194
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).
201    @param scroll
202        The relative translation of the overlay group
203    */
204    /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll)
205    {
206        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
207        {
208            if ((*it)->getName() == name)
209                (*it)->scroll(scroll);
210        }
211    }
212}
Note: See TracBrowser for help on using the repository browser.