[1588] | 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 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1622] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Definition of the OrxonoxOverlay class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1588] | 34 | #include "OrxonoxStableHeaders.h" |
---|
[1601] | 35 | #include "OrxonoxOverlay.h" |
---|
[1588] | 36 | |
---|
[1615] | 37 | #include <cmath> |
---|
[1622] | 38 | #include <OgreOverlay.h> |
---|
[1588] | 39 | #include <OgreOverlayManager.h> |
---|
[1614] | 40 | #include <OgrePanelOverlayElement.h> |
---|
[1588] | 41 | #include "util/Convert.h" |
---|
[1615] | 42 | #include "util/String.h" |
---|
[1588] | 43 | #include "core/CoreIncludes.h" |
---|
[1616] | 44 | #include "core/XMLPort.h" |
---|
| 45 | #include "core/ConsoleCommand.h" |
---|
[1590] | 46 | #include "GraphicsEngine.h" |
---|
[1588] | 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
[1615] | 50 | unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0; |
---|
| 51 | std::map<std::string, OrxonoxOverlay*> OrxonoxOverlay::overlays_s; |
---|
[1588] | 52 | |
---|
[1747] | 53 | SetConsoleCommand(OrxonoxOverlay, scaleOverlay, false).accessLevel(AccessLevel::User); |
---|
| 54 | SetConsoleCommand(OrxonoxOverlay, scrollOverlay, false).accessLevel(AccessLevel::User); |
---|
| 55 | SetConsoleCommand(OrxonoxOverlay, rotateOverlay, false).accessLevel(AccessLevel::User); |
---|
[1588] | 56 | |
---|
[1615] | 57 | OrxonoxOverlay::OrxonoxOverlay() |
---|
| 58 | : overlay_(0) |
---|
| 59 | , background_(0) |
---|
| 60 | { |
---|
| 61 | RegisterObject(OrxonoxOverlay); |
---|
[2036] | 62 | |
---|
| 63 | // add this overlay to the static map of OrxonoxOverlays |
---|
| 64 | if (overlays_s.find(this->getName()) != overlays_s.end()) |
---|
| 65 | { |
---|
| 66 | COUT(1) << "Overlay names should be unique or you cannnot access them via console." << std::endl; |
---|
| 67 | } |
---|
| 68 | overlays_s[this->getName()] = this; |
---|
| 69 | |
---|
| 70 | // create the Ogre::Overlay |
---|
| 71 | overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_" |
---|
| 72 | + convertToString(hudOverlayCounter_s++)); |
---|
| 73 | |
---|
| 74 | // create background panel (can be used to show any picture) |
---|
| 75 | this->background_ = static_cast<Ogre::PanelOverlayElement*>( |
---|
| 76 | Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", |
---|
| 77 | "OrxonoxOverlay_background_" + convertToString(hudOverlayCounter_s++))); |
---|
| 78 | this->overlay_->add2D(this->background_); |
---|
| 79 | |
---|
| 80 | // We'll have to get the aspect ratio manually for the first time. Afterwards windowResized() gets |
---|
| 81 | // called automatically by the GraphicsEngine. |
---|
| 82 | this->windowResized(GraphicsEngine::getInstance().getWindowWidth(), |
---|
| 83 | GraphicsEngine::getInstance().getWindowHeight()); |
---|
| 84 | |
---|
| 85 | this->changedVisibility(); |
---|
[1615] | 86 | } |
---|
[1614] | 87 | |
---|
[1622] | 88 | /** |
---|
| 89 | @brief |
---|
| 90 | Make sure everything gets removed/destroyed. |
---|
| 91 | @remark |
---|
| 92 | We assume that the Ogre::OverlayManager exists (there is an assert in Ogre for that!) |
---|
| 93 | */ |
---|
[1615] | 94 | OrxonoxOverlay::~OrxonoxOverlay() |
---|
| 95 | { |
---|
[1622] | 96 | // erase ourself from the map with all overlays |
---|
[1615] | 97 | std::map<std::string, OrxonoxOverlay*>::iterator it = overlays_s.find(this->getName()); |
---|
| 98 | if (it != overlays_s.end()) |
---|
| 99 | overlays_s.erase(it); |
---|
[1622] | 100 | |
---|
| 101 | if (this->background_) |
---|
| 102 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_); |
---|
| 103 | if (this->overlay_) |
---|
| 104 | Ogre::OverlayManager::getSingleton().destroy(this->overlay_); |
---|
[1615] | 105 | } |
---|
| 106 | |
---|
[1622] | 107 | /** |
---|
| 108 | @brief |
---|
| 109 | Loads the OrxonoxOverlay. |
---|
[1747] | 110 | |
---|
[1622] | 111 | This has to be called before usage, otherwise strange behaviour is |
---|
| 112 | guaranteed! (there should be no segfaults however). |
---|
| 113 | @copydoc |
---|
| 114 | BaseObject::XMLPort() |
---|
| 115 | */ |
---|
[1615] | 116 | void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
[1590] | 117 | { |
---|
[1747] | 118 | SUPER(OrxonoxOverlay, XMLPort, xmlElement, mode); |
---|
[1588] | 119 | |
---|
[1627] | 120 | XMLPortParam(OrxonoxOverlay, "size", setSize, getSize, xmlElement, mode) |
---|
[1633] | 121 | .defaultValues(Vector2(1.0f, 1.0f)); |
---|
[1627] | 122 | XMLPortParam(OrxonoxOverlay, "pickPoint", setPickPoint, getPickPoint, xmlElement, mode) |
---|
| 123 | .defaultValues(Vector2(0.0f, 0.0f)); |
---|
| 124 | XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode) |
---|
| 125 | .defaultValues(Vector2(0.0f, 0.0f)); |
---|
| 126 | XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode) |
---|
| 127 | .defaultValues(0.0f); |
---|
| 128 | XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode) |
---|
| 129 | .defaultValues(true); |
---|
| 130 | XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode) |
---|
| 131 | .defaultValues(""); |
---|
[1590] | 132 | } |
---|
[1588] | 133 | |
---|
[1622] | 134 | //! Only sets the background material name if not "" |
---|
[1615] | 135 | void OrxonoxOverlay::setBackgroundMaterial(const std::string& material) |
---|
| 136 | { |
---|
| 137 | if (this->background_ && material != "") |
---|
| 138 | this->background_->setMaterialName(material); |
---|
| 139 | } |
---|
[1588] | 140 | |
---|
[1622] | 141 | //! Returns the the material name of the background |
---|
[1615] | 142 | const std::string& OrxonoxOverlay::getBackgroundMaterial() const |
---|
| 143 | { |
---|
| 144 | if (this->background_) |
---|
| 145 | return this->background_->getMaterialName(); |
---|
| 146 | else |
---|
| 147 | return blankString; |
---|
| 148 | } |
---|
[1614] | 149 | |
---|
[1622] | 150 | //! Called by BaseObject when visibility has changed. |
---|
[1615] | 151 | void OrxonoxOverlay::changedVisibility() |
---|
[1595] | 152 | { |
---|
[1622] | 153 | if (!this->overlay_) |
---|
| 154 | return; |
---|
| 155 | |
---|
| 156 | if (this->isVisible()) |
---|
| 157 | this->overlay_->show(); |
---|
| 158 | else |
---|
| 159 | this->overlay_->hide(); |
---|
[1595] | 160 | } |
---|
[1588] | 161 | |
---|
[1622] | 162 | /** |
---|
| 163 | @brief |
---|
| 164 | Called by the GraphicsEngine whenever the window size changes. |
---|
| 165 | Calculates the aspect ratio only. |
---|
| 166 | */ |
---|
[1615] | 167 | void OrxonoxOverlay::windowResized(int newWidth, int newHeight) |
---|
| 168 | { |
---|
| 169 | this->windowAspectRatio_ = newWidth/(float)newHeight; |
---|
[1622] | 170 | this->sizeCorrectionChanged(); |
---|
[1615] | 171 | } |
---|
[1590] | 172 | |
---|
[1622] | 173 | /** |
---|
| 174 | @brief |
---|
| 175 | Called whenever the rotation angle has changed. |
---|
| 176 | */ |
---|
| 177 | void OrxonoxOverlay::angleChanged() |
---|
[1595] | 178 | { |
---|
[1622] | 179 | if (!this->overlay_) |
---|
| 180 | return; |
---|
| 181 | |
---|
| 182 | this->overlay_->setRotate(this->angle_); |
---|
[1615] | 183 | this->sizeCorrectionChanged(); |
---|
[1595] | 184 | } |
---|
[1615] | 185 | |
---|
[1622] | 186 | /** |
---|
| 187 | @brief |
---|
| 188 | Called whenever the aspect ratio or the angle has changed. |
---|
| 189 | The method calculates a correction factor for the size to compensate |
---|
| 190 | for aspect distortion if desired. |
---|
| 191 | @remarks |
---|
| 192 | This only works when the angle is about 0 or 90 degrees. |
---|
| 193 | */ |
---|
[1615] | 194 | void OrxonoxOverlay::sizeCorrectionChanged() |
---|
[1595] | 195 | { |
---|
[1615] | 196 | if (this->bCorrectAspect_) |
---|
| 197 | { |
---|
| 198 | float angle = this->angle_.valueDegrees(); |
---|
[1618] | 199 | if (angle < 0.0) |
---|
| 200 | angle = -angle; |
---|
| 201 | angle -= 180.0 * (int)(angle / 180.0); |
---|
| 202 | |
---|
[1622] | 203 | // take the reverse if angle is about 90 degrees |
---|
[1615] | 204 | float tempAspect; |
---|
[1618] | 205 | if (angle > 89.0 && angle < 91.0) |
---|
[1632] | 206 | { |
---|
[1615] | 207 | tempAspect = 1.0 / this->windowAspectRatio_; |
---|
[1632] | 208 | rotState_ = Vertical; |
---|
| 209 | } |
---|
[1618] | 210 | else if (angle > 179 || angle < 1) |
---|
[1632] | 211 | { |
---|
[1615] | 212 | tempAspect = this->windowAspectRatio_; |
---|
[1632] | 213 | rotState_ = Horizontal; |
---|
| 214 | } |
---|
[1615] | 215 | else |
---|
[1632] | 216 | { |
---|
[1615] | 217 | tempAspect = 1.0; |
---|
[1632] | 218 | rotState_ = Inbetween; |
---|
| 219 | } |
---|
[1615] | 220 | |
---|
| 221 | // note: this is only an approximation that is mostly valid when the |
---|
| 222 | // magnitude of the width is about the magnitude of the height. |
---|
| 223 | // Correctly we would have to take the square root of width*height |
---|
| 224 | this->sizeCorrection_.x = 2.0 / (tempAspect + 1.0); |
---|
| 225 | this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x; |
---|
| 226 | } |
---|
| 227 | else |
---|
| 228 | { |
---|
| 229 | this->sizeCorrection_ = Vector2::UNIT_SCALE; |
---|
| 230 | } |
---|
[1622] | 231 | |
---|
[1615] | 232 | this->sizeChanged(); |
---|
[1595] | 233 | } |
---|
| 234 | |
---|
[1615] | 235 | /** |
---|
[1622] | 236 | @brief |
---|
| 237 | Sets the overlay size using the actual corrected size. |
---|
[1615] | 238 | */ |
---|
| 239 | void OrxonoxOverlay::sizeChanged() |
---|
| 240 | { |
---|
[1622] | 241 | if (!this->overlay_) |
---|
| 242 | return; |
---|
| 243 | |
---|
[1615] | 244 | this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y); |
---|
| 245 | positionChanged(); |
---|
| 246 | } |
---|
[1595] | 247 | |
---|
[1615] | 248 | /** |
---|
[1622] | 249 | @brief |
---|
| 250 | Determines the position of the overlay. |
---|
| 251 | This works also well when a rotation angle is applied. The overlay always |
---|
| 252 | gets aligned correctly as well as possible. |
---|
[1615] | 253 | */ |
---|
[1622] | 254 | void OrxonoxOverlay::positionChanged() |
---|
[1615] | 255 | { |
---|
[1622] | 256 | if (!this->overlay_) |
---|
| 257 | return; |
---|
[1595] | 258 | |
---|
[1622] | 259 | // transform the angle to a range of 0 - pi/2 first. |
---|
[1617] | 260 | float angle = this->angle_.valueRadians(); |
---|
| 261 | if (angle < 0.0) |
---|
| 262 | angle = -angle; |
---|
[1615] | 263 | angle -= Ogre::Math::PI * (int)(angle / (Ogre::Math::PI)); |
---|
| 264 | if (angle > Ogre::Math::PI * 0.5) |
---|
| 265 | angle = Ogre::Math::PI - angle; |
---|
[1747] | 266 | |
---|
[1622] | 267 | // do some mathematical fiddling for a bounding box |
---|
[1615] | 268 | Vector2 actualSize = size_ * sizeCorrection_; |
---|
| 269 | float radius = actualSize.length(); |
---|
| 270 | float phi = atan(actualSize.y / actualSize.x); |
---|
| 271 | Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi)); |
---|
[1622] | 272 | |
---|
| 273 | // calculate the scrolling offset |
---|
| 274 | Vector2 scroll = (position_ - 0.5 - boundingBox * (pickPoint_ - 0.5)) * 2.0; |
---|
[1615] | 275 | this->overlay_->setScroll(scroll.x, -scroll.y); |
---|
| 276 | } |
---|
[1598] | 277 | |
---|
[1615] | 278 | |
---|
[1622] | 279 | //########### Console commands ############ |
---|
| 280 | |
---|
| 281 | /** |
---|
| 282 | @brief |
---|
| 283 | Scales an overlay by its name. |
---|
| 284 | @param name |
---|
| 285 | The name of the overlay defined BaseObject::setName() (usually done with the "name" |
---|
[1623] | 286 | attribute in the xml file). |
---|
[1622] | 287 | */ |
---|
[1615] | 288 | /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale) |
---|
| 289 | { |
---|
| 290 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name); |
---|
| 291 | if (it != overlays_s.end()) |
---|
| 292 | (*it).second->scale(Vector2(scale, scale)); |
---|
| 293 | } |
---|
| 294 | |
---|
[1622] | 295 | /** |
---|
| 296 | @brief |
---|
| 297 | Scrolls an overlay by its name. |
---|
| 298 | @param name |
---|
| 299 | The name of the overlay defined BaseObject::setName() (usually done with the "name" |
---|
[1623] | 300 | attribute in the xml file). |
---|
[1622] | 301 | */ |
---|
[1615] | 302 | /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll) |
---|
| 303 | { |
---|
| 304 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name); |
---|
| 305 | if (it != overlays_s.end()) |
---|
| 306 | (*it).second->scroll(scroll); |
---|
| 307 | } |
---|
| 308 | |
---|
[1622] | 309 | /** |
---|
| 310 | @brief |
---|
| 311 | Rotates an overlay by its name. |
---|
| 312 | @param name |
---|
| 313 | The name of the overlay defined BaseObject::setName() (usually done with the "name" |
---|
[1623] | 314 | attribute in the xml file). |
---|
[1622] | 315 | */ |
---|
[1615] | 316 | /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle) |
---|
| 317 | { |
---|
| 318 | std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name); |
---|
| 319 | if (it != overlays_s.end()) |
---|
| 320 | (*it).second->rotate(angle); |
---|
| 321 | } |
---|
[1588] | 322 | } |
---|