Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/orxonox/overlays/OverlayGroup.cc @ 5993

Last change on this file since 5993 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

  • 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/XMLPort.h"
39#include "OrxonoxOverlay.h"
40
41namespace orxonox
42{
43    CreateFactory(OverlayGroup);
44
45    SetConsoleCommand(OverlayGroup, toggleVisibility, false).accessLevel(AccessLevel::User);
46    SetConsoleCommand(OverlayGroup, scaleGroup, false).accessLevel(AccessLevel::User);
47    SetConsoleCommand(OverlayGroup, scrollGroup, false).accessLevel(AccessLevel::User);
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<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
63            (*it)->destroy();
64    }
65
66    /**
67    @brief
68        Loads the group and all its children OrxonoxOverlays.
69    @copydoc
70        BaseObject::XMLPort()
71    */
72    void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode)
73    {
74        SUPER(OverlayGroup, XMLPort, xmlElement, mode);
75
76        XMLPortParam(OverlayGroup, "scale",  setScale,  getScale,  xmlElement, mode);
77        XMLPortParam(OverlayGroup, "scroll", setScroll, getScroll, xmlElement, mode);
78        // loads all the child elements
79        XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlElement, mode);
80    }
81
82    //! Scales every element in the set.
83    void OverlayGroup::setScale(const Vector2& scale)
84    {
85        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
86            (*it)->scale(scale / this->scale_);
87        this->scale_ = scale;
88    }
89
90    //! Scrolls every element in the set.
91    void OverlayGroup::setScroll(const Vector2& scroll)
92    {
93        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
94            (*it)->scroll(scroll - this->scroll_);
95        this->scroll_ = scroll;
96    }
97
98    /**
99    @brief
100        Adds an element to the set (used when loading with XMLPort).
101    @remarks
102        The names of the OrxonoxOverlays have to be unique!
103    */
104    void OverlayGroup::addElement(OrxonoxOverlay* element)
105    {
106        hudElements_.insert(element);
107        element->setVisible(this->isVisible());
108        if (this->owner_)
109            element->setOwner(this->owner_);
110    }
111
112    /**
113    @brief
114        Removes an element from the map.
115    @param name
116        The name of the element that is removed.
117    @return
118        Returns true if there was such an element to remove, false if not.
119    */
120    bool OverlayGroup::removeElement(OrxonoxOverlay* element)
121    {
122        if(this->hudElements_.erase(element) == 0)
123            return false;
124        return true;
125    }
126
127    //! Returns a different element as long as index < hudElements_.size().
128    OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
129    {
130        if (index < this->hudElements_.size())
131        {
132            std::set<OrxonoxOverlay*>::const_iterator it = hudElements_.begin();
133            for (unsigned int i = 0; i != index; ++it, ++i)
134                ;
135            return (*it);
136        }
137        else
138            return 0;
139    }
140
141    //! Changes the visibility of all elements
142    void OverlayGroup::changedVisibility()
143    {
144        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
145            (*it)->setVisible(this->isVisible());
146    }
147
148    void OverlayGroup::setOwner(BaseObject* owner)
149    {
150        this->owner_ = owner;
151
152        for (std::set<OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
153            (*it)->setOwner(owner);
154    }
155
156    //########### Console commands ############
157
158    /**
159    @brief
160        Hides/shows an overlay group by its name.
161    @param name
162        The name of the group defined BaseObject::setName() (usually done with the "name"
163        attribute in the xml file).
164    */
165    /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
166    {
167        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
168        {
169            if ((*it)->getName() == name)
170                (*it)->setVisible(!((*it)->isVisible()));
171        }
172    }
173
174    /**
175    @brief
176        Scales an overlay group by its name.
177    @param name
178        The name of the group defined BaseObject::setName() (usually done with the "name"
179        attribute in the xml file).
180    */
181    /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale)
182    {
183        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
184        {
185            if ((*it)->getName() == name)
186                (*it)->scale(Vector2(scale, scale));
187        }
188    }
189
190    /**
191    @brief
192        Scrolls an overlay group by its name.
193    @param name
194        The name of the group defined BaseObject::setName() (usually done with the "name"
195        attribute in the xml file).
196    */
197    /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll)
198    {
199        for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it)
200        {
201            if ((*it)->getName() == name)
202                (*it)->scroll(scroll);
203        }
204    }
205}
Note: See TracBrowser for help on using the repository browser.