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