Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/hud/HUDRadar.cc @ 3159

Last change on this file since 3159 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 6.4 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 "HUDRadar.h"
31
32#include <OgreOverlayManager.h>
33#include <OgrePanelOverlayElement.h>
34
35#include "util/Math.h"
36#include "util/String.h"
37#include "core/ConsoleCommand.h"
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "objects/Radar.h"
41#include "objects/worldentities/WorldEntity.h"
42#include "objects/worldentities/pawns/Pawn.h"
43#include "tools/TextureGenerator.h"
44
45namespace orxonox
46{
47    CreateFactory(HUDRadar);
48
49    HUDRadar::HUDRadar(BaseObject* creator)
50        : OrxonoxOverlay(creator)
51    {
52        RegisterObject(HUDRadar);
53
54        this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
55            .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString()));
56        this->marker_->setMaterialName("Orxonox/RadarMarker");
57        this->overlay_->add2D(this->marker_);
58        this->marker_->hide();
59
60        this->setRadarSensitivity(1.0f);
61        this->setHalfDotSizeDistance(3000.0f);
62        this->setMaximumDotSize(0.1f);
63
64        this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.tga";
65        this->shapeMaterials_[RadarViewable::Triangle] = "RadarTriangle.tga";
66        this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
67
68        this->owner_ = 0;
69    }
70
71    HUDRadar::~HUDRadar()
72    {
73        if (this->isInitialized())
74        {
75            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
76            for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();
77                it != this->radarDots_.end(); ++it)
78            {
79                Ogre::OverlayManager::getSingleton().destroyOverlayElement(*it);
80            }
81        }
82    }
83
84    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
85    {
86        SUPER(HUDRadar, XMLPort, xmlElement, mode);
87
88        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
89        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
90        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
91    }
92
93    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
94    {
95        if (object == static_cast<RadarViewable*>(this->owner_))
96            return;
97
98        const WorldEntity* wePointer = object->getWorldEntity();
99
100        // Just to be sure that we actually have a WorldEntity.
101        // We could do a dynamic_cast, but that would be a lot slower.
102        if (!wePointer || !this->owner_)
103        {
104            if (!wePointer)
105                CCOUT(2) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
106            if (!this->owner_)
107                CCOUT(2) << "No owner defined" << std::endl;
108            return;
109        }
110
111        // try to find a panel already created
112        Ogre::PanelOverlayElement* panel;
113        //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
114        if (itRadarDots_ == this->radarDots_.end())
115        {
116            // we have to create a new entry
117            panel = static_cast<Ogre::PanelOverlayElement*>(
118                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
119            radarDots_.push_back(panel);
120            // get right material
121            panel->setMaterialName(TextureGenerator::getMaterialName(
122                shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
123            this->overlay_->add2D(panel);
124            this->itRadarDots_ = this->radarDots_.end();
125        }
126        else
127        {
128            panel = *itRadarDots_;
129            ++itRadarDots_;
130            std::string materialName = TextureGenerator::getMaterialName(
131                shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour());
132            if (materialName != panel->getMaterialName())
133                panel->setMaterialName(materialName);
134        }
135        panel->show();
136
137        // set size to fit distance...
138        float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
139        // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
140        float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
141        panel->setDimensions(size, size);
142
143        // calc position on radar...
144        Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
145        coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
146        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
147
148        if (bIsMarked)
149        {
150            this->marker_->show();
151            this->marker_->setDimensions(size * 1.5, size * 1.5);
152            this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5);
153        }
154    }
155
156    void HUDRadar::radarTick(float dt)
157    {
158        for (itRadarDots_ = radarDots_.begin(); itRadarDots_ != radarDots_.end(); ++itRadarDots_)
159            (*itRadarDots_)->hide();
160        this->itRadarDots_ = this->radarDots_.begin();
161        this->marker_->hide();
162    }
163
164    void HUDRadar::changedOwner()
165    {
166        SUPER(HUDRadar, changedOwner);
167
168        this->owner_ = dynamic_cast<Pawn*>(this->getOwner());
169    }
170}
Note: See TracBrowser for help on using the repository browser.