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 | * Yuning Chai |
---|
24 | * Co-authors: |
---|
25 | * Felix Schulthess |
---|
26 | * Fabian 'x3n' Landau |
---|
27 | * Reto Grieder |
---|
28 | * Benjamin Knecht |
---|
29 | * |
---|
30 | */ |
---|
31 | |
---|
32 | #include "HUDBar.h" |
---|
33 | |
---|
34 | #include <OgreMaterialManager.h> |
---|
35 | #include <OgreTechnique.h> |
---|
36 | #include <OgrePass.h> |
---|
37 | |
---|
38 | #if OGRE_VERSION >= 0x010900 |
---|
39 | # include <Overlay/OgreOverlayManager.h> |
---|
40 | # include <Overlay/OgrePanelOverlayElement.h> |
---|
41 | #else |
---|
42 | # include <OgreOverlayManager.h> |
---|
43 | # include <OgrePanelOverlayElement.h> |
---|
44 | #endif |
---|
45 | |
---|
46 | #include "util/Convert.h" |
---|
47 | #include "util/StringUtils.h" |
---|
48 | #include "core/CoreIncludes.h" |
---|
49 | #include "core/XMLPort.h" |
---|
50 | |
---|
51 | namespace orxonox |
---|
52 | { |
---|
53 | RegisterClass(BarColour); |
---|
54 | |
---|
55 | BarColour::BarColour(Context* context) : BaseObject(context) |
---|
56 | { |
---|
57 | RegisterObject(BarColour); |
---|
58 | |
---|
59 | this->setColour(ColourValue(1.0, 1.0, 1.0, 1.0)); |
---|
60 | this->setPosition(0.0); |
---|
61 | } |
---|
62 | |
---|
63 | void BarColour::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
64 | { |
---|
65 | SUPER(BarColour, XMLPort, xmlelement, mode); |
---|
66 | |
---|
67 | XMLPortParam(BarColour, "colour", setColour, getColour, xmlelement, mode); |
---|
68 | XMLPortParam(BarColour, "position", setPosition, getPosition, xmlelement, mode); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | unsigned int HUDBar::materialcount_s = 0; |
---|
73 | |
---|
74 | RegisterClass(HUDBar); |
---|
75 | |
---|
76 | HUDBar::HUDBar(Context* context) : OrxonoxOverlay(context), right2Left_(false), autoColour_(false) |
---|
77 | { |
---|
78 | RegisterObject(HUDBar); |
---|
79 | |
---|
80 | // create new material |
---|
81 | const std::string& materialname = "barmaterial" + multi_cast<std::string>(materialcount_s++); |
---|
82 | Ogre::MaterialPtr material = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().create(materialname, "General")); |
---|
83 | material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); |
---|
84 | this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState(); |
---|
85 | this->textureUnitState_->setTextureName("bar2.png"); |
---|
86 | this->textureUnitState_->setNumMipmaps(0); |
---|
87 | // use the default colour |
---|
88 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2f, 0.7f, 0.2f)); |
---|
89 | |
---|
90 | this->bar_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
91 | .createOverlayElement("Panel", "HUDBar_bar_" + getUniqueNumberString())); |
---|
92 | this->bar_->setMaterialName(materialname); |
---|
93 | |
---|
94 | this->bar_->setPosition(0.0f,0.0f); |
---|
95 | this->bar_->setDimensions(1.0f,1.0f); |
---|
96 | |
---|
97 | this->bUseIcon_ = false; |
---|
98 | this->value_ = 1.0f; // initialize with 1.0f to trigger a change when calling setValue(0.0f) on the line below |
---|
99 | this->setAutoColour(true); |
---|
100 | this->setValue(0.0f); // <-- |
---|
101 | this->setRightToLeft(false); |
---|
102 | this->currentColour_ = ColourValue::White; |
---|
103 | |
---|
104 | this->background_->addChild(bar_); |
---|
105 | |
---|
106 | this->icon_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDBar_icon_" + getUniqueNumberString())); |
---|
107 | this->icon_->setPosition(0.06f,0.0f); |
---|
108 | this->icon_->setDimensions(0.1f,1.0f); |
---|
109 | this->background_->addChild(this->icon_); |
---|
110 | } |
---|
111 | |
---|
112 | HUDBar::~HUDBar() |
---|
113 | { |
---|
114 | if (this->isInitialized()) |
---|
115 | { |
---|
116 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_); |
---|
117 | for (std::vector<BarColour*>::const_iterator it = this->barColours_.begin(); it != this->barColours_.end(); ) |
---|
118 | (*it++)->destroy(); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void HUDBar::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
123 | { |
---|
124 | SUPER(HUDBar, XMLPort, xmlelement, mode); |
---|
125 | |
---|
126 | XMLPortParam(HUDBar, "initialvalue", setValue, getValue, xmlelement, mode); |
---|
127 | XMLPortParam(HUDBar, "righttoleft", setRightToLeft, getRightToLeft, xmlelement, mode); |
---|
128 | XMLPortParam(HUDBar, "autocolour", setAutoColour, getAutoColour, xmlelement, mode); |
---|
129 | XMLPortParam(HUDBar, "bartexture", setBarTexture, getBarTexture, xmlelement, mode); |
---|
130 | XMLPortParam(HUDBar, "iconmaterial", setIconMaterial, getIconMaterial, xmlelement, mode); |
---|
131 | XMLPortObject(HUDBar, BarColour, "", addColour, getColour, xmlelement, mode); |
---|
132 | } |
---|
133 | |
---|
134 | void HUDBar::valueChanged() |
---|
135 | { |
---|
136 | if (this->autoColour_ && this->textureUnitState_) |
---|
137 | { |
---|
138 | // set colour |
---|
139 | if (this->colours_.size() > 0) |
---|
140 | { |
---|
141 | ColourValue colour1(0, 0, 0, 1); |
---|
142 | ColourValue colour2 = this->colours_.rbegin()->second; |
---|
143 | float value1(0); |
---|
144 | float value2 = this->colours_.rbegin()->first; |
---|
145 | for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it) |
---|
146 | { |
---|
147 | colour1 = colour2; |
---|
148 | value1 = value2; |
---|
149 | colour2 = it->second; |
---|
150 | value2 = it->first; |
---|
151 | |
---|
152 | if (value2 < this->value_) |
---|
153 | break; |
---|
154 | } |
---|
155 | |
---|
156 | if (value2 > this->value_) |
---|
157 | { |
---|
158 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2); |
---|
159 | this->currentColour_ = colour2; |
---|
160 | } |
---|
161 | else if (value1 < this->value_) |
---|
162 | { |
---|
163 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1); |
---|
164 | this->currentColour_ = colour1; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | //float interpolationfactor = (this->value_ - value2) / (value1 - value2); |
---|
169 | float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f); |
---|
170 | this->currentColour_ = colour1 * interpolationfactor + colour2 * (1 - interpolationfactor); |
---|
171 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, this->currentColour_); |
---|
172 | |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | float height = this->bar_->getHeight(); |
---|
178 | float top = this->bar_->getTop(); |
---|
179 | |
---|
180 | // set value |
---|
181 | if (this->right2Left_) |
---|
182 | { |
---|
183 | // backward case |
---|
184 | if (this->bUseIcon_) |
---|
185 | { |
---|
186 | this->bar_->setPosition(0.16f + 0.78f * (1 - this->value_), top); |
---|
187 | this->bar_->setDimensions(0.78f * this->value_, height); |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | this->bar_->setPosition(0.06f + 0.88f * (1 - this->value_), top); |
---|
192 | this->bar_->setDimensions(0.88f * this->value_, height); |
---|
193 | } |
---|
194 | } |
---|
195 | else |
---|
196 | { |
---|
197 | // default case |
---|
198 | if (this->bUseIcon_) |
---|
199 | { |
---|
200 | this->bar_->setPosition(0.16f, top); |
---|
201 | this->bar_->setDimensions(0.78f * this->value_, height); |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | this->bar_->setPosition(0.06f, top); |
---|
206 | this->bar_->setDimensions(0.88f * this->value_, height); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | if (this->value_ != 0) |
---|
211 | this->bar_->setTiling(this->value_, 1.0); |
---|
212 | } |
---|
213 | |
---|
214 | void HUDBar::addColour(BarColour* colour) |
---|
215 | { |
---|
216 | float value = clamp<float>(colour->getPosition(), 0.0, 1.0); |
---|
217 | this->colours_[value] = colour->getColour(); |
---|
218 | |
---|
219 | this->barColours_.push_back(colour); |
---|
220 | } |
---|
221 | |
---|
222 | BarColour* HUDBar::getColour(unsigned int index) |
---|
223 | { |
---|
224 | if (index < this->barColours_.size()) |
---|
225 | return barColours_[index]; |
---|
226 | else |
---|
227 | return nullptr; |
---|
228 | } |
---|
229 | |
---|
230 | void HUDBar::clearColours() |
---|
231 | { |
---|
232 | this->colours_.clear(); |
---|
233 | } |
---|
234 | |
---|
235 | void HUDBar::setBarTexture(const std::string& texture) |
---|
236 | { |
---|
237 | this->textureUnitState_->setTextureName(texture); |
---|
238 | } |
---|
239 | |
---|
240 | const std::string& HUDBar::getBarTexture() const |
---|
241 | { |
---|
242 | return this->textureUnitState_->getTextureName(); |
---|
243 | } |
---|
244 | |
---|
245 | void HUDBar::setIconMaterial(const std::string& iconMaterial) |
---|
246 | { |
---|
247 | this->bUseIcon_ = true; |
---|
248 | this->icon_->setMaterialName(iconMaterial); |
---|
249 | valueChanged(); |
---|
250 | } |
---|
251 | |
---|
252 | const std::string& HUDBar::getIconMaterial() const |
---|
253 | { |
---|
254 | return this->icon_->getMaterialName(); |
---|
255 | } |
---|
256 | |
---|
257 | void HUDBar::setIconPosition(Vector2 position) |
---|
258 | { |
---|
259 | this->icon_->setPosition(position.x, position.y); |
---|
260 | } |
---|
261 | |
---|
262 | void HUDBar::setIconDimensions(Vector2 dimensions) |
---|
263 | { |
---|
264 | this->icon_->setDimensions(dimensions.x, dimensions.y); |
---|
265 | } |
---|
266 | } |
---|