Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/waypoints/src/modules/overlays/hud/HUDBar.cc @ 9219

Last change on this file since 9219 was 8706, checked in by dafrick, 13 years ago

Merging presentation branch back into trunk.
There are many new features and also a lot of other changes and bugfixes, if you want to know, digg through the svn log.
Not everything is yet working as it should, but it should be fairly stable. If you habe any bug reports, just send me an email.

  • Property svn:eol-style set to native
File size: 7.3 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 *      Benjamin Knecht
29 *
30 */
31
32#include "HUDBar.h"
33
34#include <OgreOverlayManager.h>
35#include <OgreMaterialManager.h>
36#include <OgreTechnique.h>
37#include <OgrePass.h>
38#include <OgrePanelOverlayElement.h>
39
40#include "util/Convert.h"
41#include "util/StringUtils.h"
42#include "core/CoreIncludes.h"
43#include "core/XMLPort.h"
44
45namespace orxonox
46{
47    CreateFactory(BarColour);
48
49    BarColour::BarColour(BaseObject* creator)
50        : BaseObject(creator)
51    {
52        RegisterObject(BarColour);
53
54        this->setColour(ColourValue(1.0, 1.0, 1.0, 1.0));
55        this->setPosition(0.0);
56    }
57
58    void BarColour::XMLPort(Element& xmlelement, XMLPort::Mode mode)
59    {
60        SUPER(BarColour, XMLPort, xmlelement, mode);
61
62        XMLPortParam(BarColour, "colour", setColour, getColour, xmlelement, mode);
63        XMLPortParam(BarColour, "position", setPosition, getPosition, xmlelement, mode);
64    }
65
66
67    unsigned int HUDBar::materialcount_s = 0;
68
69    HUDBar::HUDBar(BaseObject* creator)
70        : OrxonoxOverlay(creator), right2Left_(false), autoColour_(false)
71    {
72        RegisterObject(HUDBar);
73
74        // create new material
75        const std::string& materialname = "barmaterial" + multi_cast<std::string>(materialcount_s++);
76        Ogre::MaterialPtr material = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().create(materialname, "General"));
77        material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
78        this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
79        this->textureUnitState_->setTextureName("bar2.png");
80        this->textureUnitState_->setNumMipmaps(0);
81        // use the default colour
82        this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2f, 0.7f, 0.2f));
83
84        this->bar_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
85            .createOverlayElement("Panel", "HUDBar_bar_" + getUniqueNumberString()));
86        this->bar_->setMaterialName(materialname);
87
88        this->value_ = 1.0f;  // initialize with 1.0f to trigger a change when calling setValue(0.0f) on the line below
89        this->setAutoColour(true);
90        this->setValue(0.0f); // <--
91        this->setRightToLeft(false);
92        this->currentColour_ = ColourValue::White;
93
94        this->background_->addChild(bar_);
95    }
96
97    HUDBar::~HUDBar()
98    {
99        if (this->isInitialized())
100        {
101            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_);
102            for (std::vector<BarColour*>::const_iterator it = this->barColours_.begin(); it != this->barColours_.end(); )
103                (*it++)->destroy();
104        }
105    }
106
107    void HUDBar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
108    {
109        SUPER(HUDBar, XMLPort, xmlelement, mode);
110
111        XMLPortParam(HUDBar, "initialvalue", setValue,       getValue,       xmlelement, mode);
112        XMLPortParam(HUDBar, "righttoleft",  setRightToLeft, getRightToLeft, xmlelement, mode);
113        XMLPortParam(HUDBar, "autocolour",   setAutoColour,  getAutoColour,  xmlelement, mode);
114        XMLPortParam(HUDBar, "bartexture",   setBarTexture,  getBarTexture, xmlelement, mode);
115        XMLPortObject(HUDBar, BarColour, "", addColour, getColour, xmlelement, mode);
116    }
117
118    void HUDBar::valueChanged()
119    {
120        if (this->autoColour_ && this->textureUnitState_)
121        {
122            // set colour
123            if (this->colours_.size() > 0)
124            {
125                ColourValue colour1(0, 0, 0, 1);
126                ColourValue colour2 = this->colours_.rbegin()->second;
127                float value1(0);
128                float value2 = this->colours_.rbegin()->first;
129                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
130                {
131                    colour1 = colour2;
132                    value1 = value2;
133                    colour2 = it->second;
134                    value2 = it->first;
135
136                    if (value2 < this->value_)
137                        break;
138                }
139
140                if (value2 > this->value_)
141                {
142                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
143                    this->currentColour_ = colour2;
144                }
145                else if (value1 < this->value_)
146                {
147                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
148                    this->currentColour_ = colour1;
149                }
150                else
151                {
152                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
153                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
154                    this->currentColour_ = colour1 * interpolationfactor + colour2 * (1 - interpolationfactor);
155                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, this->currentColour_);
156
157                }
158            }
159        }
160
161        // set value
162        if (this->right2Left_)
163        {
164            // backward case
165            this->bar_->setPosition(0.06f + 0.88f * (1 - this->value_), 0.0f);
166            this->bar_->setDimensions(0.88f * this->value_, 1.0f);
167        }
168        else
169        {
170            // default case
171            this->bar_->setPosition(0.06f, 0.0f);
172            this->bar_->setDimensions(0.88f * this->value_, 1.0f);
173        }
174        if (this->value_ != 0)
175            this->bar_->setTiling(this->value_, 1.0);
176    }
177
178    void HUDBar::addColour(BarColour* colour)
179    {
180        float value = clamp<float>(colour->getPosition(), 0.0, 1.0);
181        this->colours_[value] = colour->getColour();
182
183        this->barColours_.push_back(colour);
184    }
185
186    BarColour* HUDBar::getColour(unsigned int index)
187    {
188        if (index < this->barColours_.size())
189            return barColours_[index];
190        else
191            return 0;
192    }
193
194    void HUDBar::clearColours()
195    {
196        this->colours_.clear();
197    }
198
199    void HUDBar::setBarTexture(const std::string& texture)
200    {
201        this->textureUnitState_->setTextureName(texture);
202    }
203
204    const std::string& HUDBar::getBarTexture() const
205    {
206        return this->textureUnitState_->getTextureName();
207    }
208}
Note: See TracBrowser for help on using the repository browser.