| [1505] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
| [1454] | 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 |  * | 
|---|
| [1454] | 22 |  *   Author: | 
|---|
 | 23 |  *      Felix Schulthess | 
|---|
 | 24 |  *   Co-authors: | 
|---|
| [1590] | 25 |  *      Reto Grieder | 
|---|
| [1454] | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
| [1393] | 28 |  | 
|---|
| [1410] | 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| [1601] | 30 | #include "HUDNavigation.h" | 
|---|
| [1410] | 31 |  | 
|---|
| [1393] | 32 | #include <OgreOverlayManager.h> | 
|---|
| [1614] | 33 | #include <OgreTextAreaOverlayElement.h> | 
|---|
 | 34 | #include <OgrePanelOverlayElement.h> | 
|---|
| [1410] | 35 |  | 
|---|
| [1614] | 36 | #include "util/Math.h" | 
|---|
| [1615] | 37 | #include "util/String.h" | 
|---|
| [1616] | 38 | #include "util/Convert.h" | 
|---|
| [1604] | 39 | #include "core/ConsoleCommand.h" | 
|---|
| [1616] | 40 | #include "core/CoreIncludes.h" | 
|---|
 | 41 | #include "core/XMLPort.h" | 
|---|
| [1819] | 42 | #include "objects/Radar.h" | 
|---|
| [1393] | 43 |  | 
|---|
 | 44 | namespace orxonox | 
|---|
 | 45 | { | 
|---|
| [1601] | 46 |     CreateFactory(HUDNavigation); | 
|---|
| [1590] | 47 |  | 
|---|
| [2087] | 48 |     HUDNavigation::HUDNavigation(BaseObject* creator) | 
|---|
 | 49 |         : OrxonoxOverlay(creator) | 
|---|
| [1580] | 50 |     { | 
|---|
| [1601] | 51 |         RegisterObject(HUDNavigation); | 
|---|
| [2087] | 52 |  | 
|---|
 | 53 |         // create nav text | 
|---|
 | 54 |         navText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
 | 55 |             .createOverlayElement("TextArea", "HUDNavigation_navText_" + getUniqueNumberString())); | 
|---|
 | 56 |  | 
|---|
 | 57 |         // create nav marker | 
|---|
 | 58 |         navMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
 | 59 |             .createOverlayElement("Panel", "HUDNavigation_navMarker_" + getUniqueNumberString())); | 
|---|
 | 60 |         navMarker_->setMaterialName("Orxonox/NavArrows"); | 
|---|
 | 61 |  | 
|---|
 | 62 |         // create aim marker | 
|---|
 | 63 |         aimMarker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
 | 64 |             .createOverlayElement("Panel", "HUDNavigation_aimMarker_" + getUniqueNumberString())); | 
|---|
 | 65 |         aimMarker_->setMaterialName("Orxonox/NavCrosshair"); | 
|---|
 | 66 |         this->wasOutOfView_ = true; // Ensure the material is changed right the first time.. | 
|---|
 | 67 |  | 
|---|
 | 68 |         setFont("Monofur"); | 
|---|
 | 69 |         setTextSize(0.05f); | 
|---|
 | 70 |         setNavMarkerSize(0.05f); | 
|---|
 | 71 |         setAimMarkerSize(0.04f); | 
|---|
 | 72 |  | 
|---|
 | 73 |         background_->addChild(navMarker_); | 
|---|
 | 74 |         background_->addChild(aimMarker_); | 
|---|
 | 75 |         background_->addChild(navText_); | 
|---|
 | 76 |  | 
|---|
 | 77 |         // hide at first | 
|---|
 | 78 |         this->setVisible(false); | 
|---|
| [1393] | 79 |     } | 
|---|
 | 80 |  | 
|---|
| [1601] | 81 |     HUDNavigation::~HUDNavigation() | 
|---|
| [1564] | 82 |     { | 
|---|
| [2087] | 83 |         if (this->isInitialized()) | 
|---|
 | 84 |         { | 
|---|
| [1615] | 85 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_); | 
|---|
 | 86 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->navText_); | 
|---|
 | 87 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_); | 
|---|
| [2087] | 88 |         } | 
|---|
| [1564] | 89 |     } | 
|---|
 | 90 |  | 
