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