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