1 /* $Id: window.c,v 1.4 2004-12-21 11:24:23 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 static char rcsid[] = "$Id: window.c,v 1.4 2004-12-21 11:24:23 btb Exp $";
39 #define W_BACKGROUND (wnd->background )
42 #define W_WIDTH (wnd->width)
43 #define W_HEIGHT (wnd->height)
44 #define W_OLDCANVAS (wnd->oldcanvas)
45 #define W_CANVAS (wnd->canvas)
46 #define W_GADGET (wnd->gadget)
47 #define W_TEXT_X (wnd->text_x)
48 #define W_TEXT_Y (wnd->text_y)
49 #define W_NEXT (wnd->next)
50 #define W_PREV (wnd->prev)
53 UI_WINDOW * CurWindow = NULL;
54 UI_WINDOW * FirstWindow = NULL;
55 UI_WINDOW * LastWindow = NULL;
57 int last_keypress = 0;
59 #define BORDER_WIDTH 8
61 static unsigned int FrameCount = 0;
62 unsigned int ui_event_counter = 0;
63 unsigned int ui_number_of_events = 0;
64 static UI_EVENT * EventBuffer = NULL;
65 static int Record = 0;
66 static int RecordFlags = 0;
68 static short MouseDX=0, MouseDY=0, MouseButtons=0;
70 static unsigned char SavedState[256];
72 static int PlaybackSpeed = 1;
74 extern void ui_draw_frame( short x1, short y1, short x2, short y2 );
76 #define TICKER (*(volatile int *)0x46C)
78 // 1=1x faster, 2=2x faster, etc
79 void ui_set_playback_speed( int speed )
81 PlaybackSpeed = speed;
84 int ui_record_events( int NumberOfEvents, UI_EVENT * buffer, int Flags )
86 if ( Record > 0 || buffer==NULL ) return 1;
92 ui_number_of_events = NumberOfEvents;
97 int ui_play_events_realtime( int NumberOfEvents, UI_EVENT * buffer )
99 if ( buffer == NULL ) return 1;
101 EventBuffer = buffer;
103 ui_event_counter = 0;
104 ui_number_of_events = NumberOfEvents;
107 keyd_last_released= 0;
108 keyd_last_pressed= 0;
109 for (i=0; i<256; i++ )
110 SavedState[i] = keyd_pressed[i];
116 int ui_play_events_fast( int NumberOfEvents, UI_EVENT * buffer )
119 if ( buffer == NULL ) return 1;
121 EventBuffer = buffer;
123 ui_event_counter = 0;
124 ui_number_of_events = NumberOfEvents;
127 keyd_last_released= 0;
128 keyd_last_pressed= 0;
130 //mprintf( 0, "Before: ", i );
131 for (i=0; i<256; i++ )
133 //if (keyd_pressed[i]) mprintf( 0, "%d ", i );
134 SavedState[i] = keyd_pressed[i];
136 //mprintf( 0, "\n" );
142 // Returns: 0=Normal, 1=Recording, 2=Playback normal, 3=Playback fast
143 int ui_recorder_status()
148 void add_window_to_end( UI_WINDOW * wnd )
152 LastWindow->next = wnd;
159 void add_window_to_beg( UI_WINDOW * wnd )
162 W_NEXT = FirstWindow;
163 FirstWindow->prev = wnd;
171 void add_window_after( UI_WINDOW * w1, UI_WINDOW * w2 )
176 if (w1->next == NULL )
184 UI_WINDOW *sav, *wnd = LastWindow;
189 ui_close_window(wnd);
194 void remove_window( UI_WINDOW * wnd )
197 W_NEXT->prev = W_PREV;
199 W_PREV->next = W_NEXT;
200 if (FirstWindow == wnd )
201 FirstWindow = W_NEXT;
202 if (LastWindow == wnd )
204 W_NEXT = W_PREV = NULL;
209 UI_WINDOW * ui_open_window( short x, short y, short w, short h, int flags )
212 int sw, sh, req_w, req_h;
214 wnd = (UI_WINDOW *)malloc(sizeof(UI_WINDOW));
215 if (wnd==NULL) exit(1);
220 add_window_to_end( wnd );
222 sw = grd_curscreen->sc_w;
223 sh = grd_curscreen->sc_h;
225 mouse_set_limits( 0,0, sw-1, sh-1 );
230 if (flags & WIN_BORDER)
239 if ( (x+w-1) >= sw ) x = sw - w;
241 if ( (y+h-1) >= sh ) y = sh - h;
247 W_OLDCANVAS = grd_curcanv;
249 wnd->keyboard_focus_gadget = NULL;
253 if (flags & WIN_SAVE_BG)
255 W_BACKGROUND = gr_create_bitmap( w, h );
256 gr_bm_ubitblt(w, h, 0, 0, x, y, &(grd_curscreen->sc_canvas.cv_bitmap), W_BACKGROUND );
261 if (flags & WIN_BORDER)
263 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x+BORDER_WIDTH, y+BORDER_WIDTH, req_w, req_h );
264 gr_set_current_canvas( NULL );
265 ui_draw_frame( x, y, x+w-1, y+h-1 );
268 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x, y, req_w, req_h );
270 gr_set_current_canvas( W_CANVAS );
272 if (flags & WIN_FILLED)
273 ui_draw_box_out( 0, 0, req_w-1, req_h-1 );
275 gr_set_fontcolor( CBLACK, CWHITE );
277 selected_gadget = NULL;
286 void ui_close_window( UI_WINDOW * wnd )
291 ui_gadget_delete_all( wnd );
295 gr_bm_ubitblt(W_WIDTH, W_HEIGHT, W_X, W_Y, 0, 0, W_BACKGROUND, &(grd_curscreen->sc_canvas.cv_bitmap));
296 gr_free_bitmap( W_BACKGROUND );
299 gr_set_current_canvas( NULL );
300 gr_setcolor( CBLACK );
301 gr_rect( W_X, W_Y, W_X+W_WIDTH-1, W_Y+W_HEIGHT-1 );
304 gr_free_sub_canvas( W_CANVAS );
306 gr_set_current_canvas( W_OLDCANVAS );
308 selected_gadget = NULL;
310 remove_window( wnd );
324 //mprintf( 0, "After: " );
325 for (i=0; i<256; i++ )
327 //if (SavedState[i]) mprintf( 0, "%d ", i );
328 keyd_pressed[i] = SavedState[i];
330 //mprintf( 0, "\n" );
337 void ui_reset_idle_seconds()
342 int ui_get_idle_seconds()
344 return (((TICKER - last_event)*10)/182);
347 void ui_mega_process()
355 mouse_get_delta( &mx, &my );
358 Mouse.new_buttons = mouse_get_btns();
359 last_keypress = key_inkey();
361 if ( Mouse.new_buttons || last_keypress || Mouse.new_dx || Mouse.new_dy ) {
368 if (ui_event_counter==0 )
370 EventBuffer[ui_event_counter].frame = 0;
371 EventBuffer[ui_event_counter].type = 7;
372 EventBuffer[ui_event_counter].data = ui_number_of_events;
377 if (ui_event_counter==1 && (RecordFlags & UI_RECORD_MOUSE) )
379 Mouse.new_buttons = 0;
380 EventBuffer[ui_event_counter].frame = FrameCount;
381 EventBuffer[ui_event_counter].type = 6;
382 EventBuffer[ui_event_counter].data = ((Mouse.y & 0xFFFF) << 16) | (Mouse.x & 0xFFFF);
386 mouse_get_delta( &mx, &my );
389 MouseButtons = mouse_get_btns();
391 Mouse.new_dx = MouseDX;
392 Mouse.new_dy = MouseDY;
394 if ((MouseDX != 0 || MouseDY != 0) && (RecordFlags & UI_RECORD_MOUSE) )
396 if (ui_event_counter < ui_number_of_events-1 )
398 EventBuffer[ui_event_counter].frame = FrameCount;
399 EventBuffer[ui_event_counter].type = 1;
400 EventBuffer[ui_event_counter].data = ((MouseDY & 0xFFFF) << 16) | (MouseDX & 0xFFFF);
402 //mprintf( 0, "EVENT:%d, Mouse moved %d,%d\n", ui_event_counter, MouseDX, MouseDY );
409 if ( (MouseButtons != Mouse.new_buttons) && (RecordFlags & UI_RECORD_MOUSE) )
411 Mouse.new_buttons = MouseButtons;
413 if (ui_event_counter < ui_number_of_events-1 )
415 EventBuffer[ui_event_counter].frame = FrameCount;
416 EventBuffer[ui_event_counter].type = 2;
417 EventBuffer[ui_event_counter].data = MouseButtons;
419 //mprintf( 0, "EVENT:%d, Mouse buttons changed %d\n", ui_event_counter, MouseButtons );
427 if ( keyd_last_pressed && (RecordFlags & UI_RECORD_KEYS) )
430 k = keyd_last_pressed;
431 keyd_last_pressed= 0;
434 if (ui_event_counter < ui_number_of_events-1 )
436 EventBuffer[ui_event_counter].frame = FrameCount;
437 EventBuffer[ui_event_counter].type = 3;
438 EventBuffer[ui_event_counter].data = k;
440 //mprintf( 0, "EVENT:%d, Key %d pressed\n", ui_event_counter, k );
447 if ( keyd_last_released && (RecordFlags & UI_RECORD_KEYS) )
450 k = keyd_last_released;
451 keyd_last_released= 0;
454 if (ui_event_counter < ui_number_of_events-1 )
456 EventBuffer[ui_event_counter].frame = FrameCount;
457 EventBuffer[ui_event_counter].type = 4;
458 EventBuffer[ui_event_counter].data = k;
460 //mprintf( 0, "EVENT:%d, Key %d released\n", ui_event_counter, k );
466 last_keypress = key_inkey();
468 if (last_keypress == KEY_F12 )
470 ui_number_of_events = ui_event_counter;
476 if ((last_keypress != 0) && (RecordFlags & UI_RECORD_KEYS) )
478 if (ui_event_counter < ui_number_of_events-1 )
480 EventBuffer[ui_event_counter].frame = FrameCount;
481 EventBuffer[ui_event_counter].type = 5;
482 EventBuffer[ui_event_counter].data = last_keypress;
484 //mprintf( 0, "EVENT:%d, Keypressed %d\n", ui_event_counter, last_keypress );
497 Mouse.new_buttons = 0;
500 if ( keyd_last_pressed ) {
502 k = keyd_last_pressed;
503 keyd_last_pressed = 0;
508 if ( keyd_last_released )
511 k = keyd_last_released;
512 keyd_last_released = 0;
517 if (key_inkey() == KEY_F12 )
519 //mprintf( 0, "Playing stopped.\n" );
525 if (EventBuffer==NULL) {
531 while( (ui_event_counter < ui_number_of_events) && (EventBuffer[ui_event_counter].frame <= FrameCount) )
533 switch ( EventBuffer[ui_event_counter].type )
535 case 1: // Mouse moved
536 Mouse.new_dx = EventBuffer[ui_event_counter].data & 0xFFFF;
537 Mouse.new_dy = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
539 case 2: // Mouse buttons changed
540 Mouse.new_buttons = EventBuffer[ui_event_counter].data;
542 case 3: // Key moved down
543 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 1;
545 case 4: // Key moved up
546 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 0;
548 case 5: // Key pressed
549 last_keypress = EventBuffer[ui_event_counter].data;
551 case 6: // Initial Mouse X position
552 Mouse.x = EventBuffer[ui_event_counter].data & 0xFFFF;
553 Mouse.y = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
559 if (ui_event_counter >= ui_number_of_events )
563 //( 0, "Done playing %d events.\n", ui_number_of_events );
573 if ( ui_event_counter < ui_number_of_events )
575 next_frame = EventBuffer[ui_event_counter].frame;
577 if ( (FrameCount+PlaybackSpeed) < next_frame )
578 FrameCount = next_frame - PlaybackSpeed;
588 if ( ui_event_counter < ui_number_of_events )
589 FrameCount = EventBuffer[ui_event_counter].frame;
602 void ui_wprintf( UI_WINDOW * wnd, char * format, ... )
607 va_start(args, format );
608 vsprintf(buffer,format,args);
610 gr_set_current_canvas( W_CANVAS );
613 W_TEXT_X = gr_string( W_TEXT_X, W_TEXT_Y, buffer );
617 void ui_wprintf_at( UI_WINDOW * wnd, short x, short y, char * format, ... )
622 va_start(args, format );
623 vsprintf(buffer,format,args);
625 gr_set_current_canvas( W_CANVAS );
628 gr_string( x, y, buffer );