Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / interfaces_manager / progress_bar.cpp
blob13ea0244c0f9fe71dd83f817cd63954a318c06d1
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "stdpch.h"
22 //////////////
23 // Includes //
24 //////////////
25 // Misc.
26 #include "nel/misc/time_nl.h"
27 // 3D Interface.
28 #include "nel/3d/u_driver.h"
29 #include "nel/3d/u_text_context.h"
30 // Client
31 #include "progress_bar.h"
32 #include "interfaces_manager.h"
36 ///////////
37 // Using //
38 ///////////
39 using namespace std;
40 using namespace NL3D;
41 using namespace NLMISC;
44 /////////////
45 // Externs //
46 /////////////
47 extern UDriver *Driver;
48 extern UTextContext *TextContext;
51 //-----------------------------------------------
52 // CProgressBar :
53 // Constructor.
54 //-----------------------------------------------
55 CProgressBar::CProgressBar(uint id, float x, float y, float x_pixel, float y_pixel, float w, float h, float w_pixel, float h_pixel, uint range)
56 :CControl(id, x, y, x_pixel, y_pixel, w, h, w_pixel, h_pixel)
58 init(range);
59 }// CControl //
62 //-----------------------------------------------
63 // init :
64 //-----------------------------------------------
65 void CProgressBar::init( uint32 range)
67 _Range = range;
68 _CurrentPos = 0 ;
69 _TempPos = 0 ;
70 _StepInc = 1;
72 _Smooth = false;
73 _SmoothFillRate = 50; // 1 unit / 50ms
74 _LastUpdateSmooth = ryzomGetLocalTime ();
75 } // init //
78 //-----------------------------------------------
79 // setRange :
80 //-----------------------------------------------
81 uint32 CProgressBar::setRange( uint32 range)
83 uint32 old = _Range;
84 _Range = range;
86 if (_CurrentPos > _Range)
88 _CurrentPos = _Range;
89 _TempPos = _Range;
92 return old;
93 } // setRange //
96 //-----------------------------------------------
97 // setStep :
98 //-----------------------------------------------
99 uint32 CProgressBar::setStep( uint32 step)
101 uint32 old = _StepInc;
102 _StepInc = step;
103 return old;
104 }// setStep //
107 //-----------------------------------------------
108 // setPos :
109 //-----------------------------------------------
110 uint32 CProgressBar::setPos( uint32 pos)
112 uint32 old = _CurrentPos;
113 _CurrentPos = pos;
115 if (_CurrentPos > _Range)
117 _CurrentPos = _Range;
120 if (!_Smooth)
122 _TempPos = _CurrentPos;
125 return old;
126 }// setPos //
129 //-----------------------------------------------
130 // display :
131 //-----------------------------------------------
132 void CProgressBar::display()
134 // compute the temp pos if in smooth mode
135 if (_Smooth)
137 uint32 nbUnit = (uint32)(ryzomGetLocalTime () - _LastUpdateSmooth) / _SmoothFillRate;
138 if (nbUnit > 0)
140 _LastUpdateSmooth = ryzomGetLocalTime ();
142 uint32 diff = abs( _TempPos - _CurrentPos );
143 if (diff < nbUnit)
144 nbUnit = diff;
146 if ( _TempPos < _CurrentPos )
147 _TempPos += nbUnit;
148 else
149 _TempPos -= nbUnit;
152 else
154 nlassert( _TempPos == _CurrentPos );
157 // If the control is hide -> return
158 if(!_Show)
159 return;
162 // draw background
163 UTextureFile *utexture = CInterfMngr::getTexture( _BackgroundTexture );
164 if(utexture)
166 Driver->drawBitmap(_X_Display, _Y_Display, _W_Display, _H_Display, *utexture, true, _BackgroundColor);
169 // calculate _W_Display of the progress bar
170 const float wBar = _W_Display * ( static_cast<float>(_TempPos) / static_cast<float>(_Range) );
172 // Backup scissor and create the new scissor to clip the bar correctly.
173 CScissor oldScissor = Driver->getScissor();
174 CScissor scissor;
176 float scisX, scisY, scisWidth, scisHeight;
177 scisX = oldScissor.X;
178 scisY = oldScissor.Y;
179 scisWidth = oldScissor.Width;
180 scisHeight = oldScissor.Height;
182 float xtmp = _X_Display + wBar;
183 float ytmp = _Y_Display + _H_Display;
184 float xscistmp = scisX + scisWidth;
185 float yscistmp = scisY + scisHeight;
186 if( _X_Display > scisX )
187 scisX = _X_Display;
188 if( _Y_Display > scisY )
189 scisY = _Y_Display;
190 if( xtmp < xscistmp )
191 scisWidth = xtmp - scisX;
192 else
193 scisWidth = xscistmp - scisX;
194 if( ytmp < yscistmp )
195 scisHeight = ytmp - scisY;
196 else
197 scisHeight = yscistmp - scisY;
199 scissor.init(scisX, scisY, scisWidth, scisHeight);
200 Driver->setScissor(scissor);
202 // display progress bar
203 utexture = CInterfMngr::getTexture( _ProgressBarTexture );
204 Driver->drawBitmap(_X_Display, _Y_Display, _W_Display, _H_Display, *utexture, true, _ProgressBarColor);
206 // restore old scissor
207 Driver->setScissor(oldScissor);
209 // display text
210 if ( ! _Text.empty() )
212 TextContext->setShaded(_Shadow);
213 TextContext->setHotSpot(NL3D::UTextContext::THotSpot::MiddleMiddle);
214 TextContext->setColor(_Color);
215 TextContext->setFontSize(_FontSize);
216 TextContext->printAt(_X_Display + _W_Display/2, _Y_Display + _H_Display/2, _Text);
220 }// display //