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