| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * |
|---|
| 4 | * |
|---|
| 5 | * License notice: |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or |
|---|
| 8 | * modify it under the terms of the GNU General Public License |
|---|
| 9 | * as published by the Free Software Foundation; either version 2 |
|---|
| 10 | * of the License, or (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 20 | * |
|---|
| 21 | * Author: |
|---|
| 22 | * Yuning Chai |
|---|
| 23 | * Co-authors: |
|---|
| 24 | * Felix Schulthess |
|---|
| 25 | * |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include "OrxonoxStableHeaders.h" |
|---|
| 30 | #include <OgreOverlay.h> |
|---|
| 31 | #include <OgreOverlayContainer.h> |
|---|
| 32 | #include <OgreOverlayManager.h> |
|---|
| 33 | #include <OgreStringConverter.h> |
|---|
| 34 | |
|---|
| 35 | #include "core/Debug.h" |
|---|
| 36 | #include "core/ConsoleCommand.h" |
|---|
| 37 | #include "objects/SpaceShip.h" |
|---|
| 38 | #include "BarOverlayElement.h" |
|---|
| 39 | #include "RadarOverlayElement.h" |
|---|
| 40 | #include "OverlayElementFactories.h" |
|---|
| 41 | #include "HUD.h" |
|---|
| 42 | |
|---|
| 43 | namespace orxonox |
|---|
| 44 | { |
|---|
| 45 | ConsoleCommandShortcut(HUD, cycleNavigationFocus, AccessLevel::User); |
|---|
| 46 | |
|---|
| 47 | using namespace Ogre; |
|---|
| 48 | |
|---|
| 49 | HUD::HUD(){ |
|---|
| 50 | om = &Ogre::OverlayManager::getSingleton(); |
|---|
| 51 | firstRadarObject = NULL; |
|---|
| 52 | lastRadarObject = NULL; |
|---|
| 53 | |
|---|
| 54 | // create Factories |
|---|
| 55 | BarOverlayElementFactory *barOverlayElementFactory = new BarOverlayElementFactory(); |
|---|
| 56 | om->addOverlayElementFactory(barOverlayElementFactory); |
|---|
| 57 | RadarOverlayElementFactory *radarOverlayElementFactory = new RadarOverlayElementFactory(); |
|---|
| 58 | om->addOverlayElementFactory(radarOverlayElementFactory); |
|---|
| 59 | |
|---|
| 60 | orxonoxHUD = om->create("Orxonox/HUD"); |
|---|
| 61 | container = static_cast<Ogre::OverlayContainer*>(om->createOverlayElement("Panel", "Orxonox/HUD/container")); |
|---|
| 62 | |
|---|
| 63 | // creating text to display fps |
|---|
| 64 | fpsText = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "fpsText")); |
|---|
| 65 | fpsText->show(); |
|---|
| 66 | fpsText->setMetricsMode(Ogre::GMM_PIXELS); |
|---|
| 67 | fpsText->setDimensions(0.001, 0.001); |
|---|
| 68 | fpsText->setPosition(10, 10); |
|---|
| 69 | fpsText->setFontName("Console"); |
|---|
| 70 | fpsText->setCharHeight(20); |
|---|
| 71 | fpsText->setCaption("init"); |
|---|
| 72 | |
|---|
| 73 | // create energy bar |
|---|
| 74 | energyBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "energyBar")); |
|---|
| 75 | energyBar->show(); |
|---|
| 76 | // create speedo bar |
|---|
| 77 | speedoBar = static_cast<BarOverlayElement*>(om->createOverlayElement("Bar", "speedoBar")); |
|---|
| 78 | speedoBar->show(); |
|---|
| 79 | // create radar |
|---|
| 80 | radar = static_cast<RadarOverlayElement*>(om->createOverlayElement("Radar", "radar")); |
|---|
| 81 | radar->show(); |
|---|
| 82 | |
|---|
| 83 | // create Navigation |
|---|
| 84 | nav = new Navigation(container); |
|---|
| 85 | |
|---|
| 86 | // set up screen-wide container |
|---|
| 87 | container->show(); |
|---|
| 88 | |
|---|
| 89 | orxonoxHUD->add2D(container); |
|---|
| 90 | orxonoxHUD->show(); |
|---|
| 91 | container->setLeft(0.0); |
|---|
| 92 | container->setTop(0.0); |
|---|
| 93 | container->setWidth(1.0); |
|---|
| 94 | container->setHeight(1.0); |
|---|
| 95 | container->setMetricsMode(Ogre::GMM_RELATIVE); |
|---|
| 96 | container->addChild(fpsText); |
|---|
| 97 | |
|---|
| 98 | energyBar->init(0.01, 0.94, 0.4, container); |
|---|
| 99 | energyBar->setValue(1); |
|---|
| 100 | |
|---|
| 101 | speedoBar->init(0.01, 0.90, 0.4, container); |
|---|
| 102 | |
|---|
| 103 | radar->init(0.5, 0.9, 0.2, container); |
|---|
| 104 | addRadarObject(Vector3(2000.0, 0.0, 0.0)); |
|---|
| 105 | addRadarObject(Vector3(0.0, 2000.0, 0.0)); |
|---|
| 106 | addRadarObject(Vector3(0.0, 0.0, 2000.0)); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | HUD::~HUD(){ |
|---|
| 110 | //todo: clean up objects |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void HUD::tick(float dt) |
|---|
| 114 | { |
|---|
| 115 | energyBar->resize(); |
|---|
| 116 | |
|---|
| 117 | float v = SpaceShip::instance_s->getVelocity().length(); |
|---|
| 118 | float vmax = SpaceShip::instance_s->getMaxSpeed(); |
|---|
| 119 | speedoBar->setValue(v/vmax); |
|---|
| 120 | speedoBar->resize(); |
|---|
| 121 | |
|---|
| 122 | radar->resize(); |
|---|
| 123 | radar->update(); |
|---|
| 124 | |
|---|
| 125 | nav->update(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void HUD::addRadarObject(Vector3 pos){ |
|---|
| 129 | // check if this is the first RadarObject to create |
|---|
| 130 | if(firstRadarObject == NULL){ |
|---|
| 131 | firstRadarObject = new RadarObject(container, pos); |
|---|
| 132 | lastRadarObject = firstRadarObject; |
|---|
| 133 | } |
|---|
| 134 | else{ // if not, append to list |
|---|
| 135 | lastRadarObject->next = new RadarObject(container, pos); |
|---|
| 136 | lastRadarObject = lastRadarObject->next; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | RadarObject* HUD::getFirstRadarObject(){ |
|---|
| 141 | return firstRadarObject; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /*static*/HUD& HUD::getSingleton(){ |
|---|
| 145 | static HUD theInstance; |
|---|
| 146 | return theInstance; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /*static*/void HUD::setFPS(float fps){ |
|---|
| 150 | HUD::getSingleton().fpsText->setCaption("FPS: " + Ogre::StringConverter::toString(fps)); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /*static*/void HUD::setEnergy(float value){ |
|---|
| 154 | HUD::getSingleton().energyBar->setValue(value); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /*static*/void HUD::cycleNavigationFocus(){ |
|---|
| 158 | HUD::getSingleton().nav->cycleFocus(); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|