| [1505] | 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 |  *      Yuning Chai | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      Felix Schulthess | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "OrxonoxStableHeaders.h" | 
|---|
 | 30 | #include "HUD.h" | 
|---|
 | 31 |  | 
|---|
 | 32 | #include <string> | 
|---|
 | 33 | #include <set> | 
|---|
 | 34 | #include <OgreOverlay.h> | 
|---|
 | 35 | #include <OgreOverlayContainer.h> | 
|---|
 | 36 | #include <OgreOverlayManager.h> | 
|---|
 | 37 | #include <OgreStringConverter.h> | 
|---|
 | 38 |  | 
|---|
 | 39 | #include "core/Debug.h" | 
|---|
 | 40 | #include "core/ConsoleCommand.h" | 
|---|
 | 41 | #include "objects/SpaceShip.h" | 
|---|
| [1564] | 42 | #include "objects/WorldEntity.h" | 
|---|
| [1505] | 43 | #include "GraphicsEngine.h" | 
|---|
 | 44 | #include "BarOverlayElement.h" | 
|---|
 | 45 | #include "RadarObject.h" | 
|---|
 | 46 | #include "RadarOverlayElement.h" | 
|---|
 | 47 | #include "Navigation.h" | 
|---|
 | 48 |  | 
|---|
 | 49 | namespace orxonox | 
|---|
 | 50 | { | 
|---|
 | 51 |     SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User); | 
|---|
| [1564] | 52 |     SetConsoleCommandShortcut(HUD, releaseNavigationFocus).setAccessLevel(AccessLevel::User); | 
|---|
| [1505] | 53 |     SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User); | 
|---|
 | 54 |     SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User); | 
|---|
 | 55 |  | 
|---|
 | 56 |     using namespace Ogre; | 
|---|
 | 57 |  | 
|---|
| [1564] | 58 |     HUD::HUD() | 
|---|
 | 59 |     { | 
|---|
| [1567] | 60 |         orxonoxHUD_ = 0; | 
|---|
 | 61 |         container_ = 0; | 
|---|
 | 62 |         fpsText_ = 0; | 
|---|
 | 63 |         rTRText_ = 0; | 
|---|
 | 64 |         energyBar_ = 0; | 
|---|
 | 65 |         speedoBar_ = 0; | 
|---|
 | 66 |         radar_ = 0; | 
|---|
 | 67 |         nav_ = 0; | 
|---|
| [1568] | 68 |         showFPS_ = true; | 
|---|
 | 69 |         showRenderTime_ = true; | 
|---|
| [1567] | 70 |     } | 
|---|
 | 71 |  | 
|---|
 | 72 |     HUD::~HUD() | 
|---|
 | 73 |     { | 
|---|
 | 74 |         this->destroy(); | 
|---|
 | 75 |     } | 
|---|
 | 76 |  | 
|---|
 | 77 |     void HUD::initialise() | 
|---|
 | 78 |     { | 
|---|
| [1564] | 79 |         showFPS_ = true; | 
|---|
 | 80 |         showRenderTime_ = true; | 
|---|
| [1505] | 81 |  | 
|---|
 | 82 |         // create Factories | 
|---|
| [1567] | 83 |         Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&barOverlayElementFactory_); | 
|---|
 | 84 |         Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_); | 
|---|
| [1505] | 85 |  | 
|---|
 | 86 |         // creating text to display fps | 
|---|
| [1564] | 87 |         fpsText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "fpsText")); | 
|---|
 | 88 |         fpsText_->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
 | 89 |         fpsText_->setDimensions(0.001, 0.001); | 
|---|
 | 90 |         fpsText_->setPosition(10, 10); | 
|---|
 | 91 |         fpsText_->setFontName("Console"); | 
|---|
 | 92 |         fpsText_->setCharHeight(20); | 
|---|
 | 93 |         fpsText_->setCaption("init"); | 
|---|
| [1568] | 94 |         fpsText_->show(); | 
|---|
| [1505] | 95 |  | 
|---|
 | 96 |         // creating text to display render time ratio | 
|---|
| [1564] | 97 |         rTRText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "rTRText")); | 
|---|
 | 98 |         rTRText_->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
 | 99 |         rTRText_->setDimensions(0.001, 0.001); | 
