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 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | RegisterClass(OverlayGroup); |
---|
45 | |
---|
46 | SetConsoleCommand("OverlayGroup", "toggleVisibility", &OverlayGroup::toggleVisibility); |
---|
47 | SetConsoleCommand("OverlayGroup", "show", &OverlayGroup::show); |
---|
48 | SetConsoleCommand("OverlayGroup", "scaleGroup", &OverlayGroup::scaleGroup); |
---|
49 | SetConsoleCommand("OverlayGroup", "scrollGroup", &OverlayGroup::scrollGroup); |
---|
50 | |
---|
51 | OverlayGroup::OverlayGroup(Context* context) |
---|
52 | : BaseObject(context) |
---|
53 | { |
---|
54 | RegisterObject(OverlayGroup); |
---|
55 | |
---|
56 | this->owner_ = 0; |
---|
57 | |
---|
58 | setScale(Vector2(1.0, 1.0)); |
---|
59 | setScroll(Vector2(0.0, 0.0)); |
---|
60 | } |
---|
61 | |
---|
62 | OverlayGroup::~OverlayGroup() |
---|
63 | { |
---|
64 | for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
65 | (*it)->destroy(); |
---|
66 | this->hudElements_.clear(); |
---|
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 set. |
---|
86 | void OverlayGroup::setScale(const Vector2& scale) |
---|
87 | { |
---|
88 | for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
89 | (*it)->scale(scale / this->scale_); |
---|
90 | this->scale_ = scale; |
---|
91 | } |
---|
92 | |
---|
93 | //! Scrolls every element in the set. |
---|
94 | void OverlayGroup::setScroll(const Vector2& scroll) |
---|
95 | { |
---|
96 | for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
97 | (*it)->scroll(scroll - this->scroll_); |
---|
98 | this->scroll_ = scroll; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | @brief |
---|
103 | Adds an element to the set (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 | hudElements_.insert(element); |
---|
110 | element->setOverlayGroup( this ); |
---|
111 | if (this->owner_) |
---|
112 | element->setOwner(this->owner_); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | @brief |
---|
117 | Removes an element from the map. |
---|
118 | @param element |
---|
119 | A pointer to the element that is removed. |
---|
120 | @return |
---|
121 | Returns true if there was such an element to remove, false if not. |
---|
122 | */ |
---|
123 | bool OverlayGroup::removeElement(OrxonoxOverlay* element) |
---|
124 | { |
---|
125 | if(this->hudElements_.erase(element) == 0) |
---|
126 | return false; |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | //! Returns a different element as long as index < hudElements_.size(). |
---|
131 | OrxonoxOverlay* OverlayGroup::getElement(unsigned int index) |
---|
132 | { |
---|
133 | if (index < this->hudElements_.size()) |
---|
134 | { |
---|
135 | std::set< StrongPtr<OrxonoxOverlay> >::const_iterator it = hudElements_.begin(); |
---|
136 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
137 | ; |
---|
138 | return *it; |
---|
139 | } |
---|
140 | else |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | //! Changes the visibility of all elements |
---|
145 | void OverlayGroup::changedVisibility() |
---|
146 | { |
---|
147 | SUPER( OverlayGroup, changedVisibility ); |
---|
148 | |
---|
149 | for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
150 | (*it)->changedVisibility(); //inform all Child Overlays that our visibility has changed |
---|
151 | } |
---|
152 | |
---|
153 | //! Changes the gametype of all elements |
---|
154 | void OverlayGroup::changedGametype() |
---|
155 | { |
---|
156 | SUPER( OverlayGroup, changedGametype ); |
---|
157 | |
---|
158 | for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
159 | (*it)->setGametype(this->getGametype()); |
---|
160 | } |
---|
161 | |
---|
162 | void OverlayGroup::setOwner(BaseObject* owner) |
---|
163 | { |
---|
164 | this->owner_ = owner; |
---|
165 | |
---|
166 | for (std::set< StrongPtr<OrxonoxOverlay> >::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it) |
---|
167 | (*it)->setOwner(owner); |
---|
168 | } |
---|
169 | |
---|
170 | //########### Console commands ############ |
---|
171 | |
---|
172 | /** |
---|
173 | @brief |
---|
174 | Hides/shows an overlay group by its name. |
---|
175 | @param name |
---|
176 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
177 | attribute in the xml file). |
---|
178 | */ |
---|
179 | /*static*/ void OverlayGroup::toggleVisibility(const std::string& name) |
---|
180 | { |
---|
181 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
182 | { |
---|
183 | if ((*it)->getName() == name) |
---|
184 | (*it)->setVisible(!((*it)->isVisible())); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | @brief |
---|
190 | Shows an overlay group by its name. |
---|
191 | @param name |
---|
192 | The name of the group defined BaseObject::setName() (usually done with the "name" attribute in the xml file). |
---|
193 | */ |
---|
194 | /*static*/ void OverlayGroup::show(const std::string& name) |
---|
195 | { |
---|
196 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
197 | { |
---|
198 | if ((*it)->getName() == name) |
---|
199 | { |
---|
200 | if((*it)->isVisible()) |
---|
201 | (*it)->changedVisibility(); |
---|
202 | else |
---|
203 | (*it)->setVisible(!((*it)->isVisible())); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | @brief |
---|
210 | Scales an overlay group by its name. |
---|
211 | @param name |
---|
212 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
213 | attribute in the xml file). |
---|
214 | @param scale |
---|
215 | The scaling factor |
---|
216 | */ |
---|
217 | /*static*/ void OverlayGroup::scaleGroup(const std::string& name, float scale) |
---|
218 | { |
---|
219 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
220 | { |
---|
221 | if ((*it)->getName() == name) |
---|
222 | (*it)->scale(Vector2(scale, scale)); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | @brief |
---|
228 | Scrolls an overlay group by its name. |
---|
229 | @param name |
---|
230 | The name of the group defined BaseObject::setName() (usually done with the "name" |
---|
231 | attribute in the xml file). |
---|
232 | @param scroll |
---|
233 | The relative translation of the overlay group |
---|
234 | */ |
---|
235 | /*static*/ void OverlayGroup::scrollGroup(const std::string& name, const Vector2& scroll) |
---|
236 | { |
---|
237 | for (ObjectList<OverlayGroup>::iterator it = ObjectList<OverlayGroup>::begin(); it; ++it) |
---|
238 | { |
---|
239 | if ((*it)->getName() == name) |
---|
240 | (*it)->scroll(scroll); |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|