Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/orxonox/hud/HUDBar.cc @ 1590

Last change on this file since 1590 was 1590, checked in by rgrieder, 16 years ago

svn save, just in case our house burns down over night…

  • Property svn:eol-style set to native
File size: 6.4 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
38#include "util/Convert.h"
39
40namespace orxonox
41{
42    unsigned int HUDBar::materialcount_s = 0;
43
44    using namespace Ogre;
45
46    HUDBar::HUDBar()
47    {
48        RegisterObject(HUDBar);
49
50        this->bar_ = 0;
51        this->background_ = 0;
52        this->textureUnitState_ = 0;
53
54        barWidth_s = 0.88f;
55        barHeight_s = 0.3f;
56        barOffsetLeft_s = 0.06f;
57        barOffsetTop_s = 0.0f;
58
59        this->value_ = -1;
60        this->autoColour_ = true;
61        this->right2Left_ = false; // default is left to right progress
62    }
63
64    HUDBar::~HUDBar()
65    {
66        if (this->isInitialized())
67        {
68            if (this->bar_)
69                OverlayManager::getSingleton().destroyOverlayElement(this->bar_);
70        }
71    }
72
73    void HUDBar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
74    {
75        HUDOverlay::XMLPort(xmlElement, mode);
76
77        if (mode == XMLPort::LoadObject)
78        {
79            // create background
80            this->background_ = static_cast<PanelOverlayElement*>(
81                    OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Background"));
82            this->background_->setMaterialName("Orxonox/BarBackground");
83            this->background_->setMetricsMode(GMM_RELATIVE);
84            this->background_->setDimensions(1.0f, 0.3f);
85            this->background_->setPosition(0.0f, 0.0f);
86            this->overlay_->add2D(this->background_);
87
88            // create new material
89            std::string materialname = "barmaterial" + getConvertedValue<unsigned int, std::string>(materialcount_s++);
90            Ogre::MaterialPtr material = (Ogre::MaterialPtr)Ogre::MaterialManager::getSingleton().create(materialname, "General");
91            material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
92            this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
93            this->textureUnitState_->setTextureName("bar2.tga");
94            // use the default colour
95            this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2, 0.7, 0.2));
96
97            // create bar
98            this->bar_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "Bar"));
99            this->bar_->setMaterialName(materialname);
100            this->bar_->setMetricsMode(GMM_RELATIVE);
101            this->background_->addChild(bar_);
102        }
103
104        XMLPortParamLoadOnly(HUDBar, "value", setValue, xmlElement, mode);
105
106        if (mode == XMLPort::LoadObject)
107        {
108            this->addColour(0.7, ColourValue(0.2, 0.7, 0.2));
109            this->addColour(0.4, ColourValue(0.7, 0.5, 0.2));
110            this->addColour(0.1, ColourValue(0.7, 0.2, 0.2));
111        }
112    }
113
114    void HUDBar::setValue(float value)
115    {
116        if (value == this->value_)
117            return;
118
119        this->value_ = clamp<float>(value, 0, 1);
120        if (this->autoColour_ && this->textureUnitState_)
121        {
122            // set colour
123            if (this->colours_.size() > 0)
124            {
125                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
126                float value1, value2 = (*this->colours_.rbegin()).first;
127                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
128                {
129                    colour1 = colour2;
130                    value1 = value2;
131                    colour2 = (*it).second;
132                    value2 = (*it).first;
133
134                    if (value2 < this->value_)
135                        break;
136                }
137
138                if (value2 > this->value_)
139                {
140                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
141                }
142                else if (value1 < this->value_)
143                {
144                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
145                }
146                else
147                {
148                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
149                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
150                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1 * interpolationfactor + colour2 * (1 - interpolationfactor));
151                }
152            }
153        }
154
155        // set value
156        if (this->right2Left_)
157        {
158            // backward casew
159            this->bar_->setPosition(barOffsetLeft_s + barWidth_s * (1 - this->value_), barOffsetTop_s);
160            this->bar_->setDimensions(barWidth_s * this->value_, barHeight_s);
161        }
162        else
163        {
164            // default case
165            this->bar_->setPosition(barOffsetLeft_s, barOffsetTop_s);
166            this->bar_->setDimensions(barWidth_s * this->value_, barHeight_s);
167        }
168        if (this->value_ != 0)
169            this->bar_->setTiling(this->value_, 1.0);
170    }
171
172    void HUDBar::addColour(float value, const ColourValue& colour)
173    {
174        value = clamp<float>(value, 0, 1);
175        this->colours_[value] = colour;
176    }
177
178    void HUDBar::clearColours()
179    {
180        this->colours_.clear();
181    }
182}
Note: See TracBrowser for help on using the repository browser.