Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / gui / dbview_bar.cpp
blob7f360c158bf9c423a661d087d5d128879088187f
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_bar.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 NLMISC_REGISTER_OBJECT(CViewBase, CDBViewBar, std::string, "bar");
38 namespace NLGUI
41 // ----------------------------------------------------------------------------
42 void CDBViewBar::parseValProp(xmlNodePtr cur, CInterfaceProperty &dbProp, sint32 &intProp, const char *name)
44 CXMLAutoPtr prop((const char*) xmlGetProp( cur, (xmlChar*)name ));
45 if (prop)
47 if ( isdigit(*prop.getDatas()) || *(prop.getDatas())=='-')
48 fromString((const char*)prop, intProp);
49 else
50 dbProp.link(prop);
54 std::string CDBViewBar::getProperty( const std::string &name ) const
56 if( name == "value" )
58 if( _Value.getNodePtr() != NULL )
59 return _Value.getNodePtr()->getFullName();
60 else
61 return toString( _ValueInt );
63 else
64 if( name == "range" )
66 if( _Range.getNodePtr() != NULL )
67 return _Range.getNodePtr()->getFullName();
68 else
69 return toString( _RangeInt );
71 else
72 if( name == "reference" )
74 if( _Reference.getNodePtr() != NULL )
75 return _Reference.getNodePtr()->getFullName();
76 else
77 return toString( _ReferenceInt );
79 else
80 if( name == "color_negative" )
82 return toString( _ColorNegative );
84 else
85 if( name == "mini" )
87 if( _Type == ViewBar_Mini )
88 return "true";
89 else
90 return "false";
92 else
93 if( name == "ultra_mini" )
95 if( _Type == ViewBar_UltraMini )
96 return "true";
97 else
98 return "false";
100 else
101 if( name == "mini_thick" )
103 if( _Type == ViewBar_MiniThick )
104 return "true";
105 else
106 return "false";
108 else
109 return CViewBitmap::getProperty( name );
112 void CDBViewBar::setProperty( const std::string &name, const std::string &value )
114 if( name == "value" )
116 sint32 i;
117 if( fromString( value, i ) )
118 _ValueInt = i;
119 else
120 _Value.link( value.c_str() );
121 return;
123 else
124 if( name == "range" )
126 sint32 i;
127 if( fromString( value, i ) )
128 _RangeInt = i;
129 else
130 _Range.link( value.c_str() );
131 return;
133 else
134 if( name == "reference" )
136 sint32 i;
137 if( fromString( value, i ) )
138 _ReferenceInt = i;
139 else
140 _Reference.link( value.c_str() );
141 return;
143 else
144 if( name == "color_negative" )
146 CRGBA c;
147 if( fromString( value, c ) )
148 _ColorNegative = c;
149 return;
151 else
152 if( name == "mini" )
154 bool b;
155 if( fromString( value, b ) )
156 if( b )
157 _Type = ViewBar_Mini;
158 return;
160 else
161 if( name == "ultra_mini" )
163 bool b;
164 if( fromString( value, b ) )
165 if( b )
166 _Type = ViewBar_UltraMini;
167 return;
169 else
170 if( name == "mini_thick" )
172 bool b;
173 if( fromString( value, b ) )
174 if( b )
175 _Type = ViewBar_MiniThick;
176 return;
178 else
179 CViewBitmap::setProperty( name, value );
183 xmlNodePtr CDBViewBar::serialize( xmlNodePtr parentNode, const char *type ) const
185 xmlNodePtr node = CViewBitmap::serialize( parentNode, type );
186 if( node == NULL )
187 return NULL;
189 xmlSetProp( node, BAD_CAST "type", BAD_CAST "bar" );
191 if( _Value.getNodePtr() != NULL )
192 xmlSetProp( node, BAD_CAST "value", BAD_CAST _Value.getNodePtr()->getFullName().c_str() );
193 else
194 xmlSetProp( node, BAD_CAST "value", BAD_CAST toString( _RangeInt ).c_str() );
196 if( _Range.getNodePtr() != NULL )
197 xmlSetProp( node, BAD_CAST "range", BAD_CAST _Range.getNodePtr()->getFullName().c_str() );
198 else
199 xmlSetProp( node, BAD_CAST "range", BAD_CAST toString( _RangeInt ).c_str() );
201 if( _Reference.getNodePtr() != NULL )
202 xmlSetProp( node, BAD_CAST "reference", BAD_CAST _Reference.getNodePtr()->getFullName().c_str() );
203 else
204 xmlSetProp( node, BAD_CAST "reference", BAD_CAST toString( _ReferenceInt ).c_str() );
206 xmlSetProp( node, BAD_CAST "color_negative", BAD_CAST toString( _ColorNegative ).c_str() );
208 if( _Type == ViewBar_Mini )
209 xmlSetProp( node, BAD_CAST "mini", BAD_CAST "true" );
210 else
211 xmlSetProp( node, BAD_CAST "mini", BAD_CAST "false" );
213 if( _Type == ViewBar_UltraMini )
214 xmlSetProp( node, BAD_CAST "ultra_mini", BAD_CAST "true" );
215 else
216 xmlSetProp( node, BAD_CAST "ultra_mini", BAD_CAST "false" );
218 if( _Type == ViewBar_MiniThick )
219 xmlSetProp( node, BAD_CAST "mini_thick", BAD_CAST "true" );
220 else
221 xmlSetProp( node, BAD_CAST "mini_thick", BAD_CAST "false" );
223 return node;
226 // ----------------------------------------------------------------------------
227 bool CDBViewBar::parse (xmlNodePtr cur, CInterfaceGroup * parentGroup)
229 if (!CViewBitmap::parse(cur, parentGroup))
231 string tmp = "cannot parse view:"+getId()+", parent:"+parentGroup->getId();
232 nlinfo(tmp.c_str());
233 return false;
236 CXMLAutoPtr prop;
238 // read value, range and reference
239 parseValProp(cur, _Value, _ValueInt, "value");
240 parseValProp(cur, _Range, _RangeInt, "range");
241 parseValProp(cur, _Reference, _ReferenceInt, "reference");
243 // Get Visual props
244 prop= (char*) xmlGetProp( cur, (xmlChar*)"color_negative" );
245 _ColorNegative = CRGBA(0,0,0,0);
246 if (prop)
247 _ColorNegative = convertColor (prop);
249 // Bar Type
250 _Type = ViewBar_Normal;
252 prop = (char*) xmlGetProp( cur, (xmlChar*)"mini" );
253 if (prop)
254 if (convertBool(prop))
255 setType(ViewBar_Mini);
257 prop = (char*) xmlGetProp( cur, (xmlChar*)"ultra_mini" );
258 if (prop)
259 if (convertBool(prop))
260 setType(ViewBar_UltraMini);
262 prop = (char*) xmlGetProp( cur, (xmlChar*)"mini_thick" );
263 if (prop)
264 if (convertBool(prop))
265 setType(ViewBar_MiniThick);
267 if (_Type == ViewBar_Normal)
268 setType(ViewBar_Normal);
270 return true;
273 // ----------------------------------------------------------------------------
274 void CDBViewBar::setType (TViewBar vb)
276 _Type = vb;
277 switch(_Type)
279 case ViewBar_Normal: _Slot.setTexture ("w_slot_jauge_1.tga"); break;
280 case ViewBar_Mini: _Slot.setTexture ("w_slot_jauge_1_mini.tga"); break;
281 case ViewBar_UltraMini: _Slot.setTexture ("w_slot_jauge_1_umin.tga"); break;
282 case ViewBar_MiniThick: _Slot.setTexture ("w_slot_jauge_1_tmin.tga"); break;
285 _Slot.setPosRef (_PosRef);
286 _Slot.setParentPosRef (_ParentPosRef);
287 _Slot.setX (_X);
288 _Slot.setY (_Y);
290 _Scale = true;
291 switch(_Type)
293 case ViewBar_Normal: setTexture ("w_jauge_fill.tga"); break;
294 case ViewBar_Mini: setTexture ("w_jauge_fill_mini.tga"); break;
295 case ViewBar_UltraMini: setTexture ("w_jauge_fill_umin.tga"); break;
296 case ViewBar_MiniThick: setTexture ("w_jauge_fill_tmin.tga"); break;
299 // Get the Height Size.
300 sint32 wBar;
301 CViewRenderer::getInstance()->getTextureSizeFromId(_TextureId, wBar, _HBar);
304 // ----------------------------------------------------------------------------
305 void CDBViewBar::updateCoords ()
307 if (_ParentPos == NULL)
308 _Slot.setParentPos (_Parent);
309 else
310 _Slot.setParentPos (_ParentPos);
311 _Slot.updateCoords();
312 _W = _Slot.getW();
313 _H = _Slot.getH();
314 CViewBitmap::updateCoords();
317 // ----------------------------------------------------------------------------
318 sint64 CDBViewBar::getCurrentValProp(const CInterfaceProperty &dbProp, sint32 intProp)
320 if(dbProp.getNodePtr())
321 return dbProp.getSInt64();
322 else
323 return intProp;
326 // ----------------------------------------------------------------------------
327 void CDBViewBar::draw ()
329 float wBar = (float)(_Slot.getWReal()-4);
331 sint64 value= getCurrentValProp(_Value, _ValueInt);
332 sint64 range= getCurrentValProp(_Range, _RangeInt);
333 sint64 reference= getCurrentValProp(_Reference, _ReferenceInt);
335 // remove the reference
336 value-= reference;
337 range-= reference;
339 // draw the bar
340 CRGBA color = _Color;
342 if (range > 0)
344 float ratio= (float)value / range;
345 if (_ColorNegative.A != 0 && ratio < 0.0f)
347 ratio = - ratio;
348 color = _ColorNegative;
350 NLMISC::clamp(ratio, 0.f, 1.f);
351 wBar *= ratio;
353 else
354 wBar = 0;
356 _WReal = (sint32)wBar;
358 _Slot.draw();
360 CViewRenderer &rVR = *CViewRenderer::getInstance();
362 color.A = (uint8)(((sint32)color.A*((sint32)CWidgetManager::getInstance()->getGlobalColorForContent().A+1))>>8);
364 // compute the DeltaY: mean of dif.
365 sint32 deltaY= (_H-_HBar)/2;
366 rVR.drawRotFlipBitmap (_RenderLayer, _XReal+2, _YReal+deltaY, _WReal, _HBar, 0, false, _TextureId, color);
369 // ----------------------------------------------------------------------------
370 void CDBViewBar::setValueDbLink (const std::string &r)
372 CCDBNodeLeaf *pNL = NLGUI::CDBManager::getInstance()->getDbProp(r,false);
373 if (pNL != NULL) _Value.setNodePtr(pNL);
376 // ----------------------------------------------------------------------------
377 void CDBViewBar::setRangeDbLink (const std::string &r)
379 CCDBNodeLeaf *pNL = NLGUI::CDBManager::getInstance()->getDbProp(r,false);
380 if (pNL != NULL) _Range.setNodePtr(pNL);
383 // ----------------------------------------------------------------------------
384 void CDBViewBar::setReferenceDbLink (const std::string &r)
386 CCDBNodeLeaf *pNL = NLGUI::CDBManager::getInstance()->getDbProp(r,false);
387 if (pNL != NULL) _Reference.setNodePtr(pNL);
390 // ----------------------------------------------------------------------------
391 string CDBViewBar::getValueDbLink () const
393 if (_Value.getNodePtr() == NULL) return "";
394 return _Value.getNodePtr()->getFullName();
397 // ----------------------------------------------------------------------------
398 string CDBViewBar::getRangeDbLink () const
400 if (_Range.getNodePtr() == NULL) return "";
401 return _Range.getNodePtr()->getFullName();
404 // ----------------------------------------------------------------------------
405 string CDBViewBar::getReferenceDbLink () const
407 if (_Reference.getNodePtr() == NULL) return "";
408 return _Reference.getNodePtr()->getFullName();