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