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 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "BarOverlayElement.h" |
---|
31 | #include <OgreOverlayManager.h> |
---|
32 | #include "GraphicsEngine.h" |
---|
33 | #include "util/Math.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | using namespace Ogre; |
---|
38 | |
---|
39 | BarOverlayElement::BarOverlayElement(const String& name) : PanelOverlayElement(name) |
---|
40 | { |
---|
41 | name_ = name; |
---|
42 | widthratio_ = 100.0f / 800.0f; // calculates the ratio (backgroundwidth - barwidth) / backgroundwidth |
---|
43 | } |
---|
44 | |
---|
45 | BarOverlayElement::~BarOverlayElement() |
---|
46 | { |
---|
47 | OverlayManager::getSingleton().destroyOverlayElement(this->background_); |
---|
48 | } |
---|
49 | |
---|
50 | void BarOverlayElement::init(Real leftRel, Real topRel, Real dimRel, OverlayContainer* container){ |
---|
51 | // init some values... |
---|
52 | value_ = 0; |
---|
53 | colour_ = 2; |
---|
54 | autoColour_ = true; |
---|
55 | right2Left_ = false; // default is left to right progress |
---|
56 | leftRel_ = leftRel; |
---|
57 | topRel_ = topRel; |
---|
58 | dimRel_ = dimRel; |
---|
59 | |
---|
60 | // create background... |
---|
61 | background_ = static_cast<OverlayContainer*>(OverlayManager::getSingleton().createOverlayElement("Panel", name_+"container")); |
---|
62 | background_->show(); |
---|
63 | container->addChild(background_); |
---|
64 | background_->setMetricsMode(GMM_PIXELS); |
---|
65 | background_->setMaterialName("Orxonox/BarBackground"); |
---|
66 | |
---|
67 | // calculate absolute coordinates... |
---|
68 | resize(); |
---|
69 | |
---|
70 | show(); |
---|
71 | setMetricsMode(GMM_PIXELS); |
---|
72 | setMaterialName("Orxonox/Green"); |
---|
73 | background_->addChild(this); |
---|
74 | } |
---|
75 | |
---|
76 | void BarOverlayElement::resize(){ |
---|
77 | // calculate new absolute coordinates... |
---|
78 | left_ = (int) (leftRel_ * GraphicsEngine::getSingleton().getWindowWidth()); |
---|
79 | top_ = (int) (topRel_ * GraphicsEngine::getSingleton().getWindowHeight()); |
---|
80 | width_ = (int) (dimRel_ * GraphicsEngine::getSingleton().getWindowWidth()); |
---|
81 | height_ = (int) (0.1 * width_); // the texture has dimensions height:length = 1:10 |
---|
82 | offset_ = (int) (width_ * widthratio_ * 0.5); |
---|
83 | barwidth_ = (int) (width_ * (1.0f - widthratio_)); |
---|
84 | |
---|
85 | // adapt background |
---|
86 | background_->setPosition(left_, top_); |
---|
87 | background_->setDimensions(width_, height_); |
---|
88 | // adapt bar |
---|
89 | setValue(value_); |
---|
90 | } |
---|
91 | |
---|
92 | void BarOverlayElement::setValue(float value){ |
---|
93 | value_ = clamp<float>(value, 0, 1); |
---|
94 | // set colour, if nescessary |
---|
95 | if(autoColour_){ |
---|
96 | if (value_>0.5) {setColour(BarOverlayElement::GREEN);} |
---|
97 | else if (value_>0.25) {setColour(BarOverlayElement::YELLOW);} |
---|
98 | else setColour(BarOverlayElement::RED); |
---|
99 | } |
---|
100 | |
---|
101 | // set value |
---|
102 | if(right2Left_){ // backward case |
---|
103 | setPosition(offset_ + barwidth_ * (1 - value_), 0); |
---|
104 | setDimensions(barwidth_ * value_, height_); |
---|
105 | }else{ // default case |
---|
106 | setPosition(offset_, 0); |
---|
107 | setDimensions(barwidth_ * value_, height_); |
---|
108 | } |
---|
109 | if(value_ != 0) setTiling(value_, 1.0); |
---|
110 | } |
---|
111 | |
---|
112 | void BarOverlayElement::setColour(int colour){ |
---|
113 | colour_ = colour; |
---|
114 | switch(colour){ |
---|
115 | case 0: |
---|
116 | setMaterialName("Orxonox/Red"); |
---|
117 | break; |
---|
118 | case 1: |
---|
119 | setMaterialName("Orxonox/Yellow"); |
---|
120 | break; |
---|
121 | case 2: |
---|
122 | setMaterialName("Orxonox/Green"); |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|