Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/hud/HUDBar.cc @ 1627

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

some adjustment to the default value setting in the overlay files

  • Property svn:eol-style set to native
File size: 6.6 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    CreateFactory(BarColour);
46
47    BarColour::BarColour()
48        : position_(0.0)
49    {
50        RegisterObject(BarColour);
51    }
52
53    void BarColour::XMLPort(Element& xmlElement, XMLPort::Mode mode)
54    {
55        BaseObject::XMLPort(xmlElement, mode);
56
57        if (mode == XMLPort::LoadObject)
58        {
59            this->setColour(ColourValue(1.0, 1.0, 1.0, 1.0));
60            this->setPosition(0.0f);
61        }
62
63        XMLPortParam(BarColour, "colour", setColour, getColour, xmlElement, mode)
64            .defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0));
65        XMLPortParam(BarColour, "position", setPosition, getPosition, xmlElement, mode).defaultValues(0.0f);
66    }
67
68
69    unsigned int HUDBar::materialcount_s = 0;
70
71    HUDBar::HUDBar()
72        : bar_(0)
73        , textureUnitState_(0)
74    {
75        RegisterObject(HUDBar);
76    }
77
78    HUDBar::~HUDBar()
79    {
80        if (this->bar_)
81            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_);
82    }
83
84    void HUDBar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
85    {
86        OrxonoxOverlay::XMLPort(xmlElement, mode);
87
88        if (mode == XMLPort::LoadObject)
89        {
90            // create new material
91            std::string materialname = "barmaterial" + getConvertedValue<unsigned int, std::string>(materialcount_s++);
92            Ogre::MaterialPtr material = (Ogre::MaterialPtr)Ogre::MaterialManager::getSingleton().create(materialname, "General");
93            material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
94            this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
95            this->textureUnitState_->setTextureName("bar2.tga");
96            // use the default colour
97            this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2, 0.7, 0.2));
98
99            this->bar_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
100                .createOverlayElement("Panel", "HUDBar_bar_" + getUniqueNumberStr()));
101            this->bar_->setMaterialName(materialname);
102            this->background_->addChild(bar_);
103
104            this->setValue(0.0f);
105            this->setRightToLeft(false);
106            this->setAutoColour(true);
107        }
108
109        XMLPortParam(HUDBar, "initialValue", setValue,       getValue,       xmlElement, mode).defaultValues(0.0f);
110        XMLPortParam(HUDBar, "rightToLeft",   setRightToLeft, getRightToLeft, xmlElement, mode).defaultValues(false);
111        XMLPortParam(HUDBar, "autoColour",   setAutoColour,  getAutoColour,  xmlElement, mode).defaultValues(true);
112        XMLPortObject(HUDBar, BarColour, "", addColour, getColour, xmlElement, mode, false, true);
113    }
114
115    void HUDBar::valueChanged()
116    {
117        if (this->autoColour_ && this->textureUnitState_)
118        {
119            // set colour
120            if (this->colours_.size() > 0)
121            {
122                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
123                float value1, value2 = (*this->colours_.rbegin()).first;
124                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
125                {
126                    colour1 = colour2;
127                    value1 = value2;
128                    colour2 = (*it).second;
129                    value2 = (*it).first;
130
131                    if (value2 < this->value_)
132                        break;
133                }
134
135                if (value2 > this->value_)
136                {
137                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
138                }
139                else if (value1 < this->value_)
140                {
141                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
142                }
143                else
144                {
145                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
146                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
147                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1 * interpolationfactor + colour2 * (1 - interpolationfactor));
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}
Note: See TracBrowser for help on using the repository browser.