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