|---|
 | 100 |         rTRText_->setPosition(10, 30); | 
|---|
 | 101 |         rTRText_->setFontName("Console"); | 
|---|
 | 102 |         rTRText_->setCharHeight(20); | 
|---|
 | 103 |         rTRText_->setCaption("init"); | 
|---|
| [1568] | 104 |         rTRText_->show(); | 
|---|
| [1505] | 105 |  | 
|---|
 | 106 |         // set up screen-wide container | 
|---|
| [1568] | 107 |         container_ = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Orxonox/HUD/container")); | 
|---|
| [1564] | 108 |         container_->setLeft(0.0); | 
|---|
 | 109 |         container_->setTop(0.0); | 
|---|
 | 110 |         container_->setWidth(1.0); | 
|---|
 | 111 |         container_->setHeight(1.0); | 
|---|
 | 112 |         container_->setMetricsMode(Ogre::GMM_RELATIVE); | 
|---|
 | 113 |         container_->addChild(fpsText_); | 
|---|
 | 114 |         container_->addChild(rTRText_); | 
|---|
| [1568] | 115 |         container_->show(); | 
|---|
| [1505] | 116 |  | 
|---|
| [1568] | 117 |         orxonoxHUD_ = Ogre::OverlayManager::getSingleton().create("Orxonox/HUD"); | 
|---|
 | 118 |         orxonoxHUD_->add2D(container_); | 
|---|
 | 119 |         orxonoxHUD_->show(); | 
|---|
 | 120 |  | 
|---|
 | 121 |         // create energy bar | 
|---|
 | 122 |         energyBar_ = static_cast<BarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Bar", "energyBar")); | 
|---|
| [1564] | 123 |         energyBar_->init(0.01, 0.94, 0.4, container_); | 
|---|
 | 124 |         energyBar_->setValue(1); | 
|---|
| [1568] | 125 |         energyBar_->show(); | 
|---|
| [1505] | 126 |  | 
|---|
| [1568] | 127 |         // create speedo bar | 
|---|
 | 128 |         speedoBar_ = static_cast<BarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Bar", "speedoBar")); | 
|---|
| [1564] | 129 |         speedoBar_->init(0.01, 0.90, 0.4, container_); | 
|---|
| [1568] | 130 |         speedoBar_->addColour(0.7, ColourValue(0.2, 0.7, 0.2)); | 
|---|
 | 131 |         speedoBar_->addColour(0.4, ColourValue(0.7, 0.5, 0.2)); | 
|---|
 | 132 |         speedoBar_->addColour(0.1, ColourValue(0.7, 0.2, 0.2)); | 
|---|
 | 133 |         speedoBar_->show(); | 
|---|
| [1505] | 134 |  | 
|---|
| [1568] | 135 |         // create radar | 
|---|
 | 136 |         radar_ = static_cast<RadarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Radar", "radar")); | 
|---|
| [1564] | 137 |         radar_->init(0.5, 0.9, 0.2, container_); | 
|---|
| [1568] | 138 |         radar_->show(); | 
|---|
| [1564] | 139 |  | 
|---|
| [1568] | 140 |         // create Navigation | 
|---|
 | 141 |         nav_ = new Navigation(container_); | 
|---|
 | 142 |  | 
|---|
| [1564] | 143 |         WorldEntity* object; | 
|---|
 | 144 |         object = new WorldEntity(); | 
|---|
 | 145 |         object->setPosition(2000.0, 0.0, 0.0); | 
|---|
 | 146 |         addRadarObject(object, ColourValue(0.5, 0, 0, 1)); | 
|---|
 | 147 |         object = new WorldEntity(); | 
|---|
 | 148 |         object->setPosition(0.0, 2000.0, 0.0); | 
|---|
 | 149 |         addRadarObject(object, ColourValue(0.5, 0, 0, 1)); | 
|---|
 | 150 |         object = new WorldEntity(); | 
|---|
 | 151 |         object->setPosition(0.0, 0.0, 2000.0); | 
|---|
 | 152 |         addRadarObject(object, ColourValue(0.5, 0, 0, 1)); | 
