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