1 | /****************************************************************************
|
---|
2 |
|
---|
3 | GLUI User Interface Toolkit
|
---|
4 | ---------------------------
|
---|
5 |
|
---|
6 | glui_hslider.cpp - GLUI_HSlider control class
|
---|
7 |
|
---|
8 |
|
---|
9 | --------------------------------------------------
|
---|
10 |
|
---|
11 | Copyright (c) 1998 Paul Rademacher
|
---|
12 |
|
---|
13 | This program is freely distributable without licensing fees and is
|
---|
14 | provided without guarantee or warrantee expressed or implied. This
|
---|
15 | program is -not- in the public domain.
|
---|
16 |
|
---|
17 | *****************************************************************************/
|
---|
18 |
|
---|
19 | #include "glui.h"
|
---|
20 | #include "stdinc.h"
|
---|
21 | // #include <iostream.h>
|
---|
22 | #include <sys/types.h>
|
---|
23 | #include <time.h>
|
---|
24 | #ifdef WIN32
|
---|
25 | #include <windows.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 |
|
---|
29 | static inline int round( float );
|
---|
30 |
|
---|
31 |
|
---|
32 | /* tolerance to hit the range selector: */
|
---|
33 |
|
---|
34 | #define RANGETOL 3
|
---|
35 |
|
---|
36 |
|
---|
37 | /* find where a click landed in the control */
|
---|
38 |
|
---|
39 | int GLUI_HSlider::locate_click( int local_x, int local_y )
|
---|
40 | {
|
---|
41 | int rend, lend;
|
---|
42 |
|
---|
43 | rend = redge_to_pixel( button_right_edge );
|
---|
44 | lend = ledge_to_pixel( button_left_edge );
|
---|
45 | local_x -= x_abs;
|
---|
46 | local_y -= y_abs;
|
---|
47 |
|
---|
48 | if( local_y >= 0 && local_y < GLUI_HSLIDER_ARROW_HEIGHT )
|
---|
49 | {
|
---|
50 | /* if it's outside the area, hit nothing */
|
---|
51 | if( local_x < 0 || local_x >= w ) return GLUI_HSLIDER_STATE_NONE;
|
---|
52 | /* if it's at the left edge, hit left arrow */
|
---|
53 | if( local_x >= 0 && local_x < GLUI_HSLIDER_ARROW_WIDTH )
|
---|
54 | return GLUI_HSLIDER_STATE_DOWN;
|
---|
55 | /* if it's at the right edge, hit right arrow */
|
---|
56 | else if( local_x >= (w - GLUI_HSLIDER_ARROW_WIDTH) &&
|
---|
57 | local_x < w )
|
---|
58 | return GLUI_HSLIDER_STATE_UP;
|
---|
59 | /* if it's in the button area, it hit the button */
|
---|
60 | else if( local_x > lend + 1 && local_x < rend - 1 )
|
---|
61 | return GLUI_HSLIDER_STATE_BUTTON;
|
---|
62 | /* if it's in on the left edge of the button, resize left side */
|
---|
63 | else if( local_x >= lend - RANGETOL && local_x <= lend + RANGETOL )
|
---|
64 | return GLUI_HSLIDER_STATE_BUTTON_LEFT;
|
---|
65 | /* if it's in on the right edge of the button, resize right side */
|
---|
66 | else if( local_x <= rend + RANGETOL && local_x >= rend - RANGETOL )
|
---|
67 | return GLUI_HSLIDER_STATE_BUTTON_RIGHT;
|
---|
68 | /* if it's outside the right edge of the button, trough right side */
|
---|
69 | else if( local_x > rend + RANGETOL )
|
---|
70 | return GLUI_HSLIDER_STATE_TROUGH_RIGHT;
|
---|
71 | /* if it's outside the left edge of the button, trough left side */
|
---|
72 | else if( local_x < lend - RANGETOL )
|
---|
73 | return GLUI_HSLIDER_STATE_TROUGH_LEFT;
|
---|
74 | }
|
---|
75 | return GLUI_HSLIDER_STATE_NONE;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* make sure we didn't go past any edges */
|
---|
79 | void GLUI_HSlider::reconcile_edge( void )
|
---|
80 | {
|
---|
81 | float dx = 0.;
|
---|
82 | if( button_left_edge < float_low )
|
---|
83 | {
|
---|
84 | dx = float_low - button_left_edge;
|
---|
85 | //cerr << "bar! " << dx << endl;
|
---|
86 | } else if( button_is_resizable && button_right_edge > float_high )
|
---|
87 | {
|
---|
88 | dx = float_high - button_right_edge;
|
---|
89 | } else if( !button_is_resizable && (button_left_edge > float_high) )
|
---|
90 | {
|
---|
91 | dx = float_high - button_left_edge;
|
---|
92 | //cerr << "foo! " << dx << endl;
|
---|
93 | }
|
---|
94 |
|
---|
95 | button_left_edge += dx;
|
---|
96 | button_right_edge += dx;
|
---|
97 |
|
---|
98 | if( button_is_resizable )
|
---|
99 | {
|
---|
100 | /* we may have grown past the right/left side, this checks that */
|
---|
101 | if( button_left_edge < float_low )
|
---|
102 | {
|
---|
103 | button_left_edge = float_low;
|
---|
104 | }
|
---|
105 | if( button_right_edge > float_high )
|
---|
106 | {
|
---|
107 | button_right_edge = float_high;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /* at this point we've been clicked, and need to move around */
|
---|
115 | void GLUI_HSlider::do_click()
|
---|
116 | {
|
---|
117 | float dx;
|
---|
118 |
|
---|
119 | #ifdef WIN32
|
---|
120 | last_click_time = GetTickCount();
|
---|
121 | #else
|
---|
122 | last_click_time = clock();
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | switch( state )
|
---|
126 | {
|
---|
127 | case GLUI_HSLIDER_STATE_DOWN:
|
---|
128 | /* decrement by one tick */
|
---|
129 | move_val( -.001 * ( float_high - float_low ) );
|
---|
130 | break;
|
---|
131 |
|
---|
132 | case GLUI_HSLIDER_STATE_UP:
|
---|
133 | /* increment by one tick */
|
---|
134 | move_val( .001 * ( float_high - float_low ) );
|
---|
135 | break;
|
---|
136 |
|
---|
137 | case GLUI_HSLIDER_STATE_BUTTON_LEFT:
|
---|
138 | button_left_edge = pixel_to_ledge( new_button_center );
|
---|
139 | if( button_left_edge > button_right_edge )
|
---|
140 | button_left_edge = button_right_edge;
|
---|
141 | if( button_left_edge < float_low )
|
---|
142 | button_left_edge = float_low;
|
---|
143 | translate_and_draw_front();
|
---|
144 | break;
|
---|
145 |
|
---|
146 | case GLUI_HSLIDER_STATE_BUTTON_RIGHT:
|
---|
147 | button_right_edge = pixel_to_redge( new_button_center );
|
---|
148 | if( button_right_edge < button_left_edge )
|
---|
149 | button_right_edge = button_left_edge;
|
---|
150 | if( button_right_edge > float_high )
|
---|
151 | button_right_edge = float_high;
|
---|
152 | translate_and_draw_front();
|
---|
153 | break;
|
---|
154 |
|
---|
155 | case GLUI_HSLIDER_STATE_BUTTON:
|
---|
156 | move_button_pixels( new_button_center, old_button_center );
|
---|
157 | break;
|
---|
158 |
|
---|
159 | case GLUI_HSLIDER_STATE_TROUGH_LEFT:
|
---|
160 | /* bounce it left one width of the button */
|
---|
161 | /*
|
---|
162 | if( bounce_target < GLUI_HSLIDER_ARROW_WIDTH )
|
---|
163 | bounce_target = GLUI_HSLIDER_ARROW_WIDTH;
|
---|
164 | */
|
---|
165 |
|
---|
166 | if( bounce_target < ledge_to_pixel(button_left_edge) )
|
---|
167 | {
|
---|
168 | /* bounce it one width left */
|
---|
169 | dx = button_left_edge -
|
---|
170 | pixel_to_redge(4+redge_to_pixel(button_right_edge));
|
---|
171 | button_left_edge += dx;
|
---|
172 | button_right_edge += dx;
|
---|
173 |
|
---|
174 | /* make sure we didn't go over an edge */
|
---|
175 | reconcile_edge();
|
---|
176 | translate_and_draw_front();
|
---|
177 | }
|
---|
178 | break;
|
---|
179 |
|
---|
180 | case GLUI_HSLIDER_STATE_TROUGH_RIGHT:
|
---|
181 | /* bounce it right one width of the button */
|
---|
182 | /*
|
---|
183 | if( bounce_target >= w - GLUI_HSLIDER_ARROW_WIDTH )
|
---|
184 | bounce_target = w - GLUI_HSLIDER_ARROW_WIDTH - 1;
|
---|
185 | */
|
---|
186 | if( bounce_target > redge_to_pixel(button_right_edge) )
|
---|
187 | {
|
---|
188 | dx = pixel_to_redge(4+redge_to_pixel(button_right_edge))
|
---|
189 | - button_left_edge;
|
---|
190 | button_left_edge += dx;
|
---|
191 | button_right_edge += dx;
|
---|
192 |
|
---|
193 | /* make sure we didn't go over an edge */
|
---|
194 | reconcile_edge();
|
---|
195 | translate_and_draw_front();
|
---|
196 | }
|
---|
197 | break;
|
---|
198 |
|
---|
199 | case GLUI_HSLIDER_STATE_NONE:
|
---|
200 | return;
|
---|
201 | }
|
---|
202 |
|
---|
203 | update_slider_val();
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|
209 |
|
---|
210 | /****************************** GLUI_HSlider::mouse_down_handler() **********/
|
---|
211 | int GLUI_HSlider::mouse_down_handler( int local_x, int local_y )
|
---|
212 | {
|
---|
213 | /* three cases: clicked on trough, clicked on up/down buttons,
|
---|
214 | clicked on slider */
|
---|
215 |
|
---|
216 | state = locate_click( local_x, local_y );
|
---|
217 |
|
---|
218 | switch( state )
|
---|
219 | {
|
---|
220 | case GLUI_HSLIDER_STATE_DOWN:
|
---|
221 | case GLUI_HSLIDER_STATE_UP:
|
---|
222 | bneeds_idle = 1;
|
---|
223 | break;
|
---|
224 |
|
---|
225 | case GLUI_HSLIDER_STATE_BUTTON_LEFT:
|
---|
226 | case GLUI_HSLIDER_STATE_BUTTON_RIGHT:
|
---|
227 | if( button_is_resizable )
|
---|
228 | {
|
---|
229 | glutSetCursor( GLUT_CURSOR_LEFT_RIGHT );
|
---|
230 | } else
|
---|
231 | {
|
---|
232 | /* not resizable, pretend they clicked the button */
|
---|
233 | state = GLUI_HSLIDER_STATE_BUTTON;
|
---|
234 | }
|
---|
235 | new_button_center = old_button_center = local_x-x_abs;
|
---|
236 | break;
|
---|
237 |
|
---|
238 | case GLUI_HSLIDER_STATE_BUTTON:
|
---|
239 | new_button_center = old_button_center = local_x-x_abs;
|
---|
240 | break;
|
---|
241 |
|
---|
242 | case GLUI_HSLIDER_STATE_TROUGH_RIGHT:
|
---|
243 | case GLUI_HSLIDER_STATE_TROUGH_LEFT:
|
---|
244 | /* bounce it one width of the button */
|
---|
245 | bneeds_idle = 1;
|
---|
246 | bounce_target = local_x - x_abs;
|
---|
247 | break;
|
---|
248 |
|
---|
249 | case GLUI_HSLIDER_STATE_NONE:
|
---|
250 | return true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | do_click();
|
---|
254 | return false;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 | int GLUI_HSlider::mouse_over( int entered, int x, int y )
|
---|
260 | {
|
---|
261 | int area;
|
---|
262 |
|
---|
263 | if( ! button_is_resizable )
|
---|
264 | return false;
|
---|
265 |
|
---|
266 | /* we're busy, call us later */
|
---|
267 | if( state != GLUI_HSLIDER_STATE_NONE )
|
---|
268 | return true;
|
---|
269 |
|
---|
270 | /* make sure we set it back to left arrow */
|
---|
271 | if( !entered )
|
---|
272 | glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
|
---|
273 | else
|
---|
274 | {
|
---|
275 | area = locate_click( x, y );
|
---|
276 | if( area == GLUI_HSLIDER_STATE_BUTTON_LEFT ||
|
---|
277 | area == GLUI_HSLIDER_STATE_BUTTON_RIGHT )
|
---|
278 | {
|
---|
279 | /* in the correct area, set the cursor */
|
---|
280 | glutSetCursor( GLUT_CURSOR_LEFT_RIGHT );
|
---|
281 | } else glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
|
---|
282 | return true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | return true;
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 |
|
---|
290 | int GLUI_HSlider::needs_idle()
|
---|
291 | {
|
---|
292 | return bneeds_idle;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 |
|
---|
297 | void GLUI_HSlider::idle()
|
---|
298 | {
|
---|
299 | time_t cur_time;
|
---|
300 |
|
---|
301 | if( !bneeds_idle ) return;
|
---|
302 |
|
---|
303 | #ifdef WIN32
|
---|
304 | cur_time = GetTickCount();
|
---|
305 | // on windows it's milliseconds, not micro
|
---|
306 | if( cur_time - last_click_time > 50 )
|
---|
307 | #else
|
---|
308 | cur_time = clock();
|
---|
309 | if( cur_time - last_click_time > 50000 )
|
---|
310 | #endif
|
---|
311 | {
|
---|
312 | do_click();
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 |
|
---|
319 | /****************************** GLUI_HSlider::mouse_up_handler() **********/
|
---|
320 |
|
---|
321 | int GLUI_HSlider::mouse_up_handler( int local_x, int local_y, int inside )
|
---|
322 | {
|
---|
323 | /* redraw it, do callback? */
|
---|
324 | local_x = local_y = inside = local_x+local_y+inside;
|
---|
325 | bneeds_idle = 0;
|
---|
326 | state = GLUI_HSLIDER_STATE_NONE;
|
---|
327 | return false;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 |
|
---|
332 | /****************************** GLUI_HSlider::mouse_held_down_handler() ******/
|
---|
333 |
|
---|
334 | int GLUI_HSlider::mouse_held_down_handler( int local_x, int local_y,
|
---|
335 | int new_inside)
|
---|
336 | {
|
---|
337 | /* couple things here:
|
---|
338 | when held down in the trough, keep going up in one direction until the
|
---|
339 | mouse is let go. Don't go past the mouse area.
|
---|
340 |
|
---|
341 | when held down on an arrow, keep going in that direction until the mouse
|
---|
342 | is let go
|
---|
343 |
|
---|
344 | when in the button state, follow the mouse horizontally until the mouse
|
---|
345 | is let go
|
---|
346 | */
|
---|
347 |
|
---|
348 | switch( state )
|
---|
349 | {
|
---|
350 | case GLUI_HSLIDER_STATE_NONE:
|
---|
351 | case GLUI_HSLIDER_STATE_UP:
|
---|
352 | case GLUI_HSLIDER_STATE_DOWN:
|
---|
353 | break;
|
---|
354 |
|
---|
355 | case GLUI_HSLIDER_STATE_TROUGH_RIGHT:
|
---|
356 | case GLUI_HSLIDER_STATE_TROUGH_LEFT:
|
---|
357 | /* if it's still on the button, do another click */
|
---|
358 | if( new_inside
|
---|
359 | && locate_click(local_x, local_y) == state )
|
---|
360 | {
|
---|
361 | bounce_target = local_x-x_abs;
|
---|
362 | }
|
---|
363 | break;
|
---|
364 |
|
---|
365 | case GLUI_HSLIDER_STATE_BUTTON_RIGHT:
|
---|
366 | case GLUI_HSLIDER_STATE_BUTTON_LEFT:
|
---|
367 | case GLUI_HSLIDER_STATE_BUTTON:
|
---|
368 | /* move button over some */
|
---|
369 | new_button_center = local_x - x_abs;
|
---|
370 | do_click();
|
---|
371 | break;
|
---|
372 | }
|
---|
373 |
|
---|
374 | return false;
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 |
|
---|
379 | int GLUI_HSlider::special_handler( int key, int modifiers )
|
---|
380 | {
|
---|
381 | key = modifiers = key+modifiers;
|
---|
382 | return false;
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 |
|
---|
387 |
|
---|
388 | /****************************** GLUI_HSlider::key_handler() **********/
|
---|
389 |
|
---|
390 | int GLUI_HSlider::key_handler( unsigned char key, int modifiers )
|
---|
391 | {
|
---|
392 | key = key + 2;
|
---|
393 | modifiers = modifiers + 2;
|
---|
394 | return false;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 |
|
---|
399 | /********************************************** GLUI_HSlider::draw() **********/
|
---|
400 |
|
---|
401 | GLubyte glui_halftone[] =
|
---|
402 | {
|
---|
403 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
404 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
405 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
406 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
407 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
408 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
409 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
410 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
411 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
412 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
413 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
414 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
415 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
416 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
417 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
418 | 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
|
---|
419 | };
|
---|
420 |
|
---|
421 | void GLUI_HSlider::draw( int x, int y )
|
---|
422 | {
|
---|
423 | int orig, dbstate;
|
---|
424 | int bleft, bright;
|
---|
425 |
|
---|
426 | if ( NOT can_draw() )
|
---|
427 | return;
|
---|
428 |
|
---|
429 | if( !glui ) return;
|
---|
430 |
|
---|
431 | orig = set_to_glut_window();
|
---|
432 | dbstate = glui->set_front_draw_buffer();
|
---|
433 |
|
---|
434 | glui->std_bitmaps.draw( GLUI_STDBITMAP_LEFT_ARROW, 0, 0 );
|
---|
435 | glui->std_bitmaps.draw( GLUI_STDBITMAP_RIGHT_ARROW,
|
---|
436 | w - GLUI_HSLIDER_ARROW_WIDTH - 1, 0 );
|
---|
437 | glColor3ub( 192, 192, 192 );
|
---|
438 | glBegin( GL_TRIANGLE_STRIP );
|
---|
439 | glVertex2i( GLUI_HSLIDER_ARROW_WIDTH+1, 1 );
|
---|
440 | glVertex2i( GLUI_HSLIDER_ARROW_WIDTH+1, h );
|
---|
441 | glVertex2i( w - GLUI_HSLIDER_ARROW_WIDTH, 1 );
|
---|
442 | glVertex2i( w - GLUI_HSLIDER_ARROW_WIDTH, h );
|
---|
443 | glEnd();
|
---|
444 |
|
---|
445 | glPolygonStipple( glui_halftone );
|
---|
446 | glEnable( GL_POLYGON_STIPPLE );
|
---|
447 | glColor3ub( 64, 64, 64 );
|
---|
448 | glBegin( GL_TRIANGLE_STRIP );
|
---|
449 | glVertex2i( GLUI_HSLIDER_ARROW_WIDTH+1, 1 );
|
---|
450 | glVertex2i( GLUI_HSLIDER_ARROW_WIDTH+1, h );
|
---|
451 | glVertex2i( w - GLUI_HSLIDER_ARROW_WIDTH, 1 );
|
---|
452 | glVertex2i( w - GLUI_HSLIDER_ARROW_WIDTH, h );
|
---|
453 | glEnd();
|
---|
454 |
|
---|
455 | glDisable( GL_POLYGON_STIPPLE );
|
---|
456 |
|
---|
457 | glColor3ub( 0, 0, 0 );
|
---|
458 | glBegin( GL_LINES );
|
---|
459 | glVertex2i( GLUI_HSLIDER_ARROW_WIDTH+1, h );
|
---|
460 | glVertex2i( w - GLUI_HSLIDER_ARROW_WIDTH, h );
|
---|
461 | glVertex2i( GLUI_HSLIDER_ARROW_WIDTH+1, 1 );
|
---|
462 | glVertex2i( w - GLUI_HSLIDER_ARROW_WIDTH, 1 );
|
---|
463 | glEnd();
|
---|
464 |
|
---|
465 | bleft = ledge_to_pixel( button_left_edge );
|
---|
466 | bright = redge_to_pixel( button_right_edge );
|
---|
467 |
|
---|
468 |
|
---|
469 | glColor3ub( 192, 192, 192 );
|
---|
470 | glBegin( GL_TRIANGLE_STRIP );
|
---|
471 | glVertex2i( bleft+2, 2 );
|
---|
472 | glVertex2i( bleft+2, h );
|
---|
473 | glVertex2i( bright, 2 );
|
---|
474 | glVertex2i( bright, h );
|
---|
475 | glEnd();
|
---|
476 |
|
---|
477 | glui->draw_raised_box( bleft+1, 1, bright-bleft-1, h-1 );
|
---|
478 |
|
---|
479 |
|
---|
480 | /* mjb */
|
---|
481 | /* do something different to indicate this is a range slider: */
|
---|
482 |
|
---|
483 | #define HANDLE 4
|
---|
484 |
|
---|
485 | if( button_is_resizable )
|
---|
486 | {
|
---|
487 | glColor3ub( 0, 0, 0 );
|
---|
488 |
|
---|
489 | glBegin( GL_TRIANGLE_STRIP );
|
---|
490 | glVertex2i( bleft, 1 );
|
---|
491 | glVertex2i( bleft, h );
|
---|
492 | glVertex2i( bleft+HANDLE, 1 );
|
---|
493 | glVertex2i( bleft+HANDLE, h );
|
---|
494 | glEnd();
|
---|
495 |
|
---|
496 | glBegin( GL_TRIANGLE_STRIP );
|
---|
497 | glVertex2i( bright-HANDLE, 1 );
|
---|
498 | glVertex2i( bright-HANDLE, h );
|
---|
499 | glVertex2i( bright, 1 );
|
---|
500 | glVertex2i( bright, h );
|
---|
501 | glEnd();
|
---|
502 | }
|
---|
503 |
|
---|
504 | /* mjb */
|
---|
505 |
|
---|
506 | glui->restore_draw_buffer(dbstate);
|
---|
507 | restore_window( orig );
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 |
|
---|
512 | /************************************** GLUI_HSlider::update_size() **********/
|
---|
513 |
|
---|
514 | void GLUI_HSlider::update_size( void )
|
---|
515 | {
|
---|
516 | if ( NOT glui )
|
---|
517 | return;
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 |
|
---|
522 |
|
---|
523 | void GLUI_HSlider::set_float_limits( float low, float high )
|
---|
524 | {
|
---|
525 | float_low = low;
|
---|
526 | float_high = high;
|
---|
527 |
|
---|
528 | int_low = round( low );
|
---|
529 | int_high = round( high );
|
---|
530 |
|
---|
531 | if( button_left_edge < float_low )
|
---|
532 | {
|
---|
533 | button_right_edge += button_left_edge - float_low;
|
---|
534 | button_left_edge = float_low;
|
---|
535 | } else if( button_left_edge > float_high )
|
---|
536 | {
|
---|
537 | button_right_edge += float_high - button_left_edge;
|
---|
538 | button_left_edge = float_high;
|
---|
539 | }
|
---|
540 |
|
---|
541 | if( button_right_edge < button_left_edge )
|
---|
542 | button_right_edge = button_left_edge;
|
---|
543 | if( button_right_edge > float_high )
|
---|
544 | button_right_edge = float_high;
|
---|
545 |
|
---|
546 | translate_and_draw_front();
|
---|
547 | }
|
---|
548 |
|
---|
549 |
|
---|
550 |
|
---|
551 | void GLUI_HSlider::set_int_limits( int low, int high )
|
---|
552 | {
|
---|
553 | set_float_limits( (float)low, (float)high );
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 |
|
---|
558 | float GLUI_HSlider::get_slider_val()
|
---|
559 | {
|
---|
560 | return button_left_edge;
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 |
|
---|
565 |
|
---|
566 | /* finds the value closest to pct */
|
---|
567 | /* 0. <= t <= 1. */
|
---|
568 |
|
---|
569 | float GLUI_HSlider::find_val( float t )
|
---|
570 | {
|
---|
571 | return float_low + t * ( float_high - float_low );
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 |
|
---|
576 | static inline int round( float x )
|
---|
577 | {
|
---|
578 | if( x >= 0. )
|
---|
579 | return (int)( x + .5 );
|
---|
580 |
|
---|
581 | return (int)( x - .5 );
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 |
|
---|
586 |
|
---|
587 | /* moves the value to cur + dx */
|
---|
588 |
|
---|
589 | void GLUI_HSlider::move_val( float dx )
|
---|
590 | {
|
---|
591 | button_left_edge += dx;
|
---|
592 | button_right_edge += dx;
|
---|
593 | reconcile_edge();
|
---|
594 | update_slider_val();
|
---|
595 | translate_and_draw_front();
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 |
|
---|
600 | int GLUI_HSlider::sync_slider()
|
---|
601 | {
|
---|
602 | int changed = 0;
|
---|
603 | float left, right;
|
---|
604 |
|
---|
605 |
|
---|
606 | if( live_type == GLUI_LIVE_FLOAT )
|
---|
607 | left = ((float*)ptr_val)[0];
|
---|
608 | else
|
---|
609 | left = (float) ((int*)ptr_val)[0];
|
---|
610 |
|
---|
611 | if( button_left_edge != left )
|
---|
612 | changed = 1;
|
---|
613 | button_left_edge = left;
|
---|
614 |
|
---|
615 |
|
---|
616 | if( button_is_resizable )
|
---|
617 | {
|
---|
618 | if( live_type == GLUI_LIVE_FLOAT )
|
---|
619 | right = ((float*)ptr_val)[1];
|
---|
620 | else
|
---|
621 | right = (float) ((int*)ptr_val)[1];
|
---|
622 |
|
---|
623 | if( button_right_edge != right )
|
---|
624 | changed = 1;
|
---|
625 | button_right_edge = right;
|
---|
626 | }
|
---|
627 |
|
---|
628 | return changed;
|
---|
629 | }
|
---|
630 |
|
---|
631 |
|
---|
632 |
|
---|
633 | void GLUI_HSlider::update_slider_val()
|
---|
634 | {
|
---|
635 | int changed = 0;
|
---|
636 | float left, right;
|
---|
637 |
|
---|
638 | if( live_type == GLUI_LIVE_FLOAT )
|
---|
639 | left = ((float*)ptr_val)[0];
|
---|
640 | else
|
---|
641 | left = (float) ((int*)ptr_val)[0];
|
---|
642 |
|
---|
643 | if( button_left_edge != left )
|
---|
644 | changed = 1;
|
---|
645 |
|
---|
646 | if( button_is_resizable )
|
---|
647 | {
|
---|
648 | if( live_type == GLUI_LIVE_FLOAT )
|
---|
649 | right = ((float*)ptr_val)[1];
|
---|
650 | else
|
---|
651 | right = (float) ((int*)ptr_val)[1];
|
---|
652 |
|
---|
653 | if( button_right_edge != right )
|
---|
654 | changed = 1;
|
---|
655 |
|
---|
656 | if( live_type == GLUI_LIVE_FLOAT )
|
---|
657 | ((float*)ptr_val)[1] = button_right_edge;
|
---|
658 | else
|
---|
659 | ((int*)ptr_val)[1] = round( button_right_edge );
|
---|
660 | }
|
---|
661 |
|
---|
662 | if( live_type == GLUI_LIVE_FLOAT )
|
---|
663 | ((float*)ptr_val)[0] = button_left_edge;
|
---|
664 | else
|
---|
665 | ((int*)ptr_val)[0] = round( button_left_edge );
|
---|
666 |
|
---|
667 |
|
---|
668 | if ( changed )
|
---|
669 | {
|
---|
670 | this->execute_callback();
|
---|
671 | // glui->post_update_main_gfx(); // MJB -- was commented out
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 |
|
---|
677 | void GLUI_HSlider::set_slider_val( float low )
|
---|
678 | {
|
---|
679 | button_left_edge = low;
|
---|
680 | reconcile_edge();
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 |
|
---|
685 | void GLUI_HSlider::set_slider_val( float low, float high )
|
---|
686 | {
|
---|
687 | button_left_edge = low;
|
---|
688 | button_right_edge = high;
|
---|
689 | reconcile_edge();
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 |
|
---|
694 | void GLUI_HSlider::set_slider_val( int low, int high )
|
---|
695 | {
|
---|
696 | set_slider_val( (float)low, (float)high );
|
---|
697 | }
|
---|
698 |
|
---|
699 | #define HANDLEWIDTH 6
|
---|
700 |
|
---|
701 | int GLUI_HSlider::redge_to_pixel( float val )
|
---|
702 | {
|
---|
703 | /* remove extra space between, and left arrow space */
|
---|
704 | int pixels;
|
---|
705 | float pct;
|
---|
706 |
|
---|
707 | pixels = w - 2*GLUI_HSLIDER_ARROW_WIDTH - 4;
|
---|
708 |
|
---|
709 | if( ! button_is_resizable )
|
---|
710 | {
|
---|
711 | return ledge_to_pixel( button_left_edge ) + HANDLEWIDTH;
|
---|
712 |
|
---|
713 | #if 0
|
---|
714 | /* right edge is a "constant" offset from left edge */
|
---|
715 | /* ??? */
|
---|
716 | xdist = pixels - float_high + float_low;
|
---|
717 | if( xdist < 4 )
|
---|
718 | xdist = 4;
|
---|
719 | else
|
---|
720 | xdist += 4;
|
---|
721 |
|
---|
722 | pixels = ledge_to_pixel( button_left_edge ) + xdist;
|
---|
723 | return pixels;
|
---|
724 | #endif
|
---|
725 | }
|
---|
726 |
|
---|
727 | pct = (val - float_low) / (float_high - float_low);
|
---|
728 | return GLUI_HSLIDER_ARROW_WIDTH + 4 + round( pct*pixels );
|
---|
729 | }
|
---|
730 |
|
---|
731 |
|
---|
732 |
|
---|
733 | int GLUI_HSlider::ledge_to_pixel( float val )
|
---|
734 | {
|
---|
735 | /* remove extra space between, and left arrow space */
|
---|
736 | int xdist;
|
---|
737 | int pixels;
|
---|
738 | float pct;
|
---|
739 |
|
---|
740 | pixels = w - 2*GLUI_HSLIDER_ARROW_WIDTH - 4;
|
---|
741 | pct = (val - float_low) / (float_high - float_low);
|
---|
742 | return GLUI_HSLIDER_ARROW_WIDTH + round( pct*pixels );
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 |
|
---|
747 |
|
---|
748 | float GLUI_HSlider::pixel_to_redge( int val )
|
---|
749 | {
|
---|
750 | int pixels;
|
---|
751 | float pct;
|
---|
752 |
|
---|
753 | pixels = w - 2*GLUI_HSLIDER_ARROW_WIDTH - 4;
|
---|
754 | pct = ( (float)(val - GLUI_HSLIDER_ARROW_WIDTH - 4) ) / (float)pixels;
|
---|
755 | return find_val( pct );
|
---|
756 | }
|
---|
757 |
|
---|
758 |
|
---|
759 |
|
---|
760 |
|
---|
761 | float GLUI_HSlider::pixel_to_ledge( int val )
|
---|
762 | {
|
---|
763 | int pixels, xdist;
|
---|
764 | float pct;
|
---|
765 |
|
---|
766 | pixels = w - 2*GLUI_HSLIDER_ARROW_WIDTH - 4;
|
---|
767 |
|
---|
768 | if( ! button_is_resizable )
|
---|
769 | {
|
---|
770 | xdist = pixels - float_high + float_low;
|
---|
771 | if( xdist > 4 )
|
---|
772 | return val + float_low - GLUI_HSLIDER_ARROW_WIDTH;
|
---|
773 | }
|
---|
774 |
|
---|
775 | pct = ( (float)(val - GLUI_HSLIDER_ARROW_WIDTH) ) / (float)pixels;
|
---|
776 | return find_val( pct );
|
---|
777 | }
|
---|
778 |
|
---|
779 |
|
---|
780 |
|
---|
781 | void GLUI_HSlider::move_button_pixels( int nval, int &oval )
|
---|
782 | {
|
---|
783 | float dx;
|
---|
784 | int xdist, npos;
|
---|
785 | int pixels;
|
---|
786 |
|
---|
787 | /* nval and oval are in pixels, change them to user units: */
|
---|
788 |
|
---|
789 | pixels = w - 2*GLUI_HSLIDER_ARROW_WIDTH - 4;
|
---|
790 | dx = (float)(nval - oval) * ( float_high - float_low ) / (float)pixels;
|
---|
791 | button_left_edge += dx;
|
---|
792 | button_right_edge += dx;
|
---|
793 | oval = nval;
|
---|
794 | reconcile_edge();
|
---|
795 | update_slider_val();
|
---|
796 | translate_and_draw_front();
|
---|
797 | }
|
---|