Changeset 6942 for code/branches/presentation3/src/modules/overlays/hud
- Timestamp:
- May 20, 2010, 8:30:38 PM (15 years ago)
- Location:
- code/branches/presentation3
- Files:
-
- 5 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 } -
code/branches/presentation3/src/modules/overlays/hud/HUDNavigation.h
r6417 r6942 32 32 #include "overlays/OverlaysPrereqs.h" 33 33 34 #include <map> 35 #include <string> 36 34 37 #include "util/OgreForwardRefs.h" 35 38 #include "tools/interfaces/Tickable.h" 39 #include "interfaces/RadarListener.h" 36 40 #include "overlays/OrxonoxOverlay.h" 37 41 38 42 namespace orxonox 39 43 { 40 class _OverlaysExport HUDNavigation : public OrxonoxOverlay, public Tickable 44 class _OverlaysExport HUDNavigation : public OrxonoxOverlay, public Tickable, public RadarListener 41 45 { 42 46 public: … … 47 51 virtual void tick(float dt); 48 52 53 virtual void addObject(RadarViewable* object); 54 virtual void removeObject(RadarViewable* viewable); 55 virtual void objectChanged(RadarViewable* viewable) {} 56 57 virtual void changedOwner(); 58 virtual void sizeChanged(); 59 virtual void angleChanged() { } 60 virtual void positionChanged() { } 61 virtual void radarTick(float dt) {} 62 63 inline float getRadarSensitivity() const 64 { return 1.0f; } 65 49 66 private: 50 void sizeChanged(); 51 void angleChanged() { } 52 void positionChanged() { } 67 struct ObjectInfo 68 { 69 Ogre::PanelOverlayElement* panel_; 70 Ogre::TextAreaOverlayElement* text_; 71 bool outOfView_; 72 bool wasOutOfView_; 73 }; 53 74 54 75 // XMLPort accessors 55 void setNavMarkerSize(float size) { this->navMarkerSize_ = size; this->sizeChanged(); } 56 float getNavMarkerSize() const { return this->navMarkerSize_; } 57 58 /* 59 void setAimMarkerSize(float size) { this->aimMarkerSize_ = size; this->sizeChanged(); } 60 float getAimMarkerSize() const { return this->aimMarkerSize_; } 61 */ 76 void setNavMarkerSize(float size) 77 { navMarkerSize_ = size; this->sizeChanged(); } 78 float getNavMarkerSize() const 79 { return navMarkerSize_; } 62 80 63 81 void setTextSize(float size); … … 67 85 const std::string& getFont() const; 68 86 69 void updateMarker(); 70 void updateFocus(); 71 float getDist2Focus() const; 87 typedef std::map<RadarViewable*, ObjectInfo > ObjectMap; 88 ObjectMap activeObjectList_; 72 89 73 Ogre::PanelOverlayElement* navMarker_; //!< the panel used to show the arrow and the target marker 74 float navMarkerSize_; //!< One paramter size of the navigation marker 75 /* 76 Ogre::PanelOverlayElement* aimMarker_; //!< Panel used to show the aim Marker 77 float aimMarkerSize_; //!< One paramter size of the aim marker 78 */ 79 Ogre::TextAreaOverlayElement* navText_; //!< Text overlay to display the target distance 80 bool wasOutOfView_; //!< Performance booster variable: setMaterial is not cheap 90 float navMarkerSize_; 91 std::string fontName_; 92 float textSize_; 81 93 }; 82 94 } -
code/branches/presentation3/src/modules/overlays/hud/HUDRadar.cc
r6502 r6942 40 40 #include "worldentities/WorldEntity.h" 41 41 #include "worldentities/pawns/Pawn.h" 42 #include "Scene.h" 43 #include "Radar.h" 42 44 43 45 namespace orxonox 44 46 { 45 CreateFactory(HUDRadar); 47 CreateFactory(HUDRadar); 46 48 47 49 HUDRadar::HUDRadar(BaseObject* creator) … … 72 74 { 73 75 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_); 74 for (std:: vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();75 it != this->radar Dots_.end(); ++it)76 for (std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it = this->radarObjects_.begin(); 77 it != this->radarObjects_.end(); ++it) 76 78 { 77 Ogre::OverlayManager::getSingleton().destroyOverlayElement( *it);79 Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second); 78 80 } 79 81 } … … 89 91 } 90 92 91 void HUDRadar:: displayObject(RadarViewable* object, bool bIsMarked)93 void HUDRadar::addObject(RadarViewable* object) 92 94 { 93 if (object == static_cast<RadarViewable*>(this->owner_))95 if (object == dynamic_cast<RadarViewable*>(this->owner_)) 94 96 return; 95 97 96 const WorldEntity* wePointer = object->getWorldEntity(); 98 // Make sure the object hasn't been added yet 99 assert( this->radarObjects_.find(object) == this->radarObjects_.end() ); 97 100 98 // Just to be sure that we actually have a WorldEntity. 99 // We could do a dynamic_cast, but that would be a lot slower. 100 if (!wePointer || !this->owner_) 101 // Create everything needed to display the object on the radar and add it to the map 102 Ogre::PanelOverlayElement* panel; 103 panel = static_cast<Ogre::PanelOverlayElement*>( 104 Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString())); 105 this->overlay_->add2D(panel); 106 // get right material 107 panel->setMaterialName(TextureGenerator::getMaterialName( 108 shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour())); 109 this->radarObjects_[object] = panel; 110 } 111 112 void HUDRadar::removeObject(RadarViewable* object) 113 { 114 // If object was added at all then remove it 115 std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it; 116 it = this->radarObjects_.find( object ); 117 if( it != this->radarObjects_.end() ) 101 118 { 102 if (!wePointer) 103 CCOUT(2) << "Cannot display a non-WorldEntitiy on the radar" << std::endl; 104 if (!this->owner_) 105 CCOUT(2) << "No owner defined" << std::endl; 106 return; 119 Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second); 120 this->radarObjects_.erase(it); 107 121 } 122 } 108 123 109 // try to find a panel already created 110 Ogre::PanelOverlayElement* panel; 111 //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object); 112 if (itRadarDots_ == this->radarDots_.end()) 113 { 114 // we have to create a new entry 115 panel = static_cast<Ogre::PanelOverlayElement*>( 116 Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString())); 117 radarDots_.push_back(panel); 118 // get right material 119 panel->setMaterialName(TextureGenerator::getMaterialName( 120 shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour())); 121 this->overlay_->add2D(panel); 122 this->itRadarDots_ = this->radarDots_.end(); 123 } 124 else 125 { 126 panel = *itRadarDots_; 127 ++itRadarDots_; 128 const std::string& materialName = TextureGenerator::getMaterialName( 129 shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()); 130 if (materialName != panel->getMaterialName()) 131 panel->setMaterialName(materialName); 132 } 133 panel->show(); 124 void HUDRadar::objectChanged( RadarViewable* rv ) 125 { 126 assert( this->radarObjects_.find(rv) != this->radarObjects_.end() ); 127 Ogre::PanelOverlayElement* panel = this->radarObjects_[rv]; 128 panel->setMaterialName(TextureGenerator::getMaterialName( 129 shapeMaterials_[rv->getRadarObjectShape()], rv->getRadarObjectColour())); 130 } 134 131 135 // set size to fit distance... 136 float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length(); 137 // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) 138 float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance); 139 panel->setDimensions(size, size); 140 141 // calc position on radar... 142 Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition()); 143 coord *= Ogre::Math::PI / 3.5f; // small adjustment to make it fit the texture 144 panel->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f); 145 146 if (bIsMarked) 147 { 148 this->marker_->show(); 149 this->marker_->setDimensions(size * 1.5f, size * 1.5f); 150 this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f); 151 } 132 void HUDRadar::gatherObjects() 133 { 134 const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects(); 135 std::set<RadarViewable*>::const_iterator it; 136 for( it=objectSet.begin(); it!=objectSet.end(); ++it ) 137 this->addObject(*it); 152 138 } 153 139 154 140 void HUDRadar::radarTick(float dt) 155 141 { 156 for (itRadarDots_ = radarDots_.begin(); itRadarDots_ != radarDots_.end(); ++itRadarDots_) 157 (*itRadarDots_)->hide(); 158 this->itRadarDots_ = this->radarDots_.begin(); 159 this->marker_->hide(); 142 // Make sure the owner of the radar was defined 143 if( !this->owner_ ) 144 { 145 CCOUT(0) << "No owner defined" << std::endl; 146 assert(0); 147 } 148 149 this->marker_->hide(); // in case that no object is in focus 150 // get the focus object 151 Radar* radar = this->getOwner()->getScene()->getRadar(); 152 const RadarViewable* focusObject = radar->getFocus(); 153 154 // update the distances for all objects 155 std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it; 156 for( it = this->radarObjects_.begin(); it != this->radarObjects_.end(); ++it ) 157 { 158 // Make sure the object really is a WorldEntity 159 const WorldEntity* wePointer = it->first->getWorldEntity(); 160 if( !wePointer ) 161 { 162 CCOUT(0) << "Cannot display a non-WorldEntitiy on the radar" << std::endl; 163 assert(0); 164 } 165 bool isFocus = (it->first == focusObject); 166 // set size to fit distance... 167 float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length(); 168 // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) 169 float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance); 170 it->second->setDimensions(size, size); 171 172 // calc position on radar... 173 Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition()); 174 coord *= Ogre::Math::PI / 3.5f; // small adjustment to make it fit the texture 175 it->second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f); 176 it->second->show(); 177 178 // if this object is in focus, then set the focus marker 179 if (isFocus) 180 { 181 this->marker_->setDimensions(size * 1.5f, size * 1.5f); 182 this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f); 183 this->marker_->show(); 184 } 185 } 160 186 } 161 187 162 188 void HUDRadar::changedOwner() 163 189 { 164 190 SUPER(HUDRadar, changedOwner); 165 191 166 this->owner_ = orxonox_cast<Pawn*>(this->getOwner()); 167 } 192 this->owner_ = orxonox_cast<Pawn*>(this->getOwner()); 193 assert(this->radarObjects_.size()==0); 194 this->gatherObjects(); 168 195 } 196 } -
code/branches/presentation3/src/modules/overlays/hud/HUDRadar.h
r5781 r6942 65 65 66 66 // RadarListener interface 67 void displayObject(RadarViewable* viewable, bool bIsMarked); 67 virtual void addObject(RadarViewable* viewable); 68 virtual void removeObject(RadarViewable* viewable); 69 virtual void objectChanged( RadarViewable* rv ); 68 70 void radarTick(float dt); 71 72 void gatherObjects(); 69 73 70 74 std::map<RadarViewable::Shape, std::string> shapeMaterials_; 71 75 72 std::vector<Ogre::PanelOverlayElement*> radarDots_; 73 std::vector<Ogre::PanelOverlayElement*>::iterator itRadarDots_; 76 // std::vector<Ogre::PanelOverlayElement*> radarDots_; 77 // std::vector<Ogre::PanelOverlayElement*>::iterator itRadarDots_; 78 std::map<RadarViewable*, Ogre::PanelOverlayElement*> radarObjects_; 74 79 Ogre::PanelOverlayElement* marker_; 75 80
Note: See TracChangeset
for help on using the changeset viewer.