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