Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/overlays/hud/HUDRadar.cc @ 1614

Last change on this file since 1614 was 1614, checked in by rgrieder, 16 years ago
  • Dots on the Radar actually disappear now when a Ship gets destroyed…
  • svn save to keep History of HUDText when renaming AND moving
  • Property svn:eol-style set to native
File size: 5.8 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 *
28 */
29
30#include "OrxonoxStableHeaders.h"
31#include "HUDRadar.h"
32
33#include <OgreOverlayManager.h>
34#include <OgrePanelOverlayElement.h>
35
36#include "util/Math.h"
37#include "core/ConsoleCommand.h"
38#include "objects/SpaceShip.h"
39#include "objects/WorldEntity.h"
40#include "tools/TextureGenerator.h"
41#include "Radar.h"
42
43namespace orxonox
44{
45    CreateFactory(HUDRadar);
46
47    using namespace Ogre;
48
49    HUDRadar::HUDRadar()
50        : marker_(0)
51    {
52        RegisterObject(HUDRadar);
53    }
54
55    HUDRadar::~HUDRadar()
56    {
57        if (this->isInitialized())
58        {
59            if (this->marker_)
60                OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
61            for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();
62                it != this->radarDots_.end(); ++it)
63            {
64                OverlayManager::getSingleton().destroyOverlayElement(*it);
65            }
66        }
67    }
68
69    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
70    {
71        OrxonoxOverlay::XMLPort(xmlElement, mode);
72
73        if (mode == XMLPort::LoadObject)
74        {
75            this->sensitivity_ = 1.0f;
76            this->halfDotSizeDistance_ = 3000.0f;
77            this->maximumDotSize_ = 0.1;
78        }
79
80        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
81        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
82        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
83
84        shapeMaterials_[RadarViewable::Dot]      = "RadarSquare.tga";
85        shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga";
86        shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
87
88        if (mode == XMLPort::LoadObject)
89        {
90            marker_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Marker");
91            marker_->setMaterialName("Orxonox/RadarMarker");
92            overlay_->add2D(marker_);
93            marker_->hide();
94        }
95    }
96
97    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
98    {
99        const WorldEntity* wePointer = object->getWorldEntity();
100
101        // Just to be sure that we actually have a WorldEntity
102        // We could do a dynamic_cast, but that's a lot slower
103        if (!wePointer)
104        {
105            CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
106            return;
107        }
108
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" + getUniqueNumberStr()));
117            radarDots_.push_back(panel);
118            // get right material
119            panel->setMaterialName(TextureGenerator::getMaterialName(
120                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour()));
121            this->overlay_->add2D(panel);
122            this->itRadarDots_ = this->radarDots_.end();
123        }
124        else
125        {
126            panel = *itRadarDots_;
127            ++itRadarDots_;
128            std::string materialName = TextureGenerator::getMaterialName(
129                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour());
130            if (materialName != panel->getMaterialName())
131                panel->setMaterialName(materialName);
132        }
133
134        // set size to fit distance...
135        float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
136        // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
137        float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
138        panel->setDimensions(size, size);
139
140        // calc position on radar...
141        Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition());
142        coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
143        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
144
145        if (bIsMarked)
146        {
147            this->marker_->show();
148            this->marker_->setDimensions(size * 1.5, size * 1.5);
149            this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5);
150        }
151    }
152
153    void HUDRadar::radarTick(float dt)
154    {
155        this->itRadarDots_ = this->radarDots_.begin();
156        this->marker_->hide();
157    }
158}
Note: See TracBrowser for help on using the repository browser.