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