[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[1454] | 4 | * |
---|
[1505] | 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 | * |
---|
[1454] | 22 | * Author: |
---|
| 23 | * Felix Schulthess |
---|
| 24 | * Co-authors: |
---|
[1590] | 25 | * Reto Grieder |
---|
[1454] | 26 | * |
---|
| 27 | */ |
---|
[1393] | 28 | |
---|
[1601] | 29 | #include "HUDNavigation.h" |
---|
[1410] | 30 | |
---|
[6417] | 31 | #include <OgreCamera.h> |
---|
[7163] | 32 | #include <OgreFontManager.h> |
---|
[1393] | 33 | #include <OgreOverlayManager.h> |
---|
[1614] | 34 | #include <OgreTextAreaOverlayElement.h> |
---|
| 35 | #include <OgrePanelOverlayElement.h> |
---|
[1410] | 36 | |
---|
[1614] | 37 | #include "util/Math.h" |
---|
[1616] | 38 | #include "util/Convert.h" |
---|
| 39 | #include "core/CoreIncludes.h" |
---|
| 40 | #include "core/XMLPort.h" |
---|
[6417] | 41 | #include "CameraManager.h" |
---|
[5929] | 42 | #include "Scene.h" |
---|
[5735] | 43 | #include "Radar.h" |
---|
[6417] | 44 | #include "graphics/Camera.h" |
---|
| 45 | #include "controllers/HumanController.h" |
---|
| 46 | #include "worldentities/pawns/Pawn.h" |
---|
[7163] | 47 | #include "worldentities/WorldEntity.h" |
---|
| 48 | #include "core/ConfigValueIncludes.h" |
---|
| 49 | #include "tools/TextureGenerator.h" |
---|
| 50 | // #include <boost/bind/bind_template.hpp> |
---|
[1393] | 51 | |
---|
[7163] | 52 | |
---|
[1393] | 53 | namespace orxonox |
---|
| 54 | { |
---|
[7163] | 55 | bool compareDistance ( std::pair<RadarViewable*, unsigned int > a, std::pair<RadarViewable*, unsigned int > b ) |
---|
| 56 | { |
---|
| 57 | return a.second<b.second; |
---|
[1590] | 58 | |
---|
[7163] | 59 | } |
---|
[2087] | 60 | |
---|
[7163] | 61 | void HUDNavigation::setConfigValues() |
---|
| 62 | { |
---|
| 63 | SetConfigValue(markerLimit_, 3); |
---|
| 64 | } |
---|
[2087] | 65 | |
---|
[7163] | 66 | CreateFactory ( HUDNavigation ); |
---|
[2087] | 67 | |
---|
[7163] | 68 | HUDNavigation::HUDNavigation ( BaseObject* creator ) |
---|
| 69 | : OrxonoxOverlay ( creator ) |
---|
| 70 | { |
---|
| 71 | RegisterObject ( HUDNavigation ); |
---|
| 72 | this->setConfigValues(); |
---|
[2087] | 73 | |
---|
[7163] | 74 | // Set default values |
---|
| 75 | setFont ( "Monofur" ); |
---|
| 76 | setTextSize ( 0.05f ); |
---|
| 77 | setNavMarkerSize ( 0.05f ); |
---|
| 78 | } |
---|
[2087] | 79 | |
---|
[7163] | 80 | HUDNavigation::~HUDNavigation() |
---|
| 81 | { |
---|
| 82 | if ( this->isInitialized() ) |
---|
| 83 | { |
---|
| 84 | for ( ObjectMap::iterator it = activeObjectList_.begin(); it != activeObjectList_.end(); ) |
---|
| 85 | removeObject ( ( it++ )->first ); |
---|
[2087] | 86 | |
---|
[1393] | 87 | } |
---|
| 88 | |
---|
[7163] | 89 | sortedObjectList_.clear(); |
---|
| 90 | } |
---|
[1564] | 91 | |
---|
[7163] | 92 | void HUDNavigation::XMLPort ( Element& xmlElement, XMLPort::Mode mode ) |
---|
| 93 | { |
---|
| 94 | SUPER ( HUDNavigation, XMLPort, xmlElement, mode ); |
---|
[1394] | 95 | |
---|
[7163] | 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 | } |
---|
[1393] | 100 | |
---|
[7163] | 101 | void HUDNavigation::setFont ( const std::string& font ) |
---|
| 102 | { |
---|
| 103 | const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName ( font ); |
---|
| 104 | if ( fontPtr.isNull() ) |
---|
[1590] | 105 | { |
---|
[7163] | 106 | COUT ( 2 ) << "Warning: HUDNavigation: Font '" << font << "' not found" << std::endl; |
---|
| 107 | return; |
---|
[1590] | 108 | } |
---|
[7163] | 109 | fontName_ = font; |
---|
| 110 | for ( ObjectMap::iterator it = activeObjectList_.begin(); it != activeObjectList_.end(); ++it ) |
---|
[1590] | 111 | { |
---|
[7163] | 112 | if ( it->second.text_ != NULL ) |
---|
| 113 | it->second.text_->setFontName ( fontName_ ); |
---|
[1590] | 114 | } |
---|
[7163] | 115 | } |
---|
[1590] | 116 | |
---|
[7163] | 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 ) |
---|
[1590] | 125 | { |
---|
[7163] | 126 | COUT ( 2 ) << "Warning: HUDNavigation: Negative font size not allowed" << std::endl; |
---|
| 127 | return; |
---|
[1590] | 128 | } |
---|
[7163] | 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 | } |
---|
[1590] | 136 | |
---|
[7163] | 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 ) |
---|
[1590] | 155 | { |
---|
[7163] | 156 | listIt->second = ( int ) ( ( listIt->first->getRVWorldPosition() - HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition() ).length() + 0.5f ); |
---|
[1590] | 157 | } |
---|
| 158 | |
---|
[7163] | 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 ) |
---|
[1590] | 165 | { |
---|
[7163] | 166 | ObjectMap::iterator it = activeObjectList_.find ( listIt->first ); |
---|
[2662] | 167 | |
---|
[7163] | 168 | if ( markerCount_ < markerLimit_ ) |
---|
[1613] | 169 | { |
---|
[1400] | 170 | |
---|
[1399] | 171 | |
---|
[7163] | 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; |
---|
[1564] | 176 | |
---|
[7163] | 177 | // Transform to screen coordinates |
---|
| 178 | Vector3 pos = camTransform * it->first->getRVWorldPosition(); |
---|
[1399] | 179 | |
---|
[7163] | 180 | bool outOfView = true; |
---|
| 181 | if ( pos.z > 1.0 ) |
---|
[1580] | 182 | { |
---|
[7163] | 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; |
---|
[1411] | 189 | } |
---|
[7163] | 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 ) ); |
---|
[1590] | 194 | |
---|
[7163] | 195 | if ( outOfView ) |
---|
[1580] | 196 | { |
---|
[7163] | 197 | // Object is not in view |
---|
| 198 | |
---|
| 199 | // Change material only if outOfView changed |
---|
| 200 | if ( !it->second.wasOutOfView_ ) |
---|
[1580] | 201 | { |
---|
[7163] | 202 | it->second.panel_->setMaterialName( TextureGenerator::getMaterialName( "arrows.tga", it->first->getRadarObjectColour()) ); |
---|
| 203 | it->second.wasOutOfView_ = true; |
---|
[1399] | 204 | } |
---|
[7163] | 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 | |
---|
[1580] | 229 | else |
---|
| 230 | { |
---|
[7163] | 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 | } |
---|
[1411] | 250 | } |
---|
| 251 | } |
---|
[1590] | 252 | else |
---|
[1580] | 253 | { |
---|
[7163] | 254 | // Object is in view |
---|
| 255 | |
---|
| 256 | // Change material only if outOfView changed |
---|
| 257 | if ( it->second.wasOutOfView_ ) |
---|
[1580] | 258 | { |
---|
[7163] | 259 | //it->second.panel_->setMaterialName ( "Orxonox/NavTDC" ); |
---|
| 260 | it->second.panel_->setMaterialName( TextureGenerator::getMaterialName( "tdc.tga", it->first->getRadarObjectColour()) ); |
---|
| 261 | it->second.wasOutOfView_ = false; |
---|
[1399] | 262 | } |
---|
[7163] | 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 ); |
---|
[1393] | 272 | } |
---|
[7163] | 273 | |
---|
| 274 | // Make sure the overlays are shown |
---|
| 275 | it->second.panel_->show(); |
---|
| 276 | it->second.text_->show(); |
---|
[1393] | 277 | } |
---|
[1580] | 278 | else |
---|
| 279 | { |
---|
[7163] | 280 | it->second.panel_->hide(); |
---|
| 281 | it->second.text_->hide(); |
---|
| 282 | } |
---|
[1590] | 283 | |
---|
[7163] | 284 | } |
---|
| 285 | } |
---|
[1566] | 286 | |
---|
[7163] | 287 | |
---|
| 288 | /** Overridden method of OrxonoxOverlay. |
---|
| 289 | @details |
---|
| 290 | Usually the entire overlay scales with scale(). |
---|
| 291 | Here we obviously have to adjust this. |
---|
[2087] | 292 | */ |
---|
[7163] | 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 ); |
---|
[1410] | 305 | } |
---|
[7163] | 306 | } |
---|
[1394] | 307 | |
---|
[7163] | 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() ) |
---|
[1580] | 359 | { |
---|
[7163] | 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 | |
---|
[1410] | 370 | } |
---|
[1590] | 371 | |
---|
[7163] | 372 | for ( sortedList::iterator listIt = sortedObjectList_.begin(); listIt != sortedObjectList_.end(); ++listIt ) |
---|
[1590] | 373 | { |
---|
[7163] | 374 | if ( (listIt->first) == viewable ) |
---|
| 375 | { |
---|
| 376 | sortedObjectList_.erase ( listIt ); |
---|
| 377 | break; |
---|
| 378 | } |
---|
| 379 | |
---|
[1590] | 380 | } |
---|
[7163] | 381 | |
---|
[1393] | 382 | } |
---|
[7163] | 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 | } |
---|