Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3110 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

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