Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2722 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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