Index: code/branches/hud/src/orxonox/hud/HUD.cc
===================================================================
--- code/branches/hud/src/orxonox/hud/HUD.cc	(revision 1587)
+++ code/branches/hud/src/orxonox/hud/HUD.cc	(revision 1588)
@@ -32,4 +32,5 @@
 #include <string>
 #include <set>
+#include <assert.h>
 #include <OgreOverlay.h>
 #include <OgreOverlayContainer.h>
@@ -39,8 +40,9 @@
 #include "core/Debug.h"
 #include "core/ConsoleCommand.h"
+#include "core/CoreIncludes.h"
 #include "objects/SpaceShip.h"
 #include "objects/WorldEntity.h"
 #include "GraphicsEngine.h"
-#include "BarOverlayElement.h"
+#include "HUDBar.h"
 #include "RadarObject.h"
 #include "RadarOverlayElement.h"
@@ -49,8 +51,11 @@
 namespace orxonox
 {
+    CreateFactory(HUD);
+
     SetConsoleCommandShortcut(HUD, cycleNavigationFocus).setAccessLevel(AccessLevel::User);
     SetConsoleCommandShortcut(HUD, releaseNavigationFocus).setAccessLevel(AccessLevel::User);
-    SetConsoleCommandShortcut(HUD, toggleFPS).setAccessLevel(AccessLevel::User);
-    SetConsoleCommandShortcut(HUD, toggleRenderTime).setAccessLevel(AccessLevel::User);
+    SetConsoleCommand(HUD, toggleVisibility, false).setAccessLevel(AccessLevel::User);
+
+    HUD* HUD::instance_s = 0;
 
     using namespace Ogre;
@@ -58,4 +63,11 @@
     HUD::HUD()
     {
+        assert(instance_s == 0);
+        instance_s = this;
+        RegisterObject(HUD);
+
+        // Singleton like in Ogre. Constructor and destructor are public,
+        // but the assert prevents from having multiple instances.
+
         orxonoxHUD_ = 0;
         container_ = 0;
@@ -72,35 +84,47 @@
     HUD::~HUD()
     {
-        this->destroy();
-    }
-
-    void HUD::initialise()
-    {
+        if (this->isInitialized())
+        {
+            if (this->container_)
+                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->container_);
+            this->container_ = 0;
+            if (this->fpsText_)
+                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->fpsText_);
+            this->fpsText_ = 0;
+            if (this->rTRText_)
+                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->rTRText_);
+            this->rTRText_ = 0;
+            if (this->energyBar_)
+                delete this->energyBar_;
+            this->energyBar_ = 0;
+            /*if (this->speedoBar_)
+                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->speedoBar_);*/
+            this->speedoBar_ = 0;
+            if (this->radar_)
+                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->radar_);
+            this->radar_ = 0;
+            if (this->orxonoxHUD_)
+                Ogre::OverlayManager::getSingleton().destroy(this->orxonoxHUD_);
+            this->orxonoxHUD_ = 0;
+
+            if (this->nav_)
+                delete this->nav_;
+            this->nav_ = 0;
+        }
+
+        instance_s = 0;
+    }
+
+    void HUD::XMLPort(Element& xmlElement, XMLPort::Mode mode)
+    {
+        BaseObject::XMLPort(xmlElement, mode);
+
         showFPS_ = true;
         showRenderTime_ = true;
 
+        XMLPortObject(HUD, HUDOverlay, "", addHUDElement, getHUDElement, xmlElement, mode, false, true);
+
         // create Factories
-        Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&barOverlayElementFactory_);
         Ogre::OverlayManager::getSingleton().addOverlayElementFactory(&radarOverlayElementFactory_);
-
-        // creating text to display fps
-        fpsText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "fpsText"));
-        fpsText_->setMetricsMode(Ogre::GMM_PIXELS);
-        fpsText_->setDimensions(0.001, 0.001);
-        fpsText_->setPosition(10, 10);
-        fpsText_->setFontName("Console");
-        fpsText_->setCharHeight(20);
-        fpsText_->setCaption("init");
-        fpsText_->show();
-
-        // creating text to display render time ratio
-        rTRText_ = static_cast<TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "rTRText"));
-        rTRText_->setMetricsMode(Ogre::GMM_PIXELS);
-        rTRText_->setDimensions(0.001, 0.001);
-        rTRText_->setPosition(10, 30);
-        rTRText_->setFontName("Console");
-        rTRText_->setCharHeight(20);
-        rTRText_->setCaption("init");
-        rTRText_->show();
 
         // set up screen-wide container
@@ -111,30 +135,11 @@
         container_->setHeight(1.0);
         container_->setMetricsMode(Ogre::GMM_RELATIVE);
-        container_->addChild(fpsText_);
-        container_->addChild(rTRText_);
-        container_->show();
 
         orxonoxHUD_ = Ogre::OverlayManager::getSingleton().create("Orxonox/HUD");
         orxonoxHUD_->add2D(container_);
