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