Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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