Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

default values for XMLPort work after all. There was just a bug that got fixed now.

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