Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1616 was 1616, checked in by rgrieder, 16 years ago
  • precompiled header files have disadvantages too…
  • Property svn:eol-style set to native
File size: 5.9 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 "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "objects/SpaceShip.h"
41#include "objects/WorldEntity.h"
42#include "tools/TextureGenerator.h"
43#include "Radar.h"
44
45namespace orxonox
46{
47    CreateFactory(HUDRadar);
48
49    HUDRadar::HUDRadar()
50        : marker_(0)
51    {
52        RegisterObject(HUDRadar);
53    }
54
55    HUDRadar::~HUDRadar()
56    {
57        if (this->marker_)
58            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
59        for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();
60            it != this->radarDots_.end(); ++it)
61        {
62            Ogre::OverlayManager::getSingleton().destroyOverlayElement(*it);
63        }
64    }
65
66    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
67    {
68        if (mode == XMLPort::LoadObject)
69            this->bCorrectAspect_ = true;
70
71        OrxonoxOverlay::XMLPort(xmlElement, mode);
72
73        if (mode == XMLPort::LoadObject)
74        {
75            marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
76                .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberStr()));
77            marker_->setMaterialName("Orxonox/RadarMarker");
78            overlay_->add2D(marker_);
79            marker_->hide();
80
81            this->sensitivity_ = 1.0f;
82            this->halfDotSizeDistance_ = 3000.0f;
83            this->maximumDotSize_ = 0.1;
84        }
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        shapeMaterials_[RadarViewable::Dot]      = "RadarSquare.tga";
91        shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga";
92        shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
93    }
94
95    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
96    {
97        const WorldEntity* wePointer = object->getWorldEntity();
98
99        // Just to be sure that we actually have a WorldEntity
100        // We could do a dynamic_cast, but that's a lot slower
101        if (!wePointer)
102        {
103            CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
104            return;
105        }
106
107        // try to find a panel already created
108        Ogre::PanelOverlayElement* panel;
109        //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
110        if (itRadarDots_ == this->radarDots_.end())
111        {
112            // we have to create a new entry
113            panel = static_cast<Ogre::PanelOverlayElement*>(
114                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr()));
115            radarDots_.push_back(panel);
116            // get right material
117            panel->setMaterialName(TextureGenerator::getMaterialName(
118                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour()));
119            this->overlay_->add2D(panel);
120            this->itRadarDots_ = this->radarDots_.end();
121        }
122        else
123        {
124            panel = *itRadarDots_;
125            ++itRadarDots_;
126            std::string materialName = TextureGenerator::getMaterialName(
127                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour());
128            if (materialName != panel->getMaterialName())
129                panel->setMaterialName(materialName);
130        }
131
132        // set size to fit distance...
133        float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
134        // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
135        float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
136        panel->setDimensions(size, size);
137
138        // calc position on radar...
139        Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition());
140        coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
141        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
142
143        if (bIsMarked)
144        {
145            this->marker_->show();
146            this->marker_->setDimensions(size * 1.5, size * 1.5);
147            this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5);
148        }
149    }
150
151    void HUDRadar::radarTick(float dt)
152    {
153        this->itRadarDots_ = this->radarDots_.begin();
154        this->marker_->hide();
155    }
156}
Note: See TracBrowser for help on using the repository browser.