[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 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
[1601] | 30 | #include "OrxonoxOverlay.h" |
---|
[1588] | 31 | |
---|
| 32 | #include <OgreOverlayManager.h> |
---|
| 33 | #include "util/Convert.h" |
---|
| 34 | #include "core/CoreIncludes.h" |
---|
[1590] | 35 | #include "GraphicsEngine.h" |
---|
[1588] | 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[1601] | 39 | unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0; |
---|
[1588] | 40 | |
---|
[1601] | 41 | OrxonoxOverlay::OrxonoxOverlay() |
---|
[1588] | 42 | : overlay_(0) |
---|
[1595] | 43 | , windowAspectRatio_(1.0f) |
---|
| 44 | , bCorrectAspect_(false) |
---|
[1598] | 45 | , size_(1.0f, 1.0f) |
---|
| 46 | , sizeCorrection_(1.0f, 1.0f) |
---|
| 47 | , angle_(0.0f) |
---|
| 48 | , position_(0.0f, 0.0f) |
---|
[1601] | 49 | , origin_(0.0f, 0.0f) |
---|
[1588] | 50 | { |
---|
[1601] | 51 | RegisterObject(OrxonoxOverlay); |
---|
[1588] | 52 | } |
---|
| 53 | |
---|
[1601] | 54 | void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
[1588] | 55 | { |
---|
| 56 | BaseObject::XMLPort(xmlElement, mode); |
---|
| 57 | |
---|
[1590] | 58 | if (mode == XMLPort::LoadObject) |
---|
| 59 | { |
---|
[1601] | 60 | overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay" |
---|
[1590] | 61 | + convertToString(hudOverlayCounter_s++) + "_" + this->getName()); |
---|
[1588] | 62 | |
---|
[1595] | 63 | this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(), |
---|
| 64 | GraphicsEngine::getSingleton().getWindowHeight()); |
---|
[1590] | 65 | } |
---|
| 66 | |
---|
[1601] | 67 | XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode); |
---|
[1604] | 68 | XMLPortParam(OrxonoxOverlay, "size", setSize, getUncorrectedSize, xmlElement, mode); |
---|
[1601] | 69 | XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode); |
---|
| 70 | XMLPortParam(OrxonoxOverlay, "origin", setOrigin, getOrigin, xmlElement, mode); |
---|
| 71 | XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode); |
---|
[1588] | 72 | |
---|
[1590] | 73 | if (mode == XMLPort::LoadObject) |
---|
| 74 | { |
---|
| 75 | this->overlay_->show(); |
---|
| 76 | if (!this->isVisible()) |
---|
| 77 | this->overlay_->hide(); |
---|
[1595] | 78 | |
---|
| 79 | this->sizeChanged(); |
---|
[1604] | 80 | this->positionChanged(); |
---|
| 81 | this->angleChanged(); |
---|
[1590] | 82 | } |
---|
[1588] | 83 | } |
---|
| 84 | |
---|
[1601] | 85 | OrxonoxOverlay::~OrxonoxOverlay() |
---|
[1588] | 86 | { |
---|
| 87 | } |
---|
| 88 | |
---|
[1601] | 89 | void OrxonoxOverlay::changedVisibility() |
---|
[1588] | 90 | { |
---|
[1595] | 91 | if (this->overlay_) |
---|
| 92 | { |
---|
| 93 | if (this->isVisible()) |
---|
| 94 | this->overlay_->show(); |
---|
| 95 | else |
---|
| 96 | this->overlay_->hide(); |
---|
| 97 | } |
---|
[1588] | 98 | } |
---|
| 99 | |
---|
[1601] | 100 | void OrxonoxOverlay::windowResized(int newWidth, int newHeight) |
---|
[1590] | 101 | { |
---|
[1595] | 102 | this->windowAspectRatio_ = newWidth/(float)newHeight; |
---|
| 103 | |
---|
| 104 | this->setAspectCorrection(this->bCorrectAspect_); |
---|
[1590] | 105 | } |
---|
| 106 | |
---|
[1601] | 107 | void OrxonoxOverlay::setAspectCorrection(bool val) |
---|
[1595] | 108 | { |
---|
| 109 | if (val) |
---|
| 110 | { |
---|
| 111 | // note: this is only an approximation that is mostly valid when the |
---|
| 112 | // magnitude of the width is about the magnitude of the height. |
---|
| 113 | // Correctly we would have to take the square root of width*height |
---|
| 114 | this->sizeCorrection_.x = 2.0 / (this->windowAspectRatio_ + 1.0); |
---|
| 115 | this->sizeCorrection_.y = this->windowAspectRatio_ * this->sizeCorrection_.x; |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | this->sizeCorrection_ = Vector2::UNIT_SCALE; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | this->bCorrectAspect_ = val; |
---|
| 123 | this->sizeChanged(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | @remarks |
---|
| 128 | This function can be overriden by any derivative. |
---|
| 129 | */ |
---|
[1601] | 130 | void OrxonoxOverlay::sizeChanged() |
---|
[1595] | 131 | { |
---|
[1598] | 132 | this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y); |
---|
[1604] | 133 | positionChanged(); |
---|
[1595] | 134 | } |
---|
| 135 | |
---|
[1598] | 136 | /** |
---|
| 137 | @remarks |
---|
| 138 | This function can be overriden by any derivative. |
---|
| 139 | */ |
---|
[1601] | 140 | void OrxonoxOverlay::angleChanged() |
---|
[1598] | 141 | { |
---|
| 142 | this->overlay_->setRotate(this->angle_); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | @remarks |
---|
| 147 | This function can be overriden by any derivative. |
---|
| 148 | */ |
---|
[1601] | 149 | void OrxonoxOverlay::positionChanged() |
---|
[1598] | 150 | { |
---|
[1604] | 151 | Vector2 scroll = (position_ - 0.5 - size_ * sizeCorrection_ * (origin_ - 0.5)) * 2.0; |
---|
[1598] | 152 | this->overlay_->setScroll(scroll.x, -scroll.y); |
---|
| 153 | } |
---|
[1588] | 154 | } |
---|