|---|
| [1601] | 91 |     void HUDNavigation::XMLPort(Element& xmlElement, XMLPort::Mode mode) | 
|---|
| [1580] | 92 |     { | 
|---|
| [1747] | 93 |         SUPER(HUDNavigation, XMLPort, xmlElement, mode); | 
|---|
| [1394] | 94 |  | 
|---|
| [2087] | 95 |         XMLPortParam(HUDNavigation, "font",     setFont,     getFont,     xmlElement, mode); | 
|---|
 | 96 |         XMLPortParam(HUDNavigation, "textSize", setTextSize, getTextSize, xmlElement, mode); | 
|---|
 | 97 |         XMLPortParam(HUDNavigation, "navMarkerSize", setNavMarkerSize, getNavMarkerSize, xmlElement, mode); | 
|---|
 | 98 |         XMLPortParam(HUDNavigation, "aimMarkerSize", setAimMarkerSize, getAimMarkerSize, xmlElement, mode); | 
|---|
| [1410] | 99 |     } | 
|---|
| [1393] | 100 |  | 
|---|
| [1601] | 101 |     void HUDNavigation::setFont(const std::string& font) | 
|---|
| [1590] | 102 |     { | 
|---|
 | 103 |         if (this->navText_ && font != "") | 
|---|
 | 104 |             this->navText_->setFontName(font); | 
|---|
 | 105 |     } | 
|---|
 | 106 |  | 
|---|
| [1615] | 107 |     const std::string& HUDNavigation::getFont() const | 
|---|
| [1590] | 108 |     { | 
|---|
 | 109 |         if (this->navText_) | 
|---|
 | 110 |             return this->navText_->getFontName(); | 
|---|
 | 111 |         else | 
|---|
| [2087] | 112 |             return BLANKSTRING; | 
|---|
| [1590] | 113 |     } | 
|---|
 | 114 |  | 
|---|
| [1601] | 115 |     void HUDNavigation::setTextSize(float size) | 
|---|
| [1590] | 116 |     { | 
|---|
 | 117 |         if (this->navText_ && size >= 0.0f) | 
|---|
 | 118 |             this->navText_->setCharHeight(size); | 
|---|
 | 119 |     } | 
|---|
 | 120 |  | 
|---|
| [1601] | 121 |     float HUDNavigation::getTextSize() const | 
|---|
| [1590] | 122 |     { | 
|---|
 | 123 |         if (this->navText_) | 
|---|
 | 124 |             return this->navText_->getCharHeight(); | 
|---|
 | 125 |         else | 
|---|
 | 126 |             return 0.0f; | 
|---|
 | 127 |     } | 
|---|
 | 128 |  | 
