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