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