[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 | |
---|
| 111 | instance_s = 0; |
---|
[1567] | 112 | } |
---|
| 113 | |
---|
[1588] | 114 | void HUD::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
[1567] | 115 | { |
---|
[1588] | 116 | BaseObject::XMLPort(xmlElement, mode); |
---|
| 117 | |
---|
[1564] | 118 | showFPS_ = true; |
---|
| 119 | showRenderTime_ = true; |
---|
[1505] | 120 | |
---|
[1588] | 121 | XMLPortObject(HUD, HUDOverlay, "", addHUDElement, getHUDElement, xmlElement, mode, false, true); |
---|
| 122 | |
---|
[1505] | 123 | // create Factories |
---|
[1567] | 124 | Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_); |
---|
[1505] | 125 | |
---|
| 126 | // set up screen-wide container |
---|
[1568] | 127 | container_ = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Orxonox/HUD/container")); |
---|
[1564] | 128 | container_->setLeft(0.0); |
---|
| 129 | container_->setTop(0.0); |
---|
| 130 | container_->setWidth(1.0); |
---|
| 131 | container_->setHeight(1.0); |
---|
| 132 | container_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
[1505] | 133 | |
---|
[1568] | 134 | orxonoxHUD_ = Ogre::OverlayManager::getSingleton().create("Orxonox/HUD"); |
---|
| 135 | orxonoxHUD_->add2D(container_); |
---|
| 136 | |
---|
| 137 | // create radar |
---|
| 138 | radar_ = static_cast<RadarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Radar", "radar")); |
---|
[1564] | 139 | radar_->init(0.5, 0.9, 0.2, container_); |
---|
| 140 | |
---|
| 141 | WorldEntity* object; |
---|
| 142 | object = new WorldEntity(); |
---|
| 143 | object->setPosition(2000.0, 0.0, 0.0); |
---|
| 144 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 145 | object = new WorldEntity(); |
---|
| 146 | object->setPosition(0.0, 2000.0, 0.0); |
---|
| 147 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 148 | object = new WorldEntity(); |
---|
| 149 | object->setPosition(0.0, 0.0, 2000.0); |
---|
| 150 | addRadarObject(object, ColourValue(0.5, 0, 0, 1)); |
---|
| 151 | object = new WorldEntity(); |
---|
| 152 | object->setPosition(10000.0,16000.0,0.0); |
---|
| 153 | addRadarObject(object); |
---|
[1588] | 154 | |
---|
| 155 | orxonoxHUD_->show(); |
---|
[1505] | 156 | } |
---|
| 157 | |
---|
[1588] | 158 | void HUD::addHUDElement(HUDOverlay* element) |
---|
[1564] | 159 | { |
---|
[1588] | 160 | if (hudElements_.find(element->getName()) != hudElements_.end()) |
---|
| 161 | { |
---|
| 162 | COUT(1) << "Ambiguous names encountered while load the HUD overlays" << std::endl; |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | hudElements_[element->getName()] = element; |
---|
| 166 | } |
---|
[1564] | 167 | |
---|
[1588] | 168 | HUDOverlay* HUD::getHUDElement(unsigned int index) |
---|
| 169 | { |
---|
| 170 | if (index < this->hudElements_.size()) |
---|
| 171 | { |
---|
| 172 | std::map<std::string, HUDOverlay*>::const_iterator it = hudElements_.begin(); |
---|
| 173 | for (unsigned int i = 0; i != index; ++it, ++i) |
---|
| 174 | ; |
---|
| 175 | return (*it).second; |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | return 0; |
---|
[1505] | 179 | } |
---|
| 180 | |
---|
| 181 | void HUD::tick(float dt) |
---|
| 182 | { |
---|
[1564] | 183 | radar_->update(); |
---|
[1505] | 184 | } |
---|
| 185 | |
---|
[1590] | 186 | void HUD::windowResized(int newWidth, int newHeight) |
---|
[1564] | 187 | { |
---|
| 188 | this->radar_->resize(); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | void HUD::addRadarObject(WorldEntity* object, const ColourValue& colour){ |
---|
| 192 | RadarObject* obj = new RadarObject(container_, object, colour); |
---|
| 193 | roSet_.insert(roSet_.end(), obj); |
---|
[1505] | 194 | // // check if this is the first RadarObject to create |
---|
| 195 | // if(firstRadarObject == NULL){ |
---|
[1564] | 196 | // firstRadarObject = new RadarObject(container_, object, colour); |
---|
[1505] | 197 | // lastRadarObject = firstRadarObject; |
---|
| 198 | // } |
---|
| 199 | // else{ // if not, append to list |
---|
[1564] | 200 | // lastRadarObject->next = new RadarObject(container_, object, colour); |
---|
[1505] | 201 | // lastRadarObject = lastRadarObject->next; |
---|
| 202 | // } |
---|
| 203 | } |
---|
| 204 | |
---|
[1564] | 205 | void HUD::removeRadarObject(WorldEntity* object){ |
---|
| 206 | for(std::list<RadarObject*>::iterator it=roSet_.begin(); it!=roSet_.end(); ++it){ |
---|
| 207 | if ((*it)->getObject() == object) |
---|
[1562] | 208 | { |
---|
[1590] | 209 | /*if (this->nav_ && this->nav_->getFocus() == (*it)) |
---|
| 210 | this->nav_->releaseFocus();*/ |
---|
[1564] | 211 | |
---|
[1505] | 212 | delete (*it); |
---|
[1564] | 213 | roSet_.erase(it); |
---|
[1562] | 214 | return; |
---|
[1505] | 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
[1588] | 219 | /*static*/ HUD& HUD::getSingleton() |
---|
| 220 | { |
---|
| 221 | assert(instance_s); |
---|
| 222 | return *instance_s; |
---|
[1505] | 223 | } |
---|
| 224 | |
---|
[1588] | 225 | /*static*/ void HUD::toggleVisibility(const std::string& name) |
---|
| 226 | { |
---|
| 227 | if (HUD::getSingleton().hudElements_.find(name) != HUD::getSingleton().hudElements_.end()) |
---|
| 228 | { |
---|
| 229 | HUD::getSingleton().hudElements_[name]->setVisibility(!HUD::getSingleton().hudElements_[name]->isVisible()); |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
[1505] | 233 | /*static*/ void HUD::setEnergy(float value){ |
---|
[1564] | 234 | HUD::getSingleton().energyBar_->setValue(value); |
---|
[1505] | 235 | } |
---|
| 236 | |
---|
[1590] | 237 | /*static*/ void HUD::cycleNavigationFocus() |
---|
| 238 | { |
---|
| 239 | if (HUD::getSingleton().hudElements_.find("Navigation") != HUD::getSingleton().hudElements_.end()) |
---|
| 240 | { |
---|
| 241 | Navigation* navi = dynamic_cast<Navigation*>(HUD::getSingleton().hudElements_["Navigation"]); |
---|
| 242 | navi->cycleFocus(); |
---|
| 243 | } |
---|
[1505] | 244 | } |
---|
| 245 | |
---|
[1564] | 246 | /*static*/ void HUD::releaseNavigationFocus(){ |
---|
[1590] | 247 | //HUD::getSingleton().nav_->releaseFocus(); |
---|
[1564] | 248 | } |
---|
[1505] | 249 | } |
---|