Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/overlays/OverlayGroup.cc @ 2396

Last change on this file since 2396 was 2256, checked in by landauf, 16 years ago

The SpaceShips HUD is working again (Speedbar and Radar)

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