- Timestamp:
- May 20, 2010, 8:30:38 PM (14 years ago)
- Location:
- code/branches/presentation3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation3
- Property svn:mergeinfo changed
/code/branches/hudelements (added) merged: 6599-6600,6602,6606,6614-6615,6631,6715-6717,6727,6796,6849,6865,6868,6873-6875,6880,6886,6897-6899,6907,6913-6914,6917,6922-6924,6941
- Property svn:mergeinfo changed
-
code/branches/presentation3/src/modules/overlays/hud/HUDNavigation.cc
r6502 r6942 29 29 #include "HUDNavigation.h" 30 30 31 #include <string>32 31 #include <OgreCamera.h> 32 #include <OgreFontManager.h> 33 33 #include <OgreOverlayManager.h> 34 34 #include <OgreTextAreaOverlayElement.h> … … 45 45 #include "controllers/HumanController.h" 46 46 #include "worldentities/pawns/Pawn.h" 47 #include "worldentities/WorldEntity.h" 48 #include "interfaces/RadarViewable.h" 47 49 48 50 namespace orxonox … … 55 57 RegisterObject(HUDNavigation); 56 58 57 // create nav text 58 navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton() 59 .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); 60 61 // create nav marker 62 navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() 63 .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); 64 navMarker_->setMaterialName("Orxonox/NavArrows"); 65 66 /* 67 // create aim marker 68 aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() 69 .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberString())); 70 aimMarker_->setMaterialName("Orxonox/NavCrosshair"); 71 this->wasOutOfView_ = true; // Ensure the material is changed right the first time.. 72 59 // Set default values 73 60 setFont("Monofur"); 74 61 setTextSize(0.05f); 75 62 setNavMarkerSize(0.05f); 76 setAimMarkerSize(0.04f);77 */78 79 background_->addChild(navMarker_);80 // background_->addChild(aimMarker_);81 background_->addChild(navText_);82 83 // hide at first84 this->setVisible(false);85 63 } 86 64 … … 89 67 if (this->isInitialized()) 90 68 { 91 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_); 92 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_); 93 // Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_); 69 for (ObjectMap::iterator it = activeObjectList_.begin(); it != activeObjectList_.end();) 70 removeObject((it++)->first); 94 71 } 95 72 } … … 99 76 SUPER(HUDNavigation, XMLPort, xmlElement, mode); 100 77 101 XMLPortParam(HUDNavigation, "font", setFont, getFont,xmlElement, mode);102 XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize,xmlElement, mode);78 XMLPortParam(HUDNavigation, "font", setFont, getFont, xmlElement, mode); 79 XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode); 103 80 XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode); 104 // XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode);105 81 } 106 82 107 83 void HUDNavigation::setFont(const std::string& font) 108 84 { 109 if (this->navText_ && !font.empty()) 110 this->navText_->setFontName(font); 85 const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font); 86 if (fontPtr.isNull()) 87 { 88 COUT(2) << "Warning: HUDNavigation: Font '" << font << "' not found" << std::endl; 89 return; 90 } 91 fontName_ = font; 92 for (ObjectMap::iterator it = activeObjectList_.begin(); it != activeObjectList_.end(); ++it) 93 { 94 if (it->second.text_ != NULL) 95 it->second.text_->setFontName(fontName_); 96 } 111 97 } 112 98 113 99 const std::string& HUDNavigation::getFont() const 114 100 { 115 if (this->navText_) 116 return this->navText_->getFontName(); 117 else 118 return BLANKSTRING; 101 return fontName_; 119 102 } 120 103 121 104 void HUDNavigation::setTextSize(float size) 122 105 { 123 if (this->navText_ && size >= 0.0f) 124 this->navText_->setCharHeight(size); 106 if (size <= 0.0f) 107 { 108 COUT(2) << "Warning: HUDNavigation: Negative font size not allowed" << std::endl; 109 return; 110 } 111 textSize_ = size; 112 for (ObjectMap::iterator it = activeObjectList_.begin(); it!=activeObjectList_.end(); ++it) 113 { 114 if (it->second.text_) 115 it->second.text_->setCharHeight(size); 116 } 125 117 } 126 118 127 119 float HUDNavigation::getTextSize() const 128 120 { 129 if (this->navText_) 130 return this->navText_->getCharHeight(); 131 else 132 return 0.0f; 121 return textSize_; 133 122 } 134 123 … … 137 126 SUPER(HUDNavigation, tick, dt); 138 127 139 // Get radar 140 Radar* radar = this->getOwner()->getScene()->getRadar(); 141 142 if (!radar->getFocus()) 143 { 144 this->overlay_->hide(); 145 return; 146 } 147 else 148 { 149 this->overlay_->show(); 150 } 151 152 // set text 153 int dist = static_cast<int>(getDist2Focus()); 154 navText_->setCaption(multi_cast<std::string>(dist)); 155 float textLength = multi_cast<std::string>(dist).size() * navText_->getCharHeight() * 0.3f; 156 157 orxonox::Camera* cam = CameraManager::getInstance().getActiveCamera(); 158 if (!cam) 159 return; 160 const Matrix4& transform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix(); 161 // transform to screen coordinates 162 Vector3 pos = transform * radar->getFocus()->getRVWorldPosition(); 163 164 bool outOfView; 165 if (pos.z > 1.0) 166 { 167 // z > 1.0 means that the object is behind the camera 168 outOfView = true; 169 // we have to switch all coordinates (if you don't know why, 170 // try linear algebra lectures, because I can't explain..) 171 pos.x = -pos.x; 172 pos.y = -pos.y; 173 } 174 else 175 outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; 176 177 if (outOfView) 178 { 179 // object is not in view 180 // aimMarker_->hide(); 181 182 if (!wasOutOfView_) 128 Camera* cam = CameraManager::getInstance().getActiveCamera(); 129 if (cam == NULL) 130 return; 131 const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix(); 132 133 for (ObjectMap::iterator it = activeObjectList_.begin(); it != activeObjectList_.end(); ++it) 134 { 135 // Get Distance to HumanController and save it in the TextAreaOverlayElement. 136 int dist = (int)((it->first->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length() + 0.5f); 137 it->second.text_->setCaption(multi_cast<std::string>(dist)); 138 float textLength = multi_cast<std::string>(dist).size() * it->second.text_->getCharHeight() * 0.3f; 139 140 // Transform to screen coordinates 141 Vector3 pos = camTransform * it->first->getRVWorldPosition(); 142 143 bool outOfView = true; 144 if (pos.z > 1.0) 183 145 { 184 navMarker_->setMaterialName("Orxonox/NavArrows"); 185 wasOutOfView_ = true; 146 // z > 1.0 means that the object is behind the camera 147 outOfView = true; 148 // we have to switch all coordinates (if you don't know why, 149 // try linear algebra lectures, because I can't explain..) 150 pos.x = -pos.x; 151 pos.y = -pos.y; 186 152 } 187 188 if (pos.x < pos.y) 153 else 154 outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; 155 156 if (outOfView) 189 157 { 190 if (pos.y > -pos.x) 158 // Object is not in view 159 160 // Change material only if outOfView changed 161 if (!it->second.wasOutOfView_) 191 162 { 192 // up 193 float position = pos.x / pos.y + 1.0f; 194 navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5f, 0.0f); 195 navMarker_->setUV(0.5f, 0.0f, 1.0f, 0.5f); 196 navText_->setLeft((position - textLength) * 0.5f); 197 navText_->setTop(navMarker_->getHeight()); 163 it->second.panel_->setMaterialName("Orxonox/NavArrows"); 164 it->second.wasOutOfView_ = true; 165 } 166 167 // Switch between top, bottom, left and right position of the arrow at the screen border 168 if (pos.x < pos.y) 169 { 170 if (pos.y > -pos.x) 171 { 172 // Top 173 float position = pos.x / pos.y + 1.0f; 174 it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 0.0f); 175 it->second.panel_->setUV(0.5f, 0.0f, 1.0f, 0.5f); 176 it->second.text_->setLeft((position - textLength) * 0.5f); 177 it->second.text_->setTop(it->second.panel_->getHeight()); 178 } 179 else 180 { 181 // Left 182 float position = pos.y / pos.x + 1.0f; 183 it->second.panel_->setPosition(0.0f, (position - it->second.panel_->getWidth()) * 0.5f); 184 it->second.panel_->setUV(0.0f, 0.0f, 0.5f, 0.5f); 185 it->second.text_->setLeft(it->second.panel_->getWidth() + 0.01f); 186 it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); 187 } 198 188 } 199 189 else 200 190 { 201 // left 202 float position = pos.y / pos.x + 1.0f; 203 navMarker_->setPosition(0.0f, (position - navMarker_->getWidth()) * 0.5f); 204 navMarker_->setUV(0.0f, 0.0f, 0.5f, 0.5f); 205 navText_->setLeft(navMarker_->getWidth() + 0.01f); 206 navText_->setTop((position - navText_->getCharHeight()) * 0.5f); 191 192 if (pos.y < -pos.x) 193 { 194 // Bottom 195 float position = -pos.x / pos.y + 1.0f; 196 it->second.panel_->setPosition((position - it->second.panel_->getWidth()) * 0.5f, 1.0f - it->second.panel_->getHeight()); 197 it->second.panel_->setUV(0.0f, 0.5f, 0.5f, 1.0f); 198 it->second.text_->setLeft((position - textLength) * 0.5f); 199 it->second.text_->setTop(1.0f - it->second.panel_->getHeight() - it->second.text_->getCharHeight()); 200 } 201 else 202 { 203 // Right 204 float position = -pos.y / pos.x + 1.0f; 205 it->second.panel_->setPosition(1.0f - it->second.panel_->getWidth(), (position - it->second.panel_->getHeight()) * 0.5f); 206 it->second.panel_->setUV(0.5f, 0.5f, 1.0f, 1.0f); 207 it->second.text_->setLeft(1.0f - it->second.panel_->getWidth() - textLength - 0.01f); 208 it->second.text_->setTop((position - it->second.text_->getCharHeight()) * 0.5f); 209 } 207 210 } 208 211 } 209 212 else 210 213 { 211 if (pos.y < -pos.x) 214 // Object is in view 215 216 // Change material only if outOfView changed 217 if (it->second.wasOutOfView_) 212 218 { 213 // down 214 float position = -pos.x / pos.y + 1.0f; 215 navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5f, 1.0f - navMarker_->getHeight()); 216 navMarker_->setUV(0.0f, 0.5f, 0.5f, 1.0f); 217 navText_->setLeft((position - textLength) * 0.5f); 218 navText_->setTop(1.0f - navMarker_->getHeight() - navText_->getCharHeight()); 219 it->second.panel_->setMaterialName("Orxonox/NavTDC"); 220 it->second.wasOutOfView_ = false; 219 221 } 220 else 221 {222 // right223 float position = -pos.y / pos.x + 1.0f;224 navMarker_->setPosition(1.0f - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5f);225 navMarker_->setUV(0.5f, 0.5f, 1.0f, 1.0f); 226 navText_->setLeft(1.0f - navMarker_->getWidth() - textLength - 0.01f);227 navText_->setTop((position - navText_->getCharHeight()) * 0.5f);228 }222 223 // Position marker 224 it->second.panel_->setUV(0.0f, 0.0f, 1.0f, 1.0f); 225 it->second.panel_->setLeft((pos.x + 1.0f - it->second.panel_->getWidth()) * 0.5f); 226 it->second.panel_->setTop((-pos.y + 1.0f - it->second.panel_->getHeight()) * 0.5f); 227 228 // Position text 229 it->second.text_->setLeft((pos.x + 1.0f + it->second.panel_->getWidth()) * 0.5f); 230 it->second.text_->setTop((-pos.y + 1.0f + it->second.panel_->getHeight()) * 0.5f); 229 231 } 230 } 231 else 232 { 233 // object is in view 234 /* 235 Vector3 aimpos = transform * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(), 236 Projectile::getSpeed(), Radar::getInstance().getFocus()->getRVWorldPosition(), Radar::getInstance().getFocus()->getRVOrientedVelocity()); 237 */ 238 if (wasOutOfView_) 239 { 240 navMarker_->setMaterialName("Orxonox/NavTDC"); 241 wasOutOfView_ = false; 242 } 243 244 // object is in view 245 navMarker_->setUV(0.0f, 0.0f, 1.0f, 1.0f); 246 navMarker_->setLeft((pos.x + 1.0f - navMarker_->getWidth()) * 0.5f); 247 navMarker_->setTop((-pos.y + 1.0f - navMarker_->getHeight()) * 0.5f); 248 249 /* 250 aimMarker_->show(); 251 aimMarker_->setLeft((aimpos.x + 1.0f - aimMarker_->getWidth()) * 0.5f); 252 aimMarker_->setTop((-aimpos.y + 1.0f - aimMarker_->getHeight()) * 0.5f); 253 */ 254 navText_->setLeft((pos.x + 1.0f + navMarker_->getWidth()) * 0.5f); 255 navText_->setTop((-pos.y + 1.0f + navMarker_->getHeight()) * 0.5f); 256 } 257 } 258 259 float HUDNavigation::getDist2Focus() const 260 { 261 Radar* radar = this->getOwner()->getScene()->getRadar(); 262 if (radar->getFocus() && HumanController::getLocalControllerEntityAsPawn()) 263 return (radar->getFocus()->getRVWorldPosition() - HumanController::getLocalControllerEntityAsPawn()->getWorldPosition()).length(); 264 else 265 return 0; 266 } 267 268 /** 269 @brief Overridden method of OrxonoxOverlay. Usually the entire overlay 270 scales with scale(). Here we obviously have to adjust this. 232 233 // Make sure the overlays are shown 234 it->second.panel_->show(); 235 it->second.text_->show(); 236 } 237 } 238 239 240 /** Overridden method of OrxonoxOverlay. 241 @details 242 Usually the entire overlay scales with scale(). 243 Here we obviously have to adjust this. 271 244 */ 272 245 void HUDNavigation::sizeChanged() 273 246 { 274 // use size to compensate for aspect ratio if enabled.247 // Use size to compensate for aspect ratio if enabled. 275 248 float xScale = this->getActualSize().x; 276 249 float yScale = this->getActualSize().y; 277 if (this->navMarker_) 278 navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale); 279 /* 280 if (this->aimMarker_) 281 aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale); 282 */ 283 if (this->navText_) 284 navText_->setCharHeight(navText_->getCharHeight() * yScale); 250 251 for (ObjectMap::iterator it = activeObjectList_.begin(); it!=activeObjectList_.end(); ++it) 252 { 253 if (it->second.panel_ != NULL) 254 it->second.panel_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale); 255 if (it->second.text_ != NULL) 256 it->second.text_->setCharHeight(it->second.text_->getCharHeight() * yScale); 257 } 258 } 259 260 void HUDNavigation::addObject(RadarViewable* object) 261 { 262 if (object == NULL) 263 return; 264 265 // Don't display our own ship 266 if (object == dynamic_cast<RadarViewable*>(this->getOwner())) 267 return; 268 269 // Object hasn't been added yet (we know that) 270 assert(this->activeObjectList_.find(object) == this->activeObjectList_.end()); 271 272 // Scales used for dimensions and text size 273 float xScale = this->getActualSize().x; 274 float yScale = this->getActualSize().y; 275 276 // Create everything needed to display the object on the radar and add it to the map 277 278 // Create arrow/marker 279 Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() 280 .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); 281 panel->setMaterialName("Orxonox/NavTDC"); 282 panel->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale); 283 284 Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton() 285 .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); 286 text->setFontName(this->fontName_); 287 text->setCharHeight(text->getCharHeight() * yScale); 288 289 ObjectInfo tempStruct = {panel, text, false}; 290 activeObjectList_[object] = tempStruct; 291 292 this->background_->addChild(panel); 293 this->background_->addChild(text); 294 } 295 296 void HUDNavigation::removeObject(RadarViewable* viewable) 297 { 298 ObjectMap::iterator it = activeObjectList_.find(viewable); 299 300 if (activeObjectList_.find(viewable) != activeObjectList_.end()) 301 { 302 // Detach overlays 303 this->background_->removeChild(it->second.panel_->getName()); 304 this->background_->removeChild(it->second.text_->getName()); 305 // Properly destroy the overlay elements (do not use delete!) 306 Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.panel_); 307 Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second.text_); 308 // Remove from the list 309 activeObjectList_.erase(viewable); 310 } 311 else 312 COUT(2) << "Warning, HUDNavigation: Attempting to remove non-existent object" << std::endl; 313 } 314 315 void HUDNavigation::changedOwner() 316 { 317 // TODO: Delete old objects? 318 const std::set<RadarViewable*>& respawnObjects = this->getOwner()->getScene()->getRadar()->getRadarObjects(); 319 for (std::set<RadarViewable*>::const_iterator it = respawnObjects.begin(); it != respawnObjects.end(); ++it) 320 { 321 if (!(*it)->isHumanShip_) 322 this->addObject(*it); 323 } 285 324 } 286 325 }
Note: See TracChangeset
for help on using the changeset viewer.