Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10315 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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