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