]> git.mxchange.org Git - flightgear.git/blob - PUI/pu.cxx
Incorporated next version of PUI.
[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          ! GetCharWidth   ( hdc, 1, 255, tempSize ) )
228     {
229       LPVOID lpMsgBuf ;
230
231       FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
232                       FORMAT_MESSAGE_FROM_SYSTEM,
233                       NULL,
234                       GetLastError(),
235                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
236                       (LPTSTR) &lpMsgBuf,
237                       0, NULL ) ;
238
239       fprintf ( stderr, "PUI: Error: %s\n", (char *)lpMsgBuf ) ;
240       LocalFree ( lpMsgBuf ) ;
241     }
242
243     wglUseFontBitmaps ( hdc, 0, 256, fontBase ) ;
244 #endif
245   }
246 }
247
248 static void puSetOpenGLState ( void )
249 {
250   int w = puGetWindowWidth  () ;
251   int h = puGetWindowHeight () ;
252
253   glPushAttrib   ( GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_TRANSFORM_BIT ) ;
254   glDisable      ( GL_LIGHTING   ) ;
255   glDisable      ( GL_FOG        ) ;
256   glDisable      ( GL_TEXTURE_2D ) ;
257   glDisable      ( GL_DEPTH_TEST ) ;
258   glDisable      ( GL_CULL_FACE  ) ;
259  
260   glViewport     ( 0, 0, w, h ) ;
261   glMatrixMode   ( GL_PROJECTION ) ;
262   glPushMatrix   () ;
263   glLoadIdentity () ;
264   gluOrtho2D     ( 0, w, 0, h ) ;
265   glMatrixMode   ( GL_MODELVIEW ) ;
266   glPushMatrix   () ;
267   glLoadIdentity () ;
268 }
269
270 static void puRestoreOpenGLState ( void )
271 {
272   glMatrixMode   ( GL_PROJECTION ) ;
273   glPopMatrix    () ;
274   glMatrixMode   ( GL_MODELVIEW ) ;
275   glPopMatrix    () ;
276   glPopAttrib    () ;
277 }
278
279
280 void  puDisplay ( void )
281 {
282   puSetOpenGLState () ;
283   puGetUltimateLiveInterface () -> draw ( 0, 0 ) ;
284
285   int h = puGetWindowHeight () ;
286
287   if ( _puCursor_enable )
288     puDrawCursor ( _puCursor_x,
289                    h - _puCursor_y ) ;
290
291   puRestoreOpenGLState () ;
292 }
293
294 int puKeyboard ( int key, int updown )
295 {
296   return puGetBaseLiveInterface () -> checkKey ( key, updown ) ;
297 }
298
299
300 static int last_buttons = 0 ;
301 int puMouse ( int button, int updown, int x, int y )
302 {
303   puCursor ( x, y ) ;
304
305   int h = puGetWindowHeight () ;
306
307   if ( updown == PU_DOWN )
308     last_buttons |=  ( 1 << button ) ;
309   else
310     last_buttons &= ~( 1 << button ) ;
311
312   return puGetBaseLiveInterface () -> checkHit ( button, updown, x,
313                                  h - y ) ;
314 }
315
316 int puMouse ( int x, int y )
317 {
318   puCursor ( x, y ) ;
319
320   if ( last_buttons == 0 )
321     return FALSE ;
322
323   int button = (last_buttons & (1<<PU_LEFT_BUTTON  )) ?  PU_LEFT_BUTTON   :
324                (last_buttons & (1<<PU_MIDDLE_BUTTON)) ?  PU_MIDDLE_BUTTON :
325                (last_buttons & (1<<PU_RIGHT_BUTTON )) ?  PU_RIGHT_BUTTON  : 0 ;
326
327   int h = puGetWindowHeight () ;
328
329   return puGetBaseLiveInterface () -> checkHit ( button, PU_DRAG, x,
330                                  h - y ) ;
331 }
332