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