Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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