[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> |
---|
[1588] | 34 | #include <assert.h> |
---|
[1505] | 35 | #include <OgreOverlay.h> |
---|
| 36 | #include <OgreOverlayContainer.h> |
---|
| 37 | #include <OgreOverlayManager.h> |
---|
| 38 | #include <OgreStringConverter.h> |
---|
| 39 | |
---|
| 40 | #include "core/Debug.h" |
---|
| 41 | #include "core/ConsoleCommand.h" |
---|
[1588] | 42 | #include "core/CoreIncludes.h" |
---|
[1505] | 43 | #include "objects/SpaceShip.h" |
---|
[1564] | 44 | #include "objects/WorldEntity.h" |
---|
[1505] | 45 | #include "GraphicsEngine.h" |
---|
[1588] | 46 | #include "HUDBar.h" |
---|
[1505] | 47 | #include "RadarObject.h" |
---|
| 48 | #include "RadarOverlayElement.h" |
---|
| 49 | #include "Navigation.h" |
---|
| 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
[1588] | 53 | CreateFactory(HUD); |
---|
| 54 | |
---|
[1505] | 55 | SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User); |
---|
[1564] | 56 | SetConsoleCommandShortcut(HUD, releaseNavigationFocus).setAccessLevel(AccessLevel::User); |
---|
[1588] | 57 | SetConsoleCommand(HUD, toggleVisibility, false).setAccessLevel(AccessLevel::User); |
---|
[1505] | 58 | |
---|
[1588] | 59 | HUD* HUD::instance_s = 0; |
---|
| 60 | |
---|
[1505] | 61 | using namespace Ogre; |
---|
| 62 | |
---|
[1564] | 63 | HUD::HUD() |
---|
| 64 | { |
---|
[1588] | 65 | assert(instance_s == 0); |
---|
| 66 | instance_s = this; |
---|
| 67 | RegisterObject(HUD); |
---|
| 68 | |
---|
| 69 | // Singleton like in Ogre. Constructor and destructor are public, |
---|
| 70 | // but the assert prevents from having multiple instances. |
---|
| 71 | |
---|
[1567] | 72 | orxonoxHUD_ = 0; |
---|
| 73 | container_ = 0; |
---|
| 74 | fpsText_ = 0; |
---|
| 75 | rTRText_ = 0; |
---|
| 76 | energyBar_ = 0; |
---|
| 77 | speedoBar_ = 0; |
---|
| 78 | radar_ = 0; |
---|
| 79 | nav_ = 0; |
---|
[1568] | 80 | showFPS_ = true; |
---|
| 81 | showRenderTime_ = true; |
---|
[1567] | 82 | } |
---|
| 83 | |
---|
| 84 | HUD::~HUD() |
---|
| 85 | { |
---|
[1588] | 86 | if (this->isInitialized()) |
---|
| 87 | { |
---|
| 88 | if (this->container_) |
---|
| 89 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->container_); |
---|
| 90 | this->container_ = 0; |
---|
| 91 | if (this->fpsText_) |
---|
| 92 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->fpsText_); |
---|
| 93 | this->fpsText_ = 0; |
---|
| 94 | if (this->rTRText_) |
---|
| 95 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->rTRText_); |
---|
| 96 | this->rTRText_ = 0; |
---|
| 97 | if (this->energyBar_) |
---|
| 98 | delete this->energyBar_; |
---|
| 99 | this->energyBar_ = 0; |
---|
| 100 | /*if (this->speedoBar_) |
---|
| 101 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->speedoBar_);*/ |
---|
| 102 | this->speedoBar_ = 0; |
---|
| 103 | if (this->radar_) |
---|
| 104 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->radar_); |
---|
| 105 | this->radar_ = 0; |
---|
| 106 | if (this->orxonoxHUD_) |
---|
| 107 | Ogre::OverlayManager::getSingleton().destroy(this->orxonoxHUD_); |
---|
| 108 | this->orxonoxHUD_ = 0; |
---|
| 109 | |
---|
| 110 | if (this->nav_) |
---|
| 111 | delete this->nav_; |
---|
| 112 | this->nav_ = 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | instance_s = 0; |
---|
[1567] | 116 | } |
---|
| 117 | |
---|
[1588] | 118 | void HUD::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
[1567] | 119 | { |
---|
[1588] | 120 | BaseObject::XMLPort(xmlElement, mode); |
---|
| 121 | |
---|
[1564] | 122 | showFPS_ = true; |
---|
| 123 | showRenderTime_ = true; |
---|
[1505] | 124 | |
---|
[1588] | 125 | XMLPortObject(HUD, HUDOverlay, "", addHUDElement, getHUDElement, xmlElement, mode, false, true); |
---|
| 126 | |
---|
[1505] | 127 | // create Factories |
---|
[1567] | 128 | Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_); |
---|
[1505] | 129 | |
---|
| 130 | // set up screen-wide container |
---|
[1568] | 131 | container_ = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Orxonox/HUD/container")); |
---|
[1564] | 132 | container_->setLeft(0.0); |
---|
| 133 | container_->setTop(0.0); |
---|
| 134 | container_->setWidth(1.0); |
---|
| 135 | container_->setHeight(1.0); |
---|
| 136 | container_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
[1505] | 137 | |
---|
[1568] | 138 | orxonoxHUD_ = Ogre::OverlayManager::getSingleton().create("Orxonox/HUD"); |
---|
| 139 | orxonoxHUD_->add2D(container_); |
---|
| 140 | |
---|
| 141 | // create radar |
---|
| 142 | radar_ = static_cast<RadarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Radar", "radar")); |
---|
[1564] | 143 | radar_->init(0.5, 0.9, 0.2, container_); |
---|
| 144 | |
---|
[1568] | 145 | // create Navigation |
---|
| 146 | nav_ = new Navigation(container_); |
---|
| 147 | |
---|
[1564] | 148 | WorldEntity* object; |
---|
| 149 | object = new WorldEntity(); |
---|
| 150 | object->setPosition(2000.0, 0.0, 0.0); |
---|
| 151 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 152 | object = new WorldEntity(); |
---|
| 153 | object->setPosition(0.0, 2000.0, 0.0); |
---|
| 154 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 155 | object = new WorldEntity(); |
---|
| 156 | object->setPosition(0.0, 0.0, 2000.0); |
---|
| 157 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 158 | object = new WorldEntity(); |
---|
| 159 | object->setPosition(10000.0,16000.0,0.0); |
---|
| 160 | addRadarObject(object); |
---|
[1588] | 161 | |
---|
| 162 | orxonoxHUD_->show(); |
---|
[1505] | 163 | } |
---|
| 164 | |
---|
[1588] | 165 | void HUD::addHUDElement(HUDOverlay* element) |
---|
[1564] | 166 | { |
---|
[1588] | 167 | if (hudElements_.find(element->getName()) != hudElements_.end()) |
---|
| 168 | { |
---|
| 169 | COUT(1) << "Ambiguous names encountered while load the HUD overlays" << std::endl; |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | hudElements_[element->getName()] = element; |
---|
| 173 | } |
---|
[1564] | 174 | |
---|
[1588] | 175 | HUDOverlay* HUD::getHUDElement(unsigned int index) |
---|
| 176 | { |
---|
| 177 | if (index < this->hudElements_.size()) |
---|
| 178 | { |
---|
| 179 | std::map<std::string, HUDOverlay*>::const_iterator it = hudElements_.begin(); |
---|
| 180 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 181 | ; |
---|
| 182 | return (*it).second; |
---|
| 183 | } |
---|
| 184 | else |
---|
| 185 | return 0; |
---|
[1505] | 186 | } |
---|
| 187 | |
---|
| 188 | void HUD::tick(float dt) |
---|
| 189 | { |
---|
[1564] | 190 | radar_->update(); |
---|
| 191 | nav_->update(); |
---|
[1505] | 192 | } |
---|
| 193 | |
---|
[1564] | 194 | void HUD::resize() |
---|
| 195 | { |
---|
| 196 | this->radar_->resize(); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void HUD::addRadarObject(WorldEntity* object, const ColourValue& colour){ |
---|
| 200 | RadarObject* obj = new RadarObject(container_, object, colour); |
---|
| 201 | roSet_.insert(roSet_.end(), obj); |
---|
[1505] | 202 | // // check if this is the first RadarObject to create |
---|
| 203 | // if(firstRadarObject == NULL){ |
---|
[1564] | 204 | // firstRadarObject = new RadarObject(container_, object, colour); |
---|
[1505] | 205 | // lastRadarObject = firstRadarObject; |
---|
| 206 | // } |
---|
| 207 | // else{ // if not, append to list |
---|
[1564] | 208 | // lastRadarObject->next = new RadarObject(container_, object, colour); |
---|
[1505] | 209 | // lastRadarObject = lastRadarObject->next; |
---|
| 210 | // } |
---|
| 211 | } |
---|
| 212 | |
---|
[1564] | 213 | void HUD::removeRadarObject(WorldEntity* object){ |
---|
| 214 | for(std::list<RadarObject*>::iterator it=roSet_.begin(); it!=roSet_.end(); ++it){ |
---|
| 215 | if ((*it)->getObject() == object) |
---|
[1562] | 216 | { |
---|
[1580] | 217 | if (this->nav_ && this->nav_->getFocus() == (*it)) |
---|
| 218 | this->nav_->releaseFocus(); |
---|
[1564] | 219 | |
---|
[1505] | 220 | delete (*it); |
---|
[1564] | 221 | roSet_.erase(it); |
---|
[1562] | 222 | return; |
---|
[1505] | 223 | } |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[1588] | 227 | /*static*/ HUD& HUD::getSingleton() |
---|
| 228 | { |
---|
| 229 | assert(instance_s); |
---|
| 230 | return *instance_s; |
---|
[1505] | 231 | } |
---|
| 232 | |
---|
[1588] | 233 | /*static*/ void HUD::toggleVisibility(const std::string& name) |
---|
| 234 | { |
---|
| 235 | if (HUD::getSingleton().hudElements_.find(name) != HUD::getSingleton().hudElements_.end()) |
---|
| 236 | { |
---|
| 237 | HUD::getSingleton().hudElements_[name]->setVisibility(!HUD::getSingleton().hudElements_[name]->isVisible()); |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
[1505] | 241 | /*static*/ void HUD::setEnergy(float value){ |
---|
[1564] | 242 | HUD::getSingleton().energyBar_->setValue(value); |
---|
[1505] | 243 | } |
---|
| 244 | |
---|
| 245 | /*static*/ void HUD::cycleNavigationFocus(){ |
---|
[1564] | 246 | HUD::getSingleton().nav_->cycleFocus(); |
---|
[1505] | 247 | } |
---|
| 248 | |
---|
[1564] | 249 | /*static*/ void HUD::releaseNavigationFocus(){ |
---|
[1568] | 250 | HUD::getSingleton().nav_->releaseFocus(); |
---|
[1564] | 251 | } |
---|
[1505] | 252 | } |
---|