Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3114 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
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
[1604]30#include "HUDRadar.h"
[1283]31
[1502]32#include <OgreOverlayManager.h>
[1614]33#include <OgrePanelOverlayElement.h>
[1502]34
[1614]35#include "util/Math.h"
[2087]36#include "util/String.h"
[1502]37#include "core/ConsoleCommand.h"
[1616]38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
[1819]40#include "objects/Radar.h"
[2662]41#include "objects/worldentities/WorldEntity.h"
42#include "objects/worldentities/pawns/Pawn.h"
[1613]43#include "tools/TextureGenerator.h"
[1502]44
[1283]45namespace orxonox
46{
[1604]47    CreateFactory(HUDRadar);
48
[2087]49    HUDRadar::HUDRadar(BaseObject* creator)
50        : OrxonoxOverlay(creator)
[1604]51    {
52        RegisterObject(HUDRadar);
[2087]53
[2662]54        this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
[2087]55            .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString()));
[2662]56        this->marker_->setMaterialName("Orxonox/RadarMarker");
57        this->overlay_->add2D(this->marker_);
58        this->marker_->hide();
[2087]59
[2662]60        this->setRadarSensitivity(1.0f);
61        this->setHalfDotSizeDistance(3000.0f);
62        this->setMaximumDotSize(0.1f);
[2087]63
[2662]64        this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.tga";
[3086]65        this->shapeMaterials_[RadarViewable::Triangle] = "RadarTriangle.tga";
[2662]66        this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
67
68        this->owner_ = 0;
[1302]69    }
[1283]70
[1604]71    HUDRadar::~HUDRadar()
72    {
[2087]73        if (this->isInitialized())
74        {
[1615]75            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_);
[2087]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            }
[1604]81        }
[1302]82    }
[1283]83
[1604]84    void HUDRadar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
85    {
[1747]86        SUPER(HUDRadar, XMLPort, xmlElement, mode);
[1283]87
[2087]88        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
89        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
90        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
[1609]91    }
[1302]92
[1613]93    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
[1604]94    {
[2896]95        if (object == static_cast<RadarViewable*>(this->owner_))
[2662]96            return;
97
[1613]98        const WorldEntity* wePointer = object->getWorldEntity();
[1604]99
[1782]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.
[2662]102        if (!wePointer || !this->owner_)
[1609]103        {
[2662]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;
[1613]108            return;
[1609]109        }
[2662]110
[1613]111        // try to find a panel already created
112        Ogre::PanelOverlayElement* panel;
[1614]113        //std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
114        if (itRadarDots_ == this->radarDots_.end())
[1613]115        {
116            // we have to create a new entry
117            panel = static_cast<Ogre::PanelOverlayElement*>(
[2087]118                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString()));
[1614]119            radarDots_.push_back(panel);
[1613]120            // get right material
121            panel->setMaterialName(TextureGenerator::getMaterialName(
[2662]122                shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour()));
[1613]123            this->overlay_->add2D(panel);
[1614]124            this->itRadarDots_ = this->radarDots_.end();
[1613]125        }
[1609]126        else
[1614]127        {
128            panel = *itRadarDots_;
129            ++itRadarDots_;
130            std::string materialName = TextureGenerator::getMaterialName(
[2662]131                shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour());
[1614]132            if (materialName != panel->getMaterialName())
133                panel->setMaterialName(materialName);
134        }
[1782]135        panel->show();
[2662]136
[1613]137        // set size to fit distance...
[2662]138        float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length();
[1613]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...
[2662]144        Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition());
[1613]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();
[1614]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);
[1613]153        }
[1604]154    }
155
[1613]156    void HUDRadar::radarTick(float dt)
[1604]157    {
[1782]158        for (itRadarDots_ = radarDots_.begin(); itRadarDots_ != radarDots_.end(); ++itRadarDots_)
159            (*itRadarDots_)->hide();
[1614]160        this->itRadarDots_ = this->radarDots_.begin();
161        this->marker_->hide();
[1613]162    }
[2662]163
164    void HUDRadar::changedOwner()
165    {
166        SUPER(HUDRadar, changedOwner);
167
168        this->owner_ = dynamic_cast<Pawn*>(this->getOwner());
169    }
[1283]170}
Note: See TracBrowser for help on using the repository browser.