Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6727


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

HUDRadar now working again

Location:
code/branches/hudelements/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hudelements/src/modules/objects/triggers/CheckPoint.cc

    r5929 r6727  
    3838    CreateFactory(CheckPoint);
    3939
    40     CheckPoint::CheckPoint(BaseObject* creator) : DistanceTrigger(creator)
     40    CheckPoint::CheckPoint(BaseObject* creator)
     41        : DistanceTrigger(creator)
     42        , RadarViewable(creator)
    4143    {
    4244        RegisterObject(CheckPoint);
  • 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
  • code/branches/hudelements/src/orxonox/Radar.cc

    r6417 r6727  
    7777    }
    7878
     79    void Radar::addRadarObject(RadarViewable* rv)
     80    {
     81        assert( this->radarObjects_.find(rv) == this->radarObjects_.end() );
     82        this->radarObjects_.insert(rv);
     83        // iterate through all radarlisteners and notify them
     84        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
     85        {
     86            (*itListener)->addObject(rv);
     87        }
     88    }
     89   
     90    void Radar::removeRadarObject(RadarViewable* rv)
     91    {
     92        assert( this->radarObjects_.find(rv) != this->radarObjects_.end() );
     93        this->radarObjects_.erase(rv);
     94        // iterate through all radarlisteners and notify them
     95        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
     96        {
     97            (*itListener)->removeObject(rv);
     98        }
     99    }
     100
    79101    const RadarViewable* Radar::getFocus()
    80102    {
     
    109131        {
    110132            (*itListener)->radarTick(dt);
    111 
    112             for (ObjectList<RadarViewable>::iterator itElement = ObjectList<RadarViewable>::begin(); itElement; ++itElement)
    113             {
    114                 if ((*itElement)->getRadarVisibility())
    115                     if ((*itListener)->getRadarSensitivity() > (*itElement)->getRadarObjectCamouflage())
    116                         (*itListener)->displayObject(*itElement, *itElement == this->focus_);
    117             }
    118133        }
    119134    }
     
    188203        }
    189204    }
     205   
     206    void Radar::radarObjectChanged(RadarViewable* rv)
     207    {
     208        for (ObjectList<RadarListener>::iterator itListener = ObjectList<RadarListener>::begin(); itListener; ++itListener)
     209        {
     210          (*itListener)->objectChanged(rv);
     211        }
     212    }
    190213}
  • code/branches/hudelements/src/orxonox/Radar.h

    r6417 r6727  
    3838
    3939#include <map>
     40#include <set>
    4041#include <string>
    4142
     
    4849    class _OrxonoxExport Radar : public Tickable
    4950    {
     51        friend class RadarViewable;
    5052    public:
    5153        Radar();
     
    5860
    5961        void listObjects() const;
     62        const std::set<RadarViewable*>& getRadarObjects() const
     63            { return this->radarObjects_; }
    6064
    6165        void releaseFocus();
     
    6670
    6771        void updateFocus();
     72        void addRadarObject(RadarViewable* rv);
     73        void removeRadarObject(RadarViewable* rv);
     74        void radarObjectChanged(RadarViewable* rv);
    6875
    6976        ObjectListIterator<RadarViewable> itFocus_;
    7077        RadarViewable* focus_;
    7178        std::map<std::string, RadarViewable::Shape> objectTypes_;
     79        std::set<RadarViewable*> radarObjects_;
    7280        int objectTypeCounter_;
    7381    };
  • code/branches/hudelements/src/orxonox/interfaces/RadarListener.h

    r5781 r6727  
    4141        virtual ~RadarListener() { }
    4242
    43         virtual void displayObject(RadarViewable* viewable, bool bIsMarked) = 0;
     43        virtual void addObject(RadarViewable* viewable) = 0;
     44        virtual void removeObject(RadarViewable* viewable) = 0;
     45        virtual void objectChanged(RadarViewable* viewable) = 0;
    4446        virtual float getRadarSensitivity() const = 0;
    4547        virtual void radarTick(float dt) = 0;
  • code/branches/hudelements/src/orxonox/interfaces/RadarViewable.cc

    r6717 r6727  
    4040        @brief Constructor.
    4141    */
    42     RadarViewable::RadarViewable()
     42    RadarViewable::RadarViewable(BaseObject* creator)
    4343        : isHumanShip_(false)
    4444        , bVisibility_(true)
     45        , bInitialized_(false)
     46        , creator_(creator)
    4547        , radarObjectCamouflage_(0.0f)
    4648        , radarObjectShape_(Dot)
     
    5052
    5153        this->uniqueId_=getUniqueNumberString();
     54        this->creator_->getScene()->getRadar()->addRadarObject(this);
     55        this->bInitialized_ = true;
    5256    }
    5357
     
    5559    RadarViewable::~RadarViewable()
    5660    {
     61        if( this->bInitialized_ )
     62            this->creator_->getScene()->getRadar()->removeRadarObject(this);
    5763    }
    5864
    59     void RadarViewable::setRadarObjectDescription(const std::string& str)
    60     {
    61         Radar* radar = this->getWorldEntity()->getScene()->getRadar();
    62         if (radar)
    63             this->radarObjectShape_ = radar->addObjectDescription(str);
    64         else
    65         {
    66             CCOUT(2) << "Attempting to access the radar, but the radar is non existent." << std::endl;
    67         }
    68         this->radarObjectDescription_ = str;
    69     }
     65//     void RadarViewable::setRadarObjectDescription(const std::string& str)
     66//     {
     67//         Radar* radar = this->getWorldEntity()->getScene()->getRadar();
     68//         if (radar)
     69//             this->radarObjectShape_ = radar->addObjectDescription(str);
     70//         else
     71//         {
     72//             CCOUT(2) << "Attempting to access the radar, but the radar is non existent." << std::endl;
     73//         }
     74//         this->radarObjectDescription_ = str;
     75//     }
    7076
    7177    const Vector3& RadarViewable::getRVWorldPosition() const
     
    9197        }
    9298    }
     99   
     100    void RadarViewable::settingsChanged()
     101    {
     102        this->creator_->getScene()->getRadar()->radarObjectChanged(this);
     103    }
    93104}
  • code/branches/hudelements/src/orxonox/interfaces/RadarViewable.h

    r6717 r6727  
    4040namespace orxonox
    4141{
     42    class BaseObject;
     43
    4244    /**
    4345    @brief Interface for receiving window events.
     
    5557
    5658    public:
    57         RadarViewable();
     59        RadarViewable(BaseObject* creator);
    5860        virtual ~RadarViewable();
    5961
    6062        inline void setRadarObjectCamouflage(float camouflage)
    61             { this->radarObjectCamouflage_ = camouflage; }
     63            {
     64                if( this->radarObjectCamouflage_ != camouflage )
     65                {
     66                    this->radarObjectCamouflage_ = camouflage;
     67                    this->settingsChanged();
     68                }
     69            }
    6270        inline float getRadarObjectCamouflage() const
    6371            { return this->radarObjectCamouflage_; }
    6472
    6573        inline void setRadarObjectColour(const ColourValue& colour)
    66             { this->radarObjectColour_ = colour; }
     74            {
     75                if(this->radarObjectColour_ != colour)
     76                {
     77                    this->radarObjectColour_ = colour;
     78                    this->settingsChanged();
     79                }
     80            }
    6781        inline const ColourValue& getRadarObjectColour() const
    6882            { return this->radarObjectColour_; }
    6983
    70         void setRadarObjectDescription(const std::string& str);
    71         inline const std::string& getRadarObjectDescription() const
    72             { return this->radarObjectDescription_; }
     84//         void setRadarObjectDescription(const std::string& str);
     85//         inline const std::string& getRadarObjectDescription() const
     86//             { return this->radarObjectDescription_; }
    7387
    7488        inline void setRadarVisibility(bool b)
    75             { this->bVisibility_ = b; }
     89            {
     90                if(b!=this->bVisibility_)
     91                {
     92                    this->bVisibility_ = b;
     93                    this->settingsChanged();
     94                }
     95            }
    7696        inline bool getRadarVisibility() const
    7797            { return this->bVisibility_; }
     
    83103
    84104        inline void setRadarObjectShape(Shape shape)
    85             { this->radarObjectShape_ = shape; }
     105            {
     106                if( this->radarObjectShape_ != shape )
     107                {
     108                    this->radarObjectShape_ = shape;
     109                    this->settingsChanged();
     110                }
     111            }
    86112        inline Shape getRadarObjectShape() const
    87113            { return this->radarObjectShape_; }
     114        void settingsChanged();
    88115
    89116
     
    98125        void validate(const WorldEntity* object) const;
    99126        bool bVisibility_;
     127        bool bInitialized_;
    100128        //Map
    101129        std::string uniqueId_;
     130        BaseObject* creator_;
    102131
    103132
  • code/branches/hudelements/src/orxonox/worldentities/pawns/Pawn.cc

    r6540 r6727  
    5353    CreateFactory(Pawn);
    5454
    55     Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator)
     55    Pawn::Pawn(BaseObject* creator)
     56        : ControllableEntity(creator)
     57        , RadarViewable(creator)
    5658    {
    5759        RegisterObject(Pawn);
Note: See TracChangeset for help on using the changeset viewer.