Panda3D
pgSliderBar.I
1 // Filename: pgSliderBar.I
2 // Created by: masad (19Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: PGSliderBar::set_notify
18 // Access: Published
19 // Description: Sets the object which will be notified when the
20 // PGSliderBar changes. Set this to NULL to disable
21 // this effect. The PGSliderBar does not retain
22 // ownership of the pointer; it is your responsibility
23 // to ensure that the notify object does not destruct.
24 ////////////////////////////////////////////////////////////////////
25 INLINE void PGSliderBar::
27  PGItem::set_notify(notify);
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: PGSliderBar::get_notify
32 // Access: Published
33 // Description: Returns the object which will be notified when the
34 // PGSliderBar changes, if any. Returns NULL if there
35 // is no such object configured.
36 ////////////////////////////////////////////////////////////////////
38 get_notify() const {
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: PGSliderBar::set_axis
44 // Access: Published
45 // Description: Specifies the axis of the slider bar's motion. This
46 // should be only one of four vectors: (1, 0, 0), (0, 0,
47 // 1), (-1, 0, 0), or (0, 0, -1).
48 //
49 // This specifies the vector in which the thumb moves
50 // when it is moving from the minimum to the maximum
51 // value.
52 //
53 // The axis must be parallel to one of the screen axes,
54 // and it must be normalized. Hence, it may only be one
55 // of the above four possibilities; anything else is an
56 // error and will result in indeterminate behavior.
57 //
58 // Normally, you should not try to set the axis
59 // directly.
60 ////////////////////////////////////////////////////////////////////
61 INLINE void PGSliderBar::
62 set_axis(const LVector3 &axis) {
63  LightReMutexHolder holder(_lock);
64  _axis = axis;
65  _needs_remanage = true;
66  _needs_recompute = true;
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: PGSliderBar::get_axis
71 // Access: Published
72 // Description: Returns the axis of the slider bar's motion. See
73 // set_axis().
74 ////////////////////////////////////////////////////////////////////
75 INLINE const LVector3 &PGSliderBar::
76 get_axis() const {
77  LightReMutexHolder holder(_lock);
78  return _axis;
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: PGSliderBar::set_range
83 // Access: Published
84 // Description: Sets the minimum and maxmimum value for the slider.
85 ////////////////////////////////////////////////////////////////////
86 INLINE void PGSliderBar::
87 set_range(PN_stdfloat min_value, PN_stdfloat max_value) {
88  LightReMutexHolder holder(_lock);
89  nassertv(min_value != max_value);
90  _min_value = min_value;
91  _max_value = max_value;
92  _needs_recompute = true;
93 
94  if (has_notify()) {
95  get_notify()->slider_bar_set_range(this);
96  }
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: PGSliderBar::get_min_value
101 // Access: Published
102 // Description: Returns the value when the slider is all the way to
103 // the left.
104 ////////////////////////////////////////////////////////////////////
105 INLINE PN_stdfloat PGSliderBar::
106 get_min_value() const {
107  LightReMutexHolder holder(_lock);
108  return _min_value;
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: PGSliderBar::get_max_value
113 // Access: Published
114 // Description: Returns the value when the slider is all the way to
115 // the right.
116 ////////////////////////////////////////////////////////////////////
117 INLINE PN_stdfloat PGSliderBar::
118 get_max_value() const {
119  LightReMutexHolder holder(_lock);
120  return _max_value;
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: PGSliderBar::set_scroll_size
125 // Access: Published
126 // Description: Specifies the amount the slider will move when the
127 // user clicks on the left or right buttons.
128 ////////////////////////////////////////////////////////////////////
129 INLINE void PGSliderBar::
130 set_scroll_size(PN_stdfloat value) {
131  LightReMutexHolder holder(_lock);
132  _scroll_value = value;
133  _needs_recompute = true;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: PGSliderBar::get_scroll_size
138 // Access: Published
139 // Description: Returns the value last set by set_scroll_size().
140 ////////////////////////////////////////////////////////////////////
141 INLINE PN_stdfloat PGSliderBar::
143  LightReMutexHolder holder(_lock);
144  return _scroll_value;
145 }
146 
147 ////////////////////////////////////////////////////////////////////
148 // Function: PGSliderBar::set_page_size
149 // Access: Published
150 // Description: Specifies the amount of data contained in a single
151 // page. This indicates how much the thumb will jump
152 // when the trough is directly clicked; and if
153 // resize_thumb is true, it also controls the visible
154 // size of the thumb button.
155 ////////////////////////////////////////////////////////////////////
156 INLINE void PGSliderBar::
157 set_page_size(PN_stdfloat value) {
158  LightReMutexHolder holder(_lock);
159  _page_value = value;
160  _needs_recompute = true;
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function: PGSliderBar::get_page_size
165 // Access: Published
166 // Description: Returns the value last set by set_page_size().
167 ////////////////////////////////////////////////////////////////////
168 INLINE PN_stdfloat PGSliderBar::
169 get_page_size() const {
170  LightReMutexHolder holder(_lock);
171  return _page_value;
172 }
173 
174 ////////////////////////////////////////////////////////////////////
175 // Function: PGSliderBar::set_value
176 // Access: Published
177 // Description: Sets the current value of the slider
178 // programmatically. This should range between
179 // get_min_value() and get_max_value().
180 ////////////////////////////////////////////////////////////////////
181 INLINE void PGSliderBar::
182 set_value(PN_stdfloat value) {
183  LightReMutexHolder holder(_lock);
184  set_ratio((value - _min_value) / (_max_value - _min_value));
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: PGSliderBar::get_value
189 // Access: Published
190 // Description: Returns the current value of the slider.
191 ////////////////////////////////////////////////////////////////////
192 INLINE PN_stdfloat PGSliderBar::
193 get_value() const {
194  LightReMutexHolder holder(_lock);
195  return get_ratio() * (_max_value - _min_value) + _min_value;
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: PGSliderBar::set_ratio
200 // Access: Published
201 // Description: Sets the current value of the slider, expressed in
202 // the range 0 .. 1.
203 ////////////////////////////////////////////////////////////////////
204 INLINE void PGSliderBar::
205 set_ratio(PN_stdfloat ratio) {
206  LightReMutexHolder holder(_lock);
207  if (!is_button_down()) {
208  internal_set_ratio(ratio);
209  }
210 }
211 
212 ////////////////////////////////////////////////////////////////////
213 // Function: PGSliderBar::get_ratio
214 // Access: Published
215 // Description: Returns the current value of the slider, expressed in
216 // the range 0 .. 1.
217 ////////////////////////////////////////////////////////////////////
218 INLINE PN_stdfloat PGSliderBar::
219 get_ratio() const {
220  LightReMutexHolder holder(_lock);
221  return _ratio;
222 }
223 
224 ////////////////////////////////////////////////////////////////////
225 // Function: PGSliderBar::is_button_down
226 // Access: Published
227 // Description: Returns true if the user is currently holding down
228 // the mouse button to manipulate the slider. When
229 // true, calls to set_ratio() or set_value() will have
230 // no effect.
231 ////////////////////////////////////////////////////////////////////
232 INLINE bool PGSliderBar::
233 is_button_down() const {
234  LightReMutexHolder holder(_lock);
235  return _dragging || _mouse_button_page ||
236  (_scroll_button_held != (PGItem *)NULL);
237 }
238 
239 ////////////////////////////////////////////////////////////////////
240 // Function: PGSliderBar::set_resize_thumb
241 // Access: Published
242 // Description: Sets the resize_thumb flag. When this is true, the
243 // thumb button's frame will be adjusted so that its
244 // width visually represents the page size. When this
245 // is false, the thumb button will be left alone.
246 ////////////////////////////////////////////////////////////////////
247 INLINE void PGSliderBar::
248 set_resize_thumb(bool resize_thumb) {
249  LightReMutexHolder holder(_lock);
250  _resize_thumb = resize_thumb;
251  _needs_recompute = true;
252 }
253 
254 ////////////////////////////////////////////////////////////////////
255 // Function: PGSliderBar::get_resize_thumb
256 // Access: Published
257 // Description: Returns the resize_thumb flag. See
258 // set_resize_thumb().
259 ////////////////////////////////////////////////////////////////////
260 INLINE bool PGSliderBar::
262  LightReMutexHolder holder(_lock);
263  return _resize_thumb;
264 }
265 
266 ////////////////////////////////////////////////////////////////////
267 // Function: PGSliderBar::set_manage_pieces
268 // Access: Published
269 // Description: Sets the manage_pieces flag. When this is true, the
270 // sub-pieces of the slider bar--that is, the thumb, and
271 // the left and right scroll buttons--are automatically
272 // positioned and/or resized when the slider bar's
273 // overall frame is changed.
274 ////////////////////////////////////////////////////////////////////
275 INLINE void PGSliderBar::
276 set_manage_pieces(bool manage_pieces) {
277  LightReMutexHolder holder(_lock);
278  _manage_pieces = manage_pieces;
279  _needs_remanage = true;
280  _needs_recompute = true;
281 }
282 
283 ////////////////////////////////////////////////////////////////////
284 // Function: PGSliderBar::get_manage_pieces
285 // Access: Published
286 // Description: Returns the manage_pieces flag. See
287 // set_manage_pieces().
288 ////////////////////////////////////////////////////////////////////
289 INLINE bool PGSliderBar::
291  LightReMutexHolder holder(_lock);
292  return _manage_pieces;
293 }
294 
295 ////////////////////////////////////////////////////////////////////
296 // Function: PGSliderBar::set_thumb_button
297 // Access: Published
298 // Description: Sets the PGButton object that will serve as the thumb
299 // for this slider. This button visually represents the
300 // position of the slider, and can be dragged left and
301 // right by the user.
302 //
303 // It is the responsibility of the caller to ensure that
304 // the button object is parented to the PGSliderBar
305 // node.
306 ////////////////////////////////////////////////////////////////////
307 INLINE void PGSliderBar::
308 set_thumb_button(PGButton *thumb_button) {
309  LightReMutexHolder holder(_lock);
310  if (_thumb_button != (PGButton *)NULL) {
311  _thumb_button->set_notify(NULL);
312  }
313  _thumb_button = thumb_button;
314  if (_thumb_button != (PGButton *)NULL) {
315  _thumb_button->set_notify(this);
316  }
317  _needs_remanage = true;
318  _needs_recompute = true;
319 }
320 
321 ////////////////////////////////////////////////////////////////////
322 // Function: PGSliderBar::clear_thumb_button
323 // Access: Published
324 // Description: Removes the thumb button object from control of the
325 // frame. It is your responsibility to actually remove
326 // or hide the button itself.
327 ////////////////////////////////////////////////////////////////////
328 INLINE void PGSliderBar::
330  set_thumb_button(NULL);
331 }
332 
333 ////////////////////////////////////////////////////////////////////
334 // Function: PGSliderBar::get_thumb_button
335 // Access: Published
336 // Description: Returns the PGButton that serves as the thumb for
337 // this slider, or NULL if it is not set.
338 ////////////////////////////////////////////////////////////////////
339 INLINE PGButton *PGSliderBar::
341  LightReMutexHolder holder(_lock);
342  return _thumb_button;
343 }
344 
345 ////////////////////////////////////////////////////////////////////
346 // Function: PGSliderBar::set_left_button
347 // Access: Published
348 // Description: Sets the PGButton object that will serve as the left
349 // scroll button for this slider. This button is
350 // optional; if present, the user can click on it to
351 // move scroll_size units at a time to the left.
352 //
353 // It is the responsibility of the caller to ensure that
354 // the button object is parented to the PGSliderBar
355 // node.
356 ////////////////////////////////////////////////////////////////////
357 INLINE void PGSliderBar::
358 set_left_button(PGButton *left_button) {
359  LightReMutexHolder holder(_lock);
360  if (_left_button != (PGButton *)NULL) {
361  _left_button->set_notify(NULL);
362  }
363  _left_button = left_button;
364  if (_left_button != (PGButton *)NULL) {
365  _left_button->set_notify(this);
366  }
367  _needs_remanage = true;
368  _needs_recompute = true;
369 }
370 
371 ////////////////////////////////////////////////////////////////////
372 // Function: PGSliderBar::clear_left_button
373 // Access: Published
374 // Description: Removes the left button object from control of the
375 // frame. It is your responsibility to actually remove
376 // or hide the button itself.
377 ////////////////////////////////////////////////////////////////////
378 INLINE void PGSliderBar::
380  set_left_button(NULL);
381 }
382 
383 ////////////////////////////////////////////////////////////////////
384 // Function: PGSliderBar::get_left_button
385 // Access: Published
386 // Description: Returns the PGButton that serves as the left scroll
387 // button for this slider, if any, or NULL if it is not
388 // set.
389 ////////////////////////////////////////////////////////////////////
390 INLINE PGButton *PGSliderBar::
392  LightReMutexHolder holder(_lock);
393  return _left_button;
394 }
395 
396 ////////////////////////////////////////////////////////////////////
397 // Function: PGSliderBar::set_right_button
398 // Access: Published
399 // Description: Sets the PGButton object that will serve as the right
400 // scroll button for this slider. This button is
401 // optional; if present, the user can click on it to
402 // move scroll_size units at a time to the right.
403 //
404 // It is the responsibility of the caller to ensure that
405 // the button object is parented to the PGSliderBar
406 // node.
407 ////////////////////////////////////////////////////////////////////
408 INLINE void PGSliderBar::
409 set_right_button(PGButton *right_button) {
410  LightReMutexHolder holder(_lock);
411  if (_right_button != (PGButton *)NULL) {
412  _right_button->set_notify(NULL);
413  }
414  _right_button = right_button;
415  if (_right_button != (PGButton *)NULL) {
416  _right_button->set_notify(this);
417  }
418  _needs_remanage = true;
419  _needs_recompute = true;
420 }
421 
422 ////////////////////////////////////////////////////////////////////
423 // Function: PGSliderBar::clear_right_button
424 // Access: Published
425 // Description: Removes the right button object from control of the
426 // frame. It is your responsibility to actually remove
427 // or hide the button itself.
428 ////////////////////////////////////////////////////////////////////
429 INLINE void PGSliderBar::
431  set_right_button(NULL);
432 }
433 
434 ////////////////////////////////////////////////////////////////////
435 // Function: PGSliderBar::get_right_button
436 // Access: Published
437 // Description: Returns the PGButton that serves as the right scroll
438 // button for this slider, if any, or NULL if it is not
439 // set.
440 ////////////////////////////////////////////////////////////////////
441 INLINE PGButton *PGSliderBar::
443  LightReMutexHolder holder(_lock);
444  return _right_button;
445 }
446 
447 ////////////////////////////////////////////////////////////////////
448 // Function: PGSliderBar::get_adjust_prefix
449 // Access: Published, Static
450 // Description: Returns the prefix that is used to define the adjust
451 // event for all PGSliderBars. The adjust event is the
452 // concatenation of this string followed by get_id().
453 ////////////////////////////////////////////////////////////////////
454 INLINE string PGSliderBar::
456  return "adjust-";
457 }
458 
459 ////////////////////////////////////////////////////////////////////
460 // Function: PGSliderBar::get_adjust_event
461 // Access: Published
462 // Description: Returns the event name that will be thrown when the
463 // slider bar value is adjusted by the user or
464 // programmatically.
465 ////////////////////////////////////////////////////////////////////
466 INLINE string PGSliderBar::
468  LightReMutexHolder holder(_lock);
469  return get_adjust_prefix() + get_id();
470 }
471 
472 ////////////////////////////////////////////////////////////////////
473 // Function: PGSliderBar::internal_set_ratio
474 // Access: Private
475 // Description: Sets the current value of the slider, expressed in
476 // the range 0 .. 1, without checking whether the user
477 // is currently manipulating the slider.
478 ////////////////////////////////////////////////////////////////////
479 INLINE void PGSliderBar::
480 internal_set_ratio(PN_stdfloat ratio) {
481  _ratio = max(min(ratio, (PN_stdfloat)1.0), (PN_stdfloat)0.0);
482  _needs_reposition = true;
483  adjust();
484 }
bool get_manage_pieces() const
Returns the manage_pieces flag.
Definition: pgSliderBar.I:290
void set_page_size(PN_stdfloat page_size)
Specifies the amount of data contained in a single page.
Definition: pgSliderBar.I:157
void set_notify(PGButtonNotify *notify)
Sets the object which will be notified when the PGButton changes.
Definition: pgButton.I:26
void clear_right_button()
Removes the right button object from control of the frame.
Definition: pgSliderBar.I:430
void set_resize_thumb(bool resize_thumb)
Sets the resize_thumb flag.
Definition: pgSliderBar.I:248
This is the base class for all the various kinds of gui widget objects.
Definition: pgItem.h:58
const string & get_id() const
Returns the unique ID assigned to this PGItem.
Definition: pgItem.I:263
void clear_left_button()
Removes the left button object from control of the frame.
Definition: pgSliderBar.I:379
PN_stdfloat get_page_size() const
Returns the value last set by set_page_size().
Definition: pgSliderBar.I:169
This is a particular kind of PGItem that is specialized to behave like a normal button object...
Definition: pgButton.h:32
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
const LVector3 & get_axis() const
Returns the axis of the slider bar's motion.
Definition: pgSliderBar.I:76
void set_ratio(PN_stdfloat ratio)
Sets the current value of the slider, expressed in the range 0 .
Definition: pgSliderBar.I:205
void set_value(PN_stdfloat value)
Sets the current value of the slider programmatically.
Definition: pgSliderBar.I:182
PGButton * get_thumb_button() const
Returns the PGButton that serves as the thumb for this slider, or NULL if it is not set...
Definition: pgSliderBar.I:340
string get_adjust_event() const
Returns the event name that will be thrown when the slider bar value is adjusted by the user or progr...
Definition: pgSliderBar.I:467
PGButton * get_left_button() const
Returns the PGButton that serves as the left scroll button for this slider, if any, or NULL if it is not set.
Definition: pgSliderBar.I:391
virtual void adjust()
This is a callback hook function, called whenever the slider value is adjusted by the user or program...
PN_stdfloat get_min_value() const
Returns the value when the slider is all the way to the left.
Definition: pgSliderBar.I:106
void clear_thumb_button()
Removes the thumb button object from control of the frame.
Definition: pgSliderBar.I:329
bool has_notify() const
Returns true if there is an object configured to be notified when the PGItem changes, false otherwise.
Definition: pgItem.I:72
PN_stdfloat get_value() const
Returns the current value of the slider.
Definition: pgSliderBar.I:193
bool get_resize_thumb() const
Returns the resize_thumb flag.
Definition: pgSliderBar.I:261
void set_notify(PGSliderBarNotify *notify)
Sets the object which will be notified when the PGSliderBar changes.
Definition: pgSliderBar.I:26
PGItemNotify * get_notify() const
Returns the object which will be notified when the PGItem changes, if any.
Definition: pgItem.I:85
void set_scroll_size(PN_stdfloat scroll_size)
Specifies the amount the slider will move when the user clicks on the left or right buttons...
Definition: pgSliderBar.I:130
bool is_button_down() const
Returns true if the user is currently holding down the mouse button to manipulate the slider...
Definition: pgSliderBar.I:233
PN_stdfloat get_max_value() const
Returns the value when the slider is all the way to the right.
Definition: pgSliderBar.I:118
PGButton * get_right_button() const
Returns the PGButton that serves as the right scroll button for this slider, if any, or NULL if it is not set.
Definition: pgSliderBar.I:442
static string get_adjust_prefix()
Returns the prefix that is used to define the adjust event for all PGSliderBars.
Definition: pgSliderBar.I:455
Similar to MutexHolder, but for a light reentrant mutex.
PGSliderBarNotify * get_notify() const
Returns the object which will be notified when the PGSliderBar changes, if any.
Definition: pgSliderBar.I:38
void set_right_button(PGButton *right_button)
Sets the PGButton object that will serve as the right scroll button for this slider.
Definition: pgSliderBar.I:409
void set_manage_pieces(bool manage_pieces)
Sets the manage_pieces flag.
Definition: pgSliderBar.I:276
void set_thumb_button(PGButton *thumb_button)
Sets the PGButton object that will serve as the thumb for this slider.
Definition: pgSliderBar.I:308
void set_left_button(PGButton *left_button)
Sets the PGButton object that will serve as the left scroll button for this slider.
Definition: pgSliderBar.I:358
Objects that inherit from this class can receive notify messages when a slider bar moves or otherwise...
void set_range(PN_stdfloat min_value, PN_stdfloat max_value)
Sets the minimum and maxmimum value for the slider.
Definition: pgSliderBar.I:87
void set_axis(const LVector3 &axis)
Specifies the axis of the slider bar's motion.
Definition: pgSliderBar.I:62
PN_stdfloat get_ratio() const
Returns the current value of the slider, expressed in the range 0 .
Definition: pgSliderBar.I:219
PN_stdfloat get_scroll_size() const
Returns the value last set by set_scroll_size().
Definition: pgSliderBar.I:142
void set_notify(PGItemNotify *notify)
Sets the object which will be notified when the PGItem changes.
Definition: pgItem.I:54