| 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 <typeinfo> | 
|---|
| 40 |  | 
|---|
| 41 | #include "util/Math.h" | 
|---|
| 42 | #include "util/Convert.h" | 
|---|
| 43 | #include "core/command/ConsoleCommand.h" | 
|---|
| 44 | #include "core/CoreIncludes.h" | 
|---|
| 45 | #include "core/XMLPort.h" | 
|---|
| 46 | #include "CameraManager.h" | 
|---|
| 47 | #include "Scene.h" | 
|---|
| 48 | #include "Radar.h" | 
|---|
| 49 | #include "graphics/Camera.h" | 
|---|
| 50 | #include "controllers/HumanController.h" | 
|---|
| 51 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 52 | #include "worldentities/WorldEntity.h" | 
|---|
| 53 | #include "core/config/ConfigValueIncludes.h" | 
|---|
| 54 | #include "tools/TextureGenerator.h" | 
|---|
| 55 | // #include <boost/bind/bind_template.hpp> | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | namespace orxonox | 
|---|
| 59 | { | 
|---|
| 60 |  | 
|---|
| 61 |     SetConsoleCommand("HUDNavigation","selectClosest", &HUDNavigation::selectClosestTarget).addShortcut().keybindMode(KeybindMode::OnPress); | 
|---|
| 62 |     SetConsoleCommand("HUDNavigation","selectNext", &HUDNavigation::selectNextTarget).addShortcut().keybindMode(KeybindMode::OnPress); | 
|---|
| 63 |  | 
|---|
| 64 |     static bool compareDistance(std::pair<RadarViewable*, unsigned int> a, | 
|---|
| 65 |             std::pair<RadarViewable*, unsigned int> b) | 
|---|
| 66 |     { | 
|---|
| 67 |         return a.second < b.second; | 
|---|
| 68 |     } | 
|---|
| 69 |     RegisterClass ( HUDNavigation ); | 
|---|
| 70 |  | 
|---|
| 71 |     HUDNavigation* HUDNavigation::localHUD_s = 0; | 
|---|
| 72 |  | 
|---|
| 73 |     HUDNavigation::HUDNavigation(Context* context) : | 
|---|
| 74 |         OrxonoxOverlay(context) | 
|---|
| 75 |     { | 
|---|
| 76 |         RegisterObject(HUDNavigation); | 
|---|
| 77 |         this->setConfigValues(); | 
|---|
| 78 |  | 
|---|
| 79 |         // Set default values | 
|---|
| 80 |         this->setFont("Monofur"); | 
|---|
| 81 |         this->setTextSize(0.05f); | 
|---|
| 82 |         this->setNavMarkerSize(0.03f); | 
|---|
| 83 |         this->setAimMarkerSize(0.02f); | 
|---|
| 84 |  | 
|---|
| 85 |         this->setDetectionLimit(10000.0f); | 
|---|
| 86 |         this->currentMunitionSpeed_ = 2500.0f; | 
|---|
| 87 |  | 
|---|
| 88 |         this->closestTarget_ = true; | 
|---|
| 89 |         this->nextTarget_ = false; | 
|---|
| 90 |         HUDNavigation::localHUD_s = this; | 
|---|
| 91 |     } | 
|---|
| 92 |  | 
|---|
| 93 |     HUDNavigation::~HUDNavigation() | 
|---|
| 94 |     { | 
|---|
| 95 |         if (this->isInitialized()) | 
|---|
| 96 |         { | 
|---|
| 97 |             for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end();) | 
|---|
| 98 |             removeObject((it++)->first); | 
|---|
| 99 |         } | 
|---|
| 100 |         this->sortedObjectList_.clear(); | 
|---|
| 101 |     } | 
|---|
| 102 |  | 
|---|
| 103 |     void HUDNavigation::setConfigValues() | 
|---|
| 104 |     { | 
|---|
| 105 |         SetConfigValue(markerLimit_, 3); | 
|---|
| 106 |         SetConfigValue(showDistance_, false); | 
|---|
| 107 |     } | 
|---|
| 108 |  | 
|---|
| 109 |     void HUDNavigation::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 110 |     { | 
|---|
| 111 |         SUPER(HUDNavigation, XMLPort, xmlelement, mode); | 
|---|
| 112 |  | 
|---|
| 113 |         XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlelement, mode); | 
|---|
| 114 |         XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlelement, mode); | 
|---|
| 115 |         XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlelement, mode); | 
|---|
| 116 |         XMLPortParam(HUDNavigation, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode); | 
|---|
| 117 |         XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlelement, mode); | 
|---|
| 118 |     } | 
|---|
| 119 |  | 
|---|
| 120 |     void HUDNavigation::setFont(const std::string& font) | 
|---|
| 121 |     { | 
|---|
| 122 |         const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font); | 
|---|
| 123 |         if (fontPtr.isNull()) | 
|---|
| 124 |         { | 
|---|
| 125 |             orxout(internal_warning) << "HUDNavigation: Font '" << font << "' not found" << endl; | 
|---|
| 126 |             return; | 
|---|
| 127 |         } | 
|---|
| 128 |         this->fontName_ = font; | 
|---|
| 129 |         for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it) | 
|---|
| 130 |         { | 
|---|
| 131 |             if (it->second.text_ != NULL) | 
|---|
| 132 |             it->second.text_->setFontName(this->fontName_); | 
|---|
| 133 |         } | 
|---|
| 134 |     } | 
|---|
| 135 |  | 
|---|
| 136 |     const std::string& HUDNavigation::getFont() const | 
|---|
| 137 |     { | 
|---|
| 138 |         return this->fontName_; | 
|---|
| 139 |     } | 
|---|
| 140 |  | 
|---|
| 141 |     void HUDNavigation::setTextSize(float size) | 
|---|
| 142 |     { | 
|---|
| 143 |         if (size <= 0.0f) | 
|---|
| 144 |         { | 
|---|
| 145 |             orxout(internal_warning) << "HUDNavigation: Negative font size not allowed" << endl; | 
|---|
| 146 |             return; | 
|---|
| 147 |         } | 
|---|
| 148 |         this->textSize_ = size; | 
|---|
| 149 |         for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it!=this->activeObjectList_.end(); ++it) | 
|---|
| 150 |         { | 
|---|
| 151 |             if (it->second.text_) | 
|---|
| 152 |             it->second.text_->setCharHeight(size); | 
|---|
| 153 |         } | 
|---|
| 154 |     } | 
|---|
| 155 |  | 
|---|
| 156 |     float HUDNavigation::getTextSize() const | 
|---|
| 157 |     { | 
|---|
| 158 |         return this->textSize_; | 
|---|
| 159 |     } | 
|---|
| 160 |  | 
|---|
| 161 |     float HUDNavigation::getArrowSizeX(int dist) const | 
|---|
| 162 |     { | 
|---|
| 163 |         if (dist < 600) | 
|---|
| 164 |         dist = 600; | 
|---|
| 165 |         return this->getActualSize().x * 900 * this->navMarkerSize_ / dist; | 
|---|
| 166 |     } | 
|---|
| 167 |  | 
|---|
| 168 |     float HUDNavigation::getArrowSizeY(int dist) const | 
|---|
| 169 |     { | 
|---|
| 170 |         if (dist < 600) | 
|---|
| 171 |         dist = 600; | 
|---|
| 172 |         return this->getActualSize().y * 900 * this->navMarkerSize_ / dist; | 
|---|
| 173 |     } | 
|---|
| 174 |  | 
|---|
| 175 |     void HUDNavigation::tick(float dt) | 
|---|
| 176 |     { | 
|---|
| 177 |         SUPER(HUDNavigation, tick, dt); | 
|---|
| 178 |  | 
|---|
| 179 |         Camera* cam = CameraManager::getInstance().getActiveCamera(); | 
|---|
| 180 |         if (cam == NULL) | 
|---|
| 181 |         return; | 
|---|
| 182 |         const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix(); | 
|---|
| 183 |  | 
|---|
| 184 |         for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt) | 
|---|
| 185 |         listIt->second = (int)((listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f); | 
|---|
| 186 |  | 
|---|
| 187 |         this->sortedObjectList_.sort(compareDistance); | 
|---|
| 188 |  | 
|---|
| 189 |         unsigned int markerCount = 0; | 
|---|
| 190 |         bool closeEnough = false; // only display objects that are close enough to be relevant for the player | 
|---|
| 191 |  | 
|---|
| 192 |         // if the selected object doesn't exist any more or is now out of range select the closest object | 
|---|
| 193 |         std::map<RadarViewable*, ObjectInfo>::iterator selectedActiveObject = this->activeObjectList_.find(this->selectedTarget_); | 
|---|
| 194 |         if(selectedActiveObject == this->activeObjectList_.end()) | 
|---|
| 195 |         { | 
|---|
| 196 |             this->closestTarget_ = true; | 
|---|
| 197 |         } | 
|---|
| 198 |         else if(this->detectionLimit_ < (this->selectedTarget_->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition()).length() + 0.5f) | 
|---|
| 199 |         { | 
|---|
| 200 |             this->closestTarget_ = true; | 
|---|
| 201 |             selectedActiveObject->second.selected_ = false; | 
|---|
| 202 |         } | 
|---|
| 203 |  | 
|---|
| 204 |         bool nextHasToBeSelected = false; | 
|---|
| 205 |  | 
|---|
| 206 |         for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++markerCount, ++listIt) | 
|---|
| 207 |         { | 
|---|
| 208 |             std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(listIt->first); | 
|---|
| 209 |             closeEnough = listIt->second < this->detectionLimit_; | 
|---|
| 210 |             // display radarviewables on HUD if the marker limit and max-distance is not exceeded | 
|---|
| 211 |             if (markerCount < this->markerLimit_ && (closeEnough || this->detectionLimit_ < 0)) | 
|---|
| 212 |             { | 
|---|
| 213 |                 // Get Distance to HumanController and save it in the TextAreaOverlayElement. | 
|---|
| 214 |                 int dist = listIt->second; | 
|---|
| 215 |                 float textLength = 0.0f; | 
|---|
| 216 |  | 
|---|
| 217 |                 if (this->showDistance_) | 
|---|
| 218 |                 { | 
|---|
| 219 |                     //display distance next to cursor | 
|---|
| 220 |                     it->second.text_->setCaption(multi_cast<std::string>(dist)); | 
|---|
| 221 |                     textLength = multi_cast<std::string>(dist).size() * it->second.text_->getCharHeight() * 0.3f; | 
|---|
| 222 |                 } | 
|---|
| 223 |                 else | 
|---|
| 224 |                 { | 
|---|
| 225 |                     //display name next to cursor | 
|---|
| 226 |                     it->second.text_->setCaption(it->first->getRadarName()); | 
|---|
| 227 |                     textLength = it->first->getRadarName().size() * it->second.text_->getCharHeight() * 0.3f; | 
|---|
| 228 |                 } | 
|---|
| 229 |  | 
|---|
| 230 |                 // select the object that aim-assistant indicates | 
|---|
| 231 |                 if(this->closestTarget_) | 
|---|
| 232 |                 // select the closest object | 
|---|
| 233 |                 { | 
|---|
| 234 |                     if(listIt == this->sortedObjectList_.begin()) | 
|---|
| 235 |                     { | 
|---|
| 236 |                         it->second.selected_ = true; | 
|---|
| 237 |                         this->selectedTarget_ = it->first; | 
|---|
| 238 |                     } | 
|---|
| 239 |                     else if(it->second.selected_) | 
|---|
| 240 |                     { | 
|---|
| 241 |                         it->second.selected_ = false; | 
|---|
| 242 |                     } | 
|---|
| 243 |  | 
|---|
| 244 |                 } | 
|---|
| 245 |                 else if(this->nextTarget_) | 
|---|
| 246 |                 // select the next object in sortedObjectList | 
|---|
| 247 |                 { | 
|---|
| 248 |                     if(nextHasToBeSelected){ | 
|---|
| 249 |                         it->second.selected_ = true; | 
|---|
| 250 |                         this->selectedTarget_ = it->first; | 
|---|
| 251 |                         nextHasToBeSelected = false; | 
|---|
| 252 |                     } | 
|---|
| 253 |                     else if(it->second.selected_) | 
|---|
| 254 |                     { | 
|---|
| 255 |                         nextHasToBeSelected = true; | 
|---|
| 256 |                         it->second.selected_ = false; | 
|---|
| 257 |  | 
|---|
| 258 |                         // check if there's a next object | 
|---|
| 259 |                         listIt++; | 
|---|
| 260 |                         if(listIt != this->sortedObjectList_.end()) | 
|---|
| 261 |                         { | 
|---|
| 262 |                             // and if the marker limit and max-distance are not exceeded for it | 
|---|
| 263 |                             if (markerCount + 1 >= this->markerLimit_ || | 
|---|
| 264 |                                     (listIt->second > this->detectionLimit_ && detectionLimit_ >= 0)) | 
|---|
| 265 |                             { | 
|---|
| 266 |                                 // otherwise select the closest object | 
|---|
| 267 |                                 this->activeObjectList_.find(this->sortedObjectList_.begin()->first)->second.selected_ = true; | 
|---|
| 268 |                                 this->selectedTarget_ = it->first; | 
|---|
| 269 |                                 nextHasToBeSelected = false; | 
|---|
| 270 |                             } | 
|---|
| 271 |                         } | 
|---|
| 272 |                         listIt--; | 
|---|
| 273 |                     } | 
|---|
| 274 |                 } | 
|---|
| 275 |  | 
|---|
| 276 |  | 
|---|
| 277 |                 // Transform to screen coordinates | 
|---|
| 278 |                 Vector3 pos = camTransform * it->first->getRVWorldPosition(); | 
|---|
| 279 |  | 
|---|
| 280 |                 bool outOfView = true; | 
|---|
| 281 |                 if (pos.z > 1.0) | 
|---|
| 282 |                 { | 
|---|
| 283 |                     // z > 1.0 means that the object is behind the camera | 
|---|
| 284 |                     outOfView = true; | 
|---|
| 285 |                     // we have to switch all coordinates (if you don't know why, | 
|---|
| 286 |                     // try linear algebra lectures, because I can't explain..) | 
|---|
| 287 |                     pos.x = -pos.x; | 
|---|
| 288 |                     pos.y = -pos.y; | 
|---|
| 289 |                 } | 
|---|
| 290 |                 else | 
|---|
| 291 |                     outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; | 
|---|
| 292 |  | 
|---|
| 293 |                 if (outOfView) | 
|---|
| 294 |                 { | 
|---|
| 295 |                     // Object is not in view | 
|---|
| 296 |  | 
|---|
| 297 |                     // Change material only if outOfView changed | 
|---|
| 298 |                     if (!it->second.wasOutOfView_) | 
|---|
| 299 |                     { | 
|---|
| 300 |                         it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("arrows.png", it->first->getRadarObjectColour())); | 
|---|
| 301 |                         it->second.wasOutOfView_ = true; | 
|---|
| 302 |                         it->second.target_->hide(); | 
|---|
| 303 |                     } | 
|---|
| 304 |  | 
|---|
| 305 |                     //float xDistScale = this->getActualSize().x * 1000.0f * this->navMarkerSize_ / dist; | 
|---|
| 306 |                     //float yDistScale = this->getActualSize().y * 1000.0f * this->navMarkerSize_ / dist; | 
|---|
| 307 |  | 
|---|
| 308 |                     // Adjust Arrowsize according to distance | 
|---|
| 309 |                     it->second.panel_->setDimensions(getArrowSizeX(dist), getArrowSizeY(dist)); | 
|---|
| 310 |  | 
|---|
| 311 |                     // Switch between top, bottom, left and right position of the arrow at the screen border | 
|---|
| 312 |                     if (pos.x < pos.y) | 
|---|
| 313 |                     { | 
|---|
| 314 |                         if (pos.y > -pos.x) | 
|---|
| 315 |                         { | 
|---|
| 316 |                             // Top | 
|---|
| 317 |                             float position = pos.x / pos.y + 1.0f; | 
|---|
| 318 |                             it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 0.0f); | 
|---|
| 319 |                             it->second.panel_->setUV(0.5f, 0.0f, 1.0f, 0.5f); | 
|---|
| 320 |                             it->second.text_->setLeft((position - textLength) * 0.5f); | 
|---|
| 321 |                             it->second.text_->setTop(it->second.panel_->getHeight()); | 
|---|
| 322 |                         } | 
|---|
| 323 |                         else | 
|---|
| 324 |                         { | 
|---|
| 325 |                             // Left | 
|---|
| 326 |                             float position = pos.y / pos.x + 1.0f; | 
|---|
| 327 |                             it->second.panel_->setPosition(0.0f, (position - it->second.panel_->getWidth()) * 0.5f); | 
|---|
| 328 |                             it->second.panel_->setUV(0.0f, 0.0f, 0.5f, 0.5f); | 
|---|
| 329 |                             it->second.text_->setLeft(it->second.panel_->getWidth() + 0.01f); | 
|---|
| 330 |                             it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); | 
|---|
| 331 |                         } | 
|---|
| 332 |                     } | 
|---|
| 333 |                     else | 
|---|
| 334 |                     { | 
|---|
| 335 |                         if (pos.y < -pos.x) | 
|---|
| 336 |                         { | 
|---|
| 337 |                             // Bottom | 
|---|
| 338 |                             float position = -pos.x / pos.y + 1.0f; | 
|---|
| 339 |                             it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 1.0f - it->second.panel_->getHeight()); | 
|---|
| 340 |                             it->second.panel_->setUV(0.0f, 0.5f, 0.5f, 1.0f ); | 
|---|
| 341 |                             it->second.text_->setLeft((position - textLength) * 0.5f); | 
|---|
| 342 |                             it->second.text_->setTop(1.0f - it->second.panel_->getHeight() - it->second.text_->getCharHeight()); | 
|---|
| 343 |                         } | 
|---|
| 344 |                         else | 
|---|
| 345 |                         { | 
|---|
| 346 |                             // Right | 
|---|
| 347 |                             float position = -pos.y / pos.x + 1.0f; | 
|---|
| 348 |                             it->second.panel_->setPosition(1.0f - it->second.panel_->getWidth(), (position - it->second.panel_->getHeight()) * 0.5f); | 
|---|
| 349 |                             it->second.panel_->setUV(0.5f, 0.5f, 1.0f, 1.0f); | 
|---|
| 350 |                             it->second.text_->setLeft(1.0f - it->second.panel_->getWidth() - textLength - 0.01f); | 
|---|
| 351 |                             it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); | 
|---|
| 352 |                         } | 
|---|
| 353 |                     } | 
|---|
| 354 |                 } | 
|---|
| 355 |                 else | 
|---|
| 356 |                 { | 
|---|
| 357 |                     // Object is in view | 
|---|
| 358 |  | 
|---|
| 359 |                     // Change material only if outOfView changed | 
|---|
| 360 |                     if (it->second.wasOutOfView_) | 
|---|
| 361 |                     { | 
|---|
| 362 |                         //it->second.panel_->setMaterialName("Orxonox/NavTDC"); | 
|---|
| 363 |                         it->second.panel_->setMaterialName(TextureGenerator::getMaterialName("tdc.png", it->first->getRadarObjectColour())); | 
|---|
| 364 |                         it->second.panel_->setDimensions(this->navMarkerSize_ * this->getActualSize().x, this->navMarkerSize_ * this->getActualSize().y); | 
|---|
| 365 |                         it->second.target_->setDimensions(this->aimMarkerSize_ * this->getActualSize().x, this->aimMarkerSize_ * this->getActualSize().y); | 
|---|
| 366 |                         it->second.wasOutOfView_ = false; | 
|---|
| 367 |                     } | 
|---|
| 368 |  | 
|---|
| 369 |                     // Position marker | 
|---|
| 370 |                     it->second.panel_->setUV(0.0f, 0.0f, 1.0f, 1.0f); | 
|---|
| 371 |                     it->second.panel_->setLeft((pos.x + 1.0f - it->second.panel_->getWidth()) * 0.5f); | 
|---|
| 372 |                     it->second.panel_->setTop((-pos.y + 1.0f - it->second.panel_->getHeight()) * 0.5f); | 
|---|
| 373 |  | 
|---|
| 374 |                     // Position text | 
|---|
| 375 |                     it->second.text_->setLeft((pos.x + 1.0f + it->second.panel_->getWidth()) * 0.5f); | 
|---|
| 376 |                     it->second.text_->setTop((-pos.y + 1.0f + it->second.panel_->getHeight()) * 0.5f); | 
|---|
| 377 |  | 
|---|
| 378 |                     // Make sure the overlays are shown | 
|---|
| 379 |                     it->second.panel_->show(); | 
|---|
| 380 |                     it->second.text_->show(); | 
|---|
| 381 |  | 
|---|
| 382 |                     // Target marker | 
|---|
| 383 |                     const Pawn* pawn = dynamic_cast<const Pawn*>(it->first->getWorldEntity()); | 
|---|
| 384 |                     /* Pawn* humanPawn = HumanController::getLocalControllerEntityAsPawn();*/ | 
|---|
| 385 |                     if(!it->second.selected_ | 
|---|
| 386 |                             || it->first->getRVVelocity().squaredLength() == 0 | 
|---|
| 387 |                             || pawn == NULL | 
|---|
| 388 |                             /* TODO : improve getTeam in such a way that it works | 
|---|
| 389 |                              * || humanPawn == NULL | 
|---|
| 390 |                              * || pawn->getTeam() == humanPawn->getTeam()*/) | 
|---|
| 391 |                     { | 
|---|
| 392 |                         // don't show marker for not selected enemies nor if the selected doesn't move | 
|---|
| 393 |                         it->second.target_->hide(); | 
|---|
| 394 |                     } | 
|---|
| 395 |                     else // object is selected and moves | 
|---|
| 396 |                     { | 
|---|
| 397 |                         // get the aim position | 
|---|
| 398 |                         Vector3* targetPos = this->toAimPosition(it->first); | 
|---|
| 399 |                         // Transform to screen coordinates | 
|---|
| 400 |                         Vector3 screenPos = camTransform * (*targetPos); | 
|---|
| 401 |                         // Check if the target marker is in view too | 
|---|
| 402 |                         if(screenPos.z > 1 || screenPos.x < -1.0 || screenPos.x > 1.0 | 
|---|
| 403 |                                 || screenPos.y < -1.0 || screenPos.y > 1.0) | 
|---|
| 404 |                         { | 
|---|
| 405 |                             it->second.target_->hide(); | 
|---|
| 406 |                         } | 
|---|
| 407 |                         else | 
|---|
| 408 |                         { | 
|---|
| 409 |                             it->second.target_->setLeft((screenPos.x + 1.0f - it->second.target_->getWidth()) * 0.5f); | 
|---|
| 410 |                             it->second.target_->setTop((-screenPos.y + 1.0f - it->second.target_->getHeight()) * 0.5f); | 
|---|
| 411 |                             it->second.target_->show(); | 
|---|
| 412 |                         } | 
|---|
| 413 |                         delete targetPos; | 
|---|
| 414 |                     } | 
|---|
| 415 |  | 
|---|
| 416 |                 } | 
|---|
| 417 |             } | 
|---|
| 418 |             else // do not display on HUD | 
|---|
| 419 |  | 
|---|
| 420 |             { | 
|---|
| 421 |                 it->second.panel_->hide(); | 
|---|
| 422 |                 it->second.text_->hide(); | 
|---|
| 423 |                 it->second.target_->hide(); | 
|---|
| 424 |             } | 
|---|
| 425 |         } | 
|---|
| 426 |  | 
|---|
| 427 |         this->closestTarget_ = false; | 
|---|
| 428 |         this->nextTarget_ = false; | 
|---|
| 429 |     } | 
|---|
| 430 |  | 
|---|
| 431 |     /** Overridden method of OrxonoxOverlay. | 
|---|
| 432 |      @details | 
|---|
| 433 |      Usually the entire overlay scales with scale(). | 
|---|
| 434 |      Here we obviously have to adjust this. | 
|---|
| 435 |      */ | 
|---|
| 436 |     void HUDNavigation::sizeChanged() | 
|---|
| 437 |     { | 
|---|
| 438 |         // Use size to compensate for aspect ratio if enabled. | 
|---|
| 439 |         float xScale = this->getActualSize().x; | 
|---|
| 440 |         float yScale = this->getActualSize().y; | 
|---|
| 441 |  | 
|---|
| 442 |         for (std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.begin(); it != this->activeObjectList_.end(); ++it) | 
|---|
| 443 |         { | 
|---|
| 444 |             if (it->second.panel_ != NULL) | 
|---|
| 445 |                 it->second.panel_->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale); | 
|---|
| 446 |             if (it->second.text_ != NULL) | 
|---|
| 447 |                 it->second.text_->setCharHeight(it->second.text_->getCharHeight() * yScale); | 
|---|
| 448 |             if (it->second.target_ != NULL) | 
|---|
| 449 |                 it->second.target_->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale); | 
|---|
| 450 |         } | 
|---|
| 451 |     } | 
|---|
| 452 |  | 
|---|
| 453 |     void HUDNavigation::addObject(RadarViewable* object) | 
|---|
| 454 |     { | 
|---|
| 455 |         if (showObject(object) == false) | 
|---|
| 456 |         return; | 
|---|
| 457 |  | 
|---|
| 458 |         if (this->activeObjectList_.size() >= this->markerLimit_) | 
|---|
| 459 |         if (object == NULL) | 
|---|
| 460 |         return; | 
|---|
| 461 |  | 
|---|
| 462 |         // Object hasn't been added yet (we know that) | 
|---|
| 463 |         assert(this->activeObjectList_.find(object) == this->activeObjectList_.end()); | 
|---|
| 464 |  | 
|---|
| 465 |         // Scales used for dimensions and text size | 
|---|
| 466 |         float xScale = this->getActualSize().x; | 
|---|
| 467 |         float yScale = this->getActualSize().y; | 
|---|
| 468 |  | 
|---|
| 469 |         // Create everything needed to display the object on the radar and add it to the map | 
|---|
| 470 |  | 
|---|
| 471 |         // Create arrow/marker | 
|---|
| 472 |         Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>( Ogre::OverlayManager::getSingleton() | 
|---|
| 473 |                 .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); | 
|---|
| 474 |         //panel->setMaterialName("Orxonox/NavTDC"); | 
|---|
| 475 |         panel->setMaterialName(TextureGenerator::getMaterialName("tdc.png", object->getRadarObjectColour())); | 
|---|
| 476 |         panel->setDimensions(this->navMarkerSize_ * xScale, this->navMarkerSize_ * yScale); | 
|---|
| 477 |         //panel->setColour(object->getRadarObjectColour()); | 
|---|
| 478 |  | 
|---|
| 479 |         // Create target marker | 
|---|
| 480 |         Ogre::PanelOverlayElement* target = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
| 481 |                     .createOverlayElement("Panel", "HUDNavigation_targetMarker_" + getUniqueNumberString())); | 
|---|
| 482 |         target->setMaterialName(TextureGenerator::getMaterialName("target.png", object->getRadarObjectColour())); | 
|---|
| 483 |         target->setDimensions(this->aimMarkerSize_ * xScale, this->aimMarkerSize_ * yScale); | 
|---|
| 484 |  | 
|---|
| 485 |         // Create text | 
|---|
| 486 |         Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton() | 
|---|
| 487 |                 .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); | 
|---|
| 488 |         text->setFontName(this->fontName_); | 
|---|
| 489 |         text->setCharHeight(text->getCharHeight() * yScale); | 
|---|
| 490 |         text->setColour(object->getRadarObjectColour()); | 
|---|
| 491 |  | 
|---|
| 492 |         panel->hide(); | 
|---|
| 493 |         target->hide(); | 
|---|
| 494 |         text->hide(); | 
|---|
| 495 |  | 
|---|
| 496 |         ObjectInfo tempStruct = | 
|---|
| 497 |         {   panel, target, text, false, false, false}; | 
|---|
| 498 |         this->activeObjectList_[object] = tempStruct; | 
|---|
| 499 |  | 
|---|
| 500 |         this->background_->addChild(panel); | 
|---|
| 501 |         this->background_->addChild(target); | 
|---|
| 502 |         this->background_->addChild(text); | 
|---|
| 503 |  | 
|---|
| 504 |         this->sortedObjectList_.push_front(std::make_pair(object, (unsigned int)0)); | 
|---|
| 505 |     } | 
|---|
| 506 |  | 
|---|
| 507 |     void HUDNavigation::removeObject(RadarViewable* viewable) | 
|---|
| 508 |     { | 
|---|
| 509 |         std::map<RadarViewable*, ObjectInfo>::iterator it = this->activeObjectList_.find(viewable); | 
|---|
| 510 |  | 
|---|
| 511 |         if (this->activeObjectList_.find(viewable) != this->activeObjectList_.end()) | 
|---|
| 512 |         { | 
|---|
| 513 |             // Detach overlays | 
|---|
| 514 |             this->background_->removeChild(it->second.panel_->getName()); | 
|---|
| 515 |             this->background_->removeChild(it->second.target_->getName()); | 
|---|
| 516 |             this->background_->removeChild(it->second.text_->getName()); | 
|---|
| 517 |             // Properly destroy the overlay elements (do not use delete!) | 
|---|
| 518 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.panel_); | 
|---|
| 519 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.target_); | 
|---|
| 520 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.text_); | 
|---|
| 521 |             // Remove from the list | 
|---|
| 522 |             this->activeObjectList_.erase(viewable); | 
|---|
| 523 |         } | 
|---|
| 524 |  | 
|---|
| 525 |         for (std::list<std::pair<RadarViewable*, unsigned int> >::iterator listIt = this->sortedObjectList_.begin(); listIt != this->sortedObjectList_.end(); ++listIt) | 
|---|
| 526 |         { | 
|---|
| 527 |             if ((listIt->first) == viewable) | 
|---|
| 528 |             { | 
|---|
| 529 |                 this->sortedObjectList_.erase(listIt); | 
|---|
| 530 |                 break; | 
|---|
| 531 |             } | 
|---|
| 532 |         } | 
|---|
| 533 |     } | 
|---|
| 534 |  | 
|---|
| 535 |     void HUDNavigation::objectChanged(RadarViewable* viewable) | 
|---|
| 536 |     { | 
|---|
| 537 |         // TODO: niceification neccessary ;) | 
|---|
| 538 |         removeObject(viewable); | 
|---|
| 539 |         addObject(viewable); | 
|---|
| 540 |     } | 
|---|
| 541 |  | 
|---|
| 542 |     bool HUDNavigation::showObject(RadarViewable* rv) | 
|---|
| 543 |     { | 
|---|
| 544 |         if (rv == orxonox_cast<RadarViewable*>(this->getOwner())) | 
|---|
| 545 |         return false; | 
|---|
| 546 |         assert(rv->getWorldEntity()); | 
|---|
| 547 |         if (rv->getWorldEntity()->isVisible() == false || rv->getRadarVisibility() == false) | 
|---|
| 548 |         return false; | 
|---|
| 549 |         return true; | 
|---|
| 550 |     } | 
|---|
| 551 |  | 
|---|
| 552 |     void HUDNavigation::changedOwner() | 
|---|
| 553 |     { | 
|---|
| 554 |         const std::set<RadarViewable*>& respawnObjects = this->getOwner()->getScene()->getRadar()->getRadarObjects(); | 
|---|
| 555 |         for (std::set<RadarViewable*>::const_iterator it = respawnObjects.begin(); it != respawnObjects.end(); ++it) | 
|---|
| 556 |         { | 
|---|
| 557 |             if (!(*it)->isHumanShip_) | 
|---|
| 558 |             this->addObject(*it); | 
|---|
| 559 |         } | 
|---|
| 560 |     } | 
|---|
| 561 |  | 
|---|
| 562 |     Vector3* HUDNavigation::toAimPosition(RadarViewable* target) const | 
|---|
| 563 |     { | 
|---|
| 564 |         Vector3 wePosition = HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition(); | 
|---|
| 565 |         Vector3 targetPosition = target->getRVWorldPosition(); | 
|---|
| 566 |         Vector3 targetSpeed = target->getRVVelocity(); | 
|---|
| 567 |         Vector3 relativePosition = targetPosition - wePosition; //Vector from attacker to target | 
|---|
| 568 |  | 
|---|
| 569 |         float p_half = relativePosition.dotProduct(targetSpeed)/(targetSpeed.squaredLength() - this->currentMunitionSpeed_ * this->currentMunitionSpeed_); | 
|---|
| 570 |         float time1 = -p_half + sqrt(p_half * p_half - relativePosition.squaredLength()/(targetSpeed.squaredLength() - this->currentMunitionSpeed_ * this->currentMunitionSpeed_)); | 
|---|
| 571 |  | 
|---|
| 572 |         Vector3* result = new Vector3(targetPosition + targetSpeed * time1); | 
|---|
| 573 |         return result; | 
|---|
| 574 |     } | 
|---|
| 575 |  | 
|---|
| 576 |     void HUDNavigation::selectClosestTarget() | 
|---|
| 577 |     { | 
|---|
| 578 |         if(HUDNavigation::localHUD_s) | 
|---|
| 579 |         { | 
|---|
| 580 |             HUDNavigation::localHUD_s->closestTarget_ = true; | 
|---|
| 581 |         } | 
|---|
| 582 |     } | 
|---|
| 583 |  | 
|---|
| 584 |     void HUDNavigation::selectNextTarget() | 
|---|
| 585 |     { | 
|---|
| 586 |         if(HUDNavigation::localHUD_s) | 
|---|
| 587 |         { | 
|---|
| 588 |             HUDNavigation::localHUD_s->nextTarget_ = true; | 
|---|
| 589 |         } | 
|---|
| 590 |     } | 
|---|
| 591 | } | 
|---|