]> git.mxchange.org Git - simgear.git/blob - PUI/puSlider.cxx
Pui -> PUI.
[simgear.git] / PUI / puSlider.cxx
1
2 #include "puLocal.h"
3
4 void puSlider::draw ( int dx, int dy )
5 {
6   if ( !visible ) return ;
7
8   abox . draw ( dx, dy,
9                 style==PUSTYLE_BEVELLED ? -PUSTYLE_BOXED : -style,
10                 colour, FALSE ) ;
11
12   int sd, od ;
13
14   if ( isVertical() ) { sd = 1 ; od = 0 ; } else { sd = 0 ; od = 1 ; }
15
16   int sz = abox.max [sd] - abox.min [sd] ;
17
18   float val ;
19
20   getValue ( & val ) ;
21
22   if ( val < 0.0f ) val = 0.0f ;
23   if ( val > 1.0f ) val = 1.0f ;
24
25   val *= (float) sz * (1.0f - slider_fraction) ;
26
27   puBox bx ;
28     
29   bx . min [ sd ] = abox . min [ sd ] + (int) val ;
30   bx . max [ sd ] = (int) ( (float) bx . min [ sd ] + (float) sz * slider_fraction ) ;
31   bx . min [ od ] = abox . min [ od ] + 2 ;
32   bx . max [ od ] = abox . max [ od ] - 2 ;
33
34   bx . draw ( dx, dy, PUSTYLE_SMALL_BEVELLED, colour, FALSE ) ;
35
36   /* If greyed out then halve the opacity when drawing the label and legend */
37
38   if ( active )
39     glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
40   else
41     glColor4f ( colour [ PUCOL_LEGEND ][0],
42                 colour [ PUCOL_LEGEND ][1],
43                 colour [ PUCOL_LEGEND ][2],
44                 colour [ PUCOL_LEGEND ][3] / 2.0 ) ; /* 50% more transparent */
45
46   int xx = ( abox.max[0] - abox.min[0] - puGetStringWidth(legendFont,legend) ) / 2 ;
47   int yy = ( abox.max[1] - abox.min[1] - puGetStringHeight(legendFont) ) / 2 ;
48
49   puDrawString ( legendFont, legend,
50                   dx + abox.min[0] + xx,
51                   dy + abox.min[1] + yy ) ;
52
53   draw_label ( dx, dy ) ;
54 }
55
56
57 void puSlider::doHit ( int button, int updown, int x, int y )
58 {
59   if ( button == PU_LEFT_BUTTON )
60   {
61     int sd = isVertical() ;
62     int sz = abox.max [sd] - abox.min [sd] ;
63     int coord = isVertical() ? y : x ;
64
65     float next_value ;
66
67     if ( sz == 0 )
68       next_value = 0.5f ;
69     else
70     {
71       next_value = ( (float)coord - (float)abox.min[sd] - (float)sz * slider_fraction / 2.0f ) /
72                    ( (float) sz * (1.0f - slider_fraction) ) ;
73     }
74
75     next_value = (next_value < 0.0f) ? 0.0f : (next_value > 1.0) ? 1.0f : next_value ;
76
77     setValue ( next_value ) ;
78
79     switch ( cb_mode )
80     {
81       case PUSLIDER_CLICK :
82         if ( updown == active_mouse_edge )
83         {
84           last_cb_value = next_value ;
85           invokeCallback () ;
86         }
87         break ;
88
89       case PUSLIDER_DELTA :
90         if ( fabs ( last_cb_value - next_value ) >= cb_delta )
91         {
92           last_cb_value = next_value ;
93           invokeCallback () ;
94         }
95         break ;
96
97       case PUSLIDER_ALWAYS :
98       default :
99         last_cb_value = next_value ;
100         invokeCallback () ;
101         break ;
102     }
103   }
104 }
105
106