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