|---|
| [1601] | 129 |     void HUDNavigation::tick(float dt) | 
|---|
| [1590] | 130 |     { | 
|---|
| [1613] | 131 |         if (!Radar::getInstance().getFocus()) | 
|---|
 | 132 |         { | 
|---|
 | 133 |             this->overlay_->hide(); | 
|---|
| [1564] | 134 |             return; | 
|---|
| [1613] | 135 |         } | 
|---|
 | 136 |         else | 
|---|
 | 137 |         { | 
|---|
 | 138 |             this->overlay_->show(); | 
|---|
 | 139 |         } | 
|---|
| [1400] | 140 |  | 
|---|
| [1399] | 141 |         // set text | 
|---|
| [1613] | 142 |         int dist = (int) getDist2Focus(); | 
|---|
| [1590] | 143 |         navText_->setCaption(convertToString(dist)); | 
|---|
 | 144 |         float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3; | 
|---|
| [1399] | 145 |  | 
|---|
| [2087] | 146 | /* | 
|---|
| [1564] | 147 |         Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_; | 
|---|
| [1590] | 148 |         Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix(); | 
|---|
| [2087] | 149 | */ | 
|---|
| [1399] | 150 |         // transform to screen coordinates | 
|---|
| [2087] | 151 |         Vector3 pos = /*transformationMatrix * */Radar::getInstance().getFocus()->getWorldPosition(); | 
|---|
| [1564] | 152 |  | 
|---|
| [1590] | 153 |         bool outOfView; | 
|---|
 | 154 |         if (pos.z > 1.0) | 
|---|
 | 155 |         { | 
|---|
 | 156 |             // z > 1.0 means that the object is behind the camera | 
|---|
| [1580] | 157 |             outOfView = true; | 
|---|
| [1590] | 158 |             // we have to switch all coordinates (if you don't know why, | 
|---|
 | 159 |             // try linear algebra lectures, because I can't explain..) | 
|---|
 | 160 |             pos.x = -pos.x; | 
|---|
 | 161 |             pos.y = -pos.y; | 
|---|
 | 162 |         } | 
|---|
 | 163 |         else | 
|---|
 | 164 |             outOfView = pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0; | 
|---|
| [1399] | 165 |  | 
|---|
| [1580] | 166 |         if (outOfView) | 
|---|
 | 167 |         { | 
|---|
| [1411] | 168 |             // object is not in view | 
|---|
| [1566] | 169 |             aimMarker_->hide(); | 
|---|
| [1411] | 170 |  | 
|---|
| [1590] | 171 |             if (!wasOutOfView_) | 
|---|
| [1580] | 172 |             { | 
|---|
| [1590] | 173 |                 navMarker_->setMaterialName("Orxonox/NavArrows"); | 
|---|
 | 174 |                 wasOutOfView_ = true; | 
|---|
| [1411] | 175 |             } | 
|---|
| [1590] | 176 |  | 
|---|
 | 177 |             if (pos.x < pos.y) | 
|---|
| [1580] | 178 |             { | 
|---|
| [1590] | 179 |                 if (pos.y > -pos.x) | 
|---|
| [1580] | 180 |                 { | 
|---|
| [1590] | 181 |                     // up | 
|---|
 | 182 |                     float position = pos.x / pos.y + 1.0; | 
|---|
 | 183 |                     navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 0.0); | 
|---|
| [1399] | 184 |                     navMarker_->setUV(0.5, 0.0, 1.0, 0.5); | 
|---|
| [1590] | 185 |                     navText_->setLeft((position - textLength) * 0.5); | 
|---|
| [1399] | 186 |                     navText_->setTop(navMarker_->getHeight()); | 
|---|
 | 187 |                 } | 
|---|
| [1580] | 188 |                 else | 
|---|
 | 189 |                 { | 
|---|
| [1590] | 190 |                     // left | 
|---|
 | 191 |                     float position = pos.y / pos.x + 1.0; | 
|---|
 | 192 |                     navMarker_->setPosition(0.0, (position - navMarker_->getWidth()) * 0.5); | 
|---|
| [1411] | 193 |                     navMarker_->setUV(0.0, 0.0, 0.5, 0.5); | 
|---|
| [1590] | 194 |                     navText_->setLeft(navMarker_->getWidth() + 0.01); | 
|---|
 | 195 |                     navText_->setTop((position - navText_->getCharHeight()) * 0.5); | 
|---|
| [1411] | 196 |                 } | 
|---|
 | 197 |             } | 
|---|
| [1590] | 198 |             else | 
|---|
| [1580] | 199 |             { | 
|---|
| [1590] | 200 |                 if (pos.y < -pos.x) | 
|---|
| [1580] | 201 |                 { | 
|---|
| [1590] | 202 |                     // down | 
|---|
 | 203 |                     float position = -pos.x / pos.y + 1.0; | 
|---|
 | 204 |                     navMarker_->setPosition((position - navMarker_->getWidth()) * 0.5, 1.0 - navMarker_->getHeight()); | 
|---|
| [1399] | 205 |                     navMarker_->setUV(0.0, 0.5, 0.5, 1.0); | 
|---|
| [1590] | 206 |                     navText_->setLeft((position - textLength) * 0.5); | 
|---|
 | 207 |                     navText_->setTop(1.0 - navMarker_->getHeight() - navText_->getCharHeight()); | 
|---|
| [1399] | 208 |                 } | 
|---|
| [1580] | 209 |                 else | 
|---|
 | 210 |                 { | 
|---|
| [1590] | 211 |                     // right | 
|---|
 | 212 |                     float position = -pos.y / pos.x + 1.0; | 
|---|
 | 213 |                     navMarker_->setPosition(1.0 - navMarker_->getWidth(), (position - navMarker_->getHeight()) * 0.5); | 
|---|
 | 214 |                     navMarker_->setUV(0.5, 0.5, 1.0, 1.0); | 
|---|
 | 215 |                     navText_->setLeft(1.0 - navMarker_->getWidth() - textLength - 0.01); | 
|---|
 | 216 |                     navText_->setTop((position - navText_->getCharHeight()) * 0.5); | 
|---|
| [1399] | 217 |                 } | 
|---|
| [1393] | 218 |             } | 
|---|
 | 219 |         } | 
