Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 14, 2010, 7:54:41 PM (15 years ago)
Author:
scheusso
Message:

HUDRadar now working again

Location:
code/branches/hudelements/src/modules/overlays/hud
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hudelements/src/modules/overlays/hud/HUDRadar.cc

    r6502 r6727  
    4040#include "worldentities/WorldEntity.h"
    4141#include "worldentities/pawns/Pawn.h"
     42#include "Scene.h"
     43#include "Radar.h"
    4244
    4345namespace orxonox
     
    7274        {
    7375            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
    74             for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();
    75                 it != this->radarDots_.end(); ++it)
     76            for (std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it = this->radarObjects_.begin();
     77                it != this->radarObjects_.end(); ++it)
    7678            {
    77                 Ogre::OverlayManager::getSingleton().destroyOverlayElement(*it);
     79                Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second);
    7880            }
    7981        }
     
    8991    }
    9092
    91     void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
     93    void HUDRadar::addObject(RadarViewable* object)
    9294    {
    93         if (object == static_cast<RadarViewable*>(this->owner_))
     95        if (object == dynamic_cast<RadarViewable*>(this->owner_))
    9496            return;
    9597
    96         const WorldEntity* wePointer = object->getWorldEntity();
     98        // Make sure the object hasn't been added yet
     99        assert( this->radarObjects_.find(object) == this->radarObjects_.end() );
    97100
    98         // Just to be sure that we actually have a WorldEntity.
    99         // We could do a dynamic_cast, but that would be a lot slower.
    100         if (!wePointer || !this->owner_)
     101        // Create everything needed to display the object on the radar and add it to the map
     102        Ogre::PanelOverlayElement* panel;
     103        panel = static_cast<Ogre::PanelOverlayElement*>(
     104            Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
     105        this->overlay_->add2D(panel);
     106        // get right material
     107        panel->setMaterialName(TextureGenerator::getMaterialName(
     108            shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
     109        this->radarObjects_[object] = panel;
     110    }
     111
     112    void HUDRadar::removeObject(RadarViewable* object)
     113    {
     114        // If object was added at all then remove it
     115        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
     116        it = this->radarObjects_.find( object );
     117        if( it != this->radarObjects_.end() )
    101118        {
    102             if (!wePointer)
    103                 CCOUT(2) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
    104             if (!this->owner_)
    105                 CCOUT(2) << "No owner defined" << std::endl;
    106             return;
     119            delete it->second;
     120            this->radarObjects_.erase(it);
    107121        }
     122    }
    108123
    109         // try to find a panel already created
    110         Ogre::PanelOverlayElement* panel;
    111         //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
    112         if (itRadarDots_ == this->radarDots_.end())
    113         {
    114             // we have to create a new entry
    115             panel = static_cast<Ogre::PanelOverlayElement*>(
    116                 Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
    117             radarDots_.push_back(panel);
    118             // get right material
    119             panel->setMaterialName(TextureGenerator::getMaterialName(
    120                 shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
    121             this->overlay_->add2D(panel);
    122             this->itRadarDots_ = this->radarDots_.end();
    123         }
    124         else
    125         {
    126             panel = *itRadarDots_;
    127             ++itRadarDots_;
    128             const std::string& materialName = TextureGenerator::getMaterialName(
    129                 shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour());
    130             if (materialName != panel->getMaterialName())
    131                 panel->setMaterialName(materialName);
    132         }
    133         panel->show();
     124    void HUDRadar::objectChanged( RadarViewable* rv )
     125    {
     126        assert( this->radarObjects_.find(rv) != this->radarObjects_.end() );
     127        Ogre::PanelOverlayElement* panel = this->radarObjects_[rv];
     128        panel->setMaterialName(TextureGenerator::getMaterialName(
     129            shapeMaterials_[rv->getRadarObjectShape()], rv->getRadarObjectColour()));
     130    }
    134131
    135         // set size to fit distance...
    136         float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
    137         // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
    138         float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
    139         panel->setDimensions(size, size);
    140 
    141         // calc position on radar...
    142         Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
    143         coord *= Ogre::Math::PI / 3.5f; // small adjustment to make it fit the texture
    144         panel->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f);
    145 
    146         if (bIsMarked)
    147         {
    148             this->marker_->show();
    149             this->marker_->setDimensions(size * 1.5f, size * 1.5f);
    150             this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f);
    151         }
     132    void HUDRadar::gatherObjects()
     133    {
     134        const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects();
     135        std::set<RadarViewable*>::const_iterator it;
     136        for( it=objectSet.begin(); it!=objectSet.end(); ++it )
     137            this->addObject(*it);
    152138    }
    153139
    154140    void HUDRadar::radarTick(float dt)
    155141    {
    156         for (itRadarDots_ = radarDots_.begin(); itRadarDots_ != radarDots_.end(); ++itRadarDots_)
    157             (*itRadarDots_)->hide();
    158         this->itRadarDots_ = this->radarDots_.begin();
    159         this->marker_->hide();
     142        // Make sure the owner of the radar was defined
     143        if( !this->owner_ )
     144        {
     145            CCOUT(0) << "No owner defined" << std::endl;
     146            assert(0);
     147        }
     148
     149        this->marker_->hide();      // in case that no object is in focus
     150        // get the focus object
     151        Radar* radar = this->getOwner()->getScene()->getRadar();
     152        const RadarViewable* focusObject = radar->getFocus();
     153
     154        // update the distances for all objects
     155        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
     156        for( it = this->radarObjects_.begin(); it != this->radarObjects_.end(); ++it )
     157        {
     158            // Make sure the object really is a WorldEntity
     159            const WorldEntity* wePointer = it->first->getWorldEntity();
     160            if( !wePointer )
     161            {
     162                CCOUT(0) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
     163                assert(0);
     164            }
     165            bool isFocus = (it->first == focusObject);
     166            // set size to fit distance...
     167            float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
     168            // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
     169            float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
     170            it->second->setDimensions(size, size);
     171
     172            // calc position on radar...
     173            Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
     174            coord *= Ogre::Math::PI / 3.5f; // small adjustment to make it fit the texture
     175            it->second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f);
     176            it->second->show();
     177
     178            // if this object is in focus, then set the focus marker
     179            if (isFocus)
     180            {
     181                this->marker_->setDimensions(size * 1.5f, size * 1.5f);
     182                this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f);
     183                this->marker_->show();
     184            }
     185        }
    160186    }
    161187
     
    165191
    166192        this->owner_ = orxonox_cast<Pawn*>(this->getOwner());
     193        assert(this->radarObjects_.size()==0);
     194        this->gatherObjects();
    167195    }
    168196}
  • code/branches/hudelements/src/modules/overlays/hud/HUDRadar.h

    r5781 r6727  
    6565
    6666        // RadarListener interface
    67         void displayObject(RadarViewable* viewable, bool bIsMarked);
     67        virtual void addObject(RadarViewable* viewable);
     68        virtual void removeObject(RadarViewable* viewable);
     69        virtual void objectChanged( RadarViewable* rv );
    6870        void radarTick(float dt);
     71       
     72        void gatherObjects();
    6973
    7074        std::map<RadarViewable::Shape, std::string> shapeMaterials_;
    7175
    72         std::vector<Ogre::PanelOverlayElement*> radarDots_;
    73         std::vector<Ogre::PanelOverlayElement*>::iterator itRadarDots_;
     76//         std::vector<Ogre::PanelOverlayElement*> radarDots_;
     77//         std::vector<Ogre::PanelOverlayElement*>::iterator itRadarDots_;
     78        std::map<RadarViewable*, Ogre::PanelOverlayElement*> radarObjects_;
    7479        Ogre::PanelOverlayElement* marker_;
    7580
Note: See TracChangeset for help on using the changeset viewer.