]> git.mxchange.org Git - flightgear.git/blob - Lib/PUI/puButtonBox.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / PUI / puButtonBox.cxx
1
2 #include "puLocal.h"
3
4 puButtonBox::puButtonBox ( int minx, int miny, int maxx, int maxy,
5                 char **labels, int one_button ) :
6                     puObject ( minx, miny, maxx, maxy )
7 {
8   type |= PUCLASS_BUTTONBOX ;
9   one_only = one_button ;
10
11   button_labels = labels ;
12
13   for ( num_kids = 0 ; button_labels [ num_kids ] != NULL ; num_kids++ )
14     /* Count number of labels */ ;
15 }
16
17
18 int puButtonBox::checkKey ( int key, int updown )
19 {
20   if ( updown == PU_UP ||
21        ! isReturnDefault() ||
22        ( key != '\r' && key != '\n' ) )
23     return FALSE ;
24
25   int v = getValue () ;
26
27   if ( ! one_only )
28     v = ~v ;
29   else
30   if ( v++ > num_kids )
31     v = 0 ;
32
33   setValue ( v ) ;
34   invokeCallback() ;
35   return TRUE ;
36 }
37
38
39 int puButtonBox::checkHit ( int button, int updown, int x, int y )
40 {
41   if ( ! isHit ( x, y ) ||
42        ( updown != active_mouse_edge &&
43          active_mouse_edge != PU_UP_AND_DOWN ) )
44     return FALSE ;
45
46   int i = num_kids - 1 - (( y - abox.min[1] - PUSTR_BGAP ) * num_kids ) /
47                           ( abox.max[1] - abox.min[1] - PUSTR_BGAP - PUSTR_TGAP ) ;
48
49   if ( i < 0 ) i = 0 ;
50   if ( i >= num_kids ) i = num_kids - 1 ;
51
52   if ( one_only )
53     setValue ( i ) ;
54   else
55     setValue ( getValue () ^ ( 1 << i ) ) ;
56
57   invokeCallback () ;
58   return TRUE ;
59 }
60
61
62 void puButtonBox::draw ( int dx, int dy )
63 {
64   if ( !visible ) return ;
65
66   abox . draw ( dx, dy, style, colour, isReturnDefault() ) ;
67
68   for ( int i = 0 ; i < num_kids ; i++ )
69   {
70     puBox tbox ;
71
72     tbox . min [ 0 ] = abox.min [ 0 ] + PUSTR_LGAP + PUSTR_LGAP ;
73     tbox . min [ 1 ] = abox.min [ 1 ] + ((abox.max[1]-abox.min[1]-PUSTR_TGAP-PUSTR_BGAP)/num_kids) * (num_kids-1-i) ;
74     tbox . max [ 0 ] = tbox.min [ 0 ] ;
75     tbox . max [ 1 ] = tbox.min [ 1 ] ;
76
77     if (( one_only && i == getValue() ) ||
78         ( !one_only && ((1<<i) & getValue() ) != 0 ) ) 
79       tbox . draw ( dx, dy + PUSTR_BGAP + PUSTR_BGAP, -PUSTYLE_RADIO, colour, FALSE ) ;
80     else
81       tbox . draw ( dx, dy + PUSTR_BGAP + PUSTR_BGAP, PUSTYLE_RADIO, colour, FALSE ) ;
82
83     /* If greyed out then halve the opacity when drawing the label and legend */
84
85     if ( active )
86       glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
87     else
88       glColor4f ( colour [ PUCOL_LEGEND ][0],
89                   colour [ PUCOL_LEGEND ][1],
90                   colour [ PUCOL_LEGEND ][2],
91                   colour [ PUCOL_LEGEND ][3] / 2.0f ) ; /* 50% more transparent */
92
93     puDrawString ( legendFont, button_labels[i],
94                    dx + tbox.min[0] + PU_RADIO_BUTTON_SIZE + PUSTR_LGAP,
95                    dy + tbox.min[1] + puGetStringDescender(legendFont) + PUSTR_BGAP  + PUSTR_BGAP) ;
96   }
97
98   draw_label ( dx, dy ) ;
99 }
100