|---|
| [1580] | 220 |         else | 
|---|
 | 221 |         { | 
|---|
| [1411] | 222 |             // object is in view | 
|---|
| [2087] | 223 | /* | 
|---|
| [1590] | 224 |             Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(), | 
|---|
| [1613] | 225 |                     Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity()); | 
|---|
| [2087] | 226 | */ | 
|---|
| [1590] | 227 |             if (wasOutOfView_) | 
|---|
 | 228 |             { | 
|---|
 | 229 |                 navMarker_->setMaterialName("Orxonox/NavTDC"); | 
|---|
 | 230 |                 wasOutOfView_ = false; | 
|---|
 | 231 |             } | 
|---|
 | 232 |  | 
|---|
 | 233 |             // object is in view | 
|---|
| [1459] | 234 |             navMarker_->setUV(0.0, 0.0, 1.0, 1.0); | 
|---|
| [1590] | 235 |             navMarker_->setLeft((pos.x + 1.0 - navMarker_->getWidth()) * 0.5); | 
|---|
 | 236 |             navMarker_->setTop((-pos.y + 1.0 - navMarker_->getHeight()) * 0.5); | 
|---|
| [1566] | 237 |  | 
|---|
 | 238 |             aimMarker_->show(); | 
|---|
| [2087] | 239 | /* | 
|---|
| [1590] | 240 |             aimMarker_->setLeft((aimpos.x + 1.0 - aimMarker_->getWidth()) * 0.5); | 
|---|
 | 241 |             aimMarker_->setTop((-aimpos.y + 1.0 - aimMarker_->getHeight()) * 0.5); | 
|---|
| [2087] | 242 | */ | 
|---|
| [1590] | 243 |             navText_->setLeft((pos.x + 1.0 + navMarker_->getWidth()) * 0.5); | 
|---|
 | 244 |             navText_->setTop((-pos.y + 1.0 + navMarker_->getHeight()) * 0.5); | 
|---|
| [1393] | 245 |         } | 
|---|
| [1410] | 246 |     } | 
|---|
| [1394] | 247 |  | 
|---|
| [1601] | 248 |     float HUDNavigation::getDist2Focus() const | 
|---|
| [1580] | 249 |     { | 
|---|
| [2087] | 250 | /* | 
|---|
| [1613] | 251 |         if (Radar::getInstance().getFocus()) | 
|---|
 | 252 |             return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length(); | 
|---|
| [1580] | 253 |         else | 
|---|
| [2087] | 254 | */ | 
|---|
| [1580] | 255 |             return 0; | 
|---|
| [1410] | 256 |     } | 
|---|
| [1590] | 257 |  | 
|---|
| [1604] | 258 |     /** | 
|---|
 | 259 |     @brief Overridden method of OrxonoxOverlay. Usually the entire overlay | 
|---|
 | 260 |            scales with scale(). Here we obviously have to adjust this. | 
|---|
 | 261 |     */ | 
|---|
| [1601] | 262 |     void HUDNavigation::sizeChanged() | 
|---|
| [1590] | 263 |     { | 
|---|
| [1604] | 264 |         // use size to compensate for apspect ratio if enabled. | 
|---|
| [1622] | 265 |         float xScale = this->getActualSize().x; | 
|---|
 | 266 |         float yScale = this->getActualSize().y; | 
|---|
| [1595] | 267 |         if (this->navMarker_) | 
|---|
 | 268 |             navMarker_->setDimensions(navMarkerSize_ * xScale, navMarkerSize_ * yScale); | 
|---|
 | 269 |         if (this->aimMarker_) | 
|---|
 | 270 |             aimMarker_->setDimensions(aimMarkerSize_ * xScale, aimMarkerSize_ * yScale); | 
|---|
 | 271 |         if (this->navText_) | 
|---|
 | 272 |             navText_->setCharHeight(navText_->getCharHeight() * yScale); | 
|---|
| [1590] | 273 |     } | 
|---|
| [1393] | 274 | } | 
|---|