Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / gui / dbview_bar3.cpp
blob39fdb1e6757c84b8068ad70b6d27375e7095a349
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "stdpch.h"
22 #include "nel/gui/dbview_bar3.h"
23 #include "nel/misc/xml_auto_ptr.h"
24 #include "nel/gui/interface_group.h"
25 #include "nel/gui/widget_manager.h"
26 #include "nel/gui/db_manager.h"
28 using namespace std;
29 using namespace NL3D;
30 using namespace NLMISC;
32 #ifdef DEBUG_NEW
33 #define new DEBUG_NEW
34 #endif
36 namespace NLGUI
38 NLMISC_REGISTER_OBJECT(CViewBase, CDBViewBar3, std::string, "bar3");
40 // ----------------------------------------------------------------------------
41 CDBViewBar3::CDBViewBar3(const TCtorParam &param)
42 : CViewBitmap(param),
43 _Slot(TCtorParam())
45 _Mini = false;
46 _ColorsNegative[0] = _ColorsNegative[1] = _ColorsNegative[2] = NLMISC::CRGBA(0,0,0,0);
47 _ValueInt[0] = _ValueInt[1] = _ValueInt[2] = 0;
48 _RangeInt[0] = _RangeInt[1] = _RangeInt[2] = 255;
52 // ----------------------------------------------------------------------------
53 void CDBViewBar3::parseValProp(xmlNodePtr cur, CInterfaceProperty &dbProp, sint32 &intProp, const char *name)
55 CXMLAutoPtr prop((const char*) xmlGetProp( cur, (xmlChar*)name ));
56 if (prop)
58 if ( isdigit(*prop.getDatas()) || *(prop.getDatas())=='-')
59 fromString((const char*)prop, intProp);
60 else
61 dbProp.link(prop);
66 void CDBViewBar3::setValProp( const std::string &value, CInterfaceProperty &dbProp, sint32 &intProp )
68 sint32 i;
69 if( fromString( value, i ) )
70 intProp = i;
71 else
72 dbProp.link( value.c_str() );
76 std::string CDBViewBar3::getProperty( const std::string &name ) const
78 if( name == "value1" )
80 return getValProp( _Value[ 0 ], _ValueInt[ 0 ] );
82 else
83 if( name == "value2" )
85 return getValProp( _Value[ 1 ], _ValueInt[ 1 ] );
87 else
88 if( name == "value3" )
90 return getValProp( _Value[ 2 ], _ValueInt[ 2 ] );
92 else
93 if( name == "range1" )
95 return getValProp( _Range[ 0 ], _RangeInt[ 0 ] );
97 else
98 if( name == "range2" )
100 return getValProp( _Range[ 1 ], _RangeInt[ 1 ] );
102 else
103 if( name == "range3" )
105 return getValProp( _Range[ 2 ], _RangeInt[ 2 ] );
107 else
108 if( name == "color1" )
110 return toString( _Colors[ 0 ] );
112 else
113 if( name == "color2" )
115 return toString( _Colors[ 1 ] );
117 else
118 if( name == "color3" )
120 return toString( _Colors[ 2 ] );
122 else
123 if( name == "color1_negative" )
125 return toString( _ColorsNegative[ 0 ] );
127 else
128 if( name == "color2_negative" )
130 return toString( _ColorsNegative[ 1 ] );
132 else
133 if( name == "color3_negative" )
135 return toString( _ColorsNegative[ 2 ] );
137 else
138 if( name == "mini" )
140 if( _Mini )
141 return "true";
142 else
143 return "false";
145 else
146 return CViewBitmap::getProperty( name );
150 void CDBViewBar3::setProperty( const std::string &name, const std::string &value )
152 if( name == "value1" )
154 setValProp( value, _Value[ 0 ], _ValueInt[ 0 ] );
155 return;
157 else
158 if( name == "value2" )
160 setValProp( value, _Value[ 1 ], _ValueInt[ 1 ] );
161 return;
163 else
164 if( name == "value3" )
166 setValProp( value, _Value[ 2 ], _ValueInt[ 2 ] );
167 return;
169 else
170 if( name == "range1" )
172 setValProp( value, _Range[ 0 ], _RangeInt[ 0 ] );
173 return;
175 else
176 if( name == "range2" )
178 setValProp( value, _Range[ 1 ], _RangeInt[ 1 ] );
179 return;
181 else
182 if( name == "range3" )
184 setValProp( value, _Range[ 2 ], _RangeInt[ 2 ] );
185 return;
187 else
188 if( name == "color1" )
190 CRGBA c;
191 if( fromString( value, c ) )
192 _Colors[ 0 ] = c;
193 return;
195 else
196 if( name == "color2" )
198 CRGBA c;
199 if( fromString( value, c ) )
200 _Colors[ 1 ] = c;
201 return;
203 else
204 if( name == "color3" )
206 CRGBA c;
207 if( fromString( value, c ) )
208 _Colors[ 2 ] = c;
209 return;
211 else
212 if( name == "color1_negative" )
214 CRGBA c;
215 if( fromString( value, c ) )
216 _ColorsNegative[ 0 ] = c;
217 return;
219 else
220 if( name == "color2_negative" )
222 CRGBA c;
223 if( fromString( value, c ) )
224 _ColorsNegative[ 1 ] = c;
225 return;
227 else
228 if( name == "color3_negative" )
230 CRGBA c;
231 if( fromString( value, c ) )
232 _ColorsNegative[ 2 ] = c;
233 return;
235 else
236 if( name == "mini" )
238 bool b;
239 if( fromString( value, b ) )
240 _Mini = b;
241 return;
243 else
244 CViewBitmap::setProperty( name, value );
248 xmlNodePtr CDBViewBar3::serialize( xmlNodePtr parentNode, const char *type ) const
250 xmlNodePtr node = CViewBitmap::serialize( parentNode, type );
251 if( node == NULL )
252 return NULL;
254 xmlSetProp( node, BAD_CAST "type", BAD_CAST "bar3" );
255 xmlSetProp( node, BAD_CAST "value1", BAD_CAST getValProp( _Value[ 0 ], _ValueInt[ 0 ] ).c_str() );
256 xmlSetProp( node, BAD_CAST "value2", BAD_CAST getValProp( _Value[ 1 ], _ValueInt[ 1 ] ).c_str() );
257 xmlSetProp( node, BAD_CAST "value3", BAD_CAST getValProp( _Value[ 2 ], _ValueInt[ 2 ] ).c_str() );
259 xmlSetProp( node, BAD_CAST "range1", BAD_CAST getValProp( _Range[ 0 ], _RangeInt[ 0 ] ).c_str() );
260 xmlSetProp( node, BAD_CAST "range2", BAD_CAST getValProp( _Range[ 1 ], _RangeInt[ 1 ] ).c_str() );
261 xmlSetProp( node, BAD_CAST "range3", BAD_CAST getValProp( _Range[ 2 ], _RangeInt[ 2 ] ).c_str() );
263 xmlSetProp( node, BAD_CAST "color1", BAD_CAST toString( _Colors[ 0 ] ).c_str() );
264 xmlSetProp( node, BAD_CAST "color2", BAD_CAST toString( _Colors[ 1 ] ).c_str() );
265 xmlSetProp( node, BAD_CAST "color3", BAD_CAST toString( _Colors[ 2 ] ).c_str() );
267 xmlSetProp( node, BAD_CAST "color1_negative", BAD_CAST toString( _ColorsNegative[ 0 ] ).c_str() );
268 xmlSetProp( node, BAD_CAST "color2_negative", BAD_CAST toString( _ColorsNegative[ 1 ] ).c_str() );
269 xmlSetProp( node, BAD_CAST "color3_negative", BAD_CAST toString( _ColorsNegative[ 2 ] ).c_str() );
271 if( _Mini )
272 xmlSetProp( node, BAD_CAST "mini", BAD_CAST "true" );
273 else
274 xmlSetProp( node, BAD_CAST "mini", BAD_CAST "false" );
276 return node;
280 // ----------------------------------------------------------------------------
281 bool CDBViewBar3::parse (xmlNodePtr cur, CInterfaceGroup * parentGroup)
283 if (!CViewBitmap::parse(cur, parentGroup))
285 string tmp = "cannot parse view:"+getId()+", parent:"+parentGroup->getId();
286 nlinfo(tmp.c_str());
287 return false;
290 // read values
291 parseValProp(cur, _Value[0], _ValueInt[0], "value1" );
292 parseValProp(cur, _Value[1], _ValueInt[1], "value2" );
293 parseValProp(cur, _Value[2], _ValueInt[2], "value3" );
295 // read ranges
296 parseValProp(cur, _Range[0], _RangeInt[0], "range1" );
297 parseValProp(cur, _Range[1], _RangeInt[1], "range2" );
298 parseValProp(cur, _Range[2], _RangeInt[2], "range3" );
301 // Read colors etc....
302 CXMLAutoPtr prop;
304 prop = (char*) xmlGetProp( cur, (xmlChar*)"color1" );
305 _Colors[0] = convertColor(prop);
306 prop = (char*) xmlGetProp( cur, (xmlChar*)"color2" );
307 _Colors[1] = convertColor(prop);
308 prop = (char*) xmlGetProp( cur, (xmlChar*)"color3" );
309 _Colors[2] = convertColor(prop);
311 prop = (char*) xmlGetProp( cur, (xmlChar*)"color1_negative" );
312 _ColorsNegative[0] = convertColor(prop);
313 prop = (char*) xmlGetProp( cur, (xmlChar*)"color2_negative" );
314 _ColorsNegative[1] = convertColor(prop);
315 prop = (char*) xmlGetProp( cur, (xmlChar*)"color3_negative" );
316 _ColorsNegative[2] = convertColor(prop);
319 _Mini = false;
320 prop = (char*) xmlGetProp( cur, (xmlChar*)"mini" );
321 if (prop)
322 if (convertBool(prop))
323 setMini(true);
325 if (_Mini == false)
326 setMini(false);
328 return true;
331 // ----------------------------------------------------------------------------
332 void CDBViewBar3::setMini(bool b)
334 _Mini = b;
335 if (_Mini)
336 _Slot.setTexture ("w_slot_jauge_3_mini.tga");
337 else
338 _Slot.setTexture ("w_slot_jauge_3.tga");
340 _Slot.setPosRef (_PosRef);
341 _Slot.setParentPosRef (_ParentPosRef);
342 _Slot.setX (_X);
343 _Slot.setY (_Y);
345 _Scale = true;
346 if (_Mini)
347 setTexture ("w_jauge_fill_mini.tga");
348 else
349 setTexture ("w_jauge_fill.tga");
352 // ----------------------------------------------------------------------------
353 void CDBViewBar3::updateCoords ()
355 if (_ParentPos == NULL)
356 _Slot.setParentPos (_Parent);
357 else
358 _Slot.setParentPos (_ParentPos);
359 _Slot.updateCoords();
360 _W = _Slot.getW();
361 _H = _Slot.getH();
363 // get the height of the texture
364 sint32 dummy;
365 CViewRenderer::getInstance()->getTextureSizeFromId(_TextureId, dummy, _BarH);
367 CViewBitmap::updateCoords();
370 // ----------------------------------------------------------------------------
371 sint32 CDBViewBar3::getCurrentValProp(const CInterfaceProperty &dbProp, sint32 intProp)
373 if(dbProp.getNodePtr())
374 return dbProp.getSInt32();
375 else
376 return intProp;
379 std::string CDBViewBar3::getValProp( const CInterfaceProperty &prop, sint32 intProp ) const
381 if( prop.getNodePtr() != NULL )
382 return prop.getNodePtr()->getFullName();
383 else
384 return toString( intProp );
388 // ----------------------------------------------------------------------------
389 void CDBViewBar3::draw ()
391 _Slot.draw();
393 CViewRenderer &rVR = *CViewRenderer::getInstance();
394 CRGBA gColor = CWidgetManager::getInstance()->getGlobalColorForContent();
396 if (_Mini)
398 for (uint32 i = 0; i < 3; ++i)
400 float factor;
401 CRGBA color;
403 sint32 value= getCurrentValProp(_Value[i], _ValueInt[i]);
404 sint32 range= getCurrentValProp(_Range[i], _RangeInt[i]);
406 if (range > 0)
407 factor = ( (float)value / (float)range );
408 else
409 factor = 0;
411 if (factor < 0)
413 factor = -factor;
414 color = _ColorsNegative[i];
416 else
418 color = _Colors[i];
421 // clamp the factor to 0/1
422 clamp(factor, 0, 1);
423 float wBar= factor * (float)(_Slot.getWReal()-2);
425 color.A = (uint8)(((sint32)color.A*((sint32)gColor.A+1))>>8);
426 _WReal = (sint32)wBar;
428 rVR.drawRotFlipBitmap (_RenderLayer, _XReal+1, _YReal+i*3+2, _WReal, _BarH, 0, false, _TextureId, color);
431 else
433 for (uint32 i = 0; i < 3; ++i)
435 float factor;
436 CRGBA color;
438 sint32 value= getCurrentValProp(_Value[i], _ValueInt[i]);
439 sint32 range= getCurrentValProp(_Range[i], _RangeInt[i]);
441 if (range > 0)
442 factor = ( (float)value / (float)range );
443 else
444 factor = 0;
446 if (factor < 0)
448 factor = -factor;
449 color = _ColorsNegative[i];
451 else
453 color = _Colors[i];
456 // clamp the factor to 0/1
457 clamp(factor, 0, 1);
458 float wBar= factor * (float)(_Slot.getWReal()-4);
460 color.A = (uint8)(((sint32)color.A*((sint32)gColor.A+1))>>8);
461 _WReal = (sint32)wBar;
463 rVR.drawRotFlipBitmap (_RenderLayer, _XReal+2, _YReal+i*7+4, _WReal, _BarH, 0, false, _TextureId, color);
468 void CDBViewBar3::forceLink()