Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / gui / ctrl_col_pick.cpp
blob7daeb159108b81c609d60cc00cee63444a355074
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 // 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_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;
32 using namespace std;
34 #ifdef DEBUG_NEW
35 #define new DEBUG_NEW
36 #endif
38 NLMISC_REGISTER_OBJECT(CViewBase, CCtrlColPick, std::string, "colpick");
40 namespace NLGUI
43 // ------------------------------------------------------------------------------------------------
44 CCtrlColPick::CCtrlColPick(const TCtorParam &param)
45 :CCtrlBase(param)
47 _MouseDown = false;
48 _Texture = -2;
51 // ------------------------------------------------------------------------------------------------
52 CCtrlColPick::~CCtrlColPick()
54 // Texture has been created ?
55 if (_Texture>=0)
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 );
68 else
69 if( name == "onchange" )
71 return _AHOnChange;
73 else
74 if( name == "onchange_params" )
76 return _AHOnChangeParams;
78 else
79 if( name == "dbcolr" )
81 if( _ColSelR.getNodePtr() != NULL )
82 return _ColSelR.getNodePtr()->getFullName();
83 else
84 return "";
86 else
87 if( name == "dbcolg" )
89 if( _ColSelG.getNodePtr() != NULL )
90 return _ColSelG.getNodePtr()->getFullName();
91 else
92 return "";
94 else
95 if( name == "dbcolb" )
97 if( _ColSelB.getNodePtr() != NULL )
98 return _ColSelB.getNodePtr()->getFullName();
99 else
100 return "";
102 else
103 if( name == "dbcola" )
105 if( _ColSelA.getNodePtr() != NULL )
106 return _ColSelA.getNodePtr()->getFullName();
107 else
108 return "";
110 else
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 );
120 return;
122 else
123 if( name == "onchange" )
125 _AHOnChange = value;
127 else
128 if( name == "onchange_params" )
130 _AHOnChangeParams = value;
131 return;
133 else
134 if( name == "dbcolr" )
136 _ColSelR.link( value.c_str() );
137 return;
139 else
140 if( name == "dbcolg" )
142 _ColSelG.link( value.c_str() );
143 return;
145 else
146 if( name == "dbcolb" )
148 _ColSelB.link( value.c_str() );
149 return;
151 else
152 if( name == "dbcola" )
154 _ColSelA.link( value.c_str() );
155 return;
157 else
158 CCtrlBase::setProperty( name, value );
161 xmlNodePtr CCtrlColPick::serialize( xmlNodePtr parentNode, const char *type ) const
163 xmlNodePtr node = CCtrlBase::serialize( parentNode, type );
164 if( node == NULL )
165 return NULL;
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() );
175 std::string s;
177 if( _ColSelR.getNodePtr() != NULL )
178 s = _ColSelR.getNodePtr()->getFullName();
179 else
180 s.clear();
181 xmlSetProp( node, BAD_CAST "dbcolr", BAD_CAST s.c_str() );
183 if( _ColSelG.getNodePtr() != NULL )
184 s = _ColSelG.getNodePtr()->getFullName();
185 else
186 s.clear();
187 xmlSetProp( node, BAD_CAST "dbcolg", BAD_CAST s.c_str() );
189 if( _ColSelB.getNodePtr() != NULL )
190 s = _ColSelB.getNodePtr()->getFullName();
191 else
192 s.clear();
193 xmlSetProp( node, BAD_CAST "dbcolb", BAD_CAST s.c_str() );
195 if( _ColSelA.getNodePtr() != NULL )
196 s = _ColSelA.getNodePtr()->getFullName();
197 else
198 s.clear();
199 xmlSetProp( node, BAD_CAST "dbcola", BAD_CAST s.c_str() );
201 return node;
204 // ------------------------------------------------------------------------------------------------
205 bool CCtrlColPick::parse(xmlNodePtr node, CInterfaceGroup * parentGroup)
207 if (!CCtrlBase::parse(node, parentGroup))
208 return false;
210 CXMLAutoPtr prop;
211 // Read textures
212 prop = (char*) xmlGetProp( node, (xmlChar*)"texture" );
213 CViewRenderer &rVR = *CViewRenderer::getInstance();
214 if(prop)
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);
234 return true;
237 // ------------------------------------------------------------------------------------------------
238 void CCtrlColPick::updateCoords()
240 CViewRenderer &rVR = *CViewRenderer::getInstance();
241 sint32 txw, txh;
242 rVR.getTextureSizeFromId (_Texture, txw, txh);
243 _W = txw;
244 _H = 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,
256 _WReal, _HReal,
257 0, false,
258 _Texture,
259 col );
262 // ------------------------------------------------------------------------------------------------
263 bool CCtrlColPick::handleEvent (const NLGUI::CEventDescriptor &event)
265 if (CCtrlBase::handleEvent(event)) return true;
266 if (!_Active)
267 return false;
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)))))
276 return false;
278 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouseleftdown)
280 _MouseDown = true;
281 selectColor(eventDesc.getX()-_XReal, eventDesc.getY()-_YReal);
282 return true;
284 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouseleftup)
286 _MouseDown = false;
287 return true;
289 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mousemove)
291 if (_MouseDown)
293 selectColor(eventDesc.getX()-_XReal, eventDesc.getY()-_YReal);
295 return true;
298 return false;
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();
349 if (x < 0) x = 0;
350 if (y < 0) y = 0;
351 if (x >= _WReal) x = _WReal-1;
352 if (y >= _HReal) y = _HReal-1;
353 return rVR.getTextureColor (_Texture, x, y);