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