Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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