Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1615 was 1615, checked in by rgrieder, 16 years ago
  • added blankString to String so you can return ""; even if it's a const std::string&
  • fixed several bugs with aspect correct and margin alignment
  • added console commands for OrxonoxOverlays and OverlayGroups for rotate, scale and scroll (you can access the by name (from name=.. in xml file), e.g. "OrxonoxOverlay rotateOverlay SpeedBar 90)
  • converted everything in overlays/ to 4 spaces/tab ;)
  • removed all using namespace Ogre;
  • added background_ Panel to OrxonoxOverlay, since most of the derived classes can use that
  • should work now, but I'll have to test on a tardis box first
  • 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    HUDRadar::HUDRadar()
48        : marker_(0)
49    {
50        RegisterObject(HUDRadar);
51    }
52
53    HUDRadar::~HUDRadar()
54    {
55        if (this->marker_)
56            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
57        for (std::vector<Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.begin();
58            it != this->radarDots_.end(); ++it)
59        {
60            Ogre::OverlayManager::getSingleton().destroyOverlayElement(*it);
61        }
62    }
63
64    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
65    {
66        if (mode == XMLPort::LoadObject)
67            this->bCorrectAspect_ = true;
68
69        OrxonoxOverlay::XMLPort(xmlElement, mode);
70
71        if (mode == XMLPort::LoadObject)
72        {
73            marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
74                .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberStr()));
75            marker_->setMaterialName("Orxonox/RadarMarker");
76            overlay_->add2D(marker_);
77            marker_->hide();
78
79            this->sensitivity_ = 1.0f;
80            this->halfDotSizeDistance_ = 3000.0f;
81            this->maximumDotSize_ = 0.1;
82        }
83
84        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
85        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
86        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
87
88        shapeMaterials_[RadarViewable::Dot]      = "RadarSquare.tga";
89        shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga";
90        shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
91    }
92
93    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
94    {
95        const WorldEntity* wePointer = object->getWorldEntity();
96
97        // Just to be sure that we actually have a WorldEntity
98        // We could do a dynamic_cast, but that's a lot slower
99        if (!wePointer)
100        {
101            CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
102            return;
103        }
104
105        // try to find a panel already created
106        Ogre::PanelOverlayElement* panel;
107        //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
108        if (itRadarDots_ == this->radarDots_.end())
109        {
110            // we have to create a new entry
111            panel = static_cast<Ogre::PanelOverlayElement*>(
112                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr()));
113            radarDots_.push_back(panel);
114            // get right material
115            panel->setMaterialName(TextureGenerator::getMaterialName(
116                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour()));
117            this->overlay_->add2D(panel);
118            this->itRadarDots_ = this->radarDots_.end();
119        }
120        else
121        {
122            panel = *itRadarDots_;
123            ++itRadarDots_;
124            std::string materialName = TextureGenerator::getMaterialName(
125                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour());
126            if (materialName != panel->getMaterialName())
127                panel->setMaterialName(materialName);
128        }
129
130        // set size to fit distance...
131        float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
132        // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
133        float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
134        panel->setDimensions(size, size);
135
136        // calc position on radar...
137        Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition());
138        coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
139        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
140
141        if (bIsMarked)
142        {
143            this->marker_->show();
144            this->marker_->setDimensions(size * 1.5, size * 1.5);
145            this->marker_->setPosition((1.0 + coord.x - size * 1.5) * 0.5, (1.0 - coord.y - size * 1.5) * 0.5);
146        }
147    }
148
149    void HUDRadar::radarTick(float dt)
150    {
151        this->itRadarDots_ = this->radarDots_.begin();
152        this->marker_->hide();
153    }
154}
Note: See TracBrowser for help on using the repository browser.