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