| [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" | 
|---|
| [7880] | 40 | #include "worldentities/ControllableEntity.h" | 
|---|
| [7163] | 41 | #include "Scene.h" | 
|---|
|  | 42 | #include "Radar.h" | 
|---|
| [1502] | 43 |  | 
|---|
| [1283] | 44 | namespace orxonox | 
|---|
|  | 45 | { | 
|---|
| [1604] | 46 | CreateFactory(HUDRadar); | 
|---|
|  | 47 |  | 
|---|
| [2087] | 48 | HUDRadar::HUDRadar(BaseObject* creator) | 
|---|
|  | 49 | : OrxonoxOverlay(creator) | 
|---|
| [1604] | 50 | { | 
|---|
|  | 51 | RegisterObject(HUDRadar); | 
|---|
| [2087] | 52 |  | 
|---|
| [2662] | 53 | this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
| [2087] | 54 | .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString())); | 
|---|
| [2662] | 55 | this->marker_->setMaterialName("Orxonox/RadarMarker"); | 
|---|
|  | 56 | this->overlay_->add2D(this->marker_); | 
|---|
|  | 57 | this->marker_->hide(); | 
|---|
| [2087] | 58 |  | 
|---|
| [2662] | 59 | this->setRadarSensitivity(1.0f); | 
|---|
|  | 60 | this->setHalfDotSizeDistance(3000.0f); | 
|---|
|  | 61 | this->setMaximumDotSize(0.1f); | 
|---|
| [2087] | 62 |  | 
|---|
| [7368] | 63 | this->shapeMaterials_[RadarViewable::Dot]      = "RadarDot.png"; | 
|---|
|  | 64 | this->shapeMaterials_[RadarViewable::Triangle] = "RadarTriangle.png"; | 
|---|
|  | 65 | this->shapeMaterials_[RadarViewable::Square]   = "RadarSquare.png"; | 
|---|
| [2662] | 66 |  | 
|---|
|  | 67 | this->owner_ = 0; | 
|---|
| [1302] | 68 | } | 
|---|
| [1283] | 69 |  | 
|---|
| [1604] | 70 | HUDRadar::~HUDRadar() | 
|---|
|  | 71 | { | 
|---|
| [2087] | 72 | if (this->isInitialized()) | 
|---|
|  | 73 | { | 
|---|
| [1615] | 74 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_); | 
|---|
| [7163] | 75 | for (std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it = this->radarObjects_.begin(); | 
|---|
|  | 76 | it != this->radarObjects_.end(); ++it) | 
|---|
| [2087] | 77 | { | 
|---|
| [7163] | 78 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second); | 
|---|
| [2087] | 79 | } | 
|---|
| [1604] | 80 | } | 
|---|
| [1302] | 81 | } | 
|---|
| [1283] | 82 |  | 
|---|
| [7401] | 83 | void HUDRadar::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| [1604] | 84 | { | 
|---|
| [7401] | 85 | SUPER(HUDRadar, XMLPort, xmlelement, mode); | 
|---|
| [1283] | 86 |  | 
|---|
| [7401] | 87 | XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlelement, mode); | 
|---|
|  | 88 | XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlelement, mode); | 
|---|
|  | 89 | XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlelement, mode); | 
|---|
| [1609] | 90 | } | 
|---|
| [1302] | 91 |  | 
|---|
| [7163] | 92 | void HUDRadar::addObject(RadarViewable* object) | 
|---|
| [1604] | 93 | { | 
|---|
| [7163] | 94 | if (object == dynamic_cast<RadarViewable*>(this->owner_)) | 
|---|
| [2662] | 95 | return; | 
|---|
|  | 96 |  | 
|---|
| [7163] | 97 | // Make sure the object hasn't been added yet | 
|---|
|  | 98 | assert( this->radarObjects_.find(object) == this->radarObjects_.end() ); | 
|---|
| [1604] | 99 |  | 
|---|
| [7163] | 100 | // Create everything needed to display the object on the radar and add it to the map | 
|---|
|  | 101 | Ogre::PanelOverlayElement* panel; | 
|---|
|  | 102 | panel = static_cast<Ogre::PanelOverlayElement*>( | 
|---|
|  | 103 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString())); | 
|---|
|  | 104 | this->overlay_->add2D(panel); | 
|---|
|  | 105 | // get right material | 
|---|
|  | 106 | panel->setMaterialName(TextureGenerator::getMaterialName( | 
|---|
|  | 107 | shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour())); | 
|---|
| [8738] | 108 | panel->hide(); | 
|---|
| [7163] | 109 | this->radarObjects_[object] = panel; | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | void HUDRadar::removeObject(RadarViewable* object) | 
|---|
|  | 113 | { | 
|---|
|  | 114 | // If object was added at all then remove it | 
|---|
|  | 115 | std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it; | 
|---|
|  | 116 | it = this->radarObjects_.find( object ); | 
|---|
|  | 117 | if( it != this->radarObjects_.end() ) | 
|---|
| [1609] | 118 | { | 
|---|
| [7163] | 119 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second); | 
|---|
|  | 120 | this->radarObjects_.erase(it); | 
|---|
| [1609] | 121 | } | 
|---|
| [7163] | 122 | } | 
|---|
| [2662] | 123 |  | 
|---|
| [7163] | 124 | void HUDRadar::objectChanged( RadarViewable* rv ) | 
|---|
|  | 125 | { | 
|---|
|  | 126 | if (rv == dynamic_cast<RadarViewable*>(this->owner_)) | 
|---|
|  | 127 | return; | 
|---|
|  | 128 | assert( this->radarObjects_.find(rv) != this->radarObjects_.end() ); | 
|---|
|  | 129 | Ogre::PanelOverlayElement* panel = this->radarObjects_[rv]; | 
|---|
|  | 130 | panel->setMaterialName(TextureGenerator::getMaterialName( | 
|---|
|  | 131 | shapeMaterials_[rv->getRadarObjectShape()], rv->getRadarObjectColour())); | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | void HUDRadar::gatherObjects() | 
|---|
|  | 135 | { | 
|---|
|  | 136 | const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects(); | 
|---|
|  | 137 | std::set<RadarViewable*>::const_iterator it; | 
|---|
|  | 138 | for( it=objectSet.begin(); it!=objectSet.end(); ++it ) | 
|---|
|  | 139 | this->addObject(*it); | 
|---|
| [8737] | 140 | this->radarTick(0); | 
|---|
| [7163] | 141 | } | 
|---|
|  | 142 |  | 
|---|
|  | 143 | void HUDRadar::radarTick(float dt) | 
|---|
|  | 144 | { | 
|---|
|  | 145 | // Make sure the owner of the radar was defined | 
|---|
|  | 146 | if( !this->owner_ ) | 
|---|
| [7880] | 147 | return; | 
|---|
| [7163] | 148 |  | 
|---|
|  | 149 | this->marker_->hide();      // in case that no object is in focus | 
|---|
|  | 150 | // get the focus object | 
|---|
|  | 151 | Radar* radar = this->getOwner()->getScene()->getRadar(); | 
|---|
|  | 152 | const RadarViewable* focusObject = radar->getFocus(); | 
|---|
|  | 153 |  | 
|---|
|  | 154 | // update the distances for all objects | 
|---|
|  | 155 | std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it; | 
|---|
|  | 156 | for( it = this->radarObjects_.begin(); it != this->radarObjects_.end(); ++it ) | 
|---|
| [1614] | 157 | { | 
|---|
| [7163] | 158 | // Make sure the object really is a WorldEntity | 
|---|
|  | 159 | const WorldEntity* wePointer = it->first->getWorldEntity(); | 
|---|
|  | 160 | if( !wePointer ) | 
|---|
|  | 161 | { | 
|---|
|  | 162 | CCOUT(0) << "Cannot display a non-WorldEntitiy on the radar" << std::endl; | 
|---|
|  | 163 | assert(0); | 
|---|
|  | 164 | } | 
|---|
|  | 165 | bool isFocus = (it->first == focusObject); | 
|---|
|  | 166 | // set size to fit distance... | 
|---|
|  | 167 | float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length(); | 
|---|
|  | 168 | // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) | 
|---|
| [8738] | 169 | float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * it->first->getRadarObjectScale(); | 
|---|
| [7163] | 170 | it->second->setDimensions(size, size); | 
|---|
| [2662] | 171 |  | 
|---|
| [7163] | 172 | // calc position on radar... | 
|---|
|  | 173 | Vector2 coord = get2DViewcoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition()); | 
|---|
| [7184] | 174 | coord *= math::pi / 3.5f; // small adjustment to make it fit the texture | 
|---|
| [7163] | 175 | it->second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f); | 
|---|
|  | 176 | it->second->show(); | 
|---|
| [1613] | 177 |  | 
|---|
| [7163] | 178 | // if this object is in focus, then set the focus marker | 
|---|
|  | 179 | if (isFocus) | 
|---|
|  | 180 | { | 
|---|
|  | 181 | this->marker_->setDimensions(size * 1.5f, size * 1.5f); | 
|---|
|  | 182 | this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f); | 
|---|
|  | 183 | this->marker_->show(); | 
|---|
|  | 184 | } | 
|---|
| [1613] | 185 | } | 
|---|
| [1604] | 186 | } | 
|---|
|  | 187 |  | 
|---|
| [2662] | 188 | void HUDRadar::changedOwner() | 
|---|
|  | 189 | { | 
|---|
| [7880] | 190 | SUPER(HUDRadar, changedOwner); | 
|---|
| [2662] | 191 |  | 
|---|
| [7880] | 192 | this->owner_ = orxonox_cast<ControllableEntity*>(this->getOwner()); | 
|---|
|  | 193 | assert(this->radarObjects_.size() == 0); | 
|---|
|  | 194 | this->gatherObjects(); | 
|---|
|  | 195 | } | 
|---|
| [1283] | 196 | } | 
|---|