Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cegui0.8_ogre1.9/src/modules/overlays/hud/HUDRadar.cc @ 12035

Last change on this file since 12035 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

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