[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 | * Yuning Chai |
---|
| 24 | * Co-authors: |
---|
| 25 | * Felix Schulthess |
---|
| 26 | * Fabian 'x3n' Landau |
---|
| 27 | * Reto Grieder |
---|
[8706] | 28 | * Benjamin Knecht |
---|
[1588] | 29 | * |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | #include "HUDBar.h" |
---|
| 33 | |
---|
| 34 | #include <OgreMaterialManager.h> |
---|
| 35 | #include <OgreTechnique.h> |
---|
[3196] | 36 | #include <OgrePass.h> |
---|
[1588] | 37 | |
---|
[11795] | 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 | |
---|
[1588] | 46 | #include "util/Convert.h" |
---|
[3280] | 47 | #include "util/StringUtils.h" |
---|
[1616] | 48 | #include "core/CoreIncludes.h" |
---|
| 49 | #include "core/XMLPort.h" |
---|
[1588] | 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
[9667] | 53 | RegisterClass(BarColour); |
---|
[1618] | 54 | |
---|
[11052] | 55 | BarColour::BarColour(Context* context) : BaseObject(context) |
---|
[1618] | 56 | { |
---|
| 57 | RegisterObject(BarColour); |
---|
[11052] | 58 | |
---|
[2662] | 59 | this->setColour(ColourValue(1.0, 1.0, 1.0, 1.0)); |
---|
| 60 | this->setPosition(0.0); |
---|
[1618] | 61 | } |
---|
| 62 | |
---|
[7401] | 63 | void BarColour::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[1618] | 64 | { |
---|
[7401] | 65 | SUPER(BarColour, XMLPort, xmlelement, mode); |
---|
[1618] | 66 | |
---|
[7401] | 67 | XMLPortParam(BarColour, "colour", setColour, getColour, xmlelement, mode); |
---|
| 68 | XMLPortParam(BarColour, "position", setPosition, getPosition, xmlelement, mode); |
---|
[1618] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
[1588] | 72 | unsigned int HUDBar::materialcount_s = 0; |
---|
| 73 | |
---|
[9667] | 74 | RegisterClass(HUDBar); |
---|
| 75 | |
---|
[11052] | 76 | HUDBar::HUDBar(Context* context) : OrxonoxOverlay(context), right2Left_(false), autoColour_(false) |
---|
[1588] | 77 | { |
---|
| 78 | RegisterObject(HUDBar); |
---|
[2087] | 79 | |
---|
| 80 | // create new material |
---|
[6417] | 81 | const std::string& materialname = "barmaterial" + multi_cast<std::string>(materialcount_s++); |
---|
[3300] | 82 | Ogre::MaterialPtr material = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().create(materialname, "General")); |
---|
[2087] | 83 | material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); |
---|
| 84 | this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState(); |
---|
[7368] | 85 | this->textureUnitState_->setTextureName("bar2.png"); |
---|
[6677] | 86 | this->textureUnitState_->setNumMipmaps(0); |
---|
[2087] | 87 | // use the default colour |
---|
[3196] | 88 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2f, 0.7f, 0.2f)); |
---|
[2087] | 89 | |
---|
| 90 | this->bar_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
---|
| 91 | .createOverlayElement("Panel", "HUDBar_bar_" + getUniqueNumberString())); |
---|
| 92 | this->bar_->setMaterialName(materialname); |
---|
| 93 | |
---|
[11052] | 94 | this->bar_->setPosition(0.0f,0.0f); |
---|
| 95 | this->bar_->setDimensions(1.0f,1.0f); |
---|
| 96 | |
---|
| 97 | this->bUseIcon_ = false; |
---|
[7801] | 98 | this->value_ = 1.0f; // initialize with 1.0f to trigger a change when calling setValue(0.0f) on the line below |
---|
[6417] | 99 | this->setAutoColour(true); |
---|
[2662] | 100 | this->setValue(0.0f); // <-- |
---|
| 101 | this->setRightToLeft(false); |
---|
| 102 | this->currentColour_ = ColourValue::White; |
---|
[2087] | 103 | |
---|
| 104 | this->background_->addChild(bar_); |
---|
[11052] | 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_); |
---|
[1588] | 110 | } |
---|
| 111 | |
---|
| 112 | HUDBar::~HUDBar() |
---|
| 113 | { |
---|
[2087] | 114 | if (this->isInitialized()) |
---|
[5929] | 115 | { |
---|
[1615] | 116 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_); |
---|
[5929] | 117 | for (std::vector<BarColour*>::const_iterator it = this->barColours_.begin(); it != this->barColours_.end(); ) |
---|
| 118 | (*it++)->destroy(); |
---|
| 119 | } |
---|
[1588] | 120 | } |
---|
| 121 | |
---|
[7401] | 122 | void HUDBar::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[1588] | 123 | { |
---|
[7401] | 124 | SUPER(HUDBar, XMLPort, xmlelement, mode); |
---|
[1588] | 125 | |
---|
[7401] | 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); |
---|
[11052] | 130 | XMLPortParam(HUDBar, "iconmaterial", setIconMaterial, getIconMaterial, xmlelement, mode); |
---|
[7401] | 131 | XMLPortObject(HUDBar, BarColour, "", addColour, getColour, xmlelement, mode); |
---|
[1588] | 132 | } |
---|
| 133 | |
---|
[1627] | 134 | void HUDBar::valueChanged() |
---|
[1588] | 135 | { |
---|
| 136 | if (this->autoColour_ && this->textureUnitState_) |
---|
| 137 | { |
---|
| 138 | // set colour |
---|
| 139 | if (this->colours_.size() > 0) |
---|
| 140 | { |
---|
[2710] | 141 | ColourValue colour1(0, 0, 0, 1); |
---|
[6417] | 142 | ColourValue colour2 = this->colours_.rbegin()->second; |
---|
[2710] | 143 | float value1(0); |
---|
[6417] | 144 | float value2 = this->colours_.rbegin()->first; |
---|
[1588] | 145 | for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it) |
---|
| 146 | { |
---|
| 147 | colour1 = colour2; |
---|
| 148 | value1 = value2; |
---|
[6417] | 149 | colour2 = it->second; |
---|
| 150 | value2 = it->first; |
---|
[1588] | 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); |
---|
[2662] | 159 | this->currentColour_ = colour2; |
---|
[1588] | 160 | } |
---|
| 161 | else if (value1 < this->value_) |
---|
| 162 | { |
---|
| 163 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1); |
---|
[2662] | 164 | this->currentColour_ = colour1; |
---|
[1588] | 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); |
---|
[2662] | 170 | this->currentColour_ = colour1 * interpolationfactor + colour2 * (1 - interpolationfactor); |
---|
| 171 | this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, this->currentColour_); |
---|
| 172 | |
---|
[1588] | 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
[11052] | 177 | float height = this->bar_->getHeight(); |
---|
| 178 | float top = this->bar_->getTop(); |
---|
| 179 | |
---|
[1588] | 180 | // set value |
---|
| 181 | if (this->right2Left_) |
---|
| 182 | { |
---|
[7801] | 183 | // backward case |
---|
[11052] | 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 | } |
---|
[1588] | 194 | } |
---|
| 195 | else |
---|
| 196 | { |
---|
| 197 | // default case |
---|
[11052] | 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 | } |
---|
[1588] | 208 | } |
---|
[11052] | 209 | |
---|
[1588] | 210 | if (this->value_ != 0) |
---|
| 211 | this->bar_->setTiling(this->value_, 1.0); |
---|
| 212 | } |
---|
| 213 | |
---|
[1618] | 214 | void HUDBar::addColour(BarColour* colour) |
---|
[1588] | 215 | { |
---|
[1618] | 216 | float value = clamp<float>(colour->getPosition(), 0.0, 1.0); |
---|
| 217 | this->colours_[value] = colour->getColour(); |
---|
| 218 | |
---|
| 219 | this->barColours_.push_back(colour); |
---|
[1588] | 220 | } |
---|
| 221 | |
---|
[1618] | 222 | BarColour* HUDBar::getColour(unsigned int index) |
---|
| 223 | { |
---|
| 224 | if (index < this->barColours_.size()) |
---|
| 225 | return barColours_[index]; |
---|
| 226 | else |
---|
[11071] | 227 | return nullptr; |
---|
[1618] | 228 | } |
---|
| 229 | |
---|
[1588] | 230 | void HUDBar::clearColours() |
---|
| 231 | { |
---|
| 232 | this->colours_.clear(); |
---|
| 233 | } |
---|
[2662] | 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 | } |
---|
[11052] | 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 | } |
---|
[11795] | 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 | } |
---|
[1588] | 266 | } |
---|