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