Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/radarDreiD/src/modules/overlays/hud/HUDRadar.cc @ 9824

Last change on this file since 9824 was 9824, checked in by wroennin, 11 years ago

Updated

  • Property svn:eol-style set to native
File size: 10.0 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 *      Felix Schulthess
25 *   Co-authors:
26 *      Reto Grieder
27 *      Wolfgang Roenninger
28 *
29 */
30
31#include "HUDRadar.h"
32
33#include <OgreOverlayManager.h>
34#include <OgrePanelOverlayElement.h>
35
36#include "util/Math.h"
37#include "util/StringUtils.h"
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "tools/TextureGenerator.h"
41#include "worldentities/ControllableEntity.h"
42#include "Scene.h"
43#include "Radar.h"
44#include "core/config/ConfigValueIncludes.h"
45
46namespace orxonox
47{
48    RegisterClass(HUDRadar);
49
50    HUDRadar::HUDRadar(Context* context)
51        : OrxonoxOverlay(context)
52    {
53        RegisterObject(HUDRadar);
54        this->setConfigValues();
55
56        this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
57            .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString()));
58        this->marker_->setMaterialName("Orxonox/RadarMarker");
59        this->overlay_->add2D(this->marker_);
60        this->marker_->hide();
61
62        this->setRadarSensitivity(1.0f);
63        this->setHalfDotSizeDistance(3000.0f);
64        this->setMaximumDotSize(0.1f);
65        this->setMaximumDotSize3D(0.07f);
66
67        this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.png";
68        this->shapeMaterials_[RadarViewable::Triangle] = "RadarTriangle.png";
69        this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.png";
70        this->setDetectionLimit( 10000.0f );
71        this->owner_ = 0;
72    }
73
74    HUDRadar::~HUDRadar()
75    {
76        if (this->isInitialized())
77        {
78            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
79            for (std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it = this->radarObjects_.begin();
80                it != this->radarObjects_.end(); ++it)
81            {
82                Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second);
83            }
84        }
85    }
86
87
88
89    void HUDRadar::setConfigValues()
90       {
91           SetConfigValue(RadarMode_, true);
92       }
93
94    void HUDRadar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
95    {
96        SUPER(HUDRadar, XMLPort, xmlelement, mode);
97
98        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlelement, mode);
99        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlelement, mode);
100        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlelement, mode);
101        XMLPortParam(HUDRadar, "maximumDotSize3D", setMaximumDotSize3D, getMaximumDotSize3D, xmlelement, mode);
102        XMLPortParam(HUDRadar, "Material2D", set2DMaterial, get2DMaterial, xmlelement, mode);
103        XMLPortParam(HUDRadar, "Material3Dmiddle", set3DMaterial, get3DMaterial, xmlelement, mode);
104
105
106    }
107
108    void HUDRadar::addObject(RadarViewable* object)
109    {
110        if (object == orxonox_cast<RadarViewable*>(this->owner_))
111            return;
112        if( showObject(object) == false ) //do not show objects that are "invisible" or "radar invisible"
113            return;
114
115        // Make sure the object hasn't been added yet
116        assert( this->radarObjects_.find(object) == this->radarObjects_.end() );
117
118        // Create everything needed to display the object on the radar and add it to the map
119        Ogre::PanelOverlayElement* panel;
120        panel = static_cast<Ogre::PanelOverlayElement*>(
121            Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
122        this->overlay_->add2D(panel);
123        // get right material
124        panel->setMaterialName(TextureGenerator::getMaterialName(
125            shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
126        panel->hide();
127        this->radarObjects_[object] = panel;
128    }
129
130    void HUDRadar::removeObject(RadarViewable* object)
131    {
132        // If object was added at all then remove it
133        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
134        it = this->radarObjects_.find( object );
135        if( it != this->radarObjects_.end() )
136        {
137            Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second);
138            this->radarObjects_.erase(it);
139        }
140    }
141
142    void HUDRadar::objectChanged( RadarViewable* rv )
143    {// The new implementation behaves more precisely, since inactive RadarViewables are not displayed anymore.
144        this->removeObject(rv);
145        this->addObject(rv);
146    }
147
148    void HUDRadar::gatherObjects()
149    {
150        const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects();
151        std::set<RadarViewable*>::const_iterator it;
152        for( it=objectSet.begin(); it!=objectSet.end(); ++it )
153            this->addObject(*it);
154        this->radarTick(0);
155    }
156
157    void HUDRadar::radarTick(float dt)
158    {
159        // Make sure the owner of the radar was defined
160        if( !this->owner_ )
161            return;
162
163        this->marker_->hide();      // in case that no object is in focus
164        // get the focus object
165        Radar* radar = this->getOwner()->getScene()->getRadar();
166        const RadarViewable* focusObject = radar->getFocus();
167
168        // update the distances for all objects
169        std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it;
170
171
172        if(RadarMode_)
173        {
174                this->setBackgroundMaterial(material3D_);
175                //this->radar3Dback_->show();
176                //this
177
178
179        }
180        else
181                this->setBackgroundMaterial(material2D_);
182
183
184
185        for( it = this->radarObjects_.begin(); it != this->radarObjects_.end(); ++it )
186        {
187            // Make sure the object really is a WorldEntity
188            const WorldEntity* wePointer = it->first->getWorldEntity();
189            if( !wePointer )
190            {
191                orxout(internal_error) << "Cannot display a non-WorldEntitiy on the radar" << endl;
192                assert(0);
193            }
194            bool isFocus = (it->first == focusObject);
195            // set size to fit distance...
196            float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
197            // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
198
199            float size;
200            if(RadarMode_)
201                size = maximumDotSize3D_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * it->first->getRadarObjectScale();
202            else
203                size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * it->first->getRadarObjectScale();
204            it->second->setDimensions(size, size);
205
206            // calc position on radar...
207            Vector2 coord;
208            if(RadarMode_)
209            {
210                coord = get3DProjection(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), 0.6435011, detectionLimit_);
211
212                // set zOrder on screen
213                bool overXZPlain = isObjectHigherThanShipOnMap(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), 0.6435011);
214
215                if(overXZPlain == false && (it->second->getZOrder() >  100 * this->overlay_->getZOrder())) // it appears that zOrder of attached Overlayelements is 100 times the zOrder of the Overlay
216                        it->second->_notifyZOrder(this->overlay_->getZOrder() * 100 - 1);
217                if(overXZPlain == true && (it->second->getZOrder() <= 100 * this->overlay_->getZOrder()))
218                        it->second->_notifyZOrder(this->overlay_->getZOrder() * 100 + 1);
219            }
220            else
221                coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
222
223            coord *= math::pi / 3.5f; // small adjustment to make it fit the texture
224            it->second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f);
225
226            if( distance < detectionLimit_ || detectionLimit_ < 0 )
227                it->second->show();
228            else
229                it->second->hide();
230
231            // if this object is in focus, then set the focus marker
232            if (isFocus)
233            {
234                this->marker_->setDimensions(size * 1.5f, size * 1.5f);
235                this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f);
236                this->marker_->show();
237            }
238        }
239    }
240
241    bool HUDRadar::showObject(RadarViewable* rv)
242    {
243        if ( rv == orxonox_cast<RadarViewable*> ( this->getOwner() ) )
244            return false;
245        assert( rv->getWorldEntity() );
246        if ( rv->getWorldEntity()->isVisible()==false || rv->getRadarVisibility()==false )
247            return false;
248        return true;
249    }
250
251
252    void HUDRadar::changedOwner()
253    {
254        SUPER(HUDRadar, changedOwner);
255
256        this->owner_ = orxonox_cast<ControllableEntity*>(this->getOwner());
257        assert(this->radarObjects_.size() == 0);
258        this->gatherObjects();
259    }
260}
Note: See TracBrowser for help on using the repository browser.