Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 15, 2008, 1:09:07 AM (16 years ago)
Author:
rgrieder
Message:
  • adjusted Radar to fit in XML loading scheme
  • OverlayGroup should be more or less what I imagine for now (only supports scale method to scale the entire HUD)
  • singletonized HUDNavigation (and HUDRadar of course): These are temporary hacks!
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hud/src/orxonox/overlays/OverlayGroup.cc

    r1601 r1604  
    2121 *
    2222 *   Author:
    23  *      Yuning Chai
     23 *      Reto Grieder
    2424 *   Co-authors:
    25  *      Felix Schulthess
     25 *      ...
    2626 *
    2727 */
     
    3030#include "OverlayGroup.h"
    3131
    32 #include <string>
    33 #include <set>
    3432#include <assert.h>
    35 #include <OgreOverlay.h>
    36 #include <OgreOverlayContainer.h>
    37 #include <OgreOverlayManager.h>
    38 #include <OgreStringConverter.h>
    39 
    4033#include "core/Debug.h"
    4134#include "core/ConsoleCommand.h"
    4235#include "core/CoreIncludes.h"
    43 #include "objects/SpaceShip.h"
    44 #include "objects/WorldEntity.h"
    45 #include "GraphicsEngine.h"
    46 #include "hud/HUDBar.h"
    47 #include "hud/RadarObject.h"
    48 #include "hud/RadarOverlayElement.h"
    49 #include "hud/HUDNavigation.h"
     36#include "OrxonoxOverlay.h"
    5037
    5138namespace orxonox
    5239{
    53     CreateFactory(OverlayGroup);
     40  CreateFactory(OverlayGroup);
    5441
    55     SetConsoleCommandShortcut(OverlayGroup, cycleNavigationFocus).setAccessLevel(AccessLevel::User);
    56     SetConsoleCommandShortcut(OverlayGroup, releaseNavigationFocus).setAccessLevel(AccessLevel::User);
    57     SetConsoleCommand(OverlayGroup, toggleVisibility, false).setAccessLevel(AccessLevel::User);
     42  SetConsoleCommand(OverlayGroup, toggleVisibility, false).setAccessLevel(AccessLevel::User);
    5843
    59     OverlayGroup* OverlayGroup::hudInstance_s = 0;
     44  OverlayGroup* OverlayGroup::hudInstance_s = 0;
    6045
    61     using namespace Ogre;
     46  using namespace Ogre;
    6247
    63     OverlayGroup::OverlayGroup()
     48  OverlayGroup::OverlayGroup()
     49    : scale_(1.0, 1.0)
     50  {
     51    RegisterObject(OverlayGroup);
     52
     53    // Singleton like in Ogre. Constructor and destructor are public,
     54    // but the assert prevents from having multiple instances.
     55    assert(hudInstance_s == 0);
     56    hudInstance_s = this;
     57  }
     58
     59  OverlayGroup::~OverlayGroup()
     60  {
     61    if (this->isInitialized())
    6462    {
    65         assert(hudInstance_s == 0);
    66         hudInstance_s = this;
    67         RegisterObject(OverlayGroup);
    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;
    8263    }
    8364
    84     OverlayGroup::~OverlayGroup()
     65    hudInstance_s = 0;
     66  }
     67
     68  void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode)
     69  {
     70    BaseObject::XMLPort(xmlElement, mode);
     71
     72    XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, xmlElement, mode, false, true);
     73    XMLPortParam(OverlayGroup, "scale", scale, getScale, xmlElement, mode);
     74  }
     75
     76  void OverlayGroup::scale(const Vector2& scale)
     77  {
     78    for (std::map<std::string, OrxonoxOverlay*>::iterator it = hudElements_.begin(); it != hudElements_.end(); ++it)
     79      (*it).second->scale(scale);
     80    this->scale_ = scale;
     81  }
     82
     83  void OverlayGroup::addElement(OrxonoxOverlay* element)
     84  {
     85    if (hudElements_.find(element->getName()) != hudElements_.end())
    8586    {
    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         }
     87      COUT(1) << "Ambiguous names encountered while load the HUD overlays" << std::endl;
     88    }
     89    else
     90      hudElements_[element->getName()] = element;
     91  }
    11092
    111         hudInstance_s = 0;
     93  OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
     94  {
     95    if (index < this->hudElements_.size())
     96    {
     97      std::map<std::string, OrxonoxOverlay*>::const_iterator it = hudElements_.begin();
     98      for (unsigned int i = 0; i != index; ++it, ++i)
     99        ;
     100      return (*it).second;
    112101    }
     102    else
     103      return 0;
     104  }
    113105
    114     void OverlayGroup::XMLPort(Element& xmlElement, XMLPort::Mode mode)
     106  /*static*/ OverlayGroup& OverlayGroup::getHUD()
     107  {
     108    assert(hudInstance_s);
     109    return *hudInstance_s;
     110  }
     111
     112  /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
     113  {
     114    if (OverlayGroup::getHUD().hudElements_.find(name) != OverlayGroup::getHUD().hudElements_.end())
    115115    {
    116         BaseObject::XMLPort(xmlElement, mode);
     116      OverlayGroup::getHUD().hudElements_[name]->setVisibility(!OverlayGroup::getHUD().hudElements_[name]->isVisible());
     117    }
     118  }
    117119
    118         showFPS_ = true;
    119         showRenderTime_ = true;
    120 
    121         XMLPortObject(OverlayGroup, OrxonoxOverlay, "", addElement, getElement, 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 OverlayGroup::addElement(OrxonoxOverlay* 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     OrxonoxOverlay* OverlayGroup::getElement(unsigned int index)
    169     {
    170         if (index < this->hudElements_.size())
    171         {
    172           std::map<std::string, OrxonoxOverlay*>::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 OverlayGroup::tick(float dt)
    182     {
    183         radar_->update();
    184     }
    185 
    186     void OverlayGroup::windowResized(int newWidth, int newHeight)
    187     {
    188         this->radar_->resize();
    189     }
    190 
    191     void OverlayGroup::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 OverlayGroup::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*/ OverlayGroup& OverlayGroup::getHUD()
    220     {
    221         assert(hudInstance_s);
    222         return *hudInstance_s;
    223     }
    224 
    225     /*static*/ void OverlayGroup::toggleVisibility(const std::string& name)
    226     {
    227         if (OverlayGroup::getHUD().hudElements_.find(name) != OverlayGroup::getHUD().hudElements_.end())
    228         {
    229             OverlayGroup::getHUD().hudElements_[name]->setVisibility(!OverlayGroup::getHUD().hudElements_[name]->isVisible());
    230         }
    231     }
    232 
    233     /*static*/ void OverlayGroup::setEnergy(float value){
    234         OverlayGroup::getHUD().energyBar_->setValue(value);
    235     }
    236 
    237     /*static*/ void OverlayGroup::cycleNavigationFocus()
    238     {
    239         if (OverlayGroup::getHUD().hudElements_.find("Navigation") != OverlayGroup::getHUD().hudElements_.end())
    240         {
    241             HUDNavigation* navi = dynamic_cast<HUDNavigation*>(OverlayGroup::getHUD().hudElements_["Navigation"]);
    242             navi->cycleFocus();
    243         }
    244     }
    245 
    246     /*static*/ void OverlayGroup::releaseNavigationFocus(){
    247         //OverlayGroup::getHUD().nav_->releaseFocus();
    248     }
    249120}
Note: See TracChangeset for help on using the changeset viewer.