2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 #pragma off (unreferenced)
15 static char rcsid[] = "$Id: window.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
16 #pragma on (unreferenced)
33 #define W_BACKGROUND (wnd->background )
36 #define W_WIDTH (wnd->width)
37 #define W_HEIGHT (wnd->height)
38 #define W_OLDCANVAS (wnd->oldcanvas)
39 #define W_CANVAS (wnd->canvas)
40 #define W_GADGET (wnd->gadget)
41 #define W_TEXT_X (wnd->text_x)
42 #define W_TEXT_Y (wnd->text_y)
43 #define W_NEXT (wnd->next)
44 #define W_PREV (wnd->prev)
47 UI_WINDOW * CurWindow = NULL;
48 UI_WINDOW * FirstWindow = NULL;
49 UI_WINDOW * LastWindow = NULL;
51 int last_keypress = 0;
53 #define BORDER_WIDTH 8
55 static unsigned int FrameCount = 0;
56 unsigned int ui_event_counter = 0;
57 unsigned int ui_number_of_events = 0;
58 static UI_EVENT * EventBuffer = NULL;
59 static int Record = 0;
60 static int RecordFlags = 0;
62 static short MouseDX=0, MouseDY=0, MouseButtons=0;
64 static unsigned char SavedState[256];
66 static int PlaybackSpeed = 1;
68 extern void ui_draw_frame( short x1, short y1, short x2, short y2 );
70 #define TICKER (*(volatile int *)0x46C)
72 // 1=1x faster, 2=2x faster, etc
73 void ui_set_playback_speed( int speed )
75 PlaybackSpeed = speed;
78 int ui_record_events( int NumberOfEvents, UI_EVENT * buffer, int Flags )
80 if ( Record > 0 || buffer==NULL ) return 1;
86 ui_number_of_events = NumberOfEvents;
91 int ui_play_events_realtime( int NumberOfEvents, UI_EVENT * buffer )
93 if ( buffer == NULL ) return 1;
98 ui_number_of_events = NumberOfEvents;
101 keyd_last_released= 0;
102 keyd_last_pressed= 0;
103 for (i=0; i<256; i++ )
104 SavedState[i] = keyd_pressed[i];
110 int ui_play_events_fast( int NumberOfEvents, UI_EVENT * buffer )
113 if ( buffer == NULL ) return 1;
115 EventBuffer = buffer;
117 ui_event_counter = 0;
118 ui_number_of_events = NumberOfEvents;
121 keyd_last_released= 0;
122 keyd_last_pressed= 0;
124 //mprintf( 0, "Before: ", i );
125 for (i=0; i<256; i++ )
127 //if (keyd_pressed[i]) mprintf( 0, "%d ", i );
128 SavedState[i] = keyd_pressed[i];
130 //mprintf( 0, "\n" );
136 // Returns: 0=Normal, 1=Recording, 2=Playback normal, 3=Playback fast
137 int ui_recorder_status()
142 void add_window_to_end( UI_WINDOW * wnd )
146 LastWindow->next = wnd;
153 void add_window_to_beg( UI_WINDOW * wnd )
156 W_NEXT = FirstWindow;
157 FirstWindow->prev = wnd;
165 void add_window_after( UI_WINDOW * w1, UI_WINDOW * w2 )
170 if (w1->next == NULL )
178 UI_WINDOW *sav, *wnd = LastWindow;
183 ui_close_window(wnd);
188 void remove_window( UI_WINDOW * wnd )
191 W_NEXT->prev = W_PREV;
193 W_PREV->next = W_NEXT;
194 if (FirstWindow == wnd )
195 FirstWindow = W_NEXT;
196 if (LastWindow == wnd )
198 W_NEXT = W_PREV = NULL;
203 UI_WINDOW * ui_open_window( short x, short y, short w, short h, int flags )
206 int sw, sh, req_w, req_h;
208 wnd = (UI_WINDOW *)malloc(sizeof(UI_WINDOW));
209 if (wnd==NULL) exit(1);
214 add_window_to_end( wnd );
216 sw = grd_curscreen->sc_w;
217 sh = grd_curscreen->sc_h;
219 mouse_set_limits( 0,0, sw-1, sh-1 );
224 if (flags & WIN_BORDER)
233 if ( (x+w-1) >= sw ) x = sw - w;
235 if ( (y+h-1) >= sh ) y = sh - h;
241 W_OLDCANVAS = grd_curcanv;
243 wnd->keyboard_focus_gadget = NULL;
247 if (flags & WIN_SAVE_BG)
249 W_BACKGROUND = gr_create_bitmap( w, h );
250 gr_bm_ubitblt(w, h, 0, 0, x, y, &(grd_curscreen->sc_canvas.cv_bitmap), W_BACKGROUND );
255 if (flags & WIN_BORDER)
257 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x+BORDER_WIDTH, y+BORDER_WIDTH, req_w, req_h );
258 gr_set_current_canvas( NULL );
259 ui_draw_frame( x, y, x+w-1, y+h-1 );
262 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x, y, req_w, req_h );
264 gr_set_current_canvas( W_CANVAS );
266 if (flags & WIN_FILLED)
267 ui_draw_box_out( 0, 0, req_w-1, req_h-1 );
269 gr_set_fontcolor( CBLACK, CWHITE );
271 selected_gadget = NULL;
280 void ui_close_window( UI_WINDOW * wnd )
285 ui_gadget_delete_all( wnd );
289 gr_bm_ubitblt(W_WIDTH, W_HEIGHT, W_X, W_Y, 0, 0, W_BACKGROUND, &(grd_curscreen->sc_canvas.cv_bitmap));
290 gr_free_bitmap( W_BACKGROUND );
293 gr_set_current_canvas( NULL );
294 gr_setcolor( CBLACK );
295 gr_rect( W_X, W_Y, W_X+W_WIDTH-1, W_Y+W_HEIGHT-1 );
298 gr_free_sub_canvas( W_CANVAS );
300 gr_set_current_canvas( W_OLDCANVAS );
302 selected_gadget = NULL;
304 remove_window( wnd );
318 //mprintf( 0, "After: " );
319 for (i=0; i<256; i++ )
321 //if (SavedState[i]) mprintf( 0, "%d ", i );
322 keyd_pressed[i] = SavedState[i];
324 //mprintf( 0, "\n" );
331 void ui_reset_idle_seconds()
336 int ui_get_idle_seconds()
338 return (((TICKER - last_event)*10)/182);
341 void ui_mega_process()
349 mouse_get_delta( &mx, &my );
352 Mouse.new_buttons = mouse_get_btns();
353 last_keypress = key_inkey();
355 if ( Mouse.new_buttons || last_keypress || Mouse.new_dx || Mouse.new_dy ) {
362 if (ui_event_counter==0 )
364 EventBuffer[ui_event_counter].frame = 0;
365 EventBuffer[ui_event_counter].type = 7;
366 EventBuffer[ui_event_counter].data = ui_number_of_events;
371 if (ui_event_counter==1 && (RecordFlags & UI_RECORD_MOUSE) )
373 Mouse.new_buttons = 0;
374 EventBuffer[ui_event_counter].frame = FrameCount;
375 EventBuffer[ui_event_counter].type = 6;
376 EventBuffer[ui_event_counter].data = ((Mouse.y & 0xFFFF) << 16) | (Mouse.x & 0xFFFF);
380 mouse_get_delta( &mx, &my );
383 MouseButtons = mouse_get_btns();
385 Mouse.new_dx = MouseDX;
386 Mouse.new_dy = MouseDY;
388 if ((MouseDX != 0 || MouseDY != 0) && (RecordFlags & UI_RECORD_MOUSE) )
390 if (ui_event_counter < ui_number_of_events-1 )
392 EventBuffer[ui_event_counter].frame = FrameCount;
393 EventBuffer[ui_event_counter].type = 1;
394 EventBuffer[ui_event_counter].data = ((MouseDY & 0xFFFF) << 16) | (MouseDX & 0xFFFF);
396 //mprintf( 0, "EVENT:%d, Mouse moved %d,%d\n", ui_event_counter, MouseDX, MouseDY );
403 if ( (MouseButtons != Mouse.new_buttons) && (RecordFlags & UI_RECORD_MOUSE) )
405 Mouse.new_buttons = MouseButtons;
407 if (ui_event_counter < ui_number_of_events-1 )
409 EventBuffer[ui_event_counter].frame = FrameCount;
410 EventBuffer[ui_event_counter].type = 2;
411 EventBuffer[ui_event_counter].data = MouseButtons;
413 //mprintf( 0, "EVENT:%d, Mouse buttons changed %d\n", ui_event_counter, MouseButtons );
421 if ( keyd_last_pressed && (RecordFlags & UI_RECORD_KEYS) )
424 k = keyd_last_pressed;
425 keyd_last_pressed= 0;
428 if (ui_event_counter < ui_number_of_events-1 )
430 EventBuffer[ui_event_counter].frame = FrameCount;
431 EventBuffer[ui_event_counter].type = 3;
432 EventBuffer[ui_event_counter].data = k;
434 //mprintf( 0, "EVENT:%d, Key %d pressed\n", ui_event_counter, k );
441 if ( keyd_last_released && (RecordFlags & UI_RECORD_KEYS) )
444 k = keyd_last_released;
445 keyd_last_released= 0;
448 if (ui_event_counter < ui_number_of_events-1 )
450 EventBuffer[ui_event_counter].frame = FrameCount;
451 EventBuffer[ui_event_counter].type = 4;
452 EventBuffer[ui_event_counter].data = k;
454 //mprintf( 0, "EVENT:%d, Key %d released\n", ui_event_counter, k );
460 last_keypress = key_inkey();
462 if (last_keypress == KEY_F12 )
464 ui_number_of_events = ui_event_counter;
470 if ((last_keypress != 0) && (RecordFlags & UI_RECORD_KEYS) )
472 if (ui_event_counter < ui_number_of_events-1 )
474 EventBuffer[ui_event_counter].frame = FrameCount;
475 EventBuffer[ui_event_counter].type = 5;
476 EventBuffer[ui_event_counter].data = last_keypress;
478 //mprintf( 0, "EVENT:%d, Keypressed %d\n", ui_event_counter, last_keypress );
491 Mouse.new_buttons = 0;
494 if ( keyd_last_pressed ) {
496 k = keyd_last_pressed;
497 keyd_last_pressed = 0;
502 if ( keyd_last_released )
505 k = keyd_last_released;
506 keyd_last_released = 0;
511 if (key_inkey() == KEY_F12 )
513 //mprintf( 0, "Playing stopped.\n" );
519 if (EventBuffer==NULL) {
525 while( (ui_event_counter < ui_number_of_events) && (EventBuffer[ui_event_counter].frame <= FrameCount) )
527 switch ( EventBuffer[ui_event_counter].type )
529 case 1: // Mouse moved
530 Mouse.new_dx = EventBuffer[ui_event_counter].data & 0xFFFF;
531 Mouse.new_dy = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
533 case 2: // Mouse buttons changed
534 Mouse.new_buttons = EventBuffer[ui_event_counter].data;
536 case 3: // Key moved down
537 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 1;
539 case 4: // Key moved up
540 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 0;
542 case 5: // Key pressed
543 last_keypress = EventBuffer[ui_event_counter].data;
545 case 6: // Initial Mouse X position
546 Mouse.x = EventBuffer[ui_event_counter].data & 0xFFFF;
547 Mouse.y = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
553 if (ui_event_counter >= ui_number_of_events )
557 //( 0, "Done playing %d events.\n", ui_number_of_events );
567 if ( ui_event_counter < ui_number_of_events )
569 next_frame = EventBuffer[ui_event_counter].frame;
571 if ( (FrameCount+PlaybackSpeed) < next_frame )
572 FrameCount = next_frame - PlaybackSpeed;
582 if ( ui_event_counter < ui_number_of_events )
583 FrameCount = EventBuffer[ui_event_counter].frame;
596 void ui_wprintf( UI_WINDOW * wnd, char * format, ... )
601 va_start(args, format );
602 vsprintf(buffer,format,args);
604 gr_set_current_canvas( W_CANVAS );
607 W_TEXT_X = gr_string( W_TEXT_X, W_TEXT_Y, buffer );
611 void ui_wprintf_at( UI_WINDOW * wnd, short x, short y, char * format, ... )
616 va_start(args, format );
617 vsprintf(buffer,format,args);
619 gr_set_current_canvas( W_CANVAS );
622 gr_string( x, y, buffer );