forked from nigels-com/glui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glui_control.cpp
1249 lines (979 loc) · 30.7 KB
/
glui_control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
GLUI User Interface Toolkit
---------------------------
glui_control.cpp - top-level GLUI_Control class
--------------------------------------------------
Copyright (c) 1998 Paul Rademacher
WWW: https://github.com/libglui/glui
Issues: https://github.com/libglui/glui/issues
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*****************************************************************************/
#include "glui_internal_control.h"
#include "tinyformat.h"
#include <algorithm>
int _glui_draw_border_only = 0;
/********* GLUI_Control::GLUI_Control() **********/
GLUI_Control::GLUI_Control()
{
x_off = GLUI_XOFF;
y_off_top = GLUI_YOFF;
y_off_bot = GLUI_YOFF;
x_abs = GLUI_XOFF;
y_abs = GLUI_YOFF;
active = false;
enabled = true;
int_val = 0;
last_live_int = 0;
float_array_size = 0;
name = tfm::format("Control: %p", this);
float_val = 0.0;
last_live_float = 0.0;
ptr_val = NULL;
glui = NULL;
w = GLUI_DEFAULT_CONTROL_WIDTH;
h = GLUI_DEFAULT_CONTROL_HEIGHT;
font = NULL;
active_type = GLUI_CONTROL_ACTIVE_MOUSEDOWN;
alignment = GLUI_ALIGN_LEFT;
is_container = false;
can_activate = true; /* By default, you can activate a control */
spacebar_mouse_click = true; /* Does spacebar simulate a mouse click? */
live_type = GLUI_LIVE_NONE;
text = "";
last_live_text = "";
live_inited = false;
collapsible = false;
is_open = true;
hidden = false;
memset(char_widths, -1, sizeof(char_widths)); /* JVK */
int i;
for( i=0; i<GLUI_DEF_MAX_ARRAY; i++ )
float_array_val[i] = last_live_float_array[i] = 0.0;
}
/********* GLUI_Control::~GLUI_Control() **********/
GLUI_Control::~GLUI_Control()
{
GLUI_Control *item = (GLUI_Control*) this->first_child();
while (item)
{
GLUI_Control *tmp = item;
item = (GLUI_Control*) item->next();
delete tmp;
}
}
/*************************** Drawing Utility routines *********************/
/* Redraw this control. */
void GLUI_Control::redraw() {
if (glui==NULL || hidden) return;
if (glui->should_redraw_now(this))
translate_and_draw_front();
}
/** Redraw everybody in our window. */
void GLUI_Control::redraw_window() {
if (glui==NULL || hidden) return;
if ( glui->get_glut_window_id() == -1 ) return;
int orig = set_to_glut_window();
glutPostRedisplay();
restore_window(orig);
}
/* GLUI_Control::translate_and_draw_front() ********/
void GLUI_Control::translate_and_draw_front()
{
GLUI_DRAWINGSENTINAL_IDIOM
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
translate_to_origin();
draw(0,0);
glPopMatrix();
}
/********** GLUI_Control::set_to_bkgd_color() ********/
void GLUI_Control::set_to_bkgd_color()
{
if ( NOT glui )
return;
glColor3ubv( glui->bkgd_color );
}
/******** GLUI_Control::draw_box_inwards_outline() ********/
void GLUI_Control::draw_box_inwards_outline( int x_min, int x_max, int y_min, int y_max )
{
glBegin( GL_LINES );
glColor3f( .5, .5, .5 );
glVertex2i( x_min, y_min ); glVertex2i( x_max, y_min );
glVertex2i( x_min, y_min ); glVertex2i( x_min, y_max );
glColor3f( 1., 1., 1. );
glVertex2i( x_min, y_max ); glVertex2i( x_max, y_max );
glVertex2i( x_max, y_max ); glVertex2i( x_max, y_min );
if ( enabled )
glColor3f( 0., 0., 0. );
else
glColor3f( .25, .25, .25 );
glVertex2i( x_min+1, y_min+1 ); glVertex2i( x_max-1, y_min+1 );
glVertex2i( x_min+1, y_min+1 ); glVertex2i( x_min+1, y_max-1 );
glColor3f( .75, .75, .75 );
glVertex2i( x_min+1, y_max-1 ); glVertex2i( x_max-1, y_max-1 );
glVertex2i( x_max-1, y_max-1 ); glVertex2i( x_max-1, y_min+1 );
glEnd();
}
/******* GLUI_Control::draw_box() **********/
void GLUI_Control::draw_box( int x_min, int x_max, int y_min, int y_max, float r, float g, float b)
{
if ( r == 1.0 AND g == 1.0 AND b == 1.0 AND NOT enabled AND glui ) {
draw_bkgd_box( x_min, x_max, y_min, y_max );
return;
}
glColor3f( r, g, b );
glBegin( GL_QUADS );
glVertex2i( x_min, y_min ); glVertex2i( x_max, y_min );
glVertex2i( x_max, y_max ); glVertex2i( x_min, y_max );
glEnd();
}
/******* GLUI_Control::draw_bkgd_box() **********/
void GLUI_Control::draw_bkgd_box( int x_min, int x_max, int y_min, int y_max )
{
set_to_bkgd_color();
glBegin( GL_QUADS );
glVertex2i( x_min, y_min ); glVertex2i( x_max, y_min );
glVertex2i( x_max, y_max ); glVertex2i( x_min, y_max );
glEnd();
}
/**** GLUI_Control::draw_active_area() ********/
void GLUI_Control::draw_active_box( int x_min, int x_max, int y_min, int y_max )
{
GLUI_DRAWINGSENTINAL_IDIOM
if ( active ) {
glEnable( GL_LINE_STIPPLE );
glLineStipple( 1, 0x5555 );
glColor3f( 0., 0., 0. );
} else {
set_to_bkgd_color();
}
glBegin( GL_LINE_LOOP );
glVertex2i(x_min, y_min); glVertex2i( x_max, y_min );
glVertex2i(x_max, y_max); glVertex2i( x_min, y_max );
glEnd();
glDisable( GL_LINE_STIPPLE );
}
/**** GLUI_Control::draw_emboss_box() ********/
void GLUI_Control::draw_emboss_box(int x_min,int x_max,int y_min,int y_max)
{
glLineWidth( 1.0 );
glColor3f( 1.0, 1.0, 1.0 );
glBegin( GL_LINE_LOOP );
glVertex2i( x_min, y_min ); glVertex2i( x_max, y_min );
glVertex2i( x_max, y_max ); glVertex2i( x_min, y_max );
glEnd();
glBegin( GL_LINE_LOOP );
glVertex2i( x_min+1, y_min+1 ); glVertex2i( x_max-1, y_min+1 );
glVertex2i( x_max-1, y_max-1 ); glVertex2i( x_min+1, y_max-1 );
glEnd();
glColor3f( .5, .5, .5 );
glBegin( GL_LINE_LOOP );
glVertex2i( x_min, y_min );
glVertex2i( x_max-1, y_min );
glVertex2i( x_max-1, y_max-1 );
glVertex2i( x_min, y_max-1 );
glEnd();
}
/******* GLUT_Control::draw_recursive() **********/
void GLUI_Control::draw_recursive( int x, int y )
{
GLUI_Control *node;
/* printf( "%s %d\n", this->name.c_str(), this->hidden );*/
if ( NOT can_draw() )
return;
/*if ( 1 ) { -- Debugging to check control width
glColor3f( 1.0, 0.0, 0.0 );
glBegin( GL_LINES );
glVertex2i( x_abs, y_abs );00
glVertex2i( x_abs+w, y_abs );
glEnd();
}*/
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glTranslatef( (float) this->x_abs + .5,
(float) this->y_abs + .5,
0.0 );
if ( NOT _glui_draw_border_only ) {
if ( NOT strcmp( name.c_str(), "Rollout" ) ) {
}
this->draw( this->x_off, this->y_off_top );
}
else
{
if ( dynamic_cast<GLUI_Column*>(this) ) {
/* printf( "%s w/h: %d/%d\n", (char*) name, w, h ); */
/*w = 2; */
}
/* The following draws the area of each control */
glColor3f( 1.0, 0.0, 0.0 );
glBegin( GL_LINE_LOOP );
glVertex2i( 0, 0 ); glVertex2i( w, 0 );
glVertex2i( w, h ); glVertex2i( 0, h );
glEnd();
}
glPopMatrix();
node = (GLUI_Control*) first_child();
while( node ) {
node->draw_recursive( node->x_abs, node->y_abs );
node = (GLUI_Control*) node->next();
}
}
/****** GLUI_Control::set_to_glut_window() *********/
/* Sets the current window to the glut window associated with this control */
int GLUI_Control::set_to_glut_window()
{
int orig_window;
if ( NOT glui)
return 1;
orig_window = glutGetWindow();
glutSetWindow( glui->get_glut_window_id());
return orig_window;
}
/********** GLUI_Control::restore_window() *********/
void GLUI_Control::restore_window(int orig)
{
if ( orig > 0 )
glutSetWindow( orig );
}
/****************************** Text ***************************/
/*************** GLUI_Control::set_font() **********/
void GLUI_Control::set_font(void *new_font)
{
font = new_font;
redraw();
}
/********** GLUI_Control::draw_string() ************/
void GLUI_Control::draw_string( const GLUI_String &text )
{
_glutBitmapString( get_font(), text );
}
/**************** GLUI_Control::draw_char() ********/
void GLUI_Control::draw_char(char c)
{
glutBitmapCharacter( get_font(), c );
}
/*********** GLUI_Control::string_width() **********/
int GLUI_Control::string_width(const GLUI_String &text)
{
return _glutBitmapWidthString( get_font(), text );
}
/************* GLUI_Control::char_width() **********/
int GLUI_Control::char_width(char c)
{ /* Hash table for faster character width lookups - JVK
Speeds up the textbox a little bit.
*/
int hash_index = c % CHAR_WIDTH_HASH_SIZE;
if (char_widths[hash_index][0] != c) {
char_widths[hash_index][0] = c;
char_widths[hash_index][1] = glutBitmapWidth( get_font(), c );
}
return char_widths[hash_index][1];
}
/*************** GLUI_Control::get_font() **********/
void *GLUI_Control::get_font()
{
/*** Does this control have its own font? ***/
if ( this->font != NULL )
return this->font;
/*** Does the parent glui have a font? ***/
if ( glui )
return glui->font;
/*** Return the default font ***/
return GLUT_BITMAP_HELVETICA_12;
}
/************* GLUI_Control::draw_name() ***********/
/* This draws the name of the control as either black (if enabled), or */
/* embossed if disabled. */
void GLUI_Control::draw_name(int x, int y)
{
if ( NOT can_draw() )
return;
if ( enabled )
{
set_to_bkgd_color();
glRasterPos2i(x+1, y+1);
draw_string(name);
glColor3b( 0, 0, 0 );
glRasterPos2i(x, y);
draw_string(name);
}
else
{ /* Control is disabled - emboss the string */
glColor3f( 1.0f, 1.0f, 1.0f );
glRasterPos2i(x+1, y+1);
draw_string(name);
glColor3f( .4f, .4f, .4f );
glRasterPos2i(x, y);
draw_string(name);
}
}
/**************************** Layout and Packing *********************/
/****** GLUI_Control::align() **************/
void GLUI_Control::align()
{
int col_x, col_y, col_w, col_h, col_x_off, col_y_off;
int orig_x_abs;
orig_x_abs = x_abs;
/* Fix alignment bug relating to columns */
/*return; */
if ( NOT parent() )
return; /* Clearly this shouldn't happen, though */
get_this_column_dims(&col_x, &col_y, &col_w, &col_h,
&col_x_off, &col_y_off);
if ( dynamic_cast<GLUI_Column*>(this) ) {
/* if ( this->prev() != NULL ) {
((GLUI_Control*)prev())->get_this_column_dims(&col_x, &col_y, &col_w, &col_h,
&col_x_off, &col_y_off);
x_abs = col_x + col_w;
}
else {
x_abs = ((GLUI_Control*)parent())->x_abs;
}
*/
return;
}
if ( alignment == GLUI_ALIGN_LEFT ) {
x_abs = col_x + col_x_off;
}
else if ( alignment == GLUI_ALIGN_RIGHT ) {
x_abs = col_x + col_w - col_x_off - this->w;
}
else if ( alignment == GLUI_ALIGN_CENTER ) {
x_abs = col_x + (col_w - this->w) / 2;
}
if ( this->is_container ) {
/*** Shift all child columns ***/
int delta = x_abs - orig_x_abs;
GLUI_Control *node;
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
node->x_abs += delta;
}
node = (GLUI_Control*) node->next();
}
}
}
/************** GLUI_Control::pack() ************/
/* Recalculate positions and offsets */
void GLUI_Control::pack_old(int x, int y)
{
GLUI_Control *node;
int max_w, curr_y, curr_x, max_y;
int x_in = x, y_in =y;
int x_margin, y_margin_top, y_margin_bot;
int y_top_column;
int column_x;
GLUI_Column *curr_column = NULL;
this->update_size();
x_margin = this->x_off;
y_margin_top = this->y_off_top;
y_margin_bot = this->y_off_bot;
this->x_abs = x_in;
this->y_abs = y_in;
max_w = -1;
max_y = -1;
curr_x = this->x_abs + x_margin;
curr_y = this->y_abs + y_margin_top;
/*** Record start of this set of columns ***/
y_top_column = curr_y;
column_x = 0;
if ( this == glui->main_panel ) {
x=x;
}
/*** Iterate over children, packing them first ***/
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible) {
/* Pad some space above fixed size panels */
curr_y += GLUI_ITEMSPACING;
}
else if ( dynamic_cast<GLUI_Column*>(node)) {
curr_column = (GLUI_Column*) node;
if ( 1 ) {
column_x += max_w + 2 * x_margin;
curr_x += max_w + 2 * x_margin;
}
else {
column_x += max_w + 0 * x_margin;
curr_x += max_w + 0 * x_margin;
}
/*node->pack( curr_x, curr_y ); */
node->x_abs = curr_x;
node->y_abs = y_top_column;
node->w = 2;
node->h = curr_y - y_top_column;
curr_x += x_margin * 3 + 40;
curr_y = y_top_column;
max_w = 0;
node = (GLUI_Control*) node->next();
continue;
}
node->pack( curr_x, curr_y );
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible)
/* Pad some space below fixed size panels */
curr_y += GLUI_ITEMSPACING;
curr_y += node->h;
if ( node->w > max_w ) {
max_w = node->w;
if ( curr_column != NULL )
curr_column->w = max_w;
}
node = (GLUI_Control*) node->next();
if ( node ) {
curr_y += GLUI_ITEMSPACING;
}
if ( curr_y > max_y )
max_y = curr_y;
}
if ( this->is_container ) {
max_y += y_margin_bot; /*** Add bottom border inside box */
if ( this->first_child() ) {
if ( dynamic_cast<GLUI_Rollout*>(this) ) {
/** We don't want the rollout to shrink in width when it's
closed **/
this->w = std::max(this->w, column_x + max_w + 2 * x_margin );
}
else {
this->w = column_x + max_w + 2 * x_margin;
}
this->h = (max_y - y_in);
}
else { /* An empty container, so just assign default w & h */
this->w = GLUI_DEFAULT_CONTROL_WIDTH;
this->h = GLUI_DEFAULT_CONTROL_HEIGHT;
}
/** Expand panel if necessary (e.g., to include all the text in
a panel label) **/
this->update_size();
}
}
/*** GLUI_Control::get_this_column_dims() **********/
/* Gets the x,y,w,h,and x/y offsets of the column to which a control belongs */
void GLUI_Control::get_this_column_dims( int *col_x, int *col_y,
int *col_w, int *col_h,
int *col_x_off, int *col_y_off )
{
GLUI_Control *node, *parent_ptr;
int parent_h, parent_y_abs;
parent_ptr = (GLUI_Control*) parent();
if ( parent_ptr==NULL )
return;
parent_h = parent_ptr->h;
parent_y_abs = parent_ptr->y_abs;
if ( dynamic_cast<GLUI_Panel*>(parent_ptr) AND
parent_ptr->int_val == GLUI_PANEL_EMBOSSED AND
parent_ptr->name != "" ) {
parent_h -= GLUI_PANEL_EMBOSS_TOP;
parent_y_abs += GLUI_PANEL_EMBOSS_TOP;
}
if ( 0 ) {
GLUI_Node *first, *last, *curr;
/** Look for first control in this column **/
first = this;
while (first->prev() AND !dynamic_cast<GLUI_Column*>(first->prev()) )
first = first->prev();
/** Look for last control in this column **/
last = this;
while ( last->next() AND !dynamic_cast<GLUI_Column*>(first->next()) )
last = last->next();
curr = first;
int max_w = -1;
do {
if ( ((GLUI_Control*)curr)->w > max_w )
max_w = ((GLUI_Control*)curr)->w;
if ( curr == last )
break;
curr = curr->next();
} while( curr != NULL );
*col_x = ((GLUI_Control*)first)->x_abs;
*col_y = ((GLUI_Control*)first)->y_abs;
*col_w = max_w;
if ( parent() ) {
*col_h = ((GLUI_Control*)parent())->h;
*col_x_off = ((GLUI_Control*)parent())->x_off;
}
else {
*col_h = 10;
*col_x_off = 0;
}
*col_y_off = 0;
return;
}
if ( 1 ) { /* IS THIS WRONG? */
/*** Look for preceding column ***/
node = (GLUI_Control*) this->prev();
while( node ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
*col_x = node->x_abs;
*col_y = parent_y_abs;
*col_w = node->w;
*col_h = parent_h;
*col_x_off = node->x_off;
*col_y_off = 0;
return;
}
node = (GLUI_Control*) node->prev();
}
/*** Nope, Look for next column ***/
node = (GLUI_Control*) this->next();
while( node ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
*col_x = parent_ptr->x_abs;
*col_y = parent_y_abs;
*col_w = node->x_abs - parent_ptr->x_abs;
*col_h = parent_h;
*col_x_off = node->x_off;
*col_y_off = 0;
return;
}
node = (GLUI_Control*) node->next();
}
/*** This is single-column panel, so return panel dims ***/
*col_x = parent_ptr->x_abs;
*col_y = parent_y_abs;
*col_w = parent_ptr->w;
*col_h = parent_h;
*col_x_off = parent_ptr->x_off;
*col_y_off = 0;
}
}
void GLUI_Control::pack( int x, int y )
{
GLUI_Control *node;
int max_w, curr_y, curr_x, max_y;
int x_in = x, y_in =y;
int x_margin, y_margin_top, y_margin_bot;
int y_top_column;
int column_x;
GLUI_Column *curr_column = NULL;
this->update_size();
x_margin = this->x_off;
y_margin_top = this->y_off_top;
y_margin_bot = this->y_off_bot;
this->x_abs = x_in;
this->y_abs = y_in;
max_w = 0;
max_y = 0;
curr_x = this->x_abs + x_margin;
curr_y = this->y_abs + y_margin_top;
/*** Record start of this set of columns ***/
y_top_column = curr_y;
column_x = curr_x;
/*** Iterate over children, packing them first ***/
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible) {
/* Pad some space above fixed-size panels */
curr_y += GLUI_ITEMSPACING;
}
else if ( dynamic_cast<GLUI_Column*>(node) ) {
curr_column = (GLUI_Column*) node;
curr_x += max_w + 1 * x_margin;
column_x = curr_x;
node->x_abs = curr_x;
node->y_abs = y_top_column;
node->w = 2;
node->h = curr_y - y_top_column;
curr_x += x_margin * 1;
curr_y = y_top_column;
max_w = 0;
node = (GLUI_Control*) node->next();
continue;
}
node->pack( curr_x, curr_y );
if ( dynamic_cast<GLUI_Panel*>(node) && !node->collapsible)
/* Pad some space below fixed-size panels */
curr_y += GLUI_ITEMSPACING;
curr_y += node->h;
if ( node->w > max_w ) {
max_w = node->w;
if ( curr_column != NULL )
curr_column->w = max_w + x_margin;
}
if ( curr_y > max_y ) {
max_y = curr_y;
if ( curr_column != NULL )
curr_column->h = max_y - y_top_column;
}
node = (GLUI_Control*) node->next();
if ( node ) {
curr_y += GLUI_ITEMSPACING;
}
}
if ( this->is_container ) {
max_y += y_margin_bot; /*** Add bottom border inside box */
if ( this->first_child() ) {
this->w = column_x + max_w + 2 * x_margin - x_in;
this->h = (max_y - y_in);
}
else { /* An empty container, so just assign default w & h */
if ( !dynamic_cast<GLUI_Rollout*>(this) &&
!dynamic_cast<GLUI_Tree*>(this) ) {
this->w = GLUI_DEFAULT_CONTROL_WIDTH;
this->h = GLUI_DEFAULT_CONTROL_HEIGHT;
}
}
/** Expand panel if necessary (e.g., to include all the text in
a panel label) **/
this->update_size();
/*** Now we step through the GLUI_Columns, setting the 'h' ***/
node = (GLUI_Control*) this->first_child();
while( node != NULL ) {
if ( dynamic_cast<GLUI_Column*>(node) ) {
node->h = this->h - y_margin_bot - y_margin_top;
}
node = (GLUI_Control*) node->next();
}
}
}
/******************************** Live Variables **************************/
/*********** GLUI_Control::sync_live() ************/
/* Reads live variable and sets control to its current value */
/* This function is recursive, and operates on control's children */
void GLUI_Control::sync_live(int recurse, int draw_it)
{
GLUI_Node *node;
int sync_it=true;
int i;
float *fp;
bool changed = false;
/*** If this is currently active control, and mouse button is down,
don't sync ***/
if ( glui )
{
if ( this == glui->active_control AND glui->mouse_button_down )
sync_it = false;
/*** Actually, just disable syncing if button is down ***/
/*** Nope, go ahead and sync if mouse is down - this allows syncing in
callbacks ***/
if ( 0 ) { /* THIS CODE BELOW SHOULD NOT BE EXECUTED */
if ( glui->mouse_button_down ) {
/* printf( "Can't sync\n" ); */
return;
}
}
}
/*** If this control has a live variable, we check its current value
against the stored value in the control ***/
if ( ptr_val != NULL ) {
if ( live_type == GLUI_LIVE_NONE OR NOT sync_it ) {
}
else if ( live_type == GLUI_LIVE_INT ) {
if ( *((int*)ptr_val) != last_live_int ) {
set_int_val( *((int*)ptr_val) );
last_live_int = *((int*)ptr_val);
changed = true;
}
}
else if ( live_type == GLUI_LIVE_FLOAT ) {
if ( *((float*)ptr_val) != last_live_float ) {
set_float_val( *((float*)ptr_val) );
last_live_float = *((float*)ptr_val);
changed = true;
}
}
else if ( live_type == GLUI_LIVE_TEXT ) {
if ( last_live_text.compare((const char*)ptr_val) != 0 ) {
set_text( (char*) ptr_val );
last_live_text = (const char*)ptr_val;
changed = true;
}
}
else if ( live_type == GLUI_LIVE_STRING ) {
if ( last_live_text.compare(((std::string*) ptr_val)->c_str()) != 0 ) {
set_text( ((std::string*) ptr_val)->c_str());
last_live_text = *((std::string*) ptr_val);
changed = true;
}
}
else if ( live_type == GLUI_LIVE_FLOAT_ARRAY ) {
/*** Step through the arrays, and see if they're the same ***/
fp = (float*) ptr_val;
for ( i=0; i<float_array_size; i++ ) {
if ( *fp != last_live_float_array[i] ) {
changed = true;
break;
}
fp++;
}
if ( changed == true) {
fp = (float*) ptr_val;
set_float_array_val( fp );
for ( i=0; i<float_array_size; i++ ) {
last_live_float_array[i] = *fp;
fp++;
}
}
}
else if ( live_type == GLUI_LIVE_DOUBLE ) {
}
}
/*** If this control is changed and we're supposed to be drawing, then
draw it now ***/
if ( changed == true AND draw_it ) {
redraw();
}
if ( recurse ) {
/*** Now recursively output live vars for all children ***/
node = this->first_child();
while( node ) {
((GLUI_Control*) node)->sync_live(true, true);
node = node->next();
}
if ( collapsible == true AND is_open == false ) {
/** Here we have a collapsed control (e.g., a rollout that is closed **/
/** We need to go in and sync all the collapsed controls inside **/
node = this->collapsed_node.first_child();
while( node ) {
((GLUI_Control*) node)->sync_live(true, false);
node = node->next();
}
}
}
}
/********** GLUI_Control::output_live() ************/
/* Writes current value of control to live variable. */
void GLUI_Control::output_live(int update_main_gfx)
{
int i;
float *fp;
if ( ptr_val == NULL )
return;
if ( NOT live_inited )
return;
if ( live_type == GLUI_LIVE_NONE ) {
}
else if ( live_type == GLUI_LIVE_INT ) {
*((int*)ptr_val) = int_val;
last_live_int = int_val;
}
else if ( live_type == GLUI_LIVE_FLOAT ) {
*((float*)ptr_val) = float_val;
last_live_float = float_val;
}
else if ( live_type == GLUI_LIVE_TEXT ) {
strncpy( (char*) ptr_val, text.c_str(), text.length()+1);
last_live_text = text;
}
else if ( live_type == GLUI_LIVE_STRING ) {
(*(std::string*)ptr_val)= text.c_str();
last_live_text = text;
}
else if ( live_type == GLUI_LIVE_FLOAT_ARRAY ) {
fp = (float*) ptr_val;
for( i=0; i<float_array_size; i++ ) {
*fp = float_array_val[i];
last_live_float_array[i] = float_array_val[i];
fp++;
}
}
else if ( live_type == GLUI_LIVE_DOUBLE ) {
}
/** Update the main gfx window? **/
if ( update_main_gfx AND this->glui != NULL ) {
this->glui->post_update_main_gfx();
}
}
/****** GLUI_Control::execute_callback() **********/
void GLUI_Control::execute_callback()
{
int old_window;
old_window = glutGetWindow();
if ( glui AND glui->main_gfx_window_id != -1 )
glutSetWindow( glui->main_gfx_window_id );
this->callback( this );
// if ( this->callback )