Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/overlays/hud/HUDBar.cc @ 2369

Last change on this file since 2369 was 2369, checked in by landauf, 15 years ago
  • Added a health bar
  • Some changes in CameraManager to handle the case when no camera exists after removing the last one, but this is still somehow buggy (freezes and later crashes reproducible but inexplicable after a few respawns)
  • Added PawnManager to handle destruction of Pawns without using delete within tick()
  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
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>
37#include <OgrePanelOverlayElement.h>
38
39#include "util/Convert.h"
40#include "util/String.h"
41#include "core/CoreIncludes.h"
42#include "core/XMLPort.h"
43
44namespace orxonox
45{
46    CreateFactory(BarColour);
47
48    BarColour::BarColour(BaseObject* creator)
49        : BaseObject(creator)
50    {
51        RegisterObject(BarColour);
52
53        this->setColour(ColourValue(1.0, 1.0, 1.0, 1.0));
54        this->setPosition(0.0);
55    }
56
57    void BarColour::XMLPort(Element& xmlElement, XMLPort::Mode mode)
58    {
59        SUPER(BarColour, XMLPort, xmlElement, mode);
60
61        XMLPortParam(BarColour, "colour", setColour, getColour, xmlElement, mode);
62        XMLPortParam(BarColour, "position", setPosition, getPosition, xmlElement, mode);
63    }
64
65
66    unsigned int HUDBar::materialcount_s = 0;
67
68    HUDBar::HUDBar(BaseObject* creator)
69        : OrxonoxOverlay(creator)
70    {
71        RegisterObject(HUDBar);
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        this->setValue(0.0f);
87        this->setRightToLeft(false);
88        this->setAutoColour(true);
89        this->currentColour_ = ColourValue::White;
90
91        this->background_->addChild(bar_);
92    }
93
94    HUDBar::~HUDBar()
95    {
96        if (this->isInitialized())
97            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_);
98    }
99
100    void HUDBar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
101    {
102        SUPER(HUDBar, XMLPort, xmlElement, mode);
103
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);
108        XMLPortObject(HUDBar, BarColour, "", addColour, getColour, xmlElement, mode);
109    }
110
111    void HUDBar::valueChanged()
112    {
113        if (this->autoColour_ && this->textureUnitState_)
114        {
115            // set colour
116            if (this->colours_.size() > 0)
117            {
118                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
119                float value1, value2 = (*this->colours_.rbegin()).first;
120                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
121                {
122                    colour1 = colour2;
123                    value1 = value2;
124                    colour2 = (*it).second;
125                    value2 = (*it).first;
126
127                    if (value2 < this->value_)
128                        break;
129                }
130
131                if (value2 > this->value_)
132                {
133                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
134                    this->currentColour_ = colour2;
135                }
136                else if (value1 < this->value_)
137                {
138                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
139                    this->currentColour_ = colour1;
140                }
141                else
142                {
143                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
144                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
145                    this->currentColour_ = colour1 * interpolationfactor + colour2 * (1 - interpolationfactor);
146                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, this->currentColour_);
147
148                }
149            }
150        }
151
152        // set value
153        if (this->right2Left_)
154        {
155            // backward casew
156            this->bar_->setPosition(0.06f + 0.88f * (1 - this->value_), 0.0f);
157            this->bar_->setDimensions(0.88f * this->value_, 1.0f);
158        }
159        else
160        {
161            // default case
162            this->bar_->setPosition(0.06f, 0.0f);
163            this->bar_->setDimensions(0.88f * this->value_, 1.0f);
164        }
165        if (this->value_ != 0)
166            this->bar_->setTiling(this->value_, 1.0);
167    }
168
169    void HUDBar::addColour(BarColour* colour)
170    {
171        float value = clamp<float>(colour->getPosition(), 0.0, 1.0);
172        this->colours_[value] = colour->getColour();
173
174        this->barColours_.push_back(colour);
175    }
176
177    BarColour* HUDBar::getColour(unsigned int index)
178    {
179        if (index < this->barColours_.size())
180            return barColours_[index];
181        else
182            return 0;
183    }
184
185    void HUDBar::clearColours()
186    {
187        this->colours_.clear();
188    }
189
190    void HUDBar::setBarTexture(const std::string& texture)
191    {
192        this->textureUnitState_->setTextureName(texture);
193    }
194
195    const std::string& HUDBar::getBarTexture() const
196    {
197        return this->textureUnitState_->getTextureName();
198    }
199}
Note: See TracBrowser for help on using the repository browser.