Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1615 was 1615, checked in by rgrieder, 16 years ago
  • added blankString to String so you can return ""; even if it's a const std::string&
  • fixed several bugs with aspect correct and margin alignment
  • added console commands for OrxonoxOverlays and OverlayGroups for rotate, scale and scroll (you can access the by name (from name=.. in xml file), e.g. "OrxonoxOverlay rotateOverlay SpeedBar 90)
  • converted everything in overlays/ to 4 spaces/tab ;)
  • removed all using namespace Ogre;
  • added background_ Panel to OrxonoxOverlay, since most of the derived classes can use that
  • should work now, but I'll have to test on a tardis box first
  • Property svn:eol-style set to native
File size: 5.8 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    HUDBar::HUDBar()
46        : bar_(0)
47        , textureUnitState_(0)
48    {
49        RegisterObject(HUDBar);
50    }
51
52    HUDBar::~HUDBar()
53    {
54        if (this->bar_)
55            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->bar_);
56    }
57
58    void HUDBar::XMLPort(Element& xmlElement, XMLPort::Mode mode)
59    {
60        OrxonoxOverlay::XMLPort(xmlElement, mode);
61
62        if (mode == XMLPort::LoadObject)
63        {
64            // create new material
65            std::string materialname = "barmaterial" + getConvertedValue<unsigned int, std::string>(materialcount_s++);
66            Ogre::MaterialPtr material = (Ogre::MaterialPtr)Ogre::MaterialManager::getSingleton().create(materialname, "General");
67            material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
68            this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
69            this->textureUnitState_->setTextureName("bar2.tga");
70            // use the default colour
71            this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2, 0.7, 0.2));
72
73            // create bar
74            barWidth_s = 0.88f;
75            barHeight_s = 1.0f;
76            barOffsetLeft_s = 0.06f;
77            barOffsetTop_s = 0.0f;
78
79            this->bar_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
80                .createOverlayElement("Panel", "HUDBar_bar_" + getUniqueNumberStr()));
81            this->bar_->setMaterialName(materialname);
82            this->background_->addChild(bar_);
83
84            this->setValue(0);
85            this->autoColour_ = true;
86            this->right2Left_ = false; // default is left to right progress
87
88            this->addColour(0.7, ColourValue(0.2, 0.7, 0.2));
89            this->addColour(0.4, ColourValue(0.7, 0.5, 0.2));
90            this->addColour(0.1, ColourValue(0.7, 0.2, 0.2));
91        }
92
93        XMLPortParamLoadOnly(HUDBar, "value", setValue, xmlElement, mode);
94    }
95
96    void HUDBar::setValue(float value)
97    {
98        if (value == this->value_)
99            return;
100
101        this->value_ = clamp<float>(value, 0, 1);
102        if (this->autoColour_ && this->textureUnitState_)
103        {
104            // set colour
105            if (this->colours_.size() > 0)
106            {
107                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
108                float value1, value2 = (*this->colours_.rbegin()).first;
109                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
110                {
111                    colour1 = colour2;
112                    value1 = value2;
113                    colour2 = (*it).second;
114                    value2 = (*it).first;
115
116                    if (value2 < this->value_)
117                        break;
118                }
119
120                if (value2 > this->value_)
121                {
122                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
123                }
124                else if (value1 < this->value_)
125                {
126                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
127                }
128                else
129                {
130                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
131                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
132                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1 * interpolationfactor + colour2 * (1 - interpolationfactor));
133                }
134            }
135        }
136
137        // set value
138        if (this->right2Left_)
139        {
140            // backward casew
141            this->bar_->setPosition(barOffsetLeft_s + barWidth_s * (1 - this->value_), barOffsetTop_s);
142            this->bar_->setDimensions(barWidth_s * this->value_, barHeight_s);
143        }
144        else
145        {
146            // default case
147            this->bar_->setPosition(barOffsetLeft_s, barOffsetTop_s);
148            this->bar_->setDimensions(barWidth_s * this->value_, barHeight_s);
149        }
150        if (this->value_ != 0)
151            this->bar_->setTiling(this->value_, 1.0);
152    }
153
154    void HUDBar::addColour(float value, const ColourValue& colour)
155    {
156        value = clamp<float>(value, 0, 1);
157        this->colours_[value] = colour;
158    }
159
160    void HUDBar::clearColours()
161    {
162        this->colours_.clear();
163    }
164}
Note: See TracBrowser for help on using the repository browser.