Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / nel / src / gui / view_bitmap.cpp
blob1896ced403c1f1f1893f410d87ef2ecb73e3a13a
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2020 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/view_bitmap.h"
23 #include "nel/misc/xml_auto_ptr.h"
24 #include "nel/gui/widget_manager.h"
25 #include "nel/gui/interface_group.h"
26 #include "nel/gui/group_container_base.h"
27 #include "nel/gui/group_html.h"
29 using namespace std;
30 using namespace NLMISC;
31 using namespace NL3D;
33 #ifdef DEBUG_NEW
34 #define new DEBUG_NEW
35 #endif
37 NLMISC_REGISTER_OBJECT(CViewBase, CViewBitmap, std::string, "bitmap");
38 REGISTER_UI_CLASS(CViewBitmap)
40 namespace NLGUI
43 CViewBitmap::~CViewBitmap()
45 if (_HtmlDownload)
47 CGroupHTML *groupHtml = dynamic_cast<CGroupHTML*>(CWidgetManager::getInstance()->getElementFromId("ui:interface:webig:content:html"));
48 if (groupHtml) {
49 groupHtml->removeImageDownload(_HtmlDownload, dynamic_cast<CViewBase*>(this));
50 _HtmlDownload = NULL;
55 std::string CViewBitmap::getProperty( const std::string &name ) const
57 if( name == "color" )
59 return toString( _Color );
61 else
62 if( name == "txtoffsetx" )
64 return toString( _TxtOffsetX );
66 else
67 if( name == "txtoffsety" )
69 return toString( _TxtOffsetY );
71 else
72 if( name == "txtwidth" )
74 return toString( _TxtWidth );
76 else
77 if( name == "txtheight" )
79 return toString( _TxtHeight );
81 else
82 if( name == "texture" )
84 return getTexture();
86 else
87 if( name == "scale" )
89 return toString( _Scale );
91 else
92 if( name == "rot" )
94 return toString( _Rot );
96 else
97 if( name == "flip" )
99 return toString( _Flip );
101 else
102 if( name == "tile" )
104 return toString( _Tile );
106 else
107 if( name == "align" )
109 std::string align;
111 if( ( _Align & 1 ) != 0 )
112 align += "R";
113 else
114 align += "L";
116 if( ( _Align & 2 ) != 0 )
117 align += "T";
118 else
119 align += "B";
121 return align;
123 else
124 if( name == "inherit_gc_alpha" )
126 return toString( _InheritGCAlpha );
128 else
129 return CViewBase::getProperty( name );
132 void CViewBitmap::setProperty( const std::string &name, const std::string &value )
134 if( name == "color" )
136 CRGBA c;
137 if( fromString( value, c ) )
138 _Color = c;
139 return;
141 else
142 if( name == "txtoffsetx" )
144 sint32 i;
145 if( fromString( value, i ) )
146 _TxtOffsetX = i;
147 return;
149 else
150 if( name == "txtoffsety" )
152 sint32 i;
153 if( fromString( value, i ) )
154 _TxtOffsetY = i;
155 return;
157 else
158 if( name == "txtwidth" )
160 sint32 i;
161 if( fromString( value, i ) )
162 _TxtWidth = i;
163 return;
165 else
166 if( name == "txtheight" )
168 sint32 i;
169 if( fromString( value, i ) )
170 _TxtHeight = i;
171 return;
173 else
174 if( name == "texture" )
176 setTexture( value );
177 return;
179 else
180 if( name == "scale" )
182 bool b;
183 if( fromString( value, b ) )
184 _Scale = b;
185 return;
187 else
188 if( name == "rot" )
190 sint32 i;
191 if( fromString( value, i ) )
192 _Rot = i;
193 return;
195 else
196 if( name == "flip" )
198 bool b;
199 if( fromString( value, b ) )
200 _Flip = b;
201 return;
203 else
204 if( name == "tile" )
206 bool b;
207 if( fromString( value, b ) )
208 _Tile = b;
209 return;
211 else
212 if( name == "align" )
214 std::string::size_type i;
215 for( i = 0; i < value.size(); i++ )
217 const char c = value[ i ];
219 switch( c )
221 case 'L':
222 _Align &= ~1;
223 break;
225 case 'R':
226 _Align |= 1;
227 break;
229 case 'B':
230 _Align &= ~2;
231 break;
233 case 'T':
234 _Align |= 2;
235 break;
238 return;
240 else
241 if( name == "inherit_gc_alpha" )
243 bool b;
244 if( fromString( value, b ) )
245 _InheritGCAlpha = b;
246 return;
248 else
249 CViewBase::setProperty( name, value );
253 xmlNodePtr CViewBitmap::serialize( xmlNodePtr parentNode, const char *type ) const
255 xmlNodePtr node = CViewBase::serialize( parentNode, type );
256 if( node == NULL )
257 return NULL;
259 xmlSetProp( node, BAD_CAST "type", BAD_CAST "bitmap" );
260 xmlSetProp( node, BAD_CAST "color", BAD_CAST toString( _Color ).c_str() );
261 xmlSetProp( node, BAD_CAST "txtoffsetx", BAD_CAST toString( _TxtOffsetX ).c_str() );
262 xmlSetProp( node, BAD_CAST "txtoffsety", BAD_CAST toString( _TxtOffsetY ).c_str() );
263 xmlSetProp( node, BAD_CAST "txtwidth", BAD_CAST toString( _TxtWidth ).c_str() );
264 xmlSetProp( node, BAD_CAST "txtheight", BAD_CAST toString( _TxtHeight ).c_str() );
265 xmlSetProp( node, BAD_CAST "texture", BAD_CAST getTexture().c_str() );
266 xmlSetProp( node, BAD_CAST "scale", BAD_CAST toString( _Scale ).c_str() );
267 xmlSetProp( node, BAD_CAST "rot", BAD_CAST toString( _Rot ).c_str() );
268 xmlSetProp( node, BAD_CAST "flip", BAD_CAST toString( _Flip ).c_str() );
269 xmlSetProp( node, BAD_CAST "tile", BAD_CAST toString( _Tile ).c_str() );
270 xmlSetProp( node, BAD_CAST "inherit_gc_alpha", BAD_CAST toString( _InheritGCAlpha ).c_str() );
272 std::string align;
273 if( ( _Align & 1 ) != 0 )
274 align += "R";
275 else
276 align += "L";
278 if( ( _Align & 2 ) != 0 )
279 align += "T";
280 else
281 align += "B";
283 xmlSetProp( node, BAD_CAST "txtoffsetx", BAD_CAST align.c_str() );
285 return node;
288 // ----------------------------------------------------------------------------
290 bool CViewBitmap::parse(xmlNodePtr cur, CInterfaceGroup * parentGroup)
292 CXMLAutoPtr prop;
294 //try to get props that can be inherited from groups
295 //if a property is not defined, try to find it in the parent group.
296 //if it is undefined, set it to zero
297 if (! CViewBase::parse(cur,parentGroup) )
299 string tmp = string("cannot parse view:")+getId()+", parent:"+parentGroup->getId();
300 nlinfo (tmp.c_str());
301 return false;
304 //try to get the NEEDED specific props
305 prop= (char*) xmlGetProp( cur, (xmlChar*)"color" );
306 _Color = CRGBA(255,255,255,255);
307 if (prop)
308 _Color = convertColor (prop);
310 prop = (char*) xmlGetProp( cur, (xmlChar*)"txtoffsetx" );
311 _TxtOffsetX = 0;
312 if (prop) fromString((const char*)prop, _TxtOffsetX);
314 prop = (char*) xmlGetProp( cur, (xmlChar*)"txtoffsety" );
315 _TxtOffsetY = 0;
316 if (prop) fromString((const char*)prop, _TxtOffsetY);
318 prop = (char*) xmlGetProp( cur, (xmlChar*)"txtwidth" );
319 _TxtWidth = -1;
320 if (prop) fromString((const char*)prop, _TxtWidth);
322 prop = (char*) xmlGetProp( cur, (xmlChar*)"txtheight" );
323 _TxtHeight = -1;
324 if (prop) fromString((const char*)prop, _TxtHeight);
326 prop = (char*) xmlGetProp( cur, (xmlChar*)"texture" );
327 if (prop)
329 string TxName = (const char *) prop;
330 setTexture (TxName);
331 //CInterfaceManager *pIM = CInterfaceManager::getInstance();
332 //CViewRenderer &rVR = *CViewRenderer::getInstance();
333 //_TextureId = rVR.getTextureIdFromName (TxName);
336 prop = (char*) xmlGetProp( cur, (xmlChar*)"scale" );
337 _Scale = false;
338 if (prop)
339 _Scale = convertBool(prop);
341 prop = (char*) xmlGetProp( cur, (xmlChar*)"rot" );
342 _Rot = 0;
343 if (prop)
344 fromString((const char*)prop, _Rot);
346 prop = (char*) xmlGetProp( cur, (xmlChar*)"flip" );
347 _Flip = false;
348 if (prop)
349 _Flip = convertBool(prop);
351 prop = (char*) xmlGetProp( cur, (xmlChar*)"tile" );
352 _Tile = false;
353 if (prop)
354 _Tile = convertBool(prop);
356 prop = (char*) xmlGetProp (cur, (xmlChar*)"align");
357 _Align = 0;
358 if (prop)
360 const char *seekPtr = prop.getDatas();
361 while (*seekPtr != 0)
363 if ((*seekPtr=='l')||(*seekPtr=='L'))
365 _Align &= ~1;
367 if ((*seekPtr=='r')||(*seekPtr=='R'))
369 _Align |= 1;
371 if ((*seekPtr=='b')||(*seekPtr=='B'))
373 _Align &= ~2;
375 if ((*seekPtr=='t')||(*seekPtr=='T'))
377 _Align |= 2;
379 ++seekPtr;
383 _InheritGCAlpha = false;
384 prop = (char*) xmlGetProp( cur, (xmlChar*)"inherit_gc_alpha" );
385 if (prop)
387 _InheritGCAlpha = convertBool(prop);
390 return true;
393 // ----------------------------------------------------------------------------
394 void CViewBitmap::draw ()
396 CViewRenderer &rVR = *CViewRenderer::getInstance();
398 CRGBA col;
399 if(getModulateGlobalColor())
401 col.modulateFromColor (_Color, CWidgetManager::getInstance()->getGlobalColorForContent());
403 else
405 col= _Color;
406 col.A = (uint8)(((sint32)col.A*((sint32)CWidgetManager::getInstance()->getGlobalColorForContent().A+1))>>8);
409 if (_InheritGCAlpha)
411 // search a parent container
412 CInterfaceGroup *gr = getParent();
413 while (gr)
415 if (gr->isGroupContainer())
417 CGroupContainerBase *gc = static_cast<CGroupContainerBase*>(gr);
418 col.A = (uint8)(((sint32)col.A*((sint32)gc->getCurrentContainerAlpha()+1))>>8);
419 break;
421 gr = gr->getParent();
425 if (_Scale && !_Tile)
427 rVR.drawRotFlipBitmap (_RenderLayer, _XReal, _YReal,
428 _WReal, _HReal,
429 (uint8)_Rot, _Flip,
430 _TextureId,
431 col );
433 else
435 if (!_Tile)
437 rVR.draw11RotFlipBitmap (_RenderLayer, _XReal, _YReal,
438 (uint8)_Rot, _Flip,
439 _TextureId,
440 col);
442 else
444 rVR.drawRotFlipBitmapTiled(_RenderLayer, _XReal, _YReal,
445 _WReal, _HReal,
446 (uint8)_Rot, _Flip,
447 _TextureId,
448 _Align,
449 col);
454 // ----------------------------------------------------------------------------
455 void CViewBitmap::updateCoords()
457 if (!_Scale)
459 CViewRenderer &rVR = *CViewRenderer::getInstance();
460 sint32 txw, txh;
461 rVR.getTextureSizeFromId (_TextureId, txw, txh);
462 _W = txw;
463 _H = txh;
465 CViewBase::updateCoords();
468 // ----------------------------------------------------------------------------
469 void CViewBitmap::setTexture(const std::string & TxName)
471 if (TxName.find("://") != string::npos || TxName.find("//") == 0)
473 CGroupHTML *groupHtml = dynamic_cast<CGroupHTML*>(CWidgetManager::getInstance()->getElementFromId("ui:interface:webig:content:html"));
474 if (groupHtml) {
475 string localname = groupHtml->localImageName(TxName);
476 if (!CFile::fileExists(localname))
477 localname = "web_del.tga";
478 _TextureId.setTexture (localname.c_str(), _TxtOffsetX, _TxtOffsetY, _TxtWidth, _TxtHeight, false);
479 _HtmlDownload = groupHtml->addImageDownload(TxName, dynamic_cast<CViewBase*>(this));
482 else
484 if (_HtmlDownload)
486 CGroupHTML *groupHtml = dynamic_cast<CGroupHTML*>(CWidgetManager::getInstance()->getElementFromId("ui:interface:webig:content:html"));
487 if (groupHtml)
488 groupHtml->removeImageDownload(_HtmlDownload, dynamic_cast<CViewBase*>(this));
489 _HtmlDownload = NULL;
491 _TextureId.setTexture (TxName.c_str (), _TxtOffsetX, _TxtOffsetY, _TxtWidth, _TxtHeight, false);
495 // ----------------------------------------------------------------------------
496 std::string CViewBitmap::getTexture () const
498 CViewRenderer &rVR = *CViewRenderer::getInstance();
499 return rVR.getTextureNameFromId (_TextureId);
502 // ***************************************************************************
503 void CViewBitmap::fitTexture()
505 CViewRenderer &rVR = *CViewRenderer::getInstance();
506 sint32 w, h;
507 rVR.getTextureSizeFromId(_TextureId, w, h);
508 setW(w);
509 setH(h);
512 // ***************************************************************************
513 void CViewBitmap::setColorAsString(const std::string & col)
515 _Color = convertColor (col.c_str());
518 // ***************************************************************************
519 std::string CViewBitmap::getColorAsString() const
521 return NLMISC::toString(_Color.R) + " " + NLMISC::toString(_Color.G) + " " + NLMISC::toString(_Color.B) + " " + NLMISC::toString(_Color.A);
524 // ***************************************************************************
525 void CViewBitmap::setColorAsInt(sint32 col)
527 _Color.setPacked(col);
530 // ***************************************************************************
531 sint32 CViewBitmap::getColorAsInt() const
533 return _Color.getPacked();
536 // ***************************************************************************
537 void CViewBitmap::setColorRGBA(NLMISC::CRGBA col)
539 _Color = col;
542 // ***************************************************************************
543 NLMISC::CRGBA CViewBitmap::getColorRGBA() const
545 return _Color;
548 // ***************************************************************************
549 sint32 CViewBitmap::getMaxUsedW() const
551 if (_Scale)
552 return _WReal;
554 sint32 txw, txh;
555 CViewRenderer &rVR = *CViewRenderer::getInstance();
556 rVR.getTextureSizeFromId (_TextureId, txw, txh);
557 return txw;
560 // ***************************************************************************
561 sint32 CViewBitmap::getMinUsedW() const
563 return getMaxUsedW();
566 // ***************************************************************************
567 void CViewBitmap::serial(NLMISC::IStream &f)
569 CViewBase::serial(f);
570 f.serial(_TextureId);
571 f.serial(_Color);
572 f.serial(_Rot);
573 f.serialEnum(_Align);
574 f.serialEnum(_Type);
575 nlSerialBitBool(f, _Scale);
576 nlSerialBitBool(f, _Flip);
577 nlSerialBitBool(f, _Tile);
578 nlSerialBitBool(f, _InheritGCAlpha);
579 f.serial(_TxtOffsetX);
580 f.serial(_TxtOffsetY);
581 f.serial(_TxtWidth);
582 f.serial(_TxtHeight);
587 // ***************************************************************************