Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5703 was 3325, checked in by rgrieder, 15 years ago

Merged orxonox_cast related revisions from core4 back to trunk.

  • 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/StringUtils.h"
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39#include "tools/TextureGenerator.h"
40#include "objects/worldentities/WorldEntity.h"
41#include "objects/worldentities/pawns/Pawn.h"
42
43namespace orxonox
44{
45    CreateFactory(HUDRadar);
46
47    HUDRadar::HUDRadar(BaseObject* creator)
48        : OrxonoxOverlay(creator)
49    {
50        RegisterObject(HUDRadar);
51
52        this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
53            .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString()));
54        this->marker_->setMaterialName("Orxonox/RadarMarker");
55        this->overlay_->add2D(this->marker_);
56        this->marker_->hide();
57
58        this->setRadarSensitivity(1.0f);
59        this->setHalfDotSizeDistance(3000.0f);
60        this->setMaximumDotSize(0.1f);
61
62        this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.tga";
63        this->shapeMaterials_[RadarViewable::Triangle] = "RadarTriangle.tga";
64        this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
65
66        this->owner_ = 0;
67    }
68
69    HUDRadar::~HUDRadar()
70    {
71        if (this->isInitialized())
72        {
73            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
74            for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();
75                it != this->radarDots_.end(); ++it)
76            {
77                Ogre::OverlayManager::getSingleton().destroyOverlayElement(*it);
78            }
79        }
80    }
81
82    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
83    {
84        SUPER(HUDRadar, XMLPort, xmlElement, mode);
85
86        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
87        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
88        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
89    }
90
91    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
92    {
93        if (object == static_cast<RadarViewable*>(this->owner_))
94            return;
95
96        const WorldEntity* wePointer = object->getWorldEntity();
97
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        {
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;
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" + 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            std::string materialName = TextureGenerator::getMaterialName(
129                shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour());
130            if (materialName != panel->getMaterialName())
131                panel->setMaterialName(materialName);
132        }
133        panel->show();
134
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.5; // small adjustment to make it fit the texture
144        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
145
146        if (bIsMarked)
147        {
148            this->marker_->show();
149            this->marker_->setDimensions(size * 1.5, size * 1.5);
150            this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5);
151        }
152    }
153
154    void HUDRadar::radarTick(float dt)
155    {
156        for (itRadarDots_ = radarDots_.begin(); itRadarDots_ != radarDots_.end(); ++itRadarDots_)
157            (*itRadarDots_)->hide();
158        this->itRadarDots_ = this->radarDots_.begin();
159        this->marker_->hide();
160    }
161
162    void HUDRadar::changedOwner()
163    {
164        SUPER(HUDRadar, changedOwner);
165
166        this->owner_ = orxonox_cast<Pawn*>(this->getOwner());
167    }
168}
Note: See TracBrowser for help on using the repository browser.