Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / gui / ctrl_text_button.cpp
blob894612f394533b2f8c2a8617a6b452f82022360e
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2019 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013-2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
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 Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "stdpch.h"
23 #include "nel/gui/ctrl_text_button.h"
24 #include "nel/misc/xml_auto_ptr.h"
25 #include "nel/gui/view_text.h"
26 #include "nel/gui/view_text_id.h"
27 #include "nel/gui/group_container_base.h"
28 #include "nel/gui/lua_ihm.h"
29 #include "nel/gui/widget_manager.h"
30 #include "nel/gui/interface_factory.h"
31 #include "nel/misc/i18n.h"
33 using namespace std;
34 using namespace NLMISC;
35 using namespace NL3D;
37 #ifdef DEBUG_NEW
38 #define new DEBUG_NEW
39 #endif
41 NLMISC_REGISTER_OBJECT(CViewBase, CCtrlTextButton, std::string, "text_button");
43 namespace NLGUI
46 // ***************************************************************************
47 CCtrlTextButton::CCtrlTextButton(const TCtorParam &param)
48 : CCtrlBaseButton(param)
50 _BmpLeftW= _BmpMiddleW= _BmpRightW= _BmpH= 0;
51 _WMargin= 0;
52 _WMin= 0;
53 _HMin= 0;
54 _TextX= 0;
55 _TextY= 0;
56 _Setuped= false;
57 _ViewText= NULL;
58 _IsViewTextId= false;
59 _TextColorNormal= CRGBA(255, 255, 255, 255);
60 _TextColorPushed= CRGBA(255, 255, 255, 255);
61 _TextColorOver= CRGBA(255, 255, 255, 255);
62 _TextShadowColorNormal= CRGBA(0, 0, 0, 255);
63 _TextShadowColorPushed= CRGBA(0, 0, 0, 255);
64 _TextShadowColorOver= CRGBA(0, 0, 0, 255);
65 _TextModulateGlobalColorNormal= true;
66 _TextModulateGlobalColorPushed= true;
67 _TextModulateGlobalColorOver= true;
68 _TextHeaderColor= false;
69 _TextPosRef = Hotspot_MM;
70 _TextParentPosRef = Hotspot_MM;
71 _ForceTextOver = false;
74 CCtrlTextButton::~CCtrlTextButton()
76 if( _ViewText != NULL )
78 delete _ViewText;
79 _ViewText = NULL;
83 std::string CCtrlTextButton::getProperty( const std::string &name ) const
85 std::string prop;
86 if( _ViewText != NULL )
87 prop = _ViewText->getTextProperty( name );
89 if( !prop.empty() )
90 return prop;
91 else
92 if( name == "tx_normal" )
94 std::string tex;
95 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdNormal[ 0 ] );
96 std::string::size_type i = tex.rfind( "_l.tga" );
97 if( i != std::string::npos )
98 tex = tex.substr( 0, i );
99 return tex;
101 else
102 if( name == "tx_pushed" )
104 std::string tex;
105 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdPushed[ 0 ] );
106 std::string::size_type i = tex.rfind( "_l.tga" );
107 if( i != std::string::npos )
108 tex = tex.substr( 0, i );
109 return tex;
111 else
112 if( name == "tx_over" )
114 std::string tex;
115 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdOver[ 0 ] );
116 std::string::size_type i = tex.rfind( "_l.tga" );
117 if( i != std::string::npos )
118 tex = tex.substr( 0, i );
119 return tex;
121 else
122 if( name == "wmargin" )
124 return toString( _WMargin );
126 else
127 if( name == "wmin" )
129 return toString( _WMin );
131 else
132 if( name == "hmin" )
134 return toString( _HMin );
136 else
137 if( name == "hardtext" )
139 if( _ViewText != NULL )
140 return _ViewText->getText();
141 else
142 return std::string( "" );
144 else
145 if( name == "text_y" )
147 return toString( _TextY );
149 else
150 if( name == "text_x" )
152 return toString( _TextX );
154 else
155 if( name == "text_underlined" )
157 if( _ViewText != NULL )
158 return toString( _ViewText->getUnderlined() );
159 else
160 return std::string( "" );
162 else
163 if( name == "text_posref" )
165 std::string pr;
166 pr = CInterfaceElement::HotSpotToString( _TextPosRef );
167 return pr;
169 else
170 if( name == "text_parent_posref" )
172 std::string pr;
173 pr = CInterfaceElement::HotSpotToString( _TextParentPosRef );
174 return pr;
176 else
177 if( name == "text_color_normal" )
179 return toString( _TextColorNormal );
181 else
182 if( name == "text_color_pushed" )
184 return toString( _TextColorPushed );
186 else
187 if( name == "text_color_over" )
189 return toString( _TextColorOver );
191 else
192 if( name == "text_shadow_color_normal" )
194 return toString( _TextShadowColorNormal );
196 else
197 if( name == "text_shadow_color_pushed" )
199 return toString( _TextShadowColorPushed );
201 else
202 if( name == "text_shadow_color_over" )
204 return toString( _TextShadowColorOver );
206 else
207 if( name == "text_global_color_normal" )
209 return toString( _TextModulateGlobalColorNormal );
211 else
212 if( name == "text_global_color_pushed" )
214 return toString( _TextModulateGlobalColorPushed );
216 else
217 if( name == "text_global_color_over" )
219 return toString( _TextModulateGlobalColorOver );
221 else
222 if( name == "force_text_over" )
224 return toString( _ForceTextOver );
226 else
227 if( name == "text_header_color" )
229 return toString( _TextHeaderColor );
231 else
232 return CCtrlBaseButton::getProperty( name );
235 void CCtrlTextButton::setProperty( const std::string &name, const std::string &value )
237 if( _ViewText != NULL )
239 if( _ViewText->setTextProperty( name, value ) )
241 _ViewText->invalidateContent();
242 return;
246 if( name == "tx_normal" )
248 std::string tex;
249 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdNormal[ 0 ] );
250 std::string::size_type i = tex.rfind( "_l.tga" );
251 if( i != std::string::npos )
252 tex = tex.substr( 0, i );
254 _TextureIdNormal[ 0 ].setTexture( std::string( value + "_l.tga" ).c_str() );
255 _TextureIdNormal[ 1 ].setTexture( std::string( value + "_m.tga" ).c_str() );
256 _TextureIdNormal[ 2 ].setTexture( std::string( value + "_r.tga" ).c_str() );
258 CViewRenderer &rVR = *CViewRenderer::getInstance();
259 rVR.getTextureSizeFromId(_TextureIdNormal[0], _BmpLeftW, _BmpH);
260 rVR.getTextureSizeFromId(_TextureIdNormal[1], _BmpMiddleW, _BmpH);
261 rVR.getTextureSizeFromId(_TextureIdNormal[2], _BmpRightW, _BmpH);
262 return;
264 else
265 if( name == "tx_pushed" )
267 std::string tex;
268 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdPushed[ 0 ] );
269 std::string::size_type i = tex.rfind( "_l.tga" );
270 if( i != std::string::npos )
271 tex = tex.substr( 0, i );
273 _TextureIdPushed[ 0 ].setTexture( std::string( value + "_l.tga" ).c_str() );
274 _TextureIdPushed[ 1 ].setTexture( std::string( value + "_m.tga" ).c_str() );
275 _TextureIdPushed[ 2 ].setTexture( std::string( value + "_r.tga" ).c_str() );
276 return;
278 else
279 if( name == "tx_over" )
281 std::string tex;
282 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdOver[ 0 ] );
283 std::string::size_type i = tex.rfind( "_l.tga" );
284 if( i != std::string::npos )
285 tex = tex.substr( 0, i );
287 _TextureIdOver[ 0 ].setTexture( std::string( value + "_l.tga" ).c_str() );
288 _TextureIdOver[ 1 ].setTexture( std::string( value + "_m.tga" ).c_str() );
289 _TextureIdOver[ 2 ].setTexture( std::string( value + "_r.tga" ).c_str() );
290 return;
292 else
293 if( name == "wmargin" )
295 sint32 i;
296 if( fromString( value, i ) )
297 _WMargin = i;
298 return;
300 else
301 if( name == "wmin" )
303 sint32 i;
304 if( fromString( value, i ) )
305 _WMin = i;
306 return;
308 else
309 if( name == "hmin" )
311 sint32 i;
312 if( fromString( value, i ) )
313 _HMin = i;
314 return;
316 else
317 if( name == "hardtext" )
319 if( _ViewText != NULL )
320 _ViewText->setHardText( value );
321 return;
323 else
324 if( name == "text_y" )
326 sint32 i;
327 if( fromString( value, i ) )
328 _TextY = i;
329 return;
331 else
332 if( name == "text_x" )
334 sint32 i;
335 if( fromString( value, i ) )
336 _TextX = i;
337 return;
339 else
340 if( name == "text_underlined" )
342 bool b;
343 if( _ViewText != NULL )
344 if( fromString( value, b ) )
345 _ViewText->setUnderlined( b );
347 return;
349 else
350 if( name == "text_posref" )
352 _TextPosRef = convertHotSpot( value.c_str() );
353 return;
355 else
356 if( name == "text_parent_posref" )
358 _TextParentPosRef = convertHotSpot( value.c_str() );
359 return;
361 else
362 if( name == "text_color_normal" )
364 CRGBA c;
365 if( fromString( value, c ) )
366 _TextColorNormal = c;
367 return;
369 else
370 if( name == "text_color_pushed" )
372 CRGBA c;
373 if( fromString( value, c ) )
374 _TextColorPushed = c;
375 return;
377 else
378 if( name == "text_color_over" )
380 CRGBA c;
381 if( fromString( value, c ) )
382 _TextColorOver = c;
383 return;
385 else
386 if( name == "text_shadow_color_normal" )
388 CRGBA c;
389 if( fromString( value, c ) )
390 _TextShadowColorNormal = c;
391 return;
393 else
394 if( name == "text_shadow_color_pushed" )
396 CRGBA c;
397 if( fromString( value, c ) )
398 _TextShadowColorPushed = c;
399 return;
401 else
402 if( name == "text_shadow_color_over" )
404 CRGBA c;
405 if( fromString( value, c ) )
406 _TextShadowColorOver = c;
407 return;
409 else
410 if( name == "text_global_color_normal" )
412 bool b;
413 if( fromString( value, b ) )
414 _TextModulateGlobalColorNormal = b;
415 return;
417 else
418 if( name == "text_global_color_pushed" )
420 bool b;
421 if( fromString( value, b ) )
422 _TextModulateGlobalColorPushed = b;
423 return;
425 else
426 if( name == "text_global_color_over" )
428 bool b;
429 if( fromString( value, b ) )
430 _TextModulateGlobalColorOver = b;
431 return;
433 else
434 if( name == "force_text_over" )
436 bool b;
437 if( fromString( value, b ) )
438 _ForceTextOver = b;
439 return;
441 else
442 if( name == "text_header_color" )
444 bool b;
445 if( fromString( value, b ) )
446 _TextHeaderColor = b;
447 return;
449 else
450 CCtrlBaseButton::setProperty( name, value );
454 xmlNodePtr CCtrlTextButton::serialize( xmlNodePtr parentNode, const char *type ) const
456 xmlNodePtr node = CCtrlBaseButton::serialize( parentNode, type );
457 if( node == NULL )
458 return NULL;
460 xmlSetProp( node, BAD_CAST "type", BAD_CAST "text_button" );
462 std::string tex;
463 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdNormal[ 0 ] );
464 std::string::size_type i = tex.rfind( "_l.tga" );
465 if( i != std::string::npos )
466 tex = tex.substr( 0, i );
468 xmlNewProp( node, BAD_CAST "tx_normal", BAD_CAST tex.c_str() );
469 tex.clear();
471 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdPushed[ 0 ] );
472 i = tex.rfind( "_l.tga" );
473 if( i != std::string::npos )
474 tex = tex.substr( 0, i );
476 xmlNewProp( node, BAD_CAST "tx_pushed", BAD_CAST tex.c_str() );
477 tex.clear();
480 tex = CViewRenderer::getInstance()->getTextureNameFromId( _TextureIdOver[ 0 ] );
481 i = tex.rfind( "_l.tga" );
482 if( i != std::string::npos )
483 tex = tex.substr( 0, i );
485 xmlNewProp( node, BAD_CAST "tx_over", BAD_CAST tex.c_str() );
486 tex.clear();
488 xmlNewProp( node, BAD_CAST "wmargin", BAD_CAST toString( _WMargin ).c_str() );
489 xmlNewProp( node, BAD_CAST "wmin", BAD_CAST toString( _WMin ).c_str() );
490 xmlNewProp( node, BAD_CAST "hmin", BAD_CAST toString( _HMin ).c_str() );
491 xmlNewProp( node, BAD_CAST "hardtext", BAD_CAST _ViewText->getText().c_str() );
492 xmlNewProp( node, BAD_CAST "text_y", BAD_CAST toString( _TextY ).c_str() );
493 xmlNewProp( node, BAD_CAST "text_x", BAD_CAST toString( _TextX ).c_str() );
494 xmlNewProp( node, BAD_CAST "text_underlined", BAD_CAST toString( _ViewText->getUnderlined() ).c_str() );
496 tex = CInterfaceElement::HotSpotCoupleToString( _TextParentPosRef, _TextPosRef );
497 xmlNewProp( node, BAD_CAST "text_posref", BAD_CAST tex.c_str() );
499 xmlNewProp( node, BAD_CAST "text_color_normal", BAD_CAST toString( _TextColorNormal ).c_str() );
500 xmlNewProp( node, BAD_CAST "text_color_pushed", BAD_CAST toString( _TextColorPushed ).c_str() );
501 xmlNewProp( node, BAD_CAST "text_color_over", BAD_CAST toString( _TextColorOver ).c_str() );
503 xmlNewProp( node, BAD_CAST "text_shadow_color_normal", BAD_CAST toString( _TextShadowColorNormal ).c_str() );
504 xmlNewProp( node, BAD_CAST "text_shadow_color_pushed", BAD_CAST toString( _TextShadowColorPushed ).c_str() );
505 xmlNewProp( node, BAD_CAST "text_shadow_color_over", BAD_CAST toString( _TextShadowColorOver ).c_str() );
507 xmlNewProp( node, BAD_CAST "text_global_color_normal",
508 BAD_CAST toString( _TextModulateGlobalColorNormal ).c_str() );
510 xmlNewProp( node, BAD_CAST "text_global_color_pushed",
511 BAD_CAST toString( _TextModulateGlobalColorPushed ).c_str() );
513 xmlNewProp( node, BAD_CAST "text_global_color_over",
514 BAD_CAST toString( _TextModulateGlobalColorOver ).c_str() );
516 xmlNewProp( node, BAD_CAST "force_text_over", BAD_CAST toString( _ForceTextOver ).c_str() );
517 xmlNewProp( node, BAD_CAST "text_header_color", BAD_CAST toString( _TextHeaderColor ).c_str() );
519 if( _ViewText != NULL )
520 _ViewText->serializeTextOptions( node );
522 return node;
525 // ***************************************************************************
526 bool CCtrlTextButton::parse(xmlNodePtr cur, CInterfaceGroup * parentGroup)
528 CXMLAutoPtr prop;
529 CViewRenderer &rVR = *CViewRenderer::getInstance();
531 //try to get props that can be inherited from groups
532 //if a property is not defined, try to find it in the parent group.
533 //if it is undefined, set it to zero
534 if (! CCtrlBaseButton::parse(cur,parentGroup) )
536 string tmp = "cannot parse view:"+getId()+", parent:"+parentGroup->getId();
537 nlinfo(tmp.c_str());
538 return false;
541 // *** Read Textures.
542 prop = (char*) xmlGetProp( cur, (xmlChar*)"tx_normal" );
543 if (prop)
545 string TxName = toLowerAscii(std::string((const char *) prop));
546 _TextureIdNormal[0].setTexture((TxName+"_l.tga").c_str());
547 _TextureIdNormal[1].setTexture((TxName+"_m.tga").c_str());
548 _TextureIdNormal[2].setTexture((TxName+"_r.tga").c_str());
551 prop = (char*) xmlGetProp( cur, (xmlChar*)"tx_pushed" );
552 if (prop)
554 string TxName = toLowerAscii(std::string((const char *) prop));
555 _TextureIdPushed[0].setTexture((TxName+"_l.tga").c_str());
556 _TextureIdPushed[1].setTexture((TxName+"_m.tga").c_str());
557 _TextureIdPushed[2].setTexture((TxName+"_r.tga").c_str());
560 prop = (char*) xmlGetProp( cur, (xmlChar*)"tx_over" );
561 if (prop)
563 string TxName = toLowerAscii(std::string((const char *) prop));
564 _TextureIdOver[0].setTexture((TxName+"_l.tga").c_str());
565 _TextureIdOver[1].setTexture((TxName+"_m.tga").c_str());
566 _TextureIdOver[2].setTexture((TxName+"_r.tga").c_str());
569 // Compute Bmp Sizes
570 nlctassert(NumTexture==3);
571 rVR.getTextureSizeFromId(_TextureIdNormal[0], _BmpLeftW, _BmpH);
572 rVR.getTextureSizeFromId(_TextureIdNormal[1], _BmpMiddleW, _BmpH);
573 rVR.getTextureSizeFromId(_TextureIdNormal[2], _BmpRightW, _BmpH);
576 // *** Create the ViewText for draw, and set text
577 // ViewTextId or standard view?
578 CXMLAutoPtr ptr((const char*)xmlGetProp(cur,(xmlChar*)"textid"));
579 _IsViewTextId= (bool)ptr;
580 if(_IsViewTextId)
581 _ViewText= new CViewTextID(CViewBase::TCtorParam());
582 else
583 _ViewText= new CViewText(CViewBase::TCtorParam());
584 _ViewText->setId(_Id+"_text");
585 _ViewText->setSerializable( false );
586 _ViewText->parseTextOptions(cur);
587 _ViewText->setAvoidResizeParent(avoidResizeParent());
588 if(_IsViewTextId)
589 ((CViewTextID*)_ViewText)->parseTextIdOptions(cur);
590 // Same RenderLayer as us.
591 _ViewText->setRenderLayer(getRenderLayer());
592 _ViewText->setParentElm(this);
593 // Parse the hardText (if not text id)
594 if(!_IsViewTextId)
596 prop = (char*) xmlGetProp( cur, (xmlChar*)"hardtext" );
597 if (prop)
599 const char *propPtr = prop;
600 _ViewText->setTextLocalized(propPtr, true);
604 // wmargin
605 _WMargin= 0;
606 prop = (char*) xmlGetProp( cur, (xmlChar*)"wmargin" );
607 if (prop)
609 fromString((const char *) prop, _WMargin);
612 // wmin
613 _WMin= 0;
614 prop = (char*) xmlGetProp( cur, (xmlChar*)"wmin" );
615 if (prop)
617 fromString((const char *) prop, _WMin);
619 // _WMin is at least the size of All W Bitmaps
620 _WMin= max(_WMin, _BmpLeftW + _BmpMiddleW + _BmpRightW);
622 // hmin
623 _HMin= 0;
624 prop = (char*) xmlGetProp( cur, (xmlChar*)"hmin" );
625 if (prop)
627 fromString((const char *) prop, _HMin);
629 _HMin= max(_HMin, _BmpH);
631 // TextY
632 _TextY= 0;
633 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_y" );
634 if (prop)
636 fromString((const char *) prop, _TextY);
638 _TextX = 0;
639 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_x" );
640 if (prop)
642 fromString((const char *) prop, _TextX);
645 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_underlined" );
646 if (prop)
648 _ViewText->setUnderlined(convertBool(prop));
651 prop = (char*) xmlGetProp( cur, (xmlChar*) "text_posref" );
652 _TextParentPosRef = Hotspot_MM;
653 _TextPosRef = Hotspot_MM;
654 if (prop)
656 char *seekPtr = prop.getDatas();
657 seekPtr = strtok(seekPtr," \t");
658 if (seekPtr == NULL)
660 // mean that there s a bad formated posref (missing space or tab)
661 nlwarning("bad 'text_pos_ref' formatting");
663 else
665 _TextParentPosRef = convertHotSpot (seekPtr);
666 seekPtr = strtok (seekPtr+1+strlen(seekPtr)," \t");
667 _TextPosRef = convertHotSpot (seekPtr);
672 // *** Read Text Colors
673 // get color normal
674 prop= (char*) xmlGetProp( cur, (xmlChar*)"text_color_normal" );
675 _TextColorNormal = CRGBA(255,255,255,255);
676 if (prop)
677 _TextColorNormal = convertColor (prop);
679 // Get ColorPushed
680 prop= (char*) xmlGetProp( cur, (xmlChar*)"text_color_pushed" );
681 _TextColorPushed = CRGBA(255,255,255,255);
682 if (prop)
683 _TextColorPushed = convertColor(prop);
685 // Get ColorOver
686 prop= (char*) xmlGetProp( cur, (xmlChar*)"text_color_over" );
687 _TextColorOver = CRGBA(255,255,255,255);
688 if (prop)
689 _TextColorOver = convertColor(prop);
692 // *** Read Text Shadow Colors
693 // get color normal
694 prop= (char*) xmlGetProp( cur, (xmlChar*)"text_shadow_color_normal" );
695 _TextShadowColorNormal = CRGBA(0,0,0,255);
696 if (prop)
697 _TextShadowColorNormal = convertColor (prop);
699 // Get ColorPushed
700 prop= (char*) xmlGetProp( cur, (xmlChar*)"text_shadow_color_pushed" );
701 _TextShadowColorPushed = CRGBA(0,0,0,255);
702 if (prop)
703 _TextShadowColorPushed = convertColor(prop);
705 // Get ColorOver
706 prop= (char*) xmlGetProp( cur, (xmlChar*)"text_shadow_color_over" );
707 _TextShadowColorOver = CRGBA(0,0,0,255);
708 if (prop)
709 _TextShadowColorOver = convertColor(prop);
711 // *** Read Text Global Color
712 // Default: take "global_color" param interface_element option.
713 _TextModulateGlobalColorNormal= _TextModulateGlobalColorPushed= _TextModulateGlobalColorOver= getModulateGlobalColor();
715 // Read special text global_color for each state
716 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_global_color_normal" );
717 if (prop) _TextModulateGlobalColorNormal= convertBool(prop);
718 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_global_color_pushed" );
719 if (prop) _TextModulateGlobalColorPushed= convertBool(prop);
720 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_global_color_over" );
721 if (prop) _TextModulateGlobalColorOver= convertBool(prop);
722 prop = (char*) xmlGetProp( cur, (xmlChar*)"force_text_over" );
723 if (prop) _ForceTextOver= convertBool(prop);
725 // read Text header color
726 prop = (char*) xmlGetProp( cur, (xmlChar*)"text_header_color" );
727 if (prop) _TextHeaderColor= convertBool(prop);
729 return true;
732 // ***************************************************************************
733 void CCtrlTextButton::setTexture(const std::string &l, const std::string &m, const std::string &r, bool updateHeight)
735 nlctassert(NumTexture==3);
736 _TextureIdNormal[0].setTexture(l.c_str());
737 _TextureIdNormal[1].setTexture(m.c_str());
738 _TextureIdNormal[2].setTexture(r.c_str());
740 sint32 newH;
742 // Compute Bmp Sizes
743 CViewRenderer &rVR = *CViewRenderer::getInstance();
744 rVR.getTextureSizeFromId(_TextureIdNormal[0], _BmpLeftW, newH);
745 rVR.getTextureSizeFromId(_TextureIdNormal[1], _BmpMiddleW, newH);
746 rVR.getTextureSizeFromId(_TextureIdNormal[2], _BmpRightW, newH);
748 if (updateHeight) _BmpH = newH;
751 // ***************************************************************************
752 void CCtrlTextButton::setTexturePushed(const std::string &l, const std::string &m, const std::string &r)
754 nlctassert(NumTexture==3);
755 _TextureIdPushed[0].setTexture(l.c_str());
756 _TextureIdPushed[1].setTexture(m.c_str());
757 _TextureIdPushed[2].setTexture(r.c_str());
760 // ***************************************************************************
761 void CCtrlTextButton::setTextureOver(const std::string &l, const std::string &m, const std::string &r)
763 nlctassert(NumTexture==3);
764 _TextureIdOver[0].setTexture(l.c_str());
765 _TextureIdOver[1].setTexture(m.c_str());
766 _TextureIdOver[2].setTexture(r.c_str());
769 // ***************************************************************************
770 void CCtrlTextButton::draw ()
772 CViewRenderer::CTextureId *pTxId = NULL;
773 CRGBA color;
775 CViewRenderer &rVR = *CViewRenderer::getInstance();
776 CRGBA globalColor= CWidgetManager::getInstance()->getGlobalColorForContent();
778 // *** Detect Over
779 bool lastOver = false;
780 updateOver(lastOver);
782 // *** Choose Button State
783 switch(_Type)
785 case ToggleButton:
787 if (_Pushed && !editorMode )
789 pTxId = _TextureIdPushed;
790 color = getCurrentColorPushed(globalColor);
792 else
794 pTxId = _TextureIdNormal;
795 color = getCurrentColorNormal(globalColor);
798 break;
799 case RadioButton:
801 // CViewPointer &rIP = *CInterfaceManager::getInstance()->getPointer();
802 // Init the radio button
803 initRBRef();
805 if (*_RBRef == this)
807 // if it is equal to the ref value, then the button must appear pushed
808 pTxId = _TextureIdPushed;
809 color = getCurrentColorPushed(globalColor);
811 else
813 if ((_Over) && (CWidgetManager::getInstance()->getCapturePointerLeft() == this) && !editorMode )
815 pTxId = _TextureIdPushed;
816 color = getCurrentColorPushed(globalColor);
818 else
820 pTxId = _TextureIdNormal;
821 color = getCurrentColorNormal(globalColor);
822 _Pushed = false;
826 break;
827 case PushButton:
829 if (_Over && (CWidgetManager::getInstance()->getCapturePointerLeft() == this) && !editorMode )
831 pTxId = _TextureIdPushed;
832 color = getCurrentColorPushed(globalColor);
834 else
836 pTxId = _TextureIdNormal;
837 color = getCurrentColorNormal(globalColor);
838 _Pushed = false;
841 break;
842 default:
843 break;
846 // *** Draw
847 color.A = (uint8)(((sint32)color.A*((sint32)globalColor.A+1))>>8);
849 // Fromzen ?
850 if (getFrozen() && getFrozenHalfTone())
851 color.A >>= 2;
853 sint32 x= _XReal;
854 sint32 y= _YReal;
855 sint32 txw, txh;
856 txw = _WReal - _BmpLeftW - _BmpRightW;
857 txh = _HReal;
858 nlctassert(NumTexture==3);
859 rVR.drawRotFlipBitmap ( _RenderLayer, x, y, _BmpLeftW, txh, 0, false, pTxId[0], color );
860 rVR.drawRotFlipBitmap ( _RenderLayer, x+_BmpLeftW, y, txw, txh, 0, false, pTxId[1], color );
861 rVR.drawRotFlipBitmap ( _RenderLayer, x+_BmpLeftW+txw, y, _BmpRightW, txh, 0, false, pTxId[2], color );
863 CCtrlBase *capturePointerLeft = CWidgetManager::getInstance()->getCapturePointerLeft();
865 // *** Draw Over
866 if( ( !editorMode && _Over && (_OverWhenPushed || !(_Pushed || capturePointerLeft == this ) ) )
869 if( !editorMode && (lastOver == false) && (_AHOnOver != NULL) )
870 CAHManager::getInstance()->runActionHandler (_AHOnOver, this, _AHOverParams);
872 // the pointer is over the button.
873 color= getCurrentColorOver(globalColor);
874 color.A = (uint8)(((sint32)color.A*((sint32)globalColor.A+1))>>8);
876 // Fromzen ?
877 if (getFrozen() && getFrozenHalfTone())
878 color.A >>= 2;
880 nlctassert(NumTexture==3);
881 pTxId= _TextureIdOver;
882 uint layerOffset = _ForceTextOver ? 0 : 1; // Must write Over On Top of the text ?
884 rVR.drawRotFlipBitmap ( _RenderLayer+layerOffset, x, y, _BmpLeftW, txh, 0, false, pTxId[0], color );
885 rVR.drawRotFlipBitmap ( _RenderLayer+layerOffset, x+_BmpLeftW, y, txw, txh, 0, false, pTxId[1], color );
886 rVR.drawRotFlipBitmap ( _RenderLayer+layerOffset, x+_BmpLeftW+txw, y, _BmpRightW, txh, 0, false, pTxId[2], color );
889 // *** Setup ViewText Color according to selected one (should be drawn after because of eltorder)
890 // update header color?
891 CRGBA viewTextColor;
892 if(_TextHeaderColor)
894 CInterfaceGroup *pIG= getRootWindow();
895 if(pIG->isGroupContainer())
897 CGroupContainerBase *pGC = static_cast<CGroupContainerBase*>(pIG);
898 viewTextColor= pGC->getDrawnHeaderColor();
901 // Setup ViewText color
902 if( _ViewText != NULL )
904 if ( pTxId==_TextureIdNormal || editorMode )
906 if(_TextHeaderColor) viewTextColor.A= _TextColorNormal.A;
907 else viewTextColor= _TextColorNormal;
908 _ViewText->setColor(viewTextColor);
909 _ViewText->setShadowColor(_TextShadowColorNormal);
910 _ViewText->setModulateGlobalColor(_TextModulateGlobalColorNormal);
912 else if ( pTxId==_TextureIdPushed )
914 if(_TextHeaderColor) viewTextColor.A= _TextColorPushed.A;
915 else viewTextColor= _TextColorPushed;
916 _ViewText->setColor(viewTextColor);
917 _ViewText->setShadowColor(_TextShadowColorPushed);
918 _ViewText->setModulateGlobalColor(_TextModulateGlobalColorPushed);
920 else if ( pTxId==_TextureIdOver )
922 if(_TextHeaderColor) viewTextColor.A= _TextColorOver.A;
923 else viewTextColor= _TextColorOver;
924 _ViewText->setColor(viewTextColor);
925 _ViewText->setShadowColor(_TextShadowColorOver);
926 _ViewText->setModulateGlobalColor(_TextModulateGlobalColorOver);
928 if(getFrozen() && getFrozenHalfTone())
929 _ViewText->setAlpha(_ViewText->getAlpha()>>2);
931 _ViewText->draw();
935 // ***************************************************************************
936 void CCtrlTextButton::checkCoords()
938 if( _ViewText != NULL )
939 _ViewText->checkCoords();
941 CCtrlBaseButton::checkCoords();
945 // ***************************************************************************
946 void CCtrlTextButton::updateCoords()
948 // Should have been setuped with addCtrl
949 nlassert(_Setuped);
951 if( _Name == "==MARKED==" )
952 bool marked = true;
954 // Compute Size according to bitmap and Text.
955 if (!(_SizeRef & 1))
957 _W= _ViewText->getW() + _WMargin + _TextX;
958 // Ensure as big as possible button
959 _W= max(_W, _WMin);
961 if (!(_SizeRef & 2))
963 _H= max(_BmpH, _ViewText->getH());
964 _H= max(_H, _HMin);
967 CViewBase::updateCoords();
969 _ViewText->updateCoords();
972 // ***************************************************************************
973 sint32 CCtrlTextButton::getWMax() const
975 return max(_ViewText->getW(false) + _WMargin + _TextX, _WMin);
978 // ***************************************************************************
979 void CCtrlTextButton::setup()
981 _Setuped= true;
983 if( _ViewText == NULL )
985 CViewBase *v = CInterfaceFactory::createClass( "text" );
986 nlassert( v != NULL );
987 _ViewText = dynamic_cast< CViewText* >( v );
988 _ViewText->setId( _Id + "_text" );
989 _ViewText->setText( "text" );
990 _ViewText->setSerializable( false );
993 // setup the viewText and add to parent
994 _ViewText->setParentElm (this);
995 _ViewText->setParentPos (this);
996 _ViewText->setParentPosRef (_TextParentPosRef);
997 _ViewText->setPosRef (_TextPosRef);
998 _ViewText->setActive(_Active);
999 _ViewText->setX(_TextX);
1000 _ViewText->setY(_TextY);
1003 // ***************************************************************************
1004 void CCtrlTextButton::setTextX(sint32 x)
1006 _TextX = x;
1007 if (_ViewText) _ViewText->setX(_TextX);
1010 // ***************************************************************************
1011 sint32 CCtrlTextButton::getMaxUsedW() const
1013 return _W;
1016 // ***************************************************************************
1017 sint32 CCtrlTextButton::getMinUsedW() const
1019 return _W;
1022 // ***************************************************************************
1023 void CCtrlTextButton::setActive(bool state)
1025 _ViewText->setActive(state);
1026 CCtrlBaseButton::setActive(state);
1030 // ***************************************************************************
1031 void CCtrlTextButton::onAddToGroup()
1033 // Add the view if not done
1034 if(!_Setuped)
1035 setup();
1039 // ***************************************************************************
1040 void CCtrlTextButton::setText (const std::string &text)
1042 if (_ViewText && !_IsViewTextId)
1043 _ViewText->setText(text);
1046 // ***************************************************************************
1047 std::string CCtrlTextButton::getText () const
1049 if (_ViewText && !_IsViewTextId)
1050 return _ViewText->getText();
1051 return std::string();
1054 void CCtrlTextButton::setLocalize(bool localize)
1056 if (_ViewText && !_IsViewTextId)
1057 _ViewText->setLocalized(localize);
1060 bool CCtrlTextButton::isLocalized() const
1062 if (_ViewText && !_IsViewTextId)
1063 return _ViewText->isLocalized();
1064 return true;
1067 #ifdef RYZOM_LUA_UCSTRING
1068 // ***************************************************************************
1069 void CCtrlTextButton::setTextAsUtf16 (const ucstring &text)
1071 if (_ViewText && !_IsViewTextId)
1072 _ViewText->setText(text.toUtf8());
1075 // ***************************************************************************
1076 ucstring CCtrlTextButton::getTextAsUtf16 () const
1078 if (_ViewText && !_IsViewTextId)
1079 return CUtfStringView(_ViewText->getText()).toUtf16();
1080 return ucstring("");
1082 #endif
1084 // ***************************************************************************
1085 void CCtrlTextButton::setHardText (const std::string &text)
1087 if (_ViewText && !_IsViewTextId)
1088 _ViewText->setHardText(text);
1091 // ***************************************************************************
1092 string CCtrlTextButton::getHardText () const
1094 if (_ViewText && !_IsViewTextId)
1095 return _ViewText->getHardText();
1096 return string("");
1099 // ***************************************************************************
1101 CViewText* CCtrlTextButton::getViewText()
1103 return _ViewText;
1106 // ***************************************************************************
1108 void CCtrlTextButton::setTextureLua(const std::string &name)
1110 _TextureIdNormal[0].setTexture(std::string(name + "_l.tga").c_str());
1111 _TextureIdNormal[1].setTexture(std::string(name + "_m.tga").c_str());
1112 _TextureIdNormal[2].setTexture(std::string(name + "_r.tga").c_str());
1115 // ***************************************************************************
1117 void CCtrlTextButton::setTexturePushedLua(const std::string &name)
1119 _TextureIdPushed[0].setTexture(std::string(name + "_l.tga").c_str());
1120 _TextureIdPushed[1].setTexture(std::string(name + "_m.tga").c_str());
1121 _TextureIdPushed[2].setTexture(std::string(name + "_r.tga").c_str());
1124 // ***************************************************************************
1126 void CCtrlTextButton::setTextureOverLua(const std::string &name)
1128 _TextureIdOver[0].setTexture(std::string(name + "_l.tga").c_str());
1129 _TextureIdOver[1].setTexture(std::string(name + "_m.tga").c_str());
1130 _TextureIdOver[2].setTexture(std::string(name + "_r.tga").c_str());
1133 // ***************************************************************************
1135 std::string CCtrlTextButton::getTexture() const
1137 std::string tx = CViewRenderer::getInstance()->getTextureNameFromId(_TextureIdNormal[0]);
1138 std::string::size_type i = tx.rfind("_l.tga");
1139 if (i != std::string::npos)
1140 tx = tx.substr(0, i);
1142 return tx;
1145 // ***************************************************************************
1147 std::string CCtrlTextButton::getTexturePushed() const
1149 std::string tx = CViewRenderer::getInstance()->getTextureNameFromId(_TextureIdOver[0]);
1150 std::string::size_type i = tx.rfind("_l.tga");
1151 if (i != std::string::npos)
1152 tx = tx.substr(0, i);
1154 return tx;
1157 // ***************************************************************************
1159 std::string CCtrlTextButton::getTextureOver() const
1161 std::string tx = CViewRenderer::getInstance()->getTextureNameFromId(_TextureIdPushed[0]);
1162 std::string::size_type i = tx.rfind("_l.tga");
1163 if (i != std::string::npos)
1164 tx = tx.substr(0, i);
1166 return tx;
1169 // ***************************************************************************
1171 int CCtrlTextButton::luaGetViewText(CLuaState &ls)
1173 const char *funcName = "getViewText";
1174 CLuaIHM::checkArgCount(ls, funcName, 0);
1175 CLuaIHM::pushUIOnStack(ls, getViewText());
1176 return 1;
1179 // ***************************************************************************
1180 void CCtrlTextButton::onRemoved()
1184 void CCtrlTextButton::onWidgetDeleted( CInterfaceElement *e )
1188 void CCtrlTextButton::moveBy( sint32 x, sint32 y )
1190 CInterfaceElement::moveBy( x, y );
1192 if( _ViewText != NULL )
1193 _ViewText->updateCoords();