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