Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/overlays/OverlayGroup.cc @ 3184

Last change on this file since 3184 was 3182, checked in by rgrieder, 16 years ago

Found out that that Debug.h is officially included in CoreIncludes.h ;)

  • Property svn:eol-style set to native
File size: 6.3 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/ConsoleCommand.h"
37#include "core/CoreIncludes.h"
38#include "core/ObjectList.h"
39#include "core/XMLPort.h"
40#include "OrxonoxOverlay.h"
41
42namespace orxonox
43{
44    CreateFactory(OverlayGroup);
45
46    SetConsoleCommand(OverlayGroup, toggleVisibility, false).accessLevel(AccessLevel::User);
47    SetConsoleCommand(OverlayGroup, scaleGroup, false).accessLevel(AccessLevel::User);
48    SetConsoleCommand(OverlayGroup, scrollGroup, false).accessLevel(AccessLevel::User);
49
50    OverlayGroup::OverlayGroup(BaseObject* creator)
51        : BaseObject(creator)
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<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
64            delete (*it);
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<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<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(element);
108        element->setVisible(this->isVisible());
109        if (this->owner_)
110            element->setOwner(this->owner_);
111    }
112
113    /**
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    {
123        if(this->hudElements_.erase(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<OrxonoxOverlay*>::const_iterator it = hudElements_.begin();
134            for (unsigned int i = 0; i != index; ++it, ++i)
135                ;
136            return (*it);
137        }
138        else
139            return 0;
140    }
141
142    //! Changes the visibility of all elements
143    void OverlayGroup::changedVisibility()
144    {
145        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
146            (*it)->setVisible(this->isVisible());
147    }
148
149    void OverlayGroup::setOwner(BaseObject* owner)
150    {
151        this->owner_ = owner;
152
153        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
154            (*it)->setOwner(owner);
155    }
156
157    //########### Console commands ############
158
159    /**
160    @brief
161        Hides/shows an overlay group by its name.
162    @param name
163        The name of the group defined BaseObject::setName() (usually done with the "name"
164        attribute in the xml file).
165    */
166    /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
167    {
168        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
169        {
170            if ((*it)->getName() == name)
171                (*it)->setVisible(!((*it)->isVisible()));
172        }
173    }
174
175    /**
176    @brief
177        Scales an overlay group by its name.
178    @param name
179        The name of the group defined BaseObject::setName() (usually done with the "name"
180        attribute in the xml file).
181    */
182    /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale)
183    {
184        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
185        {
186            if ((*it)->getName() == name)
187                (*it)->scale(Vector2(scale, scale));
188        }
189    }
190
191    /**
192    @brief
193        Scrolls an overlay group by its name.
194    @param name
195        The name of the group defined BaseObject::setName() (usually done with the "name"
196        attribute in the xml file).
197    */
198    /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll)
199    {
200        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
201        {
202            if ((*it)->getName() == name)
203                (*it)->scroll(scroll);
204        }
205    }
206}
Note: See TracBrowser for help on using the repository browser.