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