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 | * Felix Schulthess |
---|
24 | * Co-authors: |
---|
25 | * Reto Grieder |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "HUDNavigation.h" |
---|
31 | |
---|
32 | #include <OgreOverlayManager.h> |
---|
33 | #include <OgreStringConverter.h> |
---|
34 | |
---|
35 | //#include "GraphicsEngine.h" |
---|
36 | // TODO: remove the SpaceShip and CameraHandler dependencies |
---|
37 | #include "core/Debug.h" |
---|
38 | #include "core/CoreIncludes.h" |
---|
39 | #include "core/ConsoleCommand.h" |
---|
40 | #include "objects/SpaceShip.h" |
---|
41 | #include "objects/Projectile.h" |
---|
42 | #include "objects/CameraHandler.h" |
---|
43 | #include "overlays/OverlayGroup.h" |
---|
44 | #include "Radar.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | CreateFactory(HUDNavigation); |
---|
49 | |
---|
50 | //HUDNavigation* HUDNavigation::instance_s = 0; |
---|
51 | |
---|
52 | using namespace Ogre; |
---|
53 | |
---|
54 | HUDNavigation::HUDNavigation() |
---|
55 | : container_(0) |
---|
56 | , navMarker_(0) |
---|
57 | , aimMarker_(0) |
---|
58 | , navText_(0) |
---|
59 | { |
---|
60 | RegisterObject(HUDNavigation); |
---|
61 | |
---|
62 | /*assert(instance_s == 0); // singleton class |
---|
63 | HUDNavigation::instance_s = this;*/ |
---|
64 | } |
---|
65 | |
---|
66 | HUDNavigation::~HUDNavigation() |
---|
67 | { |
---|
68 | if (this->isInitialized()) |
---|
69 | { |
---|
70 | if (this->container_) |
---|
71 | OverlayManager::getSingleton().destroyOverlayElement(this->container_); |
---|
72 | if (this->navMarker_) |
---|
73 | OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_); |
---|
74 | if (this->navText_) |
---|
75 | OverlayManager::getSingleton().destroyOverlayElement(this->navText_); |
---|
76 | if (this->aimMarker_) |
---|
77 | OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_); |
---|
78 | } |
---|
79 | |
---|
80 | //HUDNavigation::instance_s = 0; |
---|
81 | } |
---|
82 | |
---|
83 | void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
84 | { |
---|
85 | OrxonoxOverlay::XMLPort(xmlElement, mode); |
---|
86 | |
---|
87 | if (mode == XMLPort::LoadObject) |
---|
88 | { |
---|
89 | // create container |
---|
90 | container_ = static_cast<OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_navContainer")); |
---|
91 | |
---|
92 | // create nav text |
---|
93 | navText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", getName() + "_navText")); |
---|
94 | navText_->setCharHeight(0.05f); |
---|
95 | navText_->setFontName("Monofur"); |
---|
96 | |
---|
97 | // create nav marker |
---|
98 | navMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_navMarker")); |
---|
99 | navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
100 | navMarkerSize_ = 0.05; //default |
---|
101 | wasOutOfView_ = true; // just to ensure the material is changed right the first time.. |
---|
102 | |
---|
103 | // create aim marker |
---|
104 | aimMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_aimMarker")); |
---|
105 | aimMarker_->setMaterialName("Orxonox/NavCrosshair"); |
---|
106 | aimMarkerSize_ = 0.04; // default |
---|
107 | |
---|
108 | container_->addChild(navMarker_); |
---|
109 | container_->addChild(aimMarker_); |
---|
110 | container_->addChild(navText_); |
---|
111 | container_->show(); |
---|
112 | |
---|
113 | overlay_->add2D(container_); |
---|
114 | } |
---|
115 | |
---|
116 | XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlElement, mode); |
---|
117 | XMLPortParam(HUDNavigation, "textsize", setTextSize, getTextSize, xmlElement, mode); |
---|
118 | XMLPortParam(HUDNavigation, "navmarkersize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode); |
---|
119 | XMLPortParam(HUDNavigation, "aimmarkersize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode); |
---|
120 | |
---|
121 | if (mode == XMLPort::LoadObject) |
---|
122 | { |
---|
123 | this->sizeChanged(); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | void HUDNavigation::setNavMarkerSize(float size) |
---|
128 | { |
---|
129 | this->navMarkerSize_ = size; |
---|
130 | } |
---|
131 | |
---|
132 | float HUDNavigation::getNavMarkerSize() const |
---|
133 | { |
---|
134 | return this->navMarkerSize_; |
---|
135 | } |
---|
136 | |
---|
137 | void HUDNavigation::setAimMarkerSize(float size) |
---|
138 | { |
---|
139 | this->aimMarkerSize_ = size; |
---|
140 | } |
---|
141 | |
---|
142 | float HUDNavigation::getAimMarkerSize() const |
---|
143 | { |
---|
144 | return this->aimMarkerSize_; |
---|
145 | } |
---|
146 | |
---|
147 | void HUDNavigation::setFont(const std::string& font) |
---|
148 | { |
---|
149 | if (this->navText_ && font != "") |
---|
150 | this->navText_->setFontName(font); |
---|
151 | } |
---|
152 | |
---|
153 | std::string HUDNavigation::getFont() const |
---|
154 | { |
---|
155 | if (this->navText_) |
---|
156 | return this->navText_->getFontName(); |
---|
157 | else |
---|
158 | return ""; |
---|
159 | } |
---|
160 | |
---|
161 | void HUDNavigation::setTextSize(float size) |
---|
162 | { |
---|
163 | if (this->navText_ && size >= 0.0f) |
---|
164 | this->navText_->setCharHeight(size); |
---|
165 | } |
---|
166 | |
---|
167 | float HUDNavigation::getTextSize() const |
---|
168 | { |
---|
169 | if (this->navText_) |
---|
170 | return this->navText_->getCharHeight(); |
---|
171 | else |
---|
172 | return 0.0f; |
---|
173 | } |
---|
174 | |
---|
175 | void HUDNavigation::tick(float dt) |
---|
176 | { |
---|
177 | if (!Radar::getInstance().getFocus()) |
---|
178 | { |
---|
179 | this->overlay_->hide(); |
---|
180 | return; |
---|
181 | } |
---|
182 | else |
---|
183 | { |
---|
184 | this->overlay_->show(); |
---|
185 | } |
---|
186 | |
---|
187 | // set text |
---|
188 | int dist = (int) getDist2Focus(); |
---|
189 | navText_->setCaption(convertToString(dist)); |
---|
190 | float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3; |
---|
191 | |
---|
192 | Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_; |
---|
193 | Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix(); |
---|
194 | // transform to screen coordinates |
---|
195 | Vector3 pos = transformationMatrix * Radar::getInstance().getFocus()->getWorldPosition(); |
---|
196 | |
---|
197 | bool outOfView; |
---|
198 | if (pos.z > 1.0) |
---|
199 | { |
---|
200 | // z > 1.0 means that the object is behind the camera |
---|
201 | outOfView = true; |
---|
202 | // we have to switch all coordinates (if you don't know why, |
---|
203 | // try linear algebra lectures, because I can't explain..) |
---|
204 | pos.x = -pos.x; |
---|
205 | pos.y = -pos.y; |
---|
206 | } |
---|
207 | else |
---|
208 | outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; |
---|
209 | |
---|
210 | if (outOfView) |
---|
211 | { |
---|
212 | // object is not in view |
---|
213 | aimMarker_->hide(); |
---|
214 | |
---|
215 | if (!wasOutOfView_) |
---|
216 | { |
---|
217 | navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
218 | wasOutOfView_ = true; |
---|
219 | } |
---|
220 | |
---|
221 | if (pos.x < pos.y) |
---|
222 | { |
---|
223 | if (pos.y > -pos.x) |
---|
224 | { |
---|
225 | // up |
---|
226 | float position = pos.x / pos.y + 1.0; |
---|
227 | navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0); |
---|
228 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
229 | navText_->setLeft((position - textLength) * 0.5); |
---|
230 | navText_->setTop(navMarker_->getHeight()); |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | // left |
---|
235 | float position = pos.y / pos.x + 1.0; |
---|
236 | navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5); |
---|
237 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
238 | navText_->setLeft(navMarker_->getWidth() + 0.01); |
---|
239 | navText_->setTop((position - navText_->getCharHeight()) * 0.5); |
---|
240 | } |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | if (pos.y < -pos.x) |
---|
245 | { |
---|
246 | // down |
---|
247 | float position = -pos.x / pos.y + 1.0; |
---|
248 | navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight()); |
---|
249 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
250 | navText_->setLeft((position - textLength) * 0.5); |
---|
251 | navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight()); |
---|
252 | } |
---|
253 | else |
---|
254 | { |
---|
255 | // right |
---|
256 | float position = -pos.y / pos.x + 1.0; |
---|
257 | navMarker_->setPosition(1.0 - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5); |
---|
258 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
259 | navText_->setLeft(1.0 - navMarker_->getWidth() - textLength - 0.01); |
---|
260 | navText_->setTop((position - navText_->getCharHeight()) * 0.5); |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | // object is in view |
---|
267 | |
---|
268 | Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(), |
---|
269 | Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity()); |
---|
270 | |
---|
271 | if (wasOutOfView_) |
---|
272 | { |
---|
273 | navMarker_->setMaterialName("Orxonox/NavTDC"); |
---|
274 | wasOutOfView_ = false; |
---|
275 | } |
---|
276 | |
---|
277 | // object is in view |
---|
278 | navMarker_->setUV(0.0, 0.0, 1.0, 1.0); |
---|
279 | navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5); |
---|
280 | navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5); |
---|
281 | |
---|
282 | aimMarker_->show(); |
---|
283 | aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5); |
---|
284 | aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5); |
---|
285 | |
---|
286 | navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5); |
---|
287 | navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | float HUDNavigation::getDist2Focus() const |
---|
292 | { |
---|
293 | if (Radar::getInstance().getFocus()) |
---|
294 | return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length(); |
---|
295 | else |
---|
296 | return 0; |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | @brief Overridden method of OrxonoxOverlay. Usually the entire overlay |
---|
301 | scales with scale(). Here we obviously have to adjust this. |
---|
302 | */ |
---|
303 | void HUDNavigation::sizeChanged() |
---|
304 | { |
---|
305 | // use size to compensate for apspect ratio if enabled. |
---|
306 | float xScale = this->getSize().x; |
---|
307 | float yScale = this->getSize().y; |
---|
308 | if (this->navMarker_) |
---|
309 | navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale); |
---|
310 | if (this->aimMarker_) |
---|
311 | aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale); |
---|
312 | if (this->navText_) |
---|
313 | navText_->setCharHeight(navText_->getCharHeight() * yScale); |
---|
314 | } |
---|
315 | |
---|
316 | /*static*/ /*HUDNavigation& HUDNavigation::getInstance() |
---|
317 | { |
---|
318 | assert(instance_s); |
---|
319 | return *instance_s; |
---|
320 | }*/ |
---|
321 | } |
---|