Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1588 was 1588, checked in by rgrieder, 16 years ago
  • added XML loadable HUD
  • Radar and navi are not yet done
  • explanations follow with when things are finished
  • Property svn:eol-style set to native
File size: 7.9 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            if (this->nav_)
111                delete this->nav_;
112            this->nav_ = 0;
113        }
114
115        instance_s = 0;
116    }
117
118    void HUD::XMLPort(Element& xmlElement, XMLPort::Mode mode)
119    {
120        BaseObject::XMLPort(xmlElement, mode);
121
122        showFPS_ = true;
123        showRenderTime_ = true;
124
125        XMLPortObject(HUD, HUDOverlay, "", addHUDElement, getHUDElement, xmlElement, mode, false, true);
126
127        // create Factories
128        Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_);
129
130        // set up screen-wide container
131        container_ = static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Orxonox/HUD/container"));
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);
137
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"));
143        radar_->init(0.5, 0.9, 0.2, container_);
144
145        // create Navigation
146        nav_ = new Navigation(container_);
147
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);
161
162        orxonoxHUD_->show();
163    }
164
165    void HUD::addHUDElement(HUDOverlay* element)
166    {
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    }
174
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;
186    }
187
188    void HUD::tick(float dt)
189    {
190        radar_->update();
191        nav_->update();
192    }
193
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);
202//        // check if this is the first RadarObject to create
203//        if(firstRadarObject == NULL){
204//            firstRadarObject = new RadarObject(container_, object, colour);
205//            lastRadarObject = firstRadarObject;
206//        }
207//        else{ // if not, append to list
208//            lastRadarObject->next = new RadarObject(container_, object, colour);
209//            lastRadarObject = lastRadarObject->next;
210//        }
211    }
212
213    void HUD::removeRadarObject(WorldEntity* object){
214        for(std::list<RadarObject*>::iterator it=roSet_.begin(); it!=roSet_.end(); ++it){
215            if ((*it)->getObject() == object)
216            {
217                if (this->nav_ && this->nav_->getFocus() == (*it))
218                    this->nav_->releaseFocus();
219
220                delete (*it);
221                roSet_.erase(it);
222                return;
223            }
224        }
225    }
226
227    /*static*/ HUD& HUD::getSingleton()
228    {
229        assert(instance_s);
230        return *instance_s;
231    }
232
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
241    /*static*/ void HUD::setEnergy(float value){
242        HUD::getSingleton().energyBar_->setValue(value);
243    }
244
245    /*static*/ void HUD::cycleNavigationFocus(){
246        HUD::getSingleton().nav_->cycleFocus();
247    }
248
249    /*static*/ void HUD::releaseNavigationFocus(){
250        HUD::getSingleton().nav_->releaseFocus();
251    }
252}
Note: See TracBrowser for help on using the repository browser.