Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / gui / dbgroup_select_number.cpp
blob4d07cf19138f7973d272b4913af59014bfc24f7d
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 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
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/dbgroup_select_number.h"
24 #include "nel/gui/view_text.h"
25 #include "nel/gui/view_bitmap.h"
26 #include "nel/gui/ctrl_button.h"
27 #include "nel/gui/interface_property.h"
28 #include "nel/gui/action_handler.h"
30 using namespace std;
31 using namespace NL3D;
32 using namespace NLMISC;
34 #ifdef DEBUG_NEW
35 #define new DEBUG_NEW
36 #endif
38 namespace NLGUI
40 NLMISC_REGISTER_OBJECT(CViewBase, CDBGroupSelectNumber, std::string, "select_number");
42 void force_link_dbgroup_select_number_cpp() { }
44 // ***************************************************************************
45 CDBGroupSelectNumber::CDBGroupSelectNumber(const TCtorParam &param) :
46 CInterfaceGroup(param)
48 _SlotNumber= NULL;
49 _TextNumber= NULL;
50 _ButtonUp= NULL;
51 _ButtonDown= NULL;
52 _LoopMode= true;
53 _MinValue= 0;
54 _MaxValue= 9;
55 _DeltaMultiplier= 1;
58 // ***************************************************************************
59 CDBGroupSelectNumber::~CDBGroupSelectNumber()
63 std::string CDBGroupSelectNumber::getProperty( const std::string &name ) const
65 if( name == "value" )
67 if( _Number.getNodePtr() != NULL )
68 return _Number.getNodePtr()->getFullName();
69 else
70 return "";
72 else
73 if( name == "loop" )
75 return toString( _LoopMode );
77 else
78 if( name == "min" )
80 return toString( _MinValue );
82 else
83 if( name == "max" )
85 return toString( _MaxValue );
87 else
88 if( name == "delta" )
90 return toString( _DeltaMultiplier );
92 else
93 return CInterfaceGroup::getProperty( name );
96 void CDBGroupSelectNumber::setProperty( const std::string &name, const std::string &value )
98 if( name == "value" )
100 _Number.link( value.c_str() );
101 return;
103 else
104 if( name == "loop" )
106 bool b;
107 if( fromString( value, b ) )
108 _LoopMode = b;
109 return;
111 else
112 if( name == "min" )
114 sint i;
115 if( fromString( value, i ) )
116 _MinValue = i;
117 return;
119 else
120 if( name == "max" )
122 sint i;
123 if( fromString( value, i ) )
124 _MaxValue = i;
125 return;
127 else
128 if( name == "delta" )
130 sint i;
131 if( fromString( value, i ) )
132 _DeltaMultiplier = i;
133 return;
135 else
136 CInterfaceGroup::setProperty( name, value );
139 xmlNodePtr CDBGroupSelectNumber::serialize( xmlNodePtr parentNode, const char *type ) const
141 xmlNodePtr node = CInterfaceGroup::serialize( parentNode, type );
142 if( node == NULL )
143 return NULL;
145 xmlSetProp( node, BAD_CAST "type", BAD_CAST "select_number" );
147 if( _Number.getNodePtr() != NULL )
148 xmlSetProp( node, BAD_CAST "value", BAD_CAST _Number.getNodePtr()->getFullName().c_str() );
149 else
150 xmlSetProp( node, BAD_CAST "value", BAD_CAST "" );
152 xmlSetProp( node, BAD_CAST "loop", BAD_CAST toString( _LoopMode ).c_str() );
153 xmlSetProp( node, BAD_CAST "min", BAD_CAST toString( _MinValue ).c_str() );
154 xmlSetProp( node, BAD_CAST "max", BAD_CAST toString( _MaxValue ).c_str() );
155 xmlSetProp( node, BAD_CAST "delta", BAD_CAST toString( _DeltaMultiplier ).c_str() );
157 return node;
160 // ***************************************************************************
161 bool CDBGroupSelectNumber::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
163 if(!CInterfaceGroup::parse(cur, parentGroup))
164 return false;
166 // read params
167 CXMLAutoPtr ptr;
168 ptr = xmlGetProp (cur, (xmlChar*)"value");
169 if ( ptr )
170 _Number.link ( ptr );
171 else
173 nlinfo ("no value in %s", _Id.c_str());
174 return false;
177 // Loop, min and max
178 _LoopMode= false;
179 ptr= xmlGetProp (cur, (xmlChar*)"loop");
180 if(ptr) _LoopMode= convertBool(ptr);
181 _MinValue = 0;
182 ptr= xmlGetProp (cur, (xmlChar*)"min");
183 if(ptr) fromString((const char*)ptr, _MinValue);
184 _MaxValue = 0;
185 ptr= xmlGetProp (cur, (xmlChar*)"max");
186 if(ptr) fromString((const char*)ptr, _MaxValue);
187 ptr= xmlGetProp (cur, (xmlChar*)"delta");
188 if(ptr) fromString((const char*)ptr, _DeltaMultiplier);
190 // set min val in DB
191 _Number.setSInt32(_MinValue);
193 return true;
196 // ***************************************************************************
197 void CDBGroupSelectNumber::setup()
199 if (_SlotNumber != NULL)
200 return;
202 // bind to the controls.
203 _SlotNumber= dynamic_cast<CViewBitmap*>(CInterfaceGroup::getView("slot_number"));
204 _TextNumber= dynamic_cast<CViewText*>(CInterfaceGroup::getView("number"));
205 _ButtonUp= dynamic_cast<CCtrlBaseButton*>(CInterfaceGroup::getCtrl("arrow_up"));
206 _ButtonDown= dynamic_cast<CCtrlBaseButton*>(CInterfaceGroup::getCtrl("arrow_down"));
208 // checks
209 if(_SlotNumber==NULL)
210 nlwarning("Interface: SelectNumberGroup: bitmap 'slot_number' missing or bad type");
211 if(_TextNumber==NULL)
212 nlwarning("Interface: SelectNumberGroup: text view 'number' missing or bad type");
213 if(_ButtonUp==NULL)
214 nlwarning("Interface: SelectNumberGroup: button 'arrow_up' missing or bad type");
215 if(_ButtonDown==NULL)
216 nlwarning("Interface: SelectNumberGroup: button 'arrow_down' missing or bad type");
217 if(_SlotNumber==NULL || _TextNumber==NULL || _ButtonUp==NULL || _ButtonDown==NULL)
218 return;
220 // actions
221 _ButtonUp->setActionOnLeftClick("sn_up");
222 _ButtonDown->setActionOnLeftClick("sn_down");
226 // ***************************************************************************
227 void CDBGroupSelectNumber::updateCoords ()
229 setup();
230 CInterfaceGroup::updateCoords();
235 void CDBGroupSelectNumber::checkCoords()
237 if(_TextNumber)
238 _TextNumber->setText( toString(_Number.getSInt32()) );
239 CInterfaceGroup::checkCoords();
242 // ***************************************************************************
243 void CDBGroupSelectNumber::draw ()
245 CInterfaceGroup::draw();
248 // ***************************************************************************
249 void CDBGroupSelectNumber::clearViews ()
251 CInterfaceGroup::clearViews();
254 // ***************************************************************************
255 bool CDBGroupSelectNumber::handleEvent (const NLGUI::CEventDescriptor &event)
257 if (event.getType() == NLGUI::CEventDescriptor::mouse)
259 const NLGUI::CEventDescriptorMouse &eventDesc = (const NLGUI::CEventDescriptorMouse &)event;
260 if (isIn(eventDesc.getX(), eventDesc.getY()))
262 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mousewheel)
264 changeValue (eventDesc.getWheel());
265 return true;
269 if (CInterfaceGroup::handleEvent(event)) return true;
270 return false;
274 // ***************************************************************************
275 void CDBGroupSelectNumber::changeValue(sint delta)
277 delta*= _DeltaMultiplier;
279 // get DB and add
280 sint32 val= _Number.getSInt32();
281 val+= delta;
283 // Loop or clamp
284 if(_LoopMode)
286 sint32 dval= _MaxValue+1 - _MinValue;
287 if (dval <= 0)
289 val = 0;
290 return;
292 val -= _MinValue;
293 val = val% dval; val = (val+dval)% dval;
294 val += _MinValue;
296 else
298 clamp(val, _MinValue, _MaxValue);
301 // set in DB
302 _Number.setSInt32(val);
303 if(_TextNumber)
304 _TextNumber->setText( toString(_Number.getSInt32()) );
308 // ***************************************************************************
309 // ***************************************************************************
310 // Actions Handlers
311 // ***************************************************************************
312 // ***************************************************************************
315 // ***************************************************************************
316 class CSNUp : public IActionHandler
318 public:
319 virtual void execute (CCtrlBase *pCaller, const std::string &/* Params */)
321 CDBGroupSelectNumber *pSN = dynamic_cast<CDBGroupSelectNumber*>(pCaller->getParent());
322 if (pSN == NULL) return;
323 pSN->changeValue(+1);
326 REGISTER_ACTION_HANDLER (CSNUp, "sn_up");
328 // ***************************************************************************
329 class CSNDown : public IActionHandler
331 public:
332 virtual void execute (CCtrlBase *pCaller, const std::string &/* Params */)
334 CDBGroupSelectNumber *pSN = dynamic_cast<CDBGroupSelectNumber*>(pCaller->getParent());
335 if (pSN == NULL) return;
336 pSN->changeValue(-1);
339 REGISTER_ACTION_HANDLER (CSNDown, "sn_down");