-        orxonoxHUD_->show();
-
-        // create energy bar
-        energyBar_ = static_cast<BarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Bar", "energyBar"));
-        energyBar_->init(0.01, 0.94, 0.4, container_);
-        energyBar_->setValue(1);
-        energyBar_->show();
-
-        // create speedo bar
-        speedoBar_ = static_cast<BarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Bar", "speedoBar"));
-        speedoBar_->init(0.01, 0.90, 0.4, container_);
-        speedoBar_->addColour(0.7, ColourValue(0.2, 0.7, 0.2));
-        speedoBar_->addColour(0.4, ColourValue(0.7, 0.5, 0.2));
-        speedoBar_->addColour(0.1, ColourValue(0.7, 0.2, 0.2));
-        speedoBar_->show();
 
         // create radar
         radar_ = static_cast<RadarOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Radar", "radar"));
         radar_->init(0.5, 0.9, 0.2, container_);
-        radar_->show();
 
         // create Navigation
@@ -154,77 +159,40 @@
         object->setPosition(10000.0,16000.0,0.0);
         addRadarObject(object);
-    }
-
-    void HUD::destroy()
-    {
-        if (this->container_)
-          Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->container_);
-        this->container_ = 0;
-        if (this->fpsText_)
-            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->fpsText_);
-        this->fpsText_ = 0;
-        if (this->rTRText_)
-            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->rTRText_);
-        this->rTRText_ = 0;
-        if (this->energyBar_)
-            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->energyBar_);
-        this->energyBar_ = 0;
-        if (this->speedoBar_)
-            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->speedoBar_);
-        this->speedoBar_ = 0;
-        if (this->radar_)
-            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->radar_);
-        this->radar_ = 0;
-        if (this->orxonoxHUD_)
-            Ogre::OverlayManager::getSingleton().destroy(this->orxonoxHUD_);
-        this->orxonoxHUD_ = 0;
-
-        if (this->nav_)
-            delete this->nav_;
-        this->nav_ = 0;
+
+        orxonoxHUD_->show();
+    }
+
+    void HUD::addHUDElement(HUDOverlay* element)
+    {
+        if (hudElements_.find(element->getName()) != hudElements_.end())
+        {
+          COUT(1) << "Ambiguous names encountered while load the HUD overlays" << std::endl;
+        }
+        else
+          hudElements_[element->getName()] = element;
+    }
+
+    HUDOverlay* HUD::getHUDElement(unsigned int index)
+    {
+        if (index < this->hudElements_.size())
+        {
+          std::map<std::string, HUDOverlay*>::const_iterator it = hudElements_.begin();
+          for (unsigned int i = 0; i != index; ++it, ++i)
+            ;
+          return (*it).second;
+        }
+        else
+            return 0;
     }
 
     void HUD::tick(float dt)
     {
-        if(!SpaceShip::getLocalShip())
-          return;
-
-        float v = SpaceShip::getLocalShip()->getVelocity().length();
-        float vmax = SpaceShip::getLocalShip()->getMaxSpeed();
-        speedoBar_->setValue(v/vmax);
-
         radar_->update();
         nav_->update();
-
-        setFPS();
     }
 
     void HUD::resize()
     {
-        this->speedoBar_->resize();
-        this->energyBar_->resize();
         this->radar_->resize();
-    }
-
-    void HUD::setRenderTimeRatio(float ratio)
-    {
-        if(showRenderTime_){
-            rTRText_->setCaption("Render time ratio: " + Ogre::StringConverter::toString(ratio));
-        }
-        else{
-            rTRText_->setCaption("");
-            return;
-        }
-    }
-
-    void HUD::setFPS(){
-        if(showFPS_){
-            float fps = GraphicsEngine::getSingleton().getAverageFPS();
-            fpsText_->setCaption("FPS: " + Ogre::StringConverter::toString(fps));
-        }
-        else{
-            fpsText_->setCaption("");
-            return;
-        }
     }
 
@@ -257,7 +225,16 @@
     }
 
-    /*static*/ HUD& HUD::getSingleton(){
-        static HUD theInstance;
-        return theInstance;
+    /*static*/ HUD& HUD::getSingleton()
+    {
+        assert(instance_s);
+        return *instance_s;
+    }
+
+    /*static*/ void HUD::toggleVisibility(const std::string& name)
+    {
+        if (HUD::getSingleton().hudElements_.find(name) != HUD::getSingleton().hudElements_.end())
+        {
+            HUD::getSingleton().hudElements_[name]->setVisibility(!HUD::getSingleton().hudElements_[name]->isVisible());
+        }
     }
 
@@ -273,11 +250,3 @@
         HUD::getSingleton().nav_->releaseFocus();
     }
-
-    /*static*/ void HUD::toggleFPS(){
-        HUD::getSingleton().showFPS_ = !HUD::getSingleton().showFPS_;
-    }
-
-    /*static*/ void HUD::toggleRenderTime(){
-        HUD::getSingleton().showRenderTime_ = !HUD::getSingleton().showRenderTime_;
-    }
 }
