Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/hud/BarOverlayElement.cc @ 1576

Last change on this file since 1576 was 1568, checked in by landauf, 16 years ago
  • BarOverlayElement interpolates between given colours
  • Tried to fix a bug when releasing navigation focus
  • Property svn:eol-style set to native
File size: 6.7 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
[1502]4 *
[1505]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 *
[1502]22 *   Author:
23 *      Yuning Chai
24 *   Co-authors:
25 *      Felix Schulthess
[1568]26 *      Fabian 'x3n' Landau
[1502]27 *
28 */
[1283]29
[1502]30#include "OrxonoxStableHeaders.h"
31#include "BarOverlayElement.h"
[1568]32
[1283]33#include <OgreOverlayManager.h>
[1568]34#include <OgreMaterialManager.h>
35#include <OgreTechnique.h>
36
[1314]37#include "GraphicsEngine.h"
[1568]38#include "util/Convert.h"
[1283]39
40namespace orxonox
41{
[1502]42    using namespace Ogre;
[1283]43
[1568]44    unsigned int BarOverlayElement::materialcount_s = 0;
45
[1564]46    BarOverlayElement::BarOverlayElement(const String& name) : PanelOverlayElement(name)
47    {
[1568]48        this->textureUnitState_ = 0;
49        this->name_ = name;
50        this->widthratio_ = 100.0f / 800.0f; // calculates the ratio (backgroundwidth - barwidth) / backgroundwidth
[1328]51    }
[1283]52
[1564]53    BarOverlayElement::~BarOverlayElement()
54    {
55        OverlayManager::getSingleton().destroyOverlayElement(this->background_);
56    }
[1283]57
[1568]58    void BarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container)
59    {
[1328]60        // init some values...
[1568]61        this->value_ = -1;
62        this->autoColour_ = true;
63        this->right2Left_ = false; // default is left to right progress
64        this->leftRel_ = leftRel;
65        this->topRel_ = topRel;
66        this->dimRel_ = dimRel;
[1502]67
[1328]68        // create background...
[1568]69        this->background_ = static_cast<OverlayContainer*>(OverlayManager::getSingleton().createOverlayElement("Panel", name_+"container"));
70        this->background_->show();
[1564]71        container->addChild(background_);
[1568]72        this->background_->setMetricsMode(GMM_PIXELS);
73        this->background_->setMaterialName("Orxonox/BarBackground");
[1328]74
[1342]75        // calculate absolute coordinates...
[1568]76        this->resize();
77        this->show();
[1342]78
[1568]79        // create new material
80        std::string materialname = "barmaterial" + getConvertedValue<unsigned int, std::string>(BarOverlayElement::materialcount_s++);
81        Ogre::MaterialPtr material = (Ogre::MaterialPtr)Ogre::MaterialManager::getSingleton().create(materialname, "General");
82        material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
83        this->textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
84        this->textureUnitState_->setTextureName("bar2.tga");
85        // use the default colour
86        this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, ColourValue(0.2, 0.7, 0.2));
87        this->setMaterialName(materialname);
88        this->setMetricsMode(GMM_PIXELS);
89
90        this->background_->addChild(this);
[1329]91    }
92
[1568]93    void BarOverlayElement::resize()
94    {
[1342]95        // calculate new absolute coordinates...
[1568]96        this->left_ = (int) (this->leftRel_ * GraphicsEngine::getSingleton().getWindowWidth());
97        this->top_ = (int) (this->topRel_ * GraphicsEngine::getSingleton().getWindowHeight());
98        this->width_ = (int) (this->dimRel_ * GraphicsEngine::getSingleton().getWindowWidth());
99        this->height_ = (int) (0.1 * this->width_);     // the texture has dimensions height:length = 1:10
100        this->offset_ = (int) (this->width_ * this->widthratio_ * 0.5);
101        this->barwidth_ = (int) (this->width_ * (1.0f - this->widthratio_));
[1564]102
[1329]103        // adapt background
[1568]104        this->background_->setPosition(this->left_, this->top_);
105        this->background_->setDimensions(this->width_,this-> height_);
[1329]106        // adapt bar
[1568]107        this->setValue(this->value_);
[1283]108    }
109
[1568]110    void BarOverlayElement::setValue(float value)
111    {
112        if (value == this->value_)
113            return;
114
115        this->value_ = clamp<float>(value, 0, 1);
116        if (this->autoColour_ && this->textureUnitState_)
117        {
118            // set colour
119            if (this->colours_.size() > 0)
120            {
121                ColourValue colour1, colour2 = (*this->colours_.rbegin()).second;
122                float value1, value2 = (*this->colours_.rbegin()).first;
123                for (std::map<float, ColourValue>::reverse_iterator it = this->colours_.rbegin(); it != this->colours_.rend(); ++it)
124                {
125                    colour1 = colour2;
126                    value1 = value2;
127                    colour2 = (*it).second;
128                    value2 = (*it).first;
129
130                    if (value2 < this->value_)
131                        break;
132                }
133
134                if (value2 > this->value_)
135                {
136                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour2);
137                }
138                else if (value1 < this->value_)
139                {
140                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1);
141                }
142                else
143                {
144                    //float interpolationfactor = (this->value_ - value2) / (value1 - value2);
145                    float interpolationfactor = interpolateSmooth((this->value_ - value2) / (value1 - value2), 0.0f, 1.0f);
146                    this->textureUnitState_->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour1 * interpolationfactor + colour2 * (1 - interpolationfactor));
147                }
148            }
[1328]149        }
[1564]150
[1328]151        // set value
[1568]152        if (this->right2Left_)
153        {
154            // backward case
155            this->setPosition(this->offset_ + this->barwidth_ * (1 - this->value_), 0);
156            this->setDimensions(this->barwidth_ * this->value_, this->height_);
[1314]157        }
[1568]158        else
159        {
160            // default case
161            this->setPosition(this->offset_, 0);
162            this->setDimensions(this->barwidth_ * this->value_, this->height_);
163        }
164        if (this->value_ != 0)
165            this->setTiling(this->value_, 1.0);
[1283]166    }
[1314]167
[1568]168    void BarOverlayElement::addColour(float value, const ColourValue& colour)
169    {
170        value = clamp<float>(value, 0, 1);
171        this->colours_[value] = colour;
[1283]172    }
[1568]173
174    void BarOverlayElement::clearColours()
175    {
176        this->colours_.clear();
177    }
[1283]178}
Note: See TracBrowser for help on using the repository browser.