Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / interfaces_manager / control.cpp
blob56d8663ddd3032da348b57c9f978d6af6956473a
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "stdpch.h"
21 #include "control.h"
22 #include "interfaces_manager.h"
25 //-----------------------------------------------
26 // CControl :
27 // Constructor.
28 //-----------------------------------------------
29 CControl::CControl(uint id)
31 init(id, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
32 }// CControl //
35 //-----------------------------------------------
36 // CControl :
37 // Constructor.
38 //-----------------------------------------------
39 CControl::CControl(uint id, float x, float y, float x_pixel, float y_pixel, float w, float h, float w_pixel, float h_pixel)
41 init(id, x, y, x_pixel, y_pixel, w, h, w_pixel, h_pixel);
42 }// CControl //
45 //-----------------------------------------------
46 // init :
47 // Constructor.
48 //-----------------------------------------------
49 void CControl::init(uint id, float x, float y, float x_pixel, float y_pixel, float w, float h, float w_pixel, float h_pixel)
51 // Not child.
52 _Children.clear();
54 // Id of the control.
55 _Id = id;
57 _X = x; // Position X of the Control (between 0-1).
58 _Y = y; // Position Y of the Control (between 0-1).
59 _X_Pixel = x_pixel; // Position X of the Control (in Pixel).
60 _Y_Pixel = y_pixel; // Position Y of the Control (in Pixel).
62 _W = w; // Width of the control (between 0-1).
63 _H = h; // Height of the control (between 0-1).
64 _W_Pixel = w_pixel; // Width of the control (in Pixel).
65 _H_Pixel = h_pixel; // Height of the control (in Pixel).
67 // The control position is relative to this Reference.
68 _X_Ref = 0.f;
69 _Y_Ref = 0.f;
70 // Size of the Parent.
71 _W_Ref = 1.f;
72 _H_Ref = 1.f;
74 // Delta to add to the position because of the Hot Spot.
75 _X_HotSpot = 0.f;
76 _Y_HotSpot = 0.f;
78 // Hot Spot
79 _HotSpot = HS_MR;
80 // Do the control have to be displayed. true -> yes.
81 _Show = true;
83 _Origin = HS_MM;
84 _Parent = 0;
86 // Calculate others variables.
87 calculateDisplay();
88 }// CControl //
91 //-----------------------------------------------
92 // resize :
93 // The window size has changed -> resize the control.
94 //-----------------------------------------------
95 void CControl::resize(uint32, uint32)
97 calculateDisplay();
99 // Update chidren.
100 for(TListControl::iterator it = _Children.begin(); it != _Children.end(); ++it)
102 if((*it)->parent() == this)
104 float x, y;
105 calculateOrigin(x, y, (*it)->origin());
106 (*it)->ref(x, y, _W_Ref, _H_Ref);
109 }// resize //
111 //-----------------------------------------------
112 // ref :
113 // Set some references for the display.
114 //-----------------------------------------------
115 void CControl::ref(float x, float y, float w, float h)
117 _X_Ref = x;
118 _Y_Ref = y;
119 _W_Ref = w;
120 _H_Ref = h;
122 calculateDisplay();
124 // Update chidren.
125 for(TListControl::iterator it = _Children.begin(); it != _Children.end(); ++it)
127 if((*it)->parent() == this)
129 float x, y;
130 calculateOrigin(x, y, (*it)->origin());
131 (*it)->ref(x, y, _W_Ref, _H_Ref);
134 }// ref //
136 //-----------------------------------------------
137 // addChild :
138 // Add a child to the control.
139 //-----------------------------------------------
140 void CControl::addChild(CControl *ctrl)
142 _Children.push_back(ctrl);
143 }// addChild //
146 //-----------------------------------------------
147 // hotSpot :
148 // Change the Hot Spot.
149 //-----------------------------------------------
150 void CControl::hotSpot(THotSpot hs)
152 _HotSpot = hs;
153 calculateDisplay();
155 // Update chidren.
156 for(TListControl::iterator it = _Children.begin(); it != _Children.end(); ++it)
158 if((*it)->parent() == this)
160 float x, y;
161 calculateOrigin(x, y, (*it)->origin());
162 (*it)->ref(x, y, _W_Ref, _H_Ref);
165 }// hotSpot //
167 //-----------------------------------------------
168 // hotSpot :
169 // Return the Hot Spot.
170 //-----------------------------------------------
171 CControl::THotSpot CControl::hotSpot()
173 return _HotSpot;
174 }// hotSpot //
177 //-----------------------------------------------
178 // show :
179 // Hide or show the control. false -> hide, true -> show.
180 //-----------------------------------------------
181 void CControl::show(bool show)
183 _Show = show;
184 }// show //
186 //-----------------------------------------------
187 // show :
188 // Return the show of the control.
189 //-----------------------------------------------
190 bool CControl::show()
192 return _Show;
193 }// show //
195 //-----------------------------------------------
196 // calculateDisplay :
197 // Calculate the Display X, Y, Width, Height.
198 //-----------------------------------------------
199 void CControl::calculateDisplay()
201 uint32 w, h;
202 CInterfMngr::getWindowSize(w, h);
204 // Calculate the display Width and Height.
205 if(w!=0)
206 _W_Display = _W*_W_Ref + _W_Pixel/w;
207 else
208 _W_Display = _W*_W_Ref;
210 if(h!=0)
211 _H_Display = _H*_H_Ref + _H_Pixel/h;
212 else
213 _H_Display = _H*_H_Ref;
215 // Calculate the HotSpot.
216 calculateHS();
218 _X_Display = _X_Ref + _X*_W_Ref + _X_Pixel/w + _X_HotSpot;
219 _Y_Display = _Y_Ref + _Y*_H_Ref + _Y_Pixel/h + _Y_HotSpot;
220 }// calculateDisplay //
222 //-----------------------------------------------
223 // calculateHS :
224 // Calculate the display position of the control in relation to the position of the control (Hot Spot).
225 //-----------------------------------------------
226 void CControl::calculateHS()
228 switch(_HotSpot)
230 case HS_TL:
231 _X_HotSpot = -_W_Display;
232 _Y_HotSpot = 0.f;
233 break;
234 case HS_TM:
235 _X_HotSpot = -_W_Display/2.f;
236 _Y_HotSpot = 0.f;
237 break;
238 case HS_TR:
239 _X_HotSpot = 0.f;
240 _Y_HotSpot = 0.f;
241 break;
243 case HS_ML:
244 _X_HotSpot = -_W_Display;
245 _Y_HotSpot = -_H_Display/2.f;
246 break;
247 case HS_MM:
248 _X_HotSpot = -_W_Display/2.f;
249 _Y_HotSpot = -_H_Display/2.f;
250 break;
251 case HS_MR:
252 _X_HotSpot = 0.f;
253 _Y_HotSpot = -_H_Display/2.f;
254 break;
256 case HS_BL:
257 _X_HotSpot = -_W_Display;
258 _Y_HotSpot = -_H_Display;
259 break;
260 case HS_BM:
261 _X_HotSpot = -_W_Display/2.f;
262 _Y_HotSpot = -_H_Display;
263 break;
264 case HS_BR:
265 _X_HotSpot = 0.f;
266 _Y_HotSpot = -_H_Display;
267 break;
269 }// calculateHS() //
271 //-----------------------------------------------
272 // calculateOrigin :
273 // Function to calculate where to position a child.
274 //-----------------------------------------------
275 void CControl::calculateOrigin(float &x, float &y, THotSpot origin)
277 switch(origin)
279 case CControl::HS_TL:
280 x = _X_Display;
281 y = _Y_Display+_H_Display;
282 break;
283 case CControl::HS_TM:
284 x = _X_Display+_W_Display/2.f;
285 y = _Y_Display+_H_Display;
286 break;
287 case CControl::HS_TR:
288 x = _X_Display+_W_Display;
289 y = _Y_Display+_H_Display;
290 break;
292 case CControl::HS_ML:
293 x = _X_Display;
294 y = _Y_Display+_H_Display/2.f;
295 break;
296 case CControl::HS_MM:
297 x = _X_Display+_W_Display/2.f;
298 y = _Y_Display+_H_Display/2.f;
299 break;
300 case CControl::HS_MR:
301 x = _X_Display+_W_Display;
302 y = _Y_Display+_H_Display/2.f;
303 break;
305 case CControl::HS_BL:
306 x = _X_Display;
307 y = _Y_Display;
308 break;
309 case CControl::HS_BM:
310 x = _X_Display+_W_Display/2.f;
311 y = _Y_Display;
312 break;
313 case CControl::HS_BR:
314 x = _X_Display+_W_Display;
315 y = _Y_Display;
316 break;
318 }// calculateOrigin //
322 //-----------------------------------------------
323 // mouseMove :
324 // called when the mouse has moved, give the new coordinates
325 //-----------------------------------------------
326 void CControl::mouseMove( float x, float y)
328 // send the message to all it's children
330 TListControl::iterator itChild;
331 const TListControl::iterator itChildEnd = _Children.end();
333 for (itChild = _Children.begin() ; itChild != itChildEnd ; ++itChild)
335 (*itChild)->mouseMove( x, y );
337 }// mouseMove //
340 //-----------------------------------------------
341 // click :
342 // manage left mouse button click
343 //-----------------------------------------------
344 void CControl::click(float x, float y, bool &taken)
346 }// click //
349 //-----------------------------------------------
350 // clickRight :
351 // manage right mouse button click
352 //-----------------------------------------------
353 void CControl::clickRight(float x, float y, bool &taken)
355 }// clickRight //
358 //-----------------------------------------------
359 // getDisplayValues :
360 // get display values of this control
361 //-----------------------------------------------
362 void CControl::getDisplayValues(float &x, float &y, float &h, float &w) const
364 x = _X_Display;
365 y = _Y_Display;
366 h = _H_Display;
367 w = _W_Display;
368 }// getDisplayValues //
372 //-----------------------------------------------
373 // getSize :
374 // get the size of the control
375 //-----------------------------------------------
376 void CControl::getSize( float &w, float &h, float &wPixel, float &hPixel) const
378 w = _W;
379 h = _H;
380 wPixel = _W_Pixel;
381 hPixel = _H_Pixel;
382 }// getSize //
385 //-----------------------------------------------
386 // getPosition :
387 // get the position of the control
388 //-----------------------------------------------
389 void CControl::getPosition( float &x, float &y, float &xPixel, float &yPixel ) const
391 x = _X;
392 y = _Y;
393 xPixel = _X_Pixel;
394 yPixel = _Y_Pixel;
395 }// getPosition //
398 //-----------------------------------------------
399 // setSize :
400 // set the size of the control
401 //-----------------------------------------------
402 void CControl::setSize( float w, float h, float wPixel, float hPixel )
404 _W = w;
405 _H = h;
406 _W_Pixel = wPixel;
407 _H_Pixel = hPixel;
409 calculateDisplay();
411 // update children
412 for(TListControl::iterator it = _Children.begin(); it != _Children.end(); ++it)
414 if((*it)->parent() == this)
416 float nx, ny;
417 calculateOrigin(nx, ny, (*it)->origin());
418 (*it)->ref(nx, ny, _W_Ref, _H_Ref);
421 }// setSize //
424 //-----------------------------------------------
425 // setPosition :
426 // set the position of the control
427 //-----------------------------------------------
428 void CControl::setPosition( float x, float y, float xPixel, float yPixel)
430 _X = x;
431 _Y = y;
432 _X_Pixel = xPixel;
433 _Y_Pixel = yPixel;
435 calculateDisplay();
437 // update children
438 for(TListControl::iterator it = _Children.begin(); it != _Children.end(); ++it)
440 if((*it)->parent() == this)
442 float nx, ny;
443 calculateOrigin(nx, ny, (*it)->origin());
444 (*it)->ref(nx, ny, _W_Ref, _H_Ref);
448 }// setPosition //