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 "HUDNavigation.h" |
---|
30 | #include <utility> |
---|
31 | |
---|
32 | #include <string> |
---|
33 | #include <OgreCamera.h> |
---|
34 | #include <OgreOverlayManager.h> |
---|
35 | #include <OgreTextAreaOverlayElement.h> |
---|
36 | #include <OgrePanelOverlayElement.h> |
---|
37 | #include "util/Math.h" |
---|
38 | #include "util/Convert.h" |
---|
39 | #include "core/CoreIncludes.h" |
---|
40 | #include "core/XMLPort.h" |
---|
41 | #include "CameraManager.h" |
---|
42 | #include "Scene.h" |
---|
43 | #include "Radar.h" |
---|
44 | #include "graphics/Camera.h" |
---|
45 | #include "controllers/HumanController.h" |
---|
46 | #include "worldentities/pawns/Pawn.h" |
---|
47 | #include "worldentities/WorldEntity.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | CreateFactory(HUDNavigation); |
---|
52 | |
---|
53 | HUDNavigation::HUDNavigation(BaseObject* creator) |
---|
54 | : OrxonoxOverlay(creator) |
---|
55 | { |
---|
56 | RegisterObject(HUDNavigation); |
---|
57 | |
---|
58 | // // create nav text |
---|
59 | // navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
60 | // .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); |
---|
61 | // |
---|
62 | // // create nav marker |
---|
63 | // navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
64 | // .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); |
---|
65 | // navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
66 | |
---|
67 | /* |
---|
68 | // create aim marker |
---|
69 | aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
70 | .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberString())); |
---|
71 | aimMarker_->setMaterialName("Orxonox/NavCrosshair"); |
---|
72 | this->wasOutOfView_ = true; // Ensure the material is changed right the first time.. |
---|
73 | |
---|
74 | setFont("Monofur"); |
---|
75 | setTextSize(0.05f); |
---|
76 | setNavMarkerSize(0.05f); |
---|
77 | setAimMarkerSize(0.04f); |
---|
78 | */ |
---|
79 | /* |
---|
80 | background_->addChild(navMarker_);*/ |
---|
81 | // background_->addChild(aimMarker_); |
---|
82 | // background_->addChild(navText_); |
---|
83 | |
---|
84 | // hide at first |
---|
85 | // this->setVisible(false); |
---|
86 | |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | HUDNavigation::~HUDNavigation() |
---|
91 | { |
---|
92 | if (this->isInitialized()) |
---|
93 | { |
---|
94 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_); |
---|
95 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_); |
---|
96 | // Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
101 | { |
---|
102 | SUPER(HUDNavigation, XMLPort, xmlElement, mode); |
---|
103 | |
---|
104 | XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlElement, mode); |
---|
105 | XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode); |
---|
106 | XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode); |
---|
107 | // XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode); |
---|
108 | } |
---|
109 | |
---|
110 | void HUDNavigation::setFont(const std::string& font) |
---|
111 | { |
---|
112 | if (this->navText_ && !font.empty()) |
---|
113 | this->navText_->setFontName(font); |
---|
114 | } |
---|
115 | |
---|
116 | const std::string& HUDNavigation::getFont() const |
---|
117 | { |
---|
118 | if (this->navText_) |
---|
119 | return this->navText_->getFontName(); |
---|
120 | else |
---|
121 | return BLANKSTRING; |
---|
122 | } |
---|
123 | |
---|
124 | void HUDNavigation::setTextSize(float size) |
---|
125 | { |
---|
126 | if (this->navText_ && size >= 0.0f) |
---|
127 | this->navText_->setCharHeight(size); |
---|
128 | } |
---|
129 | |
---|
130 | float HUDNavigation::getTextSize() const |
---|
131 | { |
---|
132 | if (this->navText_) |
---|
133 | return this->navText_->getCharHeight(); |
---|
134 | else |
---|
135 | return 0.0f; |
---|
136 | } |
---|
137 | |
---|
138 | void HUDNavigation::tick(float dt) |
---|
139 | { |
---|
140 | SUPER(HUDNavigation, tick, dt); |
---|
141 | |
---|
142 | // updateActiveObjectList(activeObjectList_); |
---|
143 | |
---|
144 | // Get radar |
---|
145 | // Radar* radar = this->getOwner()->getScene()->getRadar(); |
---|
146 | // |
---|
147 | // if (!radar->getFocus()) |
---|
148 | // { |
---|
149 | // this->overlay_->hide(); |
---|
150 | // return; |
---|
151 | // } |
---|
152 | // else |
---|
153 | // { |
---|
154 | // this->overlay_->show(); |
---|
155 | // } |
---|
156 | |
---|
157 | // set text |
---|
158 | // int dist = static_cast<int>(getDist2Focus()); |
---|
159 | // navText_->setCaption(multi_cast<std::string>(dist)); |
---|
160 | // float textLength = multi_cast<std::string>(dist).size() * navText_->getCharHeight() * 0.3f; |
---|
161 | |
---|
162 | if(!activeObjectList_.empty()) { |
---|
163 | for(tempRadarViewable = activeObjectList_.begin(); tempRadarViewable!=activeObjectList_.end(); ++tempRadarViewable) |
---|
164 | { |
---|
165 | |
---|
166 | //get Distance to HumanController and save it in the TextAreaOverlayElement. |
---|
167 | int dist = (int)(tempRadarViewable->first->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length(); |
---|
168 | tempRadarViewable->second.second->setCaption(multi_cast<std::string>(dist)); |
---|
169 | float textLength = multi_cast<std::string>(dist).size() * tempRadarViewable->second.second->getCharHeight() * 0.3f; |
---|
170 | |
---|
171 | orxonox::Camera* cam = CameraManager::getInstance().getActiveCamera(); |
---|
172 | if (!cam) |
---|
173 | return; |
---|
174 | const Matrix4& transform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix(); |
---|
175 | // transform to screen coordinates |
---|
176 | Vector3 pos = transform * tempRadarViewable->first->getRVWorldPosition(); |
---|
177 | |
---|
178 | bool outOfView; |
---|
179 | if (pos.z > 1.0) |
---|
180 | { |
---|
181 | // z > 1.0 means that the object is behind the camera |
---|
182 | outOfView = true; |
---|
183 | // we have to switch all coordinates (if you don't know why, |
---|
184 | // try linear algebra lectures, because I can't explain..) |
---|
185 | pos.x = -pos.x; |
---|
186 | pos.y = -pos.y; |
---|
187 | } |
---|
188 | else |
---|
189 | outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; |
---|
190 | |
---|
191 | if (outOfView) |
---|
192 | { |
---|
193 | // object is not in view |
---|
194 | // aimMarker_->hide(); |
---|
195 | |
---|
196 | if (!wasOutOfView_) |
---|
197 | { |
---|
198 | tempRadarViewable->second.first->setMaterialName("Orxonox/NavArrows"); |
---|
199 | wasOutOfView_ = true; |
---|
200 | } |
---|
201 | |
---|
202 | if (pos.x < pos.y) |
---|
203 | { |
---|
204 | if (pos.y > -pos.x) |
---|
205 | { |
---|
206 | // up |
---|
207 | float position = pos.x / pos.y + 1.0f; |
---|
208 | tempRadarViewable->second.first->setPosition((position - tempRadarViewable->second.first->getWidth()) * 0.5f, 0.0f); |
---|
209 | tempRadarViewable->second.first->setUV(0.5f, 0.0f, 1.0f, 0.5f); |
---|
210 | tempRadarViewable->second.second->setLeft((position - textLength) * 0.5f); |
---|
211 | tempRadarViewable->second.second->setTop(tempRadarViewable->second.first->getHeight()); |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | // left |
---|
216 | float position = pos.y / pos.x + 1.0f; |
---|
217 | tempRadarViewable->second.first->setPosition(0.0f, (position - tempRadarViewable->second.first->getWidth()) * 0.5f); |
---|
218 | tempRadarViewable->second.first->setUV(0.0f, 0.0f, 0.5f, 0.5f); |
---|
219 | tempRadarViewable->second.second->setLeft(tempRadarViewable->second.first->getWidth() + 0.01f); |
---|
220 | tempRadarViewable->second.second->setTop((position - tempRadarViewable->second.second->getCharHeight()) * 0.5f); |
---|
221 | } |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | if (pos.y < -pos.x) |
---|
226 | { |
---|
227 | // down |
---|
228 | float position = -pos.x / pos.y + 1.0f; |
---|
229 | tempRadarViewable->second.first->setPosition((position - tempRadarViewable->second.first->getWidth()) * 0.5f, 1.0f - navMarker_->getHeight()); |
---|
230 | tempRadarViewable->second.first->setUV(0.0f, 0.5f, 0.5f, 1.0f); |
---|
231 | tempRadarViewable->second.second->setLeft((position - textLength) * 0.5f); |
---|
232 | tempRadarViewable->second.second->setTop(1.0f - tempRadarViewable->second.first->getHeight() - tempRadarViewable->second.second->getCharHeight()); |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | // right |
---|
237 | float position = -pos.y / pos.x + 1.0f; |
---|
238 | tempRadarViewable->second.first->setPosition(1.0f - tempRadarViewable->second.first->getWidth(), (position - tempRadarViewable->second.first->getHeight()) * 0.5f); |
---|
239 | tempRadarViewable->second.first->setUV(0.5f, 0.5f, 1.0f, 1.0f); |
---|
240 | tempRadarViewable->second.second->setLeft(1.0f - tempRadarViewable->second.first->getWidth() - textLength - 0.01f); |
---|
241 | tempRadarViewable->second.second->setTop((position - tempRadarViewable->second.second->getCharHeight()) * 0.5f); |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | else |
---|
246 | { |
---|
247 | |
---|
248 | |
---|
249 | // object is in view |
---|
250 | /* |
---|
251 | Vector3 aimpos = transform * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(), |
---|
252 | Projectile::getSpeed(), Radar::getInstance().getFocus()->getRVWorldPosition(), Radar::getInstance().getFocus()->getRVOrientedVelocity()); |
---|
253 | */ |
---|
254 | if (wasOutOfView_) |
---|
255 | { |
---|
256 | tempRadarViewable->second.first->setMaterialName("Orxonox/NavTDC"); |
---|
257 | wasOutOfView_ = false; |
---|
258 | } |
---|
259 | |
---|
260 | // object is in view |
---|
261 | tempRadarViewable->second.first->setUV(0.0f, 0.0f, 1.0f, 1.0f); |
---|
262 | tempRadarViewable->second.first->setLeft((pos.x + 1.0f - tempRadarViewable->second.first->getWidth()) * 0.5f); |
---|
263 | tempRadarViewable->second.first->setTop((-pos.y + 1.0f - tempRadarViewable->second.first->getHeight()) * 0.5f); |
---|
264 | |
---|
265 | /* |
---|
266 | aimMarker_->show(); |
---|
267 | aimMarker_->setLeft((aimpos.x + 1.0f - aimMarker_->getWidth()) * 0.5f); |
---|
268 | aimMarker_->setTop((-aimpos.y + 1.0f - aimMarker_->getHeight()) * 0.5f); |
---|
269 | */ |
---|
270 | tempRadarViewable->second.second->setLeft((pos.x + 1.0f + tempRadarViewable->second.first->getWidth()) * 0.5f); |
---|
271 | tempRadarViewable->second.second->setTop((-pos.y + 1.0f + tempRadarViewable->second.first->getHeight()) * 0.5f); |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | float HUDNavigation::getDist2Focus() const{ |
---|
279 | |
---|
280 | Radar* radar = this->getOwner()->getScene()->getRadar(); |
---|
281 | if (radar->getFocus() && HumanController::getLocalControllerEntityAsPawn()) |
---|
282 | return (radar->getFocus()->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length(); |
---|
283 | else |
---|
284 | return 0; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | @brief Overridden method of OrxonoxOverlay. Usually the entire overlay |
---|
289 | scales with scale(). Here we obviously have to adjust this. |
---|
290 | */ |
---|
291 | void HUDNavigation::sizeChanged(){ |
---|
292 | // use size to compensate for aspect ratio if enabled. |
---|
293 | float xScale = this->getActualSize().x; |
---|
294 | float yScale = this->getActualSize().y; |
---|
295 | if (this->navMarker_) |
---|
296 | navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale); |
---|
297 | // if (this->aimMarker_) |
---|
298 | // aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale); |
---|
299 | if (this->navText_) |
---|
300 | navText_->setCharHeight(navText_->getCharHeight() * yScale); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | void HUDNavigation::addObject(RadarViewable* object){ |
---|
305 | if (object == dynamic_cast<RadarViewable*>(this->getOwner())) |
---|
306 | return; |
---|
307 | |
---|
308 | assert(object); |
---|
309 | |
---|
310 | // Make sure the object hasn't been added yet |
---|
311 | assert( this->activeObjectList_.find(object) == this->activeObjectList_.end() ); |
---|
312 | |
---|
313 | // Create everything needed to display the object on the radar and add it to the map |
---|
314 | |
---|
315 | // create nav marker |
---|
316 | Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
317 | .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); |
---|
318 | |
---|
319 | panel->setMaterialName("Orxonox/NavArrows"); |
---|
320 | |
---|
321 | Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
322 | .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); |
---|
323 | |
---|
324 | int dist = (int)(object->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length(); |
---|
325 | activeObjectList_[object].second->setCaption(multi_cast<std::string>(dist)); |
---|
326 | |
---|
327 | activeObjectList_[object] = std::make_pair (panel, text) ; |
---|
328 | activeObjectList_[object].first->show(); |
---|
329 | activeObjectList_[object].second->show(); |
---|
330 | // background_->addChild(activeObjectList_[object].first); |
---|
331 | // background_->addChild(activeObjectList_[object].second); |
---|
332 | } |
---|
333 | |
---|
334 | void HUDNavigation::removeObject(RadarViewable* viewable){ |
---|
335 | assert(activeObjectList_.find(viewable)!=activeObjectList_.end()); |
---|
336 | activeObjectList_.erase(viewable); |
---|
337 | } |
---|
338 | |
---|
339 | // void updateActiveObjectList(map activeObjectList_){} |
---|
340 | // |
---|
341 | // void HUDNavigation::objectChanged(RadarViewable* viewable){} |
---|
342 | // |
---|
343 | // |
---|
344 | // float HUDNavigation::getRadarSensitivity(){} |
---|
345 | // void HUDNavigation::radarTick(float dt){} |
---|
346 | |
---|
347 | |
---|
348 | } |
---|
349 | |
---|
350 | |
---|