Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2019 was 2019, checked in by landauf, 16 years ago

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

  • 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#include <OgrePanelOverlayElement.h>
38
39#include "util/Convert.h"
40#include "util/String.h"
41#include "core/CoreIncludes.h"
42#include "core/XMLPort.h"
43
44namespace orxonox
45{
46    CreateFactory(BarColour);
47
48    BarColour::BarColour(BaseObject* creator) : BaseObject(creator), position_(0.0)
49    {
50        RegisterObject(BarColour);
51    }
52
53    void BarColour::XMLPort(Element& xmlElement, XMLPort::Mode mode)
54    {
55        SUPER(BarColour, XMLPort, xmlElement, mode);
56
57        XMLPortParam(BarColour, "colour", setColour, getColour, xmlElement, mode)
58            .defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0));
59        XMLPortParam(BarColour, "position", setPosition, getPosition, xmlElement, mode).defaultValues(0.0f);
60    }
61
62
63    unsigned int HUDBar::materialcount_s = 0;
64
65    HUDBar::HUDBar(BaseObject* creator) : OrxonoxOverlay(creator), bar_(0), textureUnitState_(0)
66    {
67        RegisterObject(HUDBar);
68    }
69
70    HUDBar::~HUDBar()
71    {
72        if (this->bar_)
73            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_);
74    }
75
76    void HUDBar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
77    {
78        SUPER(HUDBar, XMLPort, xmlElement, mode);
79
80        if (mode == XMLPort::LoadObject)
81        {
82            // create new material
83            std::string materialname = "barmaterial" + getConvertedValue<unsigned int, std::string>(materialcount_s++);
84            Ogre::MaterialPtr material = (Ogre::MaterialPtr)Ogre::MaterialManager::getSingleton().create(materialname, "General");
85            material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
86            this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
87            this->textureUnitState_->setTextureName("bar2.tga");
88            // use the default colour
89            this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2, 0.7, 0.2));
90
91            this->bar_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
92                .createOverlayElement("Panel", "HUDBar_bar_" + getUniqueNumberString()));
93            this->bar_->setMaterialName(materialname);
94            this->background_->addChild(bar_);
95        }
96
97        XMLPortParam(HUDBar, "initialValue", setValue,       getValue,       xmlElement, mode).defaultValues(0.4567654f);
98        XMLPortParam(HUDBar, "rightToLeft",  setRightToLeft, getRightToLeft, xmlElement, mode).defaultValues(false);
99        XMLPortParam(HUDBar, "autoColour",   setAutoColour,  getAutoColour,  xmlElement, mode).defaultValues(true);
100        XMLPortObject(HUDBar, BarColour, "", addColour, getColour, xmlElement, mode);
101    }
102
103    void HUDBar::valueChanged()
104    {
105        if (this->autoColour_ && this->textureUnitState_)
106        {
107            // set colour
108            if (this->colours_.size() > 0)
109            {
110                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
111                float value1, value2 = (*this->colours_.rbegin()).first;
112                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
113                {
114                    colour1 = colour2;
115                    value1 = value2;
116                    colour2 = (*it).second;
117                    value2 = (*it).first;
118
119                    if (value2 < this->value_)
120                        break;
121                }
122
123                if (value2 > this->value_)
124                {
125                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
126                }
127                else if (value1 < this->value_)
128                {
129                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
130                }
131                else
132                {
133                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
134                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
135                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1 * interpolationfactor + colour2 * (1 - interpolationfactor));
136                }
137            }
138        }
139
140        // set value
141        if (this->right2Left_)
142        {
143            // backward casew
144            this->bar_->setPosition(0.06f + 0.88f * (1 - this->value_), 0.0f);
145            this->bar_->setDimensions(0.88f * this->value_, 1.0f);
146        }
147        else
148        {
149            // default case
150            this->bar_->setPosition(0.06f, 0.0f);
151            this->bar_->setDimensions(0.88f * this->value_, 1.0f);
152        }
153        if (this->value_ != 0)
154            this->bar_->setTiling(this->value_, 1.0);
155    }
156
157    void HUDBar::addColour(BarColour* colour)
158    {
159        float value = clamp<float>(colour->getPosition(), 0.0, 1.0);
160        this->colours_[value] = colour->getColour();
161
162        this->barColours_.push_back(colour);
163    }
164
165    BarColour* HUDBar::getColour(unsigned int index)
166    {
167        if (index < this->barColours_.size())
168            return barColours_[index];
169        else
170            return 0;
171    }
172
173    void HUDBar::clearColours()
174    {
175        this->colours_.clear();
176    }
177}
Note: See TracBrowser for help on using the repository browser.