|---|
 | 153 |         object = new WorldEntity(); | 
|---|
 | 154 |         object->setPosition(10000.0,16000.0,0.0); | 
|---|
 | 155 |         addRadarObject(object); | 
|---|
| [1505] | 156 |     } | 
|---|
 | 157 |  | 
|---|
| [1567] | 158 |     void HUD::destroy() | 
|---|
| [1564] | 159 |     { | 
|---|
| [1567] | 160 |         if (this->container_) | 
|---|
 | 161 |           Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->container_); | 
|---|
 | 162 |         this->container_ = 0; | 
|---|
 | 163 |         if (this->fpsText_) | 
|---|
 | 164 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->fpsText_); | 
|---|
 | 165 |         this->fpsText_ = 0; | 
|---|
 | 166 |         if (this->rTRText_) | 
|---|
 | 167 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->rTRText_); | 
|---|
 | 168 |         this->rTRText_ = 0; | 
|---|
 | 169 |         if (this->energyBar_) | 
|---|
 | 170 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->energyBar_); | 
|---|
 | 171 |         this->energyBar_ = 0; | 
|---|
 | 172 |         if (this->speedoBar_) | 
|---|
 | 173 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->speedoBar_); | 
|---|
 | 174 |         this->speedoBar_ = 0; | 
|---|
 | 175 |         if (this->radar_) | 
|---|
 | 176 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->radar_); | 
|---|
 | 177 |         this->radar_ = 0; | 
|---|
 | 178 |         if (this->orxonoxHUD_) | 
|---|
 | 179 |             Ogre::OverlayManager::getSingleton().destroy(this->orxonoxHUD_); | 
|---|
 | 180 |         this->orxonoxHUD_ = 0; | 
|---|
| [1564] | 181 |  | 
|---|
| [1567] | 182 |         if (this->nav_) | 
|---|
 | 183 |             delete this->nav_; | 
|---|
 | 184 |         this->nav_ = 0; | 
|---|
| [1505] | 185 |     } | 
|---|
 | 186 |  | 
|---|
 | 187 |     void HUD::tick(float dt) | 
|---|
 | 188 |     { | 
|---|
 | 189 |         if(!SpaceShip::getLocalShip()) | 
|---|
 | 190 |           return; | 
|---|
| [1564] | 191 |  | 
|---|
| [1505] | 192 |         float v = SpaceShip::getLocalShip()->getVelocity().length(); | 
|---|
 | 193 |         float vmax = SpaceShip::getLocalShip()->getMaxSpeed(); | 
|---|
| [1564] | 194 |         speedoBar_->setValue(v/vmax); | 
|---|
| [1505] | 195 |  | 
|---|
| [1564] | 196 |         radar_->update(); | 
|---|
 | 197 |         nav_->update(); | 
|---|
| [1505] | 198 |  | 
|---|
 | 199 |         setFPS(); | 
|---|
 | 200 |     } | 
|---|
 | 201 |  | 
|---|
| [1564] | 202 |     void HUD::resize() | 
|---|
 | 203 |     { | 
|---|
 | 204 |         this->speedoBar_->resize(); | 
|---|
 | 205 |         this->energyBar_->resize(); | 
|---|
 | 206 |         this->radar_->resize(); | 
|---|
 | 207 |     } | 
|---|
 | 208 |  | 
|---|
| [1505] | 209 |     void HUD::setRenderTimeRatio(float ratio) | 
|---|
 | 210 |     { | 
|---|
| [1564] | 211 |         if(showRenderTime_){ | 
|---|
 | 212 |             rTRText_->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio)); | 
|---|
| [1505] | 213 |         } | 
|---|
 | 214 |         else{ | 
|---|
| [1564] | 215 |             rTRText_->setCaption(""); | 
|---|
| [1505] | 216 |             return; | 
|---|
 | 217 |         } | 
|---|
 | 218 |     } | 
|---|
 | 219 |  | 
|---|
 | 220 |     void HUD::setFPS(){ | 
|---|
| [1564] | 221 |         if(showFPS_){ | 
|---|
| [1505] | 222 |             float fps = GraphicsEngine::getSingleton().getAverageFPS(); | 
|---|
| [1564] | 223 |             fpsText_->setCaption("FPS: " + Ogre::StringConverter::toString(fps)); | 
|---|
| [1505] | 224 |         } | 
|---|
 | 225 |         else{ | 
|---|
| [1564] | 226 |             fpsText_->setCaption(""); | 
|---|
| [1505] | 227 |             return; | 
|---|
 | 228 |         } | 
|---|
 | 229 |     } | 
|---|
 | 230 |  | 
|---|
| [1564] | 231 |     void HUD::addRadarObject(WorldEntity* object, const ColourValue& colour){ | 
|---|
 | 232 |         RadarObject* obj = new RadarObject(container_, object, colour); | 
|---|
 | 233 |         roSet_.insert(roSet_.end(), obj); | 
|---|
| [1505] | 234 | //        // check if this is the first RadarObject to create | 
|---|
 | 235 | //        if(firstRadarObject == NULL){ | 
|---|
| [1564] | 236 | //            firstRadarObject = new RadarObject(container_, object, colour); | 
|---|
| [1505] | 237 | //            lastRadarObject = firstRadarObject; | 
|---|
 | 238 | //        } | 
|---|
 | 239 | //        else{ // if not, append to list | 
|---|
| [1564] | 240 | //            lastRadarObject->next = new RadarObject(container_, object, colour); | 
|---|
| [1505] | 241 | //            lastRadarObject = lastRadarObject->next; | 
|---|
 | 242 | //        } | 
|---|
 | 243 |     } | 
|---|
 | 244 |  | 
|---|
| [1564] | 245 |     void HUD::removeRadarObject(WorldEntity* object){ | 
|---|
 | 246 |         for(std::list<RadarObject*>::iterator it=roSet_.begin(); it!=roSet_.end(); ++it){ | 
|---|
 | 247 |             if ((*it)->getObject() == object) | 
|---|
| [1562] | 248 |             { | 
|---|
| [1580] | 249 |                 if (this->nav_ && this->nav_->getFocus() == (*it)) | 
|---|
 | 250 |                     this->nav_->releaseFocus(); | 
|---|
| [1564] | 251 |  | 
|---|
| [1505] | 252 |                 delete (*it); | 
|---|
| [1564] | 253 |                 roSet_.erase(it); | 
|---|
| [1562] | 254 |                 return; | 
|---|
| [1505] | 255 |             } | 
|---|
 | 256 |         } | 
|---|
 | 257 |     } | 
|---|
 | 258 |  | 
|---|
 | 259 |     /*static*/ HUD& HUD::getSingleton(){ | 
|---|
 | 260 |         static HUD theInstance; | 
|---|
 | 261 |         return theInstance; | 
|---|
 | 262 |     } | 
|---|
 | 263 |  | 
|---|
 | 264 |     /*static*/ void HUD::setEnergy(float value){ | 
|---|
| [1564] | 265 |         HUD::getSingleton().energyBar_->setValue(value); | 
|---|
| [1505] | 266 |     } | 
|---|
 | 267 |  | 
|---|
 | 268 |     /*static*/ void HUD::cycleNavigationFocus(){ | 
|---|
| [1564] | 269 |         HUD::getSingleton().nav_->cycleFocus(); | 
|---|
| [1505] | 270 |     } | 
|---|
 | 271 |  | 
|---|
| [1564] | 272 |     /*static*/ void HUD::releaseNavigationFocus(){ | 
|---|
| [1568] | 273 |         HUD::getSingleton().nav_->releaseFocus(); | 
|---|
| [1564] | 274 |     } | 
|---|
 | 275 |  | 
|---|
| [1505] | 276 |     /*static*/ void HUD::toggleFPS(){ | 
|---|
| [1564] | 277 |         HUD::getSingleton().showFPS_ = !HUD::getSingleton().showFPS_; | 
|---|
| [1505] | 278 |     } | 
|---|
 | 279 |  | 
|---|
 | 280 |     /*static*/ void HUD::toggleRenderTime(){ | 
|---|
| [1564] | 281 |         HUD::getSingleton().showRenderTime_ = !HUD::getSingleton().showRenderTime_; | 
|---|
| [1505] | 282 |     } | 
|---|
 | 283 | } | 
|---|