]> git.mxchange.org Git - flightgear.git/blob - PUI/pu.cxx
Pui -> PUI.
[flightgear.git] / PUI / pu.cxx
1
2 #include "puLocal.h"
3
4 #define PU_STRING_X_FUDGE 6
5 #define PU_STRING_Y_FUDGE 6
6
7 int puRefresh = TRUE ;
8
9 puColour _puDefaultColourTable[] =
10 {
11   { 0.5, 0.5, 0.5, 1.0 }, /* PUCOL_FOREGROUND */
12   { 0.3, 0.3, 0.3, 1.0 }, /* PUCOL_BACKGROUND */
13   { 0.7, 0.7, 0.7, 1.0 }, /* PUCOL_HIGHLIGHT  */
14   { 0.0, 0.0, 0.0, 1.0 }, /* PUCOL_LABEL      */
15   { 1.0, 1.0, 1.0, 1.0 }, /* PUCOL_TEXT       */
16
17   { 0.0, 0.0, 0.0, 0.0 }  /* ILLEGAL */
18 } ;
19  
20
21 puValue::~puValue () {}  
22
23 static int _puCursor_enable = FALSE ;
24 static int _puCursor_x      = 0 ;
25 static int _puCursor_y      = 0 ;
26 static float _puCursor_bgcolour [4] = { 1.0f, 1.0f, 1.0f, 1.0f } ; 
27 static float _puCursor_fgcolour [4] = { 0.0f, 0.0f, 0.0f, 1.0f } ;  
28
29 void   puHideCursor ( void ) { _puCursor_enable = FALSE ; }
30 void   puShowCursor ( void ) { _puCursor_enable = TRUE  ; }
31 int    puCursorIsHidden ( void ) { return ! _puCursor_enable ; }
32
33 void puCursor ( int x, int y )
34 {
35   _puCursor_x = x ;
36   _puCursor_y = y ;
37 }
38
39 int puGetStringDescender ( void *fnt )
40 {
41   if ( fnt == NULL )
42     fnt = GLUT_BITMAP_9_BY_15 ;
43
44   if ( fnt == GLUT_BITMAP_8_BY_13        ) return 2 ;
45   if ( fnt == GLUT_BITMAP_9_BY_15        ) return 3 ;
46   if ( fnt == GLUT_BITMAP_TIMES_ROMAN_10 ) return 2 ;
47   if ( fnt == GLUT_BITMAP_TIMES_ROMAN_24 ) return 5 ;
48   if ( fnt == GLUT_BITMAP_HELVETICA_10   ) return 2 ;
49   if ( fnt == GLUT_BITMAP_HELVETICA_12   ) return 3 ;
50   if ( fnt == GLUT_BITMAP_HELVETICA_18   ) return 4 ;
51
52   return 0 ;
53 }
54
55 int puGetStringHeight ( void *fnt )
56 {
57   /* Height *excluding* descender */
58
59   if ( fnt == NULL )
60     fnt = GLUT_BITMAP_9_BY_15 ;
61
62   if ( fnt == GLUT_BITMAP_8_BY_13        ) return  9 ;
63   if ( fnt == GLUT_BITMAP_9_BY_15        ) return 10 ;
64   if ( fnt == GLUT_BITMAP_TIMES_ROMAN_10 ) return  7 ;
65   if ( fnt == GLUT_BITMAP_TIMES_ROMAN_24 ) return 17 ;
66   if ( fnt == GLUT_BITMAP_HELVETICA_10   ) return  8 ;
67   if ( fnt == GLUT_BITMAP_HELVETICA_12   ) return  9 ;
68   if ( fnt == GLUT_BITMAP_HELVETICA_18   ) return 14 ;
69
70   return 0 ;
71 }
72
73 int puGetStringWidth ( void *fnt, char *str )
74 {
75   if ( str == NULL )
76     return 0 ;
77
78   if ( fnt == NULL )
79     fnt = GLUT_BITMAP_9_BY_15 ;
80
81   int res = 0 ;
82
83   while ( *str != '\0' )
84   {
85     res += glutBitmapWidth ( fnt, *str ) ;
86     str++ ;
87   }
88
89   return res ;
90 }
91
92
93 void puDrawString ( void *fnt, char *str, int x, int y )
94 {
95   if ( str == NULL )
96     return ;
97
98   if ( fnt == NULL )
99     fnt = GLUT_BITMAP_9_BY_15 ;
100
101   glRasterPos2f ( x, y ) ;
102
103   while ( *str != '\0' )
104   {
105     glutBitmapCharacter ( fnt, *str ) ;
106     str++ ;
107   }
108 }
109
110
111 static void puDrawCursor ( int x, int y )
112 {
113   glColor4fv ( _puCursor_bgcolour ) ;  
114
115   glBegin    ( GL_TRIANGLES ) ;
116   glVertex2i ( x, y ) ;
117   glVertex2i ( x + 13, y -  4 ) ;
118   glVertex2i ( x +  4, y - 13 ) ;
119
120   glVertex2i ( x +  8, y -  3 ) ;
121   glVertex2i ( x + 17, y - 12 ) ;
122   glVertex2i ( x + 12, y - 17 ) ;
123
124   glVertex2i ( x + 12, y - 17 ) ;
125   glVertex2i ( x +  3, y -  8 ) ;
126   glVertex2i ( x +  8, y -  3 ) ;
127   glEnd      () ;
128
129   glColor4fv ( _puCursor_fgcolour ) ;  
130
131   glBegin    ( GL_TRIANGLES ) ;
132   glVertex2i ( x+1, y-1 ) ;
133   glVertex2i ( x + 11, y -  4 ) ;
134   glVertex2i ( x +  4, y - 11 ) ;
135
136   glVertex2i ( x +  8, y -  5 ) ;
137   glVertex2i ( x + 15, y - 12 ) ;
138   glVertex2i ( x + 12, y - 15 ) ;
139
140   glVertex2i ( x + 12, y - 15 ) ;
141   glVertex2i ( x +  5, y -  8 ) ;
142   glVertex2i ( x +  8, y -  5 ) ;
143   glEnd      () ;
144 }
145
146 void  puInit ( void )
147 {
148   static int firsttime = TRUE ;
149
150   if ( firsttime )
151   {
152     puInterface *base_interface = new puInterface ( 0, 0 ) ;
153     puPushInterface     ( base_interface ) ;
154     puPushLiveInterface ( base_interface ) ;
155     firsttime = FALSE ;
156   }
157 }
158
159 static void puSetOpenGLState ( void )
160 {
161   int w = glutGet ( (GLenum) GLUT_WINDOW_WIDTH  ) ;
162   int h = glutGet ( (GLenum) GLUT_WINDOW_HEIGHT ) ;
163
164   glPushAttrib   ( GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_TRANSFORM_BIT ) ;
165   glDisable      ( GL_LIGHTING   ) ;
166   glDisable      ( GL_FOG        ) ;
167   glDisable      ( GL_TEXTURE_2D ) ;
168   glDisable      ( GL_DEPTH_TEST ) ;
169   glDisable      ( GL_CULL_FACE  ) ;
170  
171   glViewport     ( 0, 0, w, h ) ;
172   glMatrixMode   ( GL_PROJECTION ) ;
173   glPushMatrix   () ;
174   glLoadIdentity () ;
175   gluOrtho2D     ( 0, w, 0, h ) ;
176   glMatrixMode   ( GL_MODELVIEW ) ;
177   glPushMatrix   () ;
178   glLoadIdentity () ;
179 }
180
181 static void puRestoreOpenGLState ( void )
182 {
183   glMatrixMode   ( GL_PROJECTION ) ;
184   glPopMatrix    () ;
185   glMatrixMode   ( GL_MODELVIEW ) ;
186   glPopMatrix    () ;
187   glPopAttrib    () ;
188 }
189
190
191 void  puDisplay ( void )
192 {
193   puSetOpenGLState () ;
194   puGetUltimateLiveInterface () -> draw ( 0, 0 ) ;
195
196   if ( _puCursor_enable )
197     puDrawCursor ( _puCursor_x,
198                    glutGet((GLenum)GLUT_WINDOW_HEIGHT) - _puCursor_y ) ;
199
200   puRestoreOpenGLState () ;
201 }
202
203 int puKeyboard ( int key, int updown )
204 {
205   return puGetBaseLiveInterface () -> checkKey ( key, updown ) ;
206 }
207
208
209 static int last_buttons = 0 ;
210
211 int puMouse ( int button, int updown, int x, int y )
212 {
213   puCursor ( x, y ) ;
214
215   if ( updown == PU_DOWN )
216     last_buttons |=  ( 1 << button ) ;
217   else
218     last_buttons &= ~( 1 << button ) ;
219
220   return puGetBaseLiveInterface () -> checkHit ( button, updown, x,
221                                  glutGet((GLenum)GLUT_WINDOW_HEIGHT) - y ) ;
222 }
223
224 int puMouse ( int x, int y )
225 {
226   puCursor ( x, y ) ;
227
228   if ( last_buttons == 0 )
229     return FALSE ;
230
231   int button = (last_buttons & (1<<PU_LEFT_BUTTON  )) ?  PU_LEFT_BUTTON   :
232                (last_buttons & (1<<PU_MIDDLE_BUTTON)) ?  PU_MIDDLE_BUTTON :
233                (last_buttons & (1<<PU_RIGHT_BUTTON )) ?  PU_RIGHT_BUTTON  : 0 ;
234
235   return puGetBaseLiveInterface () -> checkHit ( button, PU_DRAG, x,
236                                  glutGet((GLenum)GLUT_WINDOW_HEIGHT) - y ) ;
237 }
238