Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

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