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