]> git.mxchange.org Git - flightgear.git/blob - Lib/PUI/puObject.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / PUI / puObject.cxx
1
2 #include "puLocal.h"
3
4 inline float clamp01 ( float x )
5 {
6   return (x >= 1.0f) ? 1.0f : x ;
7 }
8
9 static void load_colour_scheme ( float col[][4], float r, float g,
10                                                  float b, float a )
11 {
12   puSetColour ( col [ PUCOL_FOREGROUND ], r, g, b, a ) ;
13   puSetColour ( col [ PUCOL_BACKGROUND ], r/2, g/2, b/2, a ) ;
14   puSetColour ( col [ PUCOL_HIGHLIGHT  ], clamp01(r*1.3f), clamp01(g*1.3f),
15                                              clamp01(b*1.3f), a ) ;
16
17   if ( 4 * g + 3 * r + b > 0.5 )
18     puSetColour ( col [ PUCOL_LEGEND ], 0.0, 0.0, 0.0, a ) ;
19   else
20     puSetColour ( col [ PUCOL_LEGEND ], 1.0, 1.0, 1.0, a ) ;
21 }
22
23
24 static int    defaultStyle = PUSTYLE_DEFAULT ;
25 static puFont defaultLegendFont = NULL ;
26 static puFont defaultLabelFont  = NULL ;
27 static float  defaultColourScheme [ 4 ] ;
28
29 void puSetDefaultStyle ( int  style ) { defaultStyle = style ; }
30 int   puGetDefaultStyle ( void ) { return defaultStyle ; }
31
32 void puSetDefaultFonts ( puFont legendFont, puFont labelFont )
33 {
34   defaultLegendFont = legendFont ;
35   defaultLabelFont  = labelFont ;
36 }
37
38 void puGetDefaultFonts ( puFont *legendFont, puFont *labelFont )
39 {
40   if ( legendFont ) *legendFont = defaultLegendFont ;
41   if ( labelFont  ) *labelFont  = defaultLabelFont  ;
42 }
43
44 void puSetDefaultColourScheme ( float r, float g, float b, float a )
45 {
46   defaultColourScheme[0] = r ;
47   defaultColourScheme[1] = g ;
48   defaultColourScheme[2] = b ;
49   defaultColourScheme[3] = a ;
50   load_colour_scheme ( _puDefaultColourTable, r, g, b, a ) ;
51 }
52
53 void puGetDefaultColourScheme ( float *r, float *g, float *b, float *a )
54 {
55   if ( r ) *r = defaultColourScheme[0] ;
56   if ( g ) *g = defaultColourScheme[1] ;
57   if ( b ) *b = defaultColourScheme[2] ;
58   if ( a ) *a = defaultColourScheme[3] ;
59 }
60
61
62
63 void puObject::setColourScheme ( float r, float g, float b, float a )
64 {
65   load_colour_scheme ( colour, r, g, b, a ) ;
66 }
67
68 puObject::puObject ( int minx, int miny, int maxx, int maxy ) : puValue ()
69 {
70   type |= PUCLASS_OBJECT ;
71   bbox.min[0] = abox.min[0] = minx ;
72   bbox.min[1] = abox.min[1] = miny ;
73   bbox.max[0] = abox.max[0] = maxx ;
74   bbox.max[1] = abox.max[1] = maxy ;
75
76   active_mouse_edge = PU_UP ;
77   style      = defaultStyle ;
78   visible = active  = TRUE  ;
79   highlighted       = FALSE ;
80   am_default        = FALSE ;
81
82   cb          = NULL ;
83   user_data   = NULL ;
84   next = prev = NULL ;
85   label       = NULL ;
86   labelPlace  = PUPLACE_DEFAULT   ;
87   labelFont   = defaultLabelFont  ;
88   legend      = NULL ;
89   legendFont  = defaultLegendFont ;
90
91   for ( int i = 0 ; i < PUCOL_MAX ; i++ )
92     puSetColour ( colour[i], _puDefaultColourTable[i] ) ;
93
94   if ( ! puNoInterface() )
95   {
96     parent = puGetCurrInterface() ;
97     parent -> add ( this ) ;
98   }
99   else
100     parent = NULL ;
101 }
102
103
104 puObject::~puObject ()
105 {
106   if ( parent != this && parent != NULL )
107     parent -> remove ( this ) ;
108 }
109
110 void puObject::recalc_bbox ( void )
111 {
112   bbox = abox ;
113
114   if ( label != NULL )
115     switch ( labelPlace )
116     {
117       case PUPLACE_ABOVE : bbox.max[1] += puGetStringHeight ( getLabelFont() ) + puGetStringDescender ( getLabelFont () ) + PUSTR_TGAP + PUSTR_BGAP ; break ;
118       case PUPLACE_BELOW : bbox.min[1] -= puGetStringHeight ( getLabelFont() ) + puGetStringDescender ( getLabelFont () ) + PUSTR_TGAP + PUSTR_BGAP ; break ;
119       case PUPLACE_LEFT  : bbox.min[0] -= puGetStringWidth  ( getLabelFont(), getLabel() ) + PUSTR_LGAP + PUSTR_RGAP ; break ;
120       case PUPLACE_RIGHT : bbox.max[0] += puGetStringWidth  ( getLabelFont(), getLabel() ) + PUSTR_LGAP + PUSTR_RGAP ; break ;
121     }
122
123   if ( parent != NULL )
124     parent -> recalc_bbox () ;
125 }
126
127 void puObject::draw_label ( int dx, int dy )
128 {
129   if ( !visible ) return ;
130
131   /* If greyed out then halve the opacity when drawing the label */
132
133   if ( active )
134     glColor4fv ( colour [ PUCOL_LABEL ] ) ;
135   else
136     glColor4f ( colour [ PUCOL_LABEL ][0],
137                 colour [ PUCOL_LABEL ][1],
138                 colour [ PUCOL_LABEL ][2],
139                 colour [ PUCOL_LABEL ][3] / 2.0f ) ; /* 50% more transparent */
140
141   switch ( labelPlace )
142   {
143     case PUPLACE_ABOVE : puDrawString ( labelFont, label, dx + abox.min[0] + PUSTR_LGAP, dy + abox.max[1] + puGetStringDescender(labelFont) + PUSTR_BGAP ) ; break ;
144     case PUPLACE_BELOW : puDrawString ( labelFont, label, dx + abox.min[0] + PUSTR_LGAP, dy + bbox.min[1] + puGetStringDescender(labelFont) + PUSTR_BGAP ) ; break ;
145     case PUPLACE_LEFT  : puDrawString ( labelFont, label, dx + bbox.min[0] + PUSTR_LGAP, dy + abox.min[1] + puGetStringDescender(labelFont) + PUSTR_BGAP ) ; break ;
146     case PUPLACE_RIGHT : puDrawString ( labelFont, label, dx + abox.max[0] + PUSTR_LGAP, dy + abox.min[1] + puGetStringDescender(labelFont) + PUSTR_BGAP ) ; break ;
147   }
148 }
149
150
151 int puObject::checkKey ( int key, int updown )
152 {
153   if ( updown == PU_UP )
154     return FALSE ;
155
156   if ( isReturnDefault() && ( key == '\r' || key == '\n' ) )
157   {
158     checkHit ( PU_LEFT_BUTTON, PU_DOWN, (abox.min[0]+abox.max[0])/2,
159                                         (abox.min[1]+abox.max[1])/2 ) ;
160     checkHit ( PU_LEFT_BUTTON, PU_UP  , (abox.min[0]+abox.max[0])/2,
161                                         (abox.min[1]+abox.max[1])/2 ) ;
162     return TRUE ;
163   }
164
165   return FALSE ;
166 }
167
168
169 void puObject::doHit ( int button, int updown, int x, int y )
170 {
171   (x,x);(y,y);
172
173   if ( button == PU_LEFT_BUTTON )
174   {
175     if ( updown == active_mouse_edge || active_mouse_edge == PU_UP_AND_DOWN )
176     {
177       lowlight () ;
178       invokeCallback () ;
179     }
180     else
181       highlight () ;
182   }
183   else
184     lowlight () ;
185 }
186
187 int puObject::checkHit ( int button, int updown, int x, int y )
188 {
189   if ( isHit( x, y ) )
190   {
191     doHit ( button, updown, x, y ) ;
192     return TRUE ;
193   }
194
195   lowlight () ;
196   return FALSE ;
197 }
198
199  
200 char *puValue::getTypeString ( void )
201 {
202   int i = getType () ;
203
204   if ( i & PUCLASS_DIALOGBOX   ) return "puDialogBox" ;
205   if ( i & PUCLASS_SLIDER      ) return "puSlider" ;
206   if ( i & PUCLASS_BUTTONBOX   ) return "puButtonBox" ;
207   if ( i & PUCLASS_INPUT       ) return "puInput" ;
208   if ( i & PUCLASS_MENUBAR     ) return "puMenuBar" ;
209   if ( i & PUCLASS_POPUPMENU   ) return "puPopupMenu" ;
210   if ( i & PUCLASS_POPUP       ) return "puPopup" ;
211   if ( i & PUCLASS_ONESHOT     ) return "puOneShot" ;
212   if ( i & PUCLASS_BUTTON      ) return "puButton" ;
213   if ( i & PUCLASS_TEXT        ) return "puText" ;
214   if ( i & PUCLASS_FRAME       ) return "puFrame" ;
215   if ( i & PUCLASS_INTERFACE   ) return "puInterface" ;
216   if ( i & PUCLASS_OBJECT      ) return "puObject" ;
217   if ( i & PUCLASS_VALUE       ) return "puValue" ;
218
219   return "Unknown Object type." ;
220 }
221
222