Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/hud/HUD.cc @ 1590

Last change on this file since 1590 was 1590, checked in by rgrieder, 16 years ago

svn save, just in case our house burns down over night…

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
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 <assert.h>
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"
42#include "core/CoreIncludes.h"
43#include "objects/SpaceShip.h"
44#include "objects/WorldEntity.h"
45#include "GraphicsEngine.h"
46#include "HUDBar.h"
47#include "RadarObject.h"
48#include "RadarOverlayElement.h"
49#include "Navigation.h"
50
51namespace orxonox
52{
53    CreateFactory(HUD);
54
55    SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User);
56    SetConsoleCommandShortcut(HUD, releaseNavigationFocus).setAccessLevel(AccessLevel::User);
57    SetConsoleCommand(HUD, toggleVisibility, false).setAccessLevel(AccessLevel::User);
58
59    HUD* HUD::instance_s = 0;
60
61    using namespace Ogre;
62
63    HUD::HUD()
64    {
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
72        orxonoxHUD_ = 0;
73        container_ = 0;
74        fpsText_ = 0;
75        rTRText_ = 0;
76        energyBar_ = 0;
77        speedoBar_ = 0;
78        radar_ = 0;
79        nav_ = 0;
80        showFPS_ = true;
81        showRenderTime_ = true;
82    }
83
84    HUD::~HUD()
85    {
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;
112    }
113
114    void HUD::XMLPort(Element& xmlElement, XMLPort::Mode mode)
115    {
116        BaseObject::XMLPort(xmlElement, mode);
117
118        showFPS_ = true;
119        showRenderTime_ = true;
120
121        XMLPortObject(HUD, HUDOverlay, "", addHUDElement, getHUDElement, xmlElement, mode, false, true);
122
123        // create Factories
124        Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_);
125
126        // set up screen-wide container
127        container_ = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Orxonox/HUD/container"));
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);
133
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"));
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);
154
155        orxonoxHUD_->show();
156    }
157
158    void HUD::addHUDElement(HUDOverlay* element)
159    {
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    }
167
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;
179    }
180
181    void HUD::tick(float dt)
182    {
183        radar_->update();
184    }
185
186    void HUD::windowResized(int newWidth, int newHeight)
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);
194//        // check if this is the first RadarObject to create
195//        if(firstRadarObject == NULL){
196//            firstRadarObject = new RadarObject(container_, object, colour);
197//            lastRadarObject = firstRadarObject;
198//        }
199//        else{ // if not, append to list
200//            lastRadarObject->next = new RadarObject(container_, object, colour);
201//            lastRadarObject = lastRadarObject->next;
202//        }
203    }
204
205    void HUD::removeRadarObject(WorldEntity* object){
206        for(std::list<RadarObject*>::iterator it=roSet_.begin(); it!=roSet_.end(); ++it){
207            if ((*it)->getObject() == object)
208            {
209                /*if (this->nav_ && this->nav_->getFocus() == (*it))
210                    this->nav_->releaseFocus();*/
211
212                delete (*it);
213                roSet_.erase(it);
214                return;
215            }
216        }
217    }
218
219    /*static*/ HUD& HUD::getSingleton()
220    {
221        assert(instance_s);
222        return *instance_s;
223    }
224
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
233    /*static*/ void HUD::setEnergy(float value){
234        HUD::getSingleton().energyBar_->setValue(value);
235    }
236
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        }
244    }
245
246    /*static*/ void HUD::releaseNavigationFocus(){
247        //HUD::getSingleton().nav_->releaseFocus();
248    }
249}
Note: See TracBrowser for help on using the repository browser.