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 OrxonoxOverlay class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "OrxonoxStableHeaders.h" |
---|
35 | #include "OrxonoxOverlay.h" |
---|
36 | |
---|
37 | #include <cmath> |
---|
38 | #include <OgreOverlay.h> |
---|
39 | #include <OgreOverlayManager.h> |
---|
40 | #include <OgrePanelOverlayElement.h> |
---|
41 | #include "util/Convert.h" |
---|
42 | #include "util/Exception.h" |
---|
43 | #include "util/String.h" |
---|
44 | #include "core/Core.h" |
---|
45 | #include "core/CoreIncludes.h" |
---|
46 | #include "core/XMLPort.h" |
---|
47 | #include "core/ConsoleCommand.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0; |
---|
52 | std::map<std::string, OrxonoxOverlay*> OrxonoxOverlay::overlays_s; |
---|
53 | |
---|
54 | SetConsoleCommand(OrxonoxOverlay, scaleOverlay, false).accessLevel(AccessLevel::User); |
---|
55 | SetConsoleCommand(OrxonoxOverlay, scrollOverlay, false).accessLevel(AccessLevel::User); |
---|
56 | SetConsoleCommand(OrxonoxOverlay, rotateOverlay, false).accessLevel(AccessLevel::User); |
---|
57 | |
---|
58 | OrxonoxOverlay::OrxonoxOverlay(BaseObject* creator) |
---|
59 | : BaseObject(creator) |
---|
60 | { |
---|
61 | RegisterObject(OrxonoxOverlay); |
---|
62 | |
---|
63 | this->owner_ = 0; |
---|
64 | |
---|
65 | if (!Core::showsGraphics()) |
---|
66 | ThrowException(NoGraphics, "Can't create OrxonoxOverlay, graphics engine not initialized"); |
---|
67 | |
---|
68 | // add this overlay to the static map of OrxonoxOverlays |
---|
69 | if (overlays_s.find(this->getName()) != overlays_s.end()) |
---|
70 | { |
---|
71 | COUT(1) << "Overlay names should be unique or you cannnot access them via console. Name: \"" << this->getName() << "\"" << std::endl; |
---|
72 | } |
---|
73 | overlays_s[this->getName()] = this; |
---|
74 | |
---|
75 | // create the Ogre::Overlay |
---|
76 | overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_" |
---|
77 | + convertToString(hudOverlayCounter_s++)); |
---|
78 | |
---|
79 | // create background panel (can be used to show any picture) |
---|
80 | this->background_ = static_cast<Ogre::PanelOverlayElement*>( |
---|
81 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", |
---|
82 | "OrxonoxOverlay_background_" + convertToString(hudOverlayCounter_s++))); |
---|
83 | this->overlay_->add2D(this->background_); |
---|
84 | |
---|
85 | // We'll have to set the aspect ratio to a default value first. |
---|
86 | // GSGraphics gets informed about our construction here and can update us in the next tick. |
---|
87 | this->windowAspectRatio_ = 1.0; |
---|
88 | this->sizeCorrectionChanged(); |
---|
89 | |
---|
90 | this->changedVisibility(); |
---|
91 | |
---|
92 | setSize(Vector2(1.0f, 1.0f)); |
---|
93 | setPickPoint(Vector2(0.0f, 0.0f)); |
---|
94 | setPosition(Vector2(0.0f, 0.0f)); |
---|
95 | setRotation(Degree(0.0)); |
---|
96 | setAspectCorrection(true); |
---|
97 | setBackgroundMaterial(""); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | @brief |
---|
102 | Make sure everything gets removed/destroyed. |
---|
103 | @remark |
---|
104 | We assume that the Ogre::OverlayManager exists (there is an assert in Ogre for that!) |
---|
105 | */ |
---|
106 | OrxonoxOverlay::~OrxonoxOverlay() |
---|
107 | { |
---|
108 | if (this->isInitialized()) |
---|
109 | { |
---|
110 | // erase ourself from the map with all overlays |
---|
111 | std::map<std::string, OrxonoxOverlay*>::iterator it = overlays_s.find(this->getName()); |
---|
112 | if (it != overlays_s.end()) |
---|
113 | overlays_s.erase(it); |
---|
114 | |
---|
115 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_); |
---|
116 | Ogre::OverlayManager::getSingleton().destroy(this->overlay_); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | @brief |
---|
122 | Loads the OrxonoxOverlay. |
---|
123 | |
---|
124 | This has to be called before usage, otherwise strange behaviour is |
---|
125 | guaranteed! (there should be no segfaults however). |
---|
126 | @copydoc |
---|
127 | BaseObject::XMLPort() |
---|
128 | */ |
---|
129 | void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
130 | { |
---|
131 | SUPER(OrxonoxOverlay, XMLPort, xmlElement, mode); |
---|
132 | |
---|
133 | XMLPortParam(OrxonoxOverlay, "size", setSize, getSize, xmlElement, mode); |
---|
134 | XMLPortParam(OrxonoxOverlay, "pickPoint", setPickPoint, getPickPoint, xmlElement, mode); |
---|
135 | XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode); |
---|
136 | XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode); |
---|
137 | XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode); |
---|
138 | XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode); |
---|
139 | } |
---|
140 | |
---|
141 | void OrxonoxOverlay::changedName() |
---|
142 | { |
---|
143 | OrxonoxOverlay::overlays_s.erase(this->getOldName()); |
---|
144 | |
---|
145 | if (OrxonoxOverlay::overlays_s.find(this->getName()) != OrxonoxOverlay::overlays_s.end()) |
---|
146 | COUT(1) << "Overlay names should be unique or you cannnot access them via console. Name: \"" << this->getName() << "\"" << std::endl; |
---|
147 | |
---|
148 | OrxonoxOverlay::overlays_s[this->getName()] = this; |
---|
149 | } |
---|
150 | |
---|
151 | //! Only sets the background material name if not "" |
---|
152 | void OrxonoxOverlay::setBackgroundMaterial(const std::string& material) |
---|
153 | { |
---|
154 | if (this->background_ && material != "") |
---|
155 | this->background_->setMaterialName(material); |
---|
156 | } |
---|
157 | |
---|
158 | //! Returns the the material name of the background |
---|
159 | const std::string& OrxonoxOverlay::getBackgroundMaterial() const |
---|
160 | { |
---|
161 | if (this->background_) |
---|
162 | return this->background_->getMaterialName(); |
---|
163 | else |
---|
164 | return BLANKSTRING; |
---|
165 | } |
---|
166 | |
---|
167 | //! Called by BaseObject when visibility has changed. |
---|
168 | void OrxonoxOverlay::changedVisibility() |
---|
169 | { |
---|
170 | if (!this->overlay_) |
---|
171 | return; |
---|
172 | |
---|
173 | if (this->isVisible()) |
---|
174 | this->overlay_->show(); |
---|
175 | else |
---|
176 | this->overlay_->hide(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | @brief |
---|
181 | Called by the GraphicsEngine whenever the window size changes. |
---|
182 | Calculates the aspect ratio only. |
---|
183 | */ |
---|
184 | void OrxonoxOverlay::windowResized(int newWidth, int newHeight) |
---|
185 | { |
---|
186 | this->windowAspectRatio_ = newWidth/(float)newHeight; |
---|
187 | this->sizeCorrectionChanged(); |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | @brief |
---|
192 | Called whenever the rotation angle has changed. |
---|
193 | */ |
---|
194 | void OrxonoxOverlay::angleChanged() |
---|
195 | { |
---|
196 | if (!this->overlay_) |
---|
197 | return; |
---|
198 | |
---|
199 | this->overlay_->setRotate(this->angle_); |
---|
200 | this->sizeCorrectionChanged(); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | @brief |
---|
205 | Called whenever the aspect ratio or the angle has changed. |
---|
206 | The method calculates a correction factor for the size to compensate |
---|
207 | for aspect distortion if desired. |
---|
208 | @remarks |
---|
209 | This only works when the angle is about 0 or 90 degrees. |
---|
210 | */ |
---|
211 | void OrxonoxOverlay::sizeCorrectionChanged() |
---|
212 | { |
---|
213 | if (this->bCorrectAspect_) |
---|
214 | { |
---|
215 | float angle = this->angle_.valueDegrees(); |
---|
216 | if (angle < 0.0) |
---|
217 | angle = -angle; |
---|
218 | angle -= 180.0 * (int)(angle / 180.0); |
---|
219 | |
---|
220 | // take the reverse if angle is about 90 degrees |
---|
221 | float tempAspect; |
---|
222 | if (angle > 89.0 && angle < 91.0) |
---|
223 | { |
---|
224 | tempAspect = 1.0 / this->windowAspectRatio_; |
---|
225 | rotState_ = Vertical; |
---|
226 | } |
---|
227 | else if (angle > 179 || angle < 1) |
---|
228 | { |
---|
229 | tempAspect = this->windowAspectRatio_; |
---|
230 | rotState_ = Horizontal; |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | tempAspect = 1.0; |
---|
235 | rotState_ = Inbetween; |
---|
236 | } |
---|
237 | |
---|
238 | // note: this is only an approximation that is mostly valid when the |
---|
239 | // magnitude of the width is about the magnitude of the height. |
---|
240 | // Correctly we would have to take the square root of width*height |
---|
241 | this->sizeCorrection_.x = 2.0 / (tempAspect + 1.0); |
---|
242 | this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x; |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | this->sizeCorrection_ = Vector2::UNIT_SCALE; |
---|
247 | } |
---|
248 | |
---|
249 | this->sizeChanged(); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | @brief |
---|
254 | Sets the overlay size using the actual corrected size. |
---|
255 | */ |
---|
256 | void OrxonoxOverlay::sizeChanged() |
---|
257 | { |
---|
258 | if (!this->overlay_) |
---|
259 | return; |
---|
260 | |
---|
261 | this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y); |
---|
262 | positionChanged(); |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | @brief |
---|
267 | Determines the position of the overlay. |
---|
268 | This works also well when a rotation angle is applied. The overlay always |
---|
269 | gets aligned correctly as well as possible. |
---|
270 | */ |
---|
271 | void OrxonoxOverlay::positionChanged() |
---|
272 | { |
---|
273 | if (!this->overlay_) |
---|
274 | return; |
---|
275 | |
---|
276 | // transform the angle to a range of 0 - pi/2 first. |
---|
277 | float angle = this->angle_.valueRadians(); |
---|
278 | if (angle < 0.0) |
---|
279 | angle = -angle; |
---|
280 | angle -= Ogre::Math::PI * (int)(angle / (Ogre::Math::PI)); |
---|
281 | if (angle > Ogre::Math::PI * 0.5) |
---|
282 | angle = Ogre::Math::PI - angle; |
---|
283 | |
---|
284 | // do some mathematical fiddling for a bounding box |
---|
285 | Vector2 actualSize = size_ * sizeCorrection_; |
---|
286 | float radius = actualSize.length(); |
---|
287 | float phi = atan(actualSize.y / actualSize.x); |
---|
288 | Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi)); |
---|
289 | |
---|
290 | // calculate the scrolling offset |
---|
291 | Vector2 scroll = (position_ - 0.5 - boundingBox * (pickPoint_ - 0.5)) * 2.0; |
---|
292 | this->overlay_->setScroll(scroll.x, -scroll.y); |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | //########### Console commands ############ |
---|
297 | |
---|
298 | /** |
---|
299 | @brief |
---|
300 | Scales an overlay by its name. |
---|
301 | @param name |
---|
302 | The name of the overlay defined BaseObject::setName() (usually done with the "name" |
---|
303 | attribute in the xml file). |
---|
304 | */ |
---|
305 | /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale) |
---|
306 | { |
---|
307 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name); |
---|
308 | if (it != overlays_s.end()) |
---|
309 | (*it).second->scale(Vector2(scale, scale)); |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | @brief |
---|
314 | Scrolls an overlay by its name. |
---|
315 | @param name |
---|
316 | The name of the overlay defined BaseObject::setName() (usually done with the "name" |
---|
317 | attribute in the xml file). |
---|
318 | */ |
---|
319 | /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll) |
---|
320 | { |
---|
321 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name); |
---|
322 | if (it != overlays_s.end()) |
---|
323 | (*it).second->scroll(scroll); |
---|
324 | } |
---|
325 | |
---|
326 | /** |
---|
327 | @brief |
---|
328 | Rotates an overlay by its name. |
---|
329 | @param name |
---|
330 | The name of the overlay defined BaseObject::setName() (usually done with the "name" |
---|
331 | attribute in the xml file). |
---|
332 | */ |
---|
333 | /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle) |
---|
334 | { |
---|
335 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name); |
---|
336 | if (it != overlays_s.end()) |
---|
337 | (*it).second->rotate(angle); |
---|
338 | } |
---|
339 | } |
---|