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