1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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/>.
23 #include "nel/gui/ctrl_col_pick.h"
24 #include "nel/misc/xml_auto_ptr.h"
25 #include "nel/gui/view_renderer.h"
26 #include "nel/gui/action_handler.h"
27 #include "nel/gui/widget_manager.h"
28 #include "nel/gui/interface_group.h"
31 using namespace NLMISC
;
38 NLMISC_REGISTER_OBJECT(CViewBase
, CCtrlColPick
, std::string
, "colpick");
43 // ------------------------------------------------------------------------------------------------
44 CCtrlColPick::CCtrlColPick(const TCtorParam
¶m
)
51 // ------------------------------------------------------------------------------------------------
52 CCtrlColPick::~CCtrlColPick()
54 // Texture has been created ?
57 CViewRenderer
&rVR
= *CViewRenderer::getInstance();
58 rVR
.deleteTexture (_Texture
);
62 std::string
CCtrlColPick::getProperty( const std::string
&name
) const
64 if( name
== "texture" )
66 return CViewRenderer::getInstance()->getTextureNameFromId( _Texture
);
69 if( name
== "onchange" )
74 if( name
== "onchange_params" )
76 return _AHOnChangeParams
;
79 if( name
== "dbcolr" )
81 if( _ColSelR
.getNodePtr() != NULL
)
82 return _ColSelR
.getNodePtr()->getFullName();
87 if( name
== "dbcolg" )
89 if( _ColSelG
.getNodePtr() != NULL
)
90 return _ColSelG
.getNodePtr()->getFullName();
95 if( name
== "dbcolb" )
97 if( _ColSelB
.getNodePtr() != NULL
)
98 return _ColSelB
.getNodePtr()->getFullName();
103 if( name
== "dbcola" )
105 if( _ColSelA
.getNodePtr() != NULL
)
106 return _ColSelA
.getNodePtr()->getFullName();
111 return CCtrlBase::getProperty( name
);
114 void CCtrlColPick::setProperty( const std::string
&name
, const std::string
&value
)
116 if( name
== "texture" )
118 CViewRenderer::getInstance()->deleteTexture( _Texture
);
119 _Texture
= CViewRenderer::getInstance()->createTexture( value
, 0, 0, 256, 64, false, false );
123 if( name
== "onchange" )
128 if( name
== "onchange_params" )
130 _AHOnChangeParams
= value
;
134 if( name
== "dbcolr" )
136 _ColSelR
.link( value
.c_str() );
140 if( name
== "dbcolg" )
142 _ColSelG
.link( value
.c_str() );
146 if( name
== "dbcolb" )
148 _ColSelB
.link( value
.c_str() );
152 if( name
== "dbcola" )
154 _ColSelA
.link( value
.c_str() );
158 CCtrlBase::setProperty( name
, value
);
161 xmlNodePtr
CCtrlColPick::serialize( xmlNodePtr parentNode
, const char *type
) const
163 xmlNodePtr node
= CCtrlBase::serialize( parentNode
, type
);
167 xmlSetProp( node
, BAD_CAST
"type", BAD_CAST
"colpick" );
169 xmlSetProp( node
, BAD_CAST
"texture",
170 BAD_CAST
CViewRenderer::getInstance()->getTextureNameFromId( _Texture
).c_str() );
172 xmlSetProp( node
, BAD_CAST
"onchange", BAD_CAST _AHOnChange
.c_str() );
173 xmlSetProp( node
, BAD_CAST
"onchange_params", BAD_CAST _AHOnChangeParams
.c_str() );
177 if( _ColSelR
.getNodePtr() != NULL
)
178 s
= _ColSelR
.getNodePtr()->getFullName();
181 xmlSetProp( node
, BAD_CAST
"dbcolr", BAD_CAST s
.c_str() );
183 if( _ColSelG
.getNodePtr() != NULL
)
184 s
= _ColSelG
.getNodePtr()->getFullName();
187 xmlSetProp( node
, BAD_CAST
"dbcolg", BAD_CAST s
.c_str() );
189 if( _ColSelB
.getNodePtr() != NULL
)
190 s
= _ColSelB
.getNodePtr()->getFullName();
193 xmlSetProp( node
, BAD_CAST
"dbcolb", BAD_CAST s
.c_str() );
195 if( _ColSelA
.getNodePtr() != NULL
)
196 s
= _ColSelA
.getNodePtr()->getFullName();
199 xmlSetProp( node
, BAD_CAST
"dbcola", BAD_CAST s
.c_str() );
204 // ------------------------------------------------------------------------------------------------
205 bool CCtrlColPick::parse(xmlNodePtr node
, CInterfaceGroup
* parentGroup
)
207 if (!CCtrlBase::parse(node
, parentGroup
))
212 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"texture" );
213 CViewRenderer
&rVR
= *CViewRenderer::getInstance();
216 string sTmp
= NLMISC::toLowerAscii((const char*)prop
);
217 _Texture
= rVR
.createTexture (sTmp
, 0, 0, 256, 64, false, false);
220 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"onchange" );
221 if (prop
) _AHOnChange
= NLMISC::toLowerAscii((const char*)prop
);
222 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"onchange_params" );
223 if (prop
) _AHOnChangeParams
= string((const char*)prop
);
225 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"dbcolr" );
226 if (prop
) _ColSelR
.link(prop
);
227 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"dbcolg" );
228 if (prop
) _ColSelG
.link(prop
);
229 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"dbcolb" );
230 if (prop
) _ColSelB
.link(prop
);
231 prop
= (char*) xmlGetProp( node
, (xmlChar
*)"dbcola" );
232 if (prop
) _ColSelA
.link(prop
);
237 // ------------------------------------------------------------------------------------------------
238 void CCtrlColPick::updateCoords()
240 CViewRenderer
&rVR
= *CViewRenderer::getInstance();
242 rVR
.getTextureSizeFromId (_Texture
, txw
, txh
);
245 CCtrlBase::updateCoords();
248 // ------------------------------------------------------------------------------------------------
249 void CCtrlColPick::draw()
251 CViewRenderer
&rVR
= *CViewRenderer::getInstance();
253 CRGBA col
= CRGBA(255,255,255,(uint8
)CWidgetManager::getInstance()->getGlobalColor().A
);
255 rVR
.drawRotFlipBitmap (_RenderLayer
, _XReal
, _YReal
,
262 // ------------------------------------------------------------------------------------------------
263 bool CCtrlColPick::handleEvent (const NLGUI::CEventDescriptor
&event
)
265 if (CCtrlBase::handleEvent(event
)) return true;
268 if (event
.getType() == NLGUI::CEventDescriptor::mouse
)
270 const NLGUI::CEventDescriptorMouse
&eventDesc
= (const NLGUI::CEventDescriptorMouse
&)event
;
271 if ((CWidgetManager::getInstance()->getCapturePointerLeft() != this) &&
272 (!((eventDesc
.getX() >= _XReal
) &&
273 (eventDesc
.getX() < (_XReal
+ _WReal
))&&
274 (eventDesc
.getY() > _YReal
) &&
275 (eventDesc
.getY() <= (_YReal
+ _HReal
)))))
278 if (eventDesc
.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouseleftdown
)
281 selectColor(eventDesc
.getX()-_XReal
, eventDesc
.getY()-_YReal
);
284 if (eventDesc
.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouseleftup
)
289 if (eventDesc
.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mousemove
)
293 selectColor(eventDesc
.getX()-_XReal
, eventDesc
.getY()-_YReal
);
301 // ------------------------------------------------------------------------------------------------
302 string
CCtrlColPick::getColor () const
304 return NLMISC::toString(_ColorSelect
.R
) + " " + NLMISC::toString(_ColorSelect
.G
) + " " + NLMISC::toString(_ColorSelect
.B
) + " " + NLMISC::toString(_ColorSelect
.A
);
307 // ------------------------------------------------------------------------------------------------
308 void CCtrlColPick::setColor (const string
&col
)
310 _ColorSelect
= convertColor (col
.c_str());
313 // ------------------------------------------------------------------------------------------------
314 string
CCtrlColPick::getColorOver () const
316 return NLMISC::toString(_ColorOver
.R
) + " " + NLMISC::toString(_ColorOver
.G
) + " " + NLMISC::toString(_ColorOver
.B
) + " " + NLMISC::toString(_ColorOver
.A
);
319 // ------------------------------------------------------------------------------------------------
320 void CCtrlColPick::setColorOver (const string
&col
)
322 _ColorOver
= convertColor (col
.c_str());
325 // ------------------------------------------------------------------------------------------------
326 void CCtrlColPick::selectColor (sint32 x
, sint32 y
)
329 _ColorSelect
= getColor (x
, y
);
331 if (_ColSelR
.getNodePtr() != NULL
)
332 _ColSelR
.setSInt32(_ColorSelect
.R
);
333 if (_ColSelG
.getNodePtr() != NULL
)
334 _ColSelG
.setSInt32(_ColorSelect
.G
);
335 if (_ColSelB
.getNodePtr() != NULL
)
336 _ColSelB
.setSInt32(_ColorSelect
.B
);
337 if (_ColSelA
.getNodePtr() != NULL
)
338 _ColSelA
.setSInt32(_ColorSelect
.A
);
340 if (!_AHOnChange
.empty())
341 CAHManager::getInstance()->runActionHandler(_AHOnChange
, this, _AHOnChangeParams
);
344 // ------------------------------------------------------------------------------------------------
345 CRGBA
CCtrlColPick::getColor (sint32 x
, sint32 y
)
347 CViewRenderer
&rVR
= *CViewRenderer::getInstance();
351 if (x
>= _WReal
) x
= _WReal
-1;
352 if (y
>= _HReal
) y
= _HReal
-1;
353 return rVR
.getTextureColor (_Texture
, x
, y
);