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 | * Oliver Scheuss |
---|
27 | * Matthias Spalinger |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | #include "HUDNavigation.h" |
---|
32 | |
---|
33 | #include <OgreCamera.h> |
---|
34 | #include <OgreFontManager.h> |
---|
35 | #include <OgreOverlayManager.h> |
---|
36 | #include <OgreTextAreaOverlayElement.h> |
---|
37 | #include <OgrePanelOverlayElement.h> |
---|
38 | |
---|
39 | #include "util/Math.h" |
---|
40 | #include "util/Convert.h" |
---|
41 | #include "core/CoreIncludes.h" |
---|
42 | #include "core/XMLPort.h" |
---|
43 | #include "CameraManager.h" |
---|
44 | #include "Scene.h" |
---|
45 | #include "Radar.h" |
---|
46 | #include "graphics/Camera.h" |
---|
47 | #include "controllers/HumanController.h" |
---|
48 | #include "worldentities/pawns/Pawn.h" |
---|
49 | #include "worldentities/WorldEntity.h" |
---|
50 | #include "core/ConfigValueIncludes.h" |
---|
51 | #include "tools/TextureGenerator.h" |
---|
52 | // #include <boost/bind/bind_template.hpp> |
---|
53 | |
---|
54 | |
---|
55 | namespace orxonox |
---|
56 | { |
---|
57 | bool compareDistance(std::pair<RadarViewable*, unsigned int> a, std::pair<RadarViewable*, unsigned int> b) |
---|
58 | { |
---|
59 | return a.second < b.second; |
---|
60 | } |
---|
61 | |
---|
62 | CreateFactory ( HUDNavigation ); |
---|
63 | |
---|
64 | HUDNavigation::HUDNavigation(BaseObject* creator) : OrxonoxOverlay(creator) |
---|
65 | { |
---|
66 | RegisterObject(HUDNavigation); |
---|
67 | this->setConfigValues(); |
---|
68 | |
---|
69 | // Set default values |
---|
70 | this->setFont("Monofur"); |
---|
71 | this->setTextSize(0.05f); |
---|
72 | this->setNavMarkerSize(0.05f); |
---|
73 | this->setDetectionLimit(10000.0f); |
---|
74 | } |
---|
75 | |
---|
76 | HUDNavigation::~HUDNavigation() |
---|
77 | { |
---|
78 | if (this->isInitialized()) |
---|
79 | { |
---|
80 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end();) |
---|
81 | removeObject((it++)->first); |
---|
82 | } |
---|
83 | this->sortedObjectList_.clear(); |
---|
84 | } |
---|
85 | |
---|
86 | void HUDNavigation::setConfigValues() |
---|
87 | { |
---|
88 | SetConfigValue(markerLimit_, 3); |
---|
89 | SetConfigValue(showDistance_, false); |
---|
90 | } |
---|
91 | |
---|
92 | void HUDNavigation::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
93 | { |
---|
94 | SUPER(HUDNavigation, XMLPort, xmlelement, mode); |
---|
95 | |
---|
96 | XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlelement, mode); |
---|
97 | XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlelement, mode); |
---|
98 | XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlelement, mode); |
---|
99 | XMLPortParam(HUDNavigation, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode); |
---|
100 | } |
---|
101 | |
---|
102 | void HUDNavigation::setFont(const std::string& font) |
---|
103 | { |
---|
104 | const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font); |
---|
105 | if (fontPtr.isNull()) |
---|
106 | { |
---|
107 | orxout(internal_warning) << "HUDNavigation: Font '" << font << "' not found" << endl; |
---|
108 | return; |
---|
109 | } |
---|
110 | this->fontName_ = font; |
---|
111 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it) |
---|
112 | { |
---|
113 | if (it->second.text_ != NULL) |
---|
114 | it->second.text_->setFontName(this->fontName_); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | const std::string& HUDNavigation::getFont() const |
---|
119 | { |
---|
120 | return this->fontName_; |
---|
121 | } |
---|
122 | |
---|
123 | void HUDNavigation::setTextSize(float size) |
---|
124 | { |
---|
125 | if (size <= 0.0f) |
---|
126 | { |
---|
127 | orxout(internal_warning) << "HUDNavigation: Negative font size not allowed" << endl; |
---|
128 | return; |
---|
129 | } |
---|
130 | this->textSize_ = size; |
---|
131 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it!=this->activeObjectList_.end(); ++it) |
---|
132 | { |
---|
133 | if (it->second.text_) |
---|
134 | it->second.text_->setCharHeight(size); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | float HUDNavigation::getTextSize() const |
---|
139 | { |
---|
140 | return this->textSize_; |
---|
141 | } |
---|
142 | |
---|
143 | float HUDNavigation::getArrowSizeX(int dist) const |
---|
144 | { |
---|
145 | if (dist < 600) |
---|
146 | dist = 600; |
---|
147 | return this->getActualSize().x * 900 * this->navMarkerSize_ / dist; |
---|
148 | } |
---|
149 | |
---|
150 | float HUDNavigation::getArrowSizeY(int dist) const |
---|
151 | { |
---|
152 | if (dist < 600) |
---|
153 | dist = 600; |
---|
154 | return this->getActualSize().y * 900 * this->navMarkerSize_ / dist; |
---|
155 | } |
---|
156 | |
---|
157 | void HUDNavigation::tick(float dt) |
---|
158 | { |
---|
159 | SUPER(HUDNavigation, tick, dt); |
---|
160 | |
---|
161 | Camera* cam = CameraManager::getInstance().getActiveCamera(); |
---|
162 | if (cam == NULL) |
---|
163 | return; |
---|
164 | const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix(); |
---|
165 | |
---|
166 | |
---|
167 | for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt) |
---|
168 | listIt->second = (int)((listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f); |
---|
169 | |
---|
170 | this->sortedObjectList_.sort(compareDistance); |
---|
171 | |
---|
172 | unsigned int markerCount = 0; |
---|
173 | bool closeEnough = false; // only display objects that are close enough to be relevant for the player |
---|
174 | |
---|
175 | for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++markerCount, ++listIt) |
---|
176 | { |
---|
177 | std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(listIt->first); |
---|
178 | closeEnough = listIt->second < this->detectionLimit_; |
---|
179 | // display radarviewables on HUD if the marker limit and max-distance is not exceeded |
---|
180 | if (markerCount < this->markerLimit_ && (closeEnough || this->detectionLimit_ < 0)) |
---|
181 | { |
---|
182 | // Get Distance to HumanController and save it in the TextAreaOverlayElement. |
---|
183 | int dist = listIt->second; |
---|
184 | float textLength = 0.0f; |
---|
185 | |
---|
186 | if (this->showDistance_) |
---|
187 | { |
---|
188 | //display distance next to cursor |
---|
189 | it->second.text_->setCaption(multi_cast<std::string>(dist)); |
---|
190 | textLength = multi_cast<std::string>(dist).size() * it->second.text_->getCharHeight() * 0.3f; |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | //display name next to cursor |
---|
195 | it->second.text_->setCaption(it->first->getRadarName()); |
---|
196 | textLength = it->first->getRadarName().size() * it->second.text_->getCharHeight() * 0.3f; |
---|
197 | } |
---|
198 | |
---|
199 | // Transform to screen coordinates |
---|
200 | Vector3 pos = camTransform * it->first->getRVWorldPosition(); |
---|
201 | |
---|
202 | bool outOfView = true; |
---|
203 | if (pos.z > 1.0) |
---|
204 | { |
---|
205 | // z > 1.0 means that the object is behind the camera |
---|
206 | outOfView = true; |
---|
207 | // we have to switch all coordinates (if you don't know why, |
---|
208 | // try linear algebra lectures, because I can't explain..) |
---|
209 | pos.x = -pos.x; |
---|
210 | pos.y = -pos.y; |
---|
211 | } |
---|
212 | else |
---|
213 | outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; |
---|
214 | |
---|
215 | if (outOfView) |
---|
216 | { |
---|
217 | // Object is not in view |
---|
218 | |
---|
219 | // Change material only if outOfView changed |
---|
220 | if (!it->second.wasOutOfView_) |
---|
221 | { |
---|
222 | it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("arrows.png", it->first->getRadarObjectColour())); |
---|
223 | it->second.wasOutOfView_ = true; |
---|
224 | } |
---|
225 | |
---|
226 | //float xDistScale = this->getActualSize().x * 1000.0f * this->navMarkerSize_ / dist; |
---|
227 | //float yDistScale = this->getActualSize().y * 1000.0f * this->navMarkerSize_ / dist; |
---|
228 | |
---|
229 | // Adjust Arrowsize according to distance |
---|
230 | it->second.panel_->setDimensions(getArrowSizeX(dist), getArrowSizeY(dist)); |
---|
231 | |
---|
232 | // Switch between top, bottom, left and right position of the arrow at the screen border |
---|
233 | if (pos.x < pos.y) |
---|
234 | { |
---|
235 | if (pos.y > -pos.x) |
---|
236 | { |
---|
237 | // Top |
---|
238 | float position = pos.x / pos.y + 1.0f; |
---|
239 | it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 0.0f); |
---|
240 | it->second.panel_->setUV(0.5f, 0.0f, 1.0f, 0.5f); |
---|
241 | it->second.text_->setLeft((position - textLength) * 0.5f); |
---|
242 | it->second.text_->setTop(it->second.panel_->getHeight()); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | // Left |
---|
247 | float position = pos.y / pos.x + 1.0f; |
---|
248 | it->second.panel_->setPosition(0.0f, (position - it->second.panel_->getWidth()) * 0.5f); |
---|
249 | it->second.panel_->setUV(0.0f, 0.0f, 0.5f, 0.5f); |
---|
250 | it->second.text_->setLeft(it->second.panel_->getWidth() + 0.01f); |
---|
251 | it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); |
---|
252 | } |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | if (pos.y < -pos.x) |
---|
257 | { |
---|
258 | // Bottom |
---|
259 | float position = -pos.x / pos.y + 1.0f; |
---|
260 | it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 1.0f - it->second.panel_->getHeight()); |
---|
261 | it->second.panel_->setUV(0.0f, 0.5f, 0.5f, 1.0f ); |
---|
262 | it->second.text_->setLeft((position - textLength) * 0.5f); |
---|
263 | it->second.text_->setTop(1.0f - it->second.panel_->getHeight() - it->second.text_->getCharHeight()); |
---|
264 | } |
---|
265 | else |
---|
266 | { |
---|
267 | // Right |
---|
268 | float position = -pos.y / pos.x + 1.0f; |
---|
269 | it->second.panel_->setPosition(1.0f - it->second.panel_->getWidth(), (position - it->second.panel_->getHeight()) * 0.5f); |
---|
270 | it->second.panel_->setUV(0.5f, 0.5f, 1.0f, 1.0f); |
---|
271 | it->second.text_->setLeft(1.0f - it->second.panel_->getWidth() - textLength - 0.01f); |
---|
272 | it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | else |
---|
277 | { |
---|
278 | // Object is in view |
---|
279 | |
---|
280 | // Change material only if outOfView changed |
---|
281 | if (it->second.wasOutOfView_) |
---|
282 | { |
---|
283 | //it->second.panel_->setMaterialName("Orxonox/NavTDC"); |
---|
284 | it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("tdc.png", it->first->getRadarObjectColour())); |
---|
285 | it->second.panel_->setDimensions(this->navMarkerSize_ * this->getActualSize().x, this->navMarkerSize_ * this->getActualSize().y); |
---|
286 | it->second.wasOutOfView_ = false; |
---|
287 | } |
---|
288 | |
---|
289 | // Position marker |
---|
290 | it->second.panel_->setUV(0.0f, 0.0f, 1.0f, 1.0f); |
---|
291 | it->second.panel_->setLeft((pos.x + 1.0f - it->second.panel_->getWidth()) * 0.5f); |
---|
292 | it->second.panel_->setTop((-pos.y + 1.0f - it->second.panel_->getHeight()) * 0.5f); |
---|
293 | |
---|
294 | // Position text |
---|
295 | it->second.text_->setLeft((pos.x + 1.0f + it->second.panel_->getWidth()) * 0.5f); |
---|
296 | it->second.text_->setTop((-pos.y + 1.0f + it->second.panel_->getHeight()) * 0.5f); |
---|
297 | } |
---|
298 | |
---|
299 | // Make sure the overlays are shown |
---|
300 | it->second.panel_->show(); |
---|
301 | it->second.text_->show(); |
---|
302 | } |
---|
303 | else // do not display on HUD |
---|
304 | { |
---|
305 | it->second.panel_->hide(); |
---|
306 | it->second.text_->hide(); |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | /** Overridden method of OrxonoxOverlay. |
---|
312 | @details |
---|
313 | Usually the entire overlay scales with scale(). |
---|
314 | Here we obviously have to adjust this. |
---|
315 | */ |
---|
316 | void HUDNavigation::sizeChanged() |
---|
317 | { |
---|
318 | // Use size to compensate for aspect ratio if enabled. |
---|
319 | float xScale = this->getActualSize().x; |
---|
320 | float yScale = this->getActualSize().y; |
---|
321 | |
---|
322 | for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it) |
---|
323 | { |
---|
324 | if (it->second.panel_ != NULL) |
---|
325 | it->second.panel_->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale); |
---|
326 | if (it->second.text_ != NULL) |
---|
327 | it->second.text_->setCharHeight(it->second.text_->getCharHeight() * yScale); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | void HUDNavigation::addObject(RadarViewable* object) |
---|
332 | { |
---|
333 | if (showObject(object) == false) |
---|
334 | return; |
---|
335 | |
---|
336 | if (this->activeObjectList_.size() >= this->markerLimit_) |
---|
337 | if (object == NULL) |
---|
338 | return; |
---|
339 | |
---|
340 | // Object hasn't been added yet (we know that) |
---|
341 | assert(this->activeObjectList_.find(object) == this->activeObjectList_.end()); |
---|
342 | |
---|
343 | // Scales used for dimensions and text size |
---|
344 | float xScale = this->getActualSize().x; |
---|
345 | float yScale = this->getActualSize().y; |
---|
346 | |
---|
347 | // Create everything needed to display the object on the radar and add it to the map |
---|
348 | |
---|
349 | // Create arrow/marker |
---|
350 | Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton() |
---|
351 | .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); |
---|
352 | //panel->setMaterialName("Orxonox/NavTDC"); |
---|
353 | panel->setMaterialName(TextureGenerator::getMaterialName("tdc.png", object->getRadarObjectColour())); |
---|
354 | panel->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale); |
---|
355 | //panel->setColour(object->getRadarObjectColour()); |
---|
356 | |
---|
357 | Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton() |
---|
358 | .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); |
---|
359 | text->setFontName(this->fontName_); |
---|
360 | text->setCharHeight(text->getCharHeight() * yScale); |
---|
361 | text->setColour(object->getRadarObjectColour()); |
---|
362 | |
---|
363 | panel->hide(); |
---|
364 | text->hide(); |
---|
365 | |
---|
366 | ObjectInfo tempStruct = {panel, text, false /*, TODO: initialize wasOutOfView_ */}; |
---|
367 | this->activeObjectList_[object] = tempStruct; |
---|
368 | |
---|
369 | this->background_->addChild(panel); |
---|
370 | this->background_->addChild(text); |
---|
371 | |
---|
372 | this->sortedObjectList_.push_front(std::make_pair(object, (unsigned int)0)); |
---|
373 | } |
---|
374 | |
---|
375 | void HUDNavigation::removeObject(RadarViewable* viewable) |
---|
376 | { |
---|
377 | std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(viewable); |
---|
378 | |
---|
379 | if (this->activeObjectList_.find(viewable) != this->activeObjectList_.end()) |
---|
380 | { |
---|
381 | // Detach overlays |
---|
382 | this->background_->removeChild(it->second.panel_->getName()); |
---|
383 | this->background_->removeChild(it->second.text_->getName()); |
---|
384 | // Properly destroy the overlay elements (do not use delete!) |
---|
385 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.panel_); |
---|
386 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.text_); |
---|
387 | // Remove from the list |
---|
388 | this->activeObjectList_.erase(viewable); |
---|
389 | } |
---|
390 | |
---|
391 | for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt) |
---|
392 | { |
---|
393 | if ((listIt->first) == viewable) |
---|
394 | { |
---|
395 | this->sortedObjectList_.erase(listIt); |
---|
396 | break; |
---|
397 | } |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | void HUDNavigation::objectChanged(RadarViewable* viewable) |
---|
402 | { |
---|
403 | // TODO: niceification neccessary ;) |
---|
404 | removeObject(viewable); |
---|
405 | addObject(viewable); |
---|
406 | } |
---|
407 | |
---|
408 | bool HUDNavigation::showObject(RadarViewable* rv) |
---|
409 | { |
---|
410 | if (rv == orxonox_cast<RadarViewable*>(this->getOwner())) |
---|
411 | return false; |
---|
412 | assert(rv->getWorldEntity()); |
---|
413 | if (rv->getWorldEntity()->isVisible() == false || rv->getRadarVisibility() == false) |
---|
414 | return false; |
---|
415 | return true; |
---|
416 | } |
---|
417 | |
---|
418 | void HUDNavigation::changedOwner() |
---|
419 | { |
---|
420 | const std::set<RadarViewable*>& respawnObjects = this->getOwner()->getScene()->getRadar()->getRadarObjects(); |
---|
421 | for (std::set<RadarViewable*>::const_iterator it = respawnObjects.begin(); it != respawnObjects.end(); ++it) |
---|
422 | { |
---|
423 | if (!(*it)->isHumanShip_) |
---|
424 | this->addObject(*it); |
---|
425 | } |
---|
426 | } |
---|
427 | } |
---|