]> git.mxchange.org Git - flightgear.git/blob - PUI/pu.cxx
ea197f7451aa1ccbc93e2c55455872d75e52918f
[flightgear.git] / PUI / pu.cxx
1
2 #include "puLocal.h"
3
4 #ifdef PU_NOT_USING_GLUT
5 #include <assert.h>
6 #include <iostream.h>
7 #endif
8
9 #define PU_STRING_X_FUDGE 6
10 #define PU_STRING_Y_FUDGE 6
11
12 int puRefresh = TRUE ;
13
14 #ifdef PU_NOT_USING_GLUT
15
16 static int puWindowWidth  = 400 ;
17 static int puWindowHeight = 400 ;
18
19 int puGetWindowHeight () { return puWindowHeight ; }
20 int puGetWindowWidth  () { return puWindowWidth  ; }
21
22 void puSetWindowSize ( int width, int height )
23 {
24   puWindowWidth  = width  ;
25   puWindowHeight = height ;
26 }
27
28 static int fontBase = 0;
29 static int fontSize[257];
30 #else
31
32 int puGetWindowHeight () { return glutGet ( (GLenum) GLUT_WINDOW_HEIGHT ) ; }
33 int puGetWindowWidth  () { return glutGet ( (GLenum) GLUT_WINDOW_WIDTH  ) ; }
34
35 void puSetWindowSize ( int width, int height )
36 {
37   fprintf ( stderr, "PUI: puSetWindowSize shouldn't be used with GLUT.\n" ) ;
38 }
39
40 #endif
41
42 puColour _puDefaultColourTable[] =
43 {
44   { 0.5f, 0.5f, 0.5f, 1.0f }, /* PUCOL_FOREGROUND */
45   { 0.3f, 0.3f, 0.3f, 1.0f }, /* PUCOL_BACKGROUND */
46   { 0.7f, 0.7f, 0.7f, 1.0f }, /* PUCOL_HIGHLIGHT  */
47   { 0.0f, 0.0f, 0.0f, 1.0f }, /* PUCOL_LABEL      */
48   { 1.0f, 1.0f, 1.0f, 1.0f }, /* PUCOL_TEXT       */
49
50   { 0.0f, 0.0f, 0.0f, 0.0f }  /* ILLEGAL */
51 } ;
52  
53
54 puValue::~puValue () {}  
55
56 static int _puCursor_enable = FALSE ;
57 static int _puCursor_x      = 0 ;
58 static int _puCursor_y      = 0 ;
59 static float _puCursor_bgcolour [4] = { 1.0f, 1.0f, 1.0f, 1.0f } ; 
60 static float _puCursor_fgcolour [4] = { 0.0f, 0.0f, 0.0f, 1.0f } ;  
61
62 void   puHideCursor ( void ) { _puCursor_enable = FALSE ; }
63 void   puShowCursor ( void ) { _puCursor_enable = TRUE  ; }
64 int    puCursorIsHidden ( void ) { return ! _puCursor_enable ; }
65
66 void puCursor ( int x, int y )
67 {
68   _puCursor_x = x ;
69   _puCursor_y = y ;
70 }
71
72 int puGetStringDescender ( void *fnt )
73 {
74   if ( fnt == NULL )
75     fnt = PUFONT_9_BY_15 ;
76
77   if ( fnt == PUFONT_8_BY_13        ) return 2 ;
78   if ( fnt == PUFONT_9_BY_15        ) return 3 ;
79   if ( fnt == PUFONT_TIMES_ROMAN_10 ) return 2 ;
80   if ( fnt == PUFONT_TIMES_ROMAN_24 ) return 5 ;
81   if ( fnt == PUFONT_HELVETICA_10   ) return 2 ;
82   if ( fnt == PUFONT_HELVETICA_12   ) return 3 ;
83   if ( fnt == PUFONT_HELVETICA_18   ) return 4 ;
84
85   return 0 ;
86 }
87
88 int puGetStringHeight ( void *fnt )
89 {
90   /* Height *excluding* descender */
91   if ( fnt == NULL )
92     fnt = PUFONT_9_BY_15 ;
93
94   if ( fnt == PUFONT_8_BY_13        ) return  9 ;
95   if ( fnt == PUFONT_9_BY_15        ) return 10 ;
96   if ( fnt == PUFONT_TIMES_ROMAN_10 ) return  7 ;
97   if ( fnt == PUFONT_TIMES_ROMAN_24 ) return 17 ;
98   if ( fnt == PUFONT_HELVETICA_10   ) return  8 ;
99   if ( fnt == PUFONT_HELVETICA_12   ) return  9 ;
100   if ( fnt == PUFONT_HELVETICA_18   ) return 14 ;
101
102   return 0 ;
103 }
104
105 int puGetStringWidth ( void *fnt, char *str )
106 {
107
108   if ( str == NULL )
109     return 0 ;
110
111   int res = 0 ;
112
113 #ifdef PU_NOT_USING_GLUT
114   while ( *str != '\0' )
115   {
116     res += fontSize [ *str ] ;
117     str++ ;
118   }
119 #else
120   if ( fnt == NULL )
121     fnt = PUFONT_9_BY_15 ;
122
123   while ( *str != '\0' )
124   {
125     res += glutBitmapWidth ( fnt, *str ) ;
126     str++ ;
127   }
128 #endif
129
130   return res ;
131 }
132
133
134 void puDrawString ( void *fnt, char *str, int x, int y )
135 {
136   if ( str == NULL )
137     return ;
138
139   glRasterPos2f((float)x, (float)y); 
140
141 #ifdef PU_NOT_USING_GLUT
142   /*
143     Display a string: 
144        indicate start of glyph display lists 
145   */
146
147   glListBase (fontBase);
148
149   /* Now draw the characters in a string */
150
151   int len = strlen(str);
152   glCallLists(len, GL_UNSIGNED_BYTE, str); 
153   glListBase(0);
154 #else
155   if ( fnt == NULL )
156     fnt = PUFONT_9_BY_15 ;
157
158   while ( *str != '\0' )
159   {
160     glutBitmapCharacter ( fnt, *str ) ;
161     str++ ;
162   }
163 #endif
164 }
165
166
167 static void puDrawCursor ( int x, int y )
168 {
169   glColor4fv ( _puCursor_bgcolour ) ;  
170
171   glBegin    ( GL_TRIANGLES ) ;
172   glVertex2i ( x, y ) ;
173   glVertex2i ( x + 13, y -  4 ) ;
174   glVertex2i ( x +  4, y - 13 ) ;
175
176   glVertex2i ( x +  8, y -  3 ) ;
177   glVertex2i ( x + 17, y - 12 ) ;
178   glVertex2i ( x + 12, y - 17 ) ;
179
180   glVertex2i ( x + 12, y - 17 ) ;
181   glVertex2i ( x +  3, y -  8 ) ;
182   glVertex2i ( x +  8, y -  3 ) ;
183   glEnd      () ;
184
185   glColor4fv ( _puCursor_fgcolour ) ;  
186
187   glBegin    ( GL_TRIANGLES ) ;
188   glVertex2i ( x+1, y-1 ) ;
189   glVertex2i ( x + 11, y -  4 ) ;
190   glVertex2i ( x +  4, y - 11 ) ;
191
192   glVertex2i ( x +  8, y -  5 ) ;
193   glVertex2i ( x + 15, y - 12 ) ;
194   glVertex2i ( x + 12, y - 15 ) ;
195
196   glVertex2i ( x + 12, y - 15 ) ;
197   glVertex2i ( x +  5, y -  8 ) ;
198   glVertex2i ( x +  8, y -  5 ) ;
199   glEnd      () ;
200 }
201
202 void  puInit ( void )
203 {
204   static int firsttime = TRUE ;
205
206   if ( firsttime )
207   {
208     puInterface *base_interface = new puInterface ( 0, 0 ) ;
209     puPushInterface     ( base_interface ) ;
210     puPushLiveInterface ( base_interface ) ;
211     firsttime = FALSE ;
212 #ifdef PU_NOT_USING_GLUT
213
214     /* Create bitmaps for the device context font's first 256 glyphs */
215
216     fontBase = glGenLists(256);
217     assert(fontBase);
218     HDC hdc = wglGetCurrentDC();
219
220     /* Make the system font the device context's selected font */
221
222     SelectObject (hdc, GetStockObject (SYSTEM_FONT)); 
223
224     int *tempSize = &fontSize[1];
225
226     if ( ! GetCharWidth32 ( hdc, 1, 255, tempSize ) )
227     {
228       LPVOID lpMsgBuf ;
229
230       FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
231                       FORMAT_MESSAGE_FROM_SYSTEM,
232                       NULL,
233                       GetLastError(),
234                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
235                       (LPTSTR) &lpMsgBuf,
236                       0, NULL ) ;
237
238       fprintf ( stderr, "PUI: Error: %s\n" (char *)lpMsgBuf ) ;
239       LocalFree ( lpMsgBuf ) ;
240     }
241     wglUseFontBitmaps ( hdc, 0, 256, fontBase ) ;
242 #endif
243   }
244 }
245
246 static void puSetOpenGLState ( void )
247 {
248   int w = puGetWindowWidth  () ;
249   int h = puGetWindowHeight () ;
250
251   glPushAttrib   ( GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_TRANSFORM_BIT ) ;
252   glDisable      ( GL_LIGHTING   ) ;
253   glDisable      ( GL_FOG        ) ;
254   glDisable      ( GL_TEXTURE_2D ) ;
255   glDisable      ( GL_DEPTH_TEST ) ;
256   glDisable      ( GL_CULL_FACE  ) ;
257  
258   glViewport     ( 0, 0, w, h ) ;
259   glMatrixMode   ( GL_PROJECTION ) ;
260   glPushMatrix   () ;
261   glLoadIdentity () ;
262   gluOrtho2D     ( 0, w, 0, h ) ;
263   glMatrixMode   ( GL_MODELVIEW ) ;
264   glPushMatrix   () ;
265   glLoadIdentity () ;
266 }
267
268 static void puRestoreOpenGLState ( void )
269 {
270   glMatrixMode   ( GL_PROJECTION ) ;
271   glPopMatrix    () ;
272   glMatrixMode   ( GL_MODELVIEW ) ;
273   glPopMatrix    () ;
274   glPopAttrib    () ;
275 }
276
277
278 void  puDisplay ( void )
279 {
280   puSetOpenGLState () ;
281   puGetUltimateLiveInterface () -> draw ( 0, 0 ) ;
282
283   int h = puGetWindowHeight () ;
284
285   if ( _puCursor_enable )
286     puDrawCursor ( _puCursor_x,
287                    h - _puCursor_y ) ;
288
289   puRestoreOpenGLState () ;
290 }
291
292 int puKeyboard ( int key, int updown )
293 {
294   return puGetBaseLiveInterface () -> checkKey ( key, updown ) ;
295 }
296
297
298 static int last_buttons = 0 ;
299 int puMouse ( int button, int updown, int x, int y )
300 {
301   puCursor ( x, y ) ;
302
303   int h = puGetWindowHeight () ;
304
305   if ( updown == PU_DOWN )
306     last_buttons |=  ( 1 << button ) ;
307   else
308     last_buttons &= ~( 1 << button ) ;
309
310   return puGetBaseLiveInterface () -> checkHit ( button, updown, x,
311                                  h - y ) ;
312 }
313
314 int puMouse ( int x, int y )
315 {
316   puCursor ( x, y ) ;
317
318   if ( last_buttons == 0 )
319     return FALSE ;
320
321   int button = (last_buttons & (1<<PU_LEFT_BUTTON  )) ?  PU_LEFT_BUTTON   :
322                (last_buttons & (1<<PU_MIDDLE_BUTTON)) ?  PU_MIDDLE_BUTTON :
323                (last_buttons & (1<<PU_RIGHT_BUTTON )) ?  PU_RIGHT_BUTTON  : 0 ;
324
325   int h = puGetWindowHeight () ;
326
327   return puGetBaseLiveInterface () -> checkHit ( button, PU_DRAG, x,
328                                  h - y ) ;
329 }
330