Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1588 was 1588, checked in by rgrieder, 16 years ago
  • added XML loadable HUD
  • Radar and navi are not yet done
  • explanations follow with when things are finished
  • Property svn:eol-style set to native
File size: 6.2 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        // create background
78        this->background_ = static_cast<PanelOverlayElement*>(
79                OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Background"));
80        this->background_->setMaterialName("Orxonox/BarBackground");
81        this->background_->setMetricsMode(GMM_RELATIVE);
82        this->background_->setDimensions(1.0f, 0.3f);
83        this->background_->setPosition(0.0f, 0.0f);
84        this->overlay_->add2D(this->background_);
85
86        // create new material
87        std::string materialname = "barmaterial" + getConvertedValue<unsigned int, std::string>(materialcount_s++);
88        Ogre::MaterialPtr material = (Ogre::MaterialPtr)Ogre::MaterialManager::getSingleton().create(materialname, "General");
89        material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
90        this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
91        this->textureUnitState_->setTextureName("bar2.tga");
92        // use the default colour
93        this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2, 0.7, 0.2));
94
95        // create bar
96        this->bar_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "Bar"));
97        this->bar_->setMaterialName(materialname);
98        this->bar_->setMetricsMode(GMM_RELATIVE);
99        this->background_->addChild(bar_);
100
101        XMLPortParamLoadOnly(HUDBar, "value", setValue, xmlElement, mode);
102
103        this->addColour(0.7, ColourValue(0.2, 0.7, 0.2));
104        this->addColour(0.4, ColourValue(0.7, 0.5, 0.2));
105        this->addColour(0.1, ColourValue(0.7, 0.2, 0.2));
106    }
107
108    void HUDBar::setValue(float value)
109    {
110        if (value == this->value_)
111            return;
112
113        this->value_ = clamp<float>(value, 0, 1);
114        if (this->autoColour_ && this->textureUnitState_)
115        {
116            // set colour
117            if (this->colours_.size() > 0)
118            {
119                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
120                float value1, value2 = (*this->colours_.rbegin()).first;
121                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
122                {
123                    colour1 = colour2;
124                    value1 = value2;
125                    colour2 = (*it).second;
126                    value2 = (*it).first;
127
128                    if (value2 < this->value_)
129                        break;
130                }
131
132                if (value2 > this->value_)
133                {
134                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
135                }
136                else if (value1 < this->value_)
137                {
138                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
139                }
140                else
141                {
142                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
143                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
144                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1 * interpolationfactor + colour2 * (1 - interpolationfactor));
145                }
146            }
147        }
148
149        // set value
150        if (this->right2Left_)
151        {
152            // backward casew
153            this->bar_->setPosition(barOffsetLeft_s + barWidth_s * (1 - this->value_), barOffsetTop_s);
154            this->bar_->setDimensions(barWidth_s * this->value_, barHeight_s);
155        }
156        else
157        {
158            // default case
159            this->bar_->setPosition(barOffsetLeft_s, barOffsetTop_s);
160            this->bar_->setDimensions(barWidth_s * this->value_, barHeight_s);
161        }
162        if (this->value_ != 0)
163            this->bar_->setTiling(this->value_, 1.0);
164    }
165
166    void HUDBar::addColour(float value, const ColourValue& colour)
167    {
168        value = clamp<float>(value, 0, 1);
169        this->colours_[value] = colour;
170    }
171
172    void HUDBar::clearColours()
173    {
174        this->colours_.clear();
175    }
176}
Note: See TracBrowser for help on using the repository browser.