1 /* $Id: window.c,v 1.10 2005-02-26 10:13:51 chris 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.10 2005-02-26 10:13:51 chris Exp $";
45 #define W_BACKGROUND (wnd->background )
48 #define W_WIDTH (wnd->width)
49 #define W_HEIGHT (wnd->height)
50 #define W_OLDCANVAS (wnd->oldcanvas)
51 #define W_CANVAS (wnd->canvas)
52 #define W_GADGET (wnd->gadget)
53 #define W_TEXT_X (wnd->text_x)
54 #define W_TEXT_Y (wnd->text_y)
55 #define W_NEXT (wnd->next)
56 #define W_PREV (wnd->prev)
59 UI_WINDOW * CurWindow = NULL;
60 UI_WINDOW * FirstWindow = NULL;
61 UI_WINDOW * LastWindow = NULL;
63 int last_keypress = 0;
65 #define BORDER_WIDTH 8
67 static unsigned int FrameCount = 0;
68 unsigned int ui_event_counter = 0;
69 unsigned int ui_number_of_events = 0;
70 static UI_EVENT * EventBuffer = NULL;
71 static int Record = 0;
72 static int RecordFlags = 0;
74 static short MouseDX=0, MouseDY=0, MouseButtons=0;
76 static unsigned char SavedState[256];
78 static int PlaybackSpeed = 1;
80 extern void ui_draw_frame( short x1, short y1, short x2, short y2 );
82 // 1=1x faster, 2=2x faster, etc
83 void ui_set_playback_speed( int speed )
85 PlaybackSpeed = speed;
88 int ui_record_events( int NumberOfEvents, UI_EVENT * buffer, int Flags )
90 if ( Record > 0 || buffer==NULL ) return 1;
96 ui_number_of_events = NumberOfEvents;
101 int ui_play_events_realtime( int NumberOfEvents, UI_EVENT * buffer )
103 if ( buffer == NULL ) return 1;
105 EventBuffer = buffer;
107 ui_event_counter = 0;
108 ui_number_of_events = NumberOfEvents;
111 keyd_last_released= 0;
112 keyd_last_pressed= 0;
113 for (i=0; i<256; i++ )
114 SavedState[i] = keyd_pressed[i];
120 int ui_play_events_fast( int NumberOfEvents, UI_EVENT * buffer )
123 if ( buffer == NULL ) return 1;
125 EventBuffer = buffer;
127 ui_event_counter = 0;
128 ui_number_of_events = NumberOfEvents;
131 keyd_last_released= 0;
132 keyd_last_pressed= 0;
134 //mprintf( 0, "Before: ", i );
135 for (i=0; i<256; i++ )
137 //if (keyd_pressed[i]) mprintf( 0, "%d ", i );
138 SavedState[i] = keyd_pressed[i];
140 //mprintf( 0, "\n" );
146 // Returns: 0=Normal, 1=Recording, 2=Playback normal, 3=Playback fast
147 int ui_recorder_status()
152 void add_window_to_end( UI_WINDOW * wnd )
156 LastWindow->next = wnd;
163 void add_window_to_beg( UI_WINDOW * wnd )
166 W_NEXT = FirstWindow;
167 FirstWindow->prev = wnd;
175 void add_window_after( UI_WINDOW * w1, UI_WINDOW * w2 )
180 if (w1->next == NULL )
188 UI_WINDOW *sav, *wnd = LastWindow;
193 ui_close_window(wnd);
198 void remove_window( UI_WINDOW * wnd )
201 W_NEXT->prev = W_PREV;
203 W_PREV->next = W_NEXT;
204 if (FirstWindow == wnd )
205 FirstWindow = W_NEXT;
206 if (LastWindow == wnd )
208 W_NEXT = W_PREV = NULL;
213 UI_WINDOW * ui_open_window( short x, short y, short w, short h, int flags )
216 int sw, sh, req_w, req_h;
218 wnd = (UI_WINDOW *) d_malloc(sizeof(UI_WINDOW));
219 if (wnd==NULL) exit(1);
224 add_window_to_end( wnd );
226 sw = grd_curscreen->sc_w;
227 sh = grd_curscreen->sc_h;
229 //mouse_set_limits(0, 0, sw - 1, sh - 1);
234 if (flags & WIN_BORDER)
243 if ( (x+w-1) >= sw ) x = sw - w;
245 if ( (y+h-1) >= sh ) y = sh - h;
251 W_OLDCANVAS = grd_curcanv;
253 wnd->keyboard_focus_gadget = NULL;
257 if (flags & WIN_SAVE_BG)
259 W_BACKGROUND = gr_create_bitmap( w, h );
260 gr_bm_ubitblt(w, h, 0, 0, x, y, &(grd_curscreen->sc_canvas.cv_bitmap), W_BACKGROUND );
265 if (flags & WIN_BORDER)
267 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x+BORDER_WIDTH, y+BORDER_WIDTH, req_w, req_h );
268 gr_set_current_canvas( NULL );
269 ui_draw_frame( x, y, x+w-1, y+h-1 );
272 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x, y, req_w, req_h );
274 gr_set_current_canvas( W_CANVAS );
276 if (flags & WIN_FILLED)
277 ui_draw_box_out( 0, 0, req_w-1, req_h-1 );
279 gr_set_fontcolor( CBLACK, CWHITE );
281 selected_gadget = NULL;
290 void ui_close_window( UI_WINDOW * wnd )
295 ui_gadget_delete_all( wnd );
299 gr_bm_ubitblt(W_WIDTH, W_HEIGHT, W_X, W_Y, 0, 0, W_BACKGROUND, &(grd_curscreen->sc_canvas.cv_bitmap));
300 gr_free_bitmap( W_BACKGROUND );
303 gr_set_current_canvas( NULL );
304 gr_setcolor( CBLACK );
305 gr_rect( W_X, W_Y, W_X+W_WIDTH-1, W_Y+W_HEIGHT-1 );
308 gr_free_sub_canvas( W_CANVAS );
310 gr_set_current_canvas( W_OLDCANVAS );
312 selected_gadget = NULL;
314 remove_window( wnd );
328 //mprintf( 0, "After: " );
329 for (i=0; i<256; i++ )
331 //if (SavedState[i]) mprintf( 0, "%d ", i );
332 keyd_pressed[i] = SavedState[i];
334 //mprintf( 0, "\n" );
341 void ui_reset_idle_seconds()
343 last_event = timer_get_fixed_seconds();
346 int ui_get_idle_seconds()
348 return (timer_get_fixed_seconds() - last_event)/F1_0;
351 void ui_mega_process()
359 mouse_get_delta( &mx, &my );
362 Mouse.new_buttons = mouse_get_btns();
363 last_keypress = key_inkey();
365 if ( Mouse.new_buttons || last_keypress || Mouse.new_dx || Mouse.new_dy ) {
372 if (ui_event_counter==0 )
374 EventBuffer[ui_event_counter].frame = 0;
375 EventBuffer[ui_event_counter].type = 7;
376 EventBuffer[ui_event_counter].data = ui_number_of_events;
381 if (ui_event_counter==1 && (RecordFlags & UI_RECORD_MOUSE) )
383 Mouse.new_buttons = 0;
384 EventBuffer[ui_event_counter].frame = FrameCount;
385 EventBuffer[ui_event_counter].type = 6;
386 EventBuffer[ui_event_counter].data = ((Mouse.y & 0xFFFF) << 16) | (Mouse.x & 0xFFFF);
390 mouse_get_delta( &mx, &my );
393 MouseButtons = mouse_get_btns();
395 Mouse.new_dx = MouseDX;
396 Mouse.new_dy = MouseDY;
398 if ((MouseDX != 0 || MouseDY != 0) && (RecordFlags & UI_RECORD_MOUSE) )
400 if (ui_event_counter < ui_number_of_events-1 )
402 EventBuffer[ui_event_counter].frame = FrameCount;
403 EventBuffer[ui_event_counter].type = 1;
404 EventBuffer[ui_event_counter].data = ((MouseDY & 0xFFFF) << 16) | (MouseDX & 0xFFFF);
406 //mprintf( 0, "EVENT:%d, Mouse moved %d,%d\n", ui_event_counter, MouseDX, MouseDY );
413 if ( (MouseButtons != Mouse.new_buttons) && (RecordFlags & UI_RECORD_MOUSE) )
415 Mouse.new_buttons = MouseButtons;
417 if (ui_event_counter < ui_number_of_events-1 )
419 EventBuffer[ui_event_counter].frame = FrameCount;
420 EventBuffer[ui_event_counter].type = 2;
421 EventBuffer[ui_event_counter].data = MouseButtons;
423 //mprintf( 0, "EVENT:%d, Mouse buttons changed %d\n", ui_event_counter, MouseButtons );
431 if ( keyd_last_pressed && (RecordFlags & UI_RECORD_KEYS) )
434 k = keyd_last_pressed;
435 keyd_last_pressed= 0;
438 if (ui_event_counter < ui_number_of_events-1 )
440 EventBuffer[ui_event_counter].frame = FrameCount;
441 EventBuffer[ui_event_counter].type = 3;
442 EventBuffer[ui_event_counter].data = k;
444 //mprintf( 0, "EVENT:%d, Key %d pressed\n", ui_event_counter, k );
451 if ( keyd_last_released && (RecordFlags & UI_RECORD_KEYS) )
454 k = keyd_last_released;
455 keyd_last_released= 0;
458 if (ui_event_counter < ui_number_of_events-1 )
460 EventBuffer[ui_event_counter].frame = FrameCount;
461 EventBuffer[ui_event_counter].type = 4;
462 EventBuffer[ui_event_counter].data = k;
464 //mprintf( 0, "EVENT:%d, Key %d released\n", ui_event_counter, k );
470 last_keypress = key_inkey();
472 if (last_keypress == KEY_F12 )
474 ui_number_of_events = ui_event_counter;
480 if ((last_keypress != 0) && (RecordFlags & UI_RECORD_KEYS) )
482 if (ui_event_counter < ui_number_of_events-1 )
484 EventBuffer[ui_event_counter].frame = FrameCount;
485 EventBuffer[ui_event_counter].type = 5;
486 EventBuffer[ui_event_counter].data = last_keypress;
488 //mprintf( 0, "EVENT:%d, Keypressed %d\n", ui_event_counter, last_keypress );
501 Mouse.new_buttons = 0;
504 if ( keyd_last_pressed ) {
506 k = keyd_last_pressed;
507 keyd_last_pressed = 0;
512 if ( keyd_last_released )
515 k = keyd_last_released;
516 keyd_last_released = 0;
521 if (key_inkey() == KEY_F12 )
523 //mprintf( 0, "Playing stopped.\n" );
529 if (EventBuffer==NULL) {
535 while( (ui_event_counter < ui_number_of_events) && (EventBuffer[ui_event_counter].frame <= FrameCount) )
537 switch ( EventBuffer[ui_event_counter].type )
539 case 1: // Mouse moved
540 Mouse.new_dx = EventBuffer[ui_event_counter].data & 0xFFFF;
541 Mouse.new_dy = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
543 case 2: // Mouse buttons changed
544 Mouse.new_buttons = EventBuffer[ui_event_counter].data;
546 case 3: // Key moved down
547 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 1;
549 case 4: // Key moved up
550 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 0;
552 case 5: // Key pressed
553 last_keypress = EventBuffer[ui_event_counter].data;
555 case 6: // Initial Mouse X position
556 Mouse.x = EventBuffer[ui_event_counter].data & 0xFFFF;
557 Mouse.y = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
563 if (ui_event_counter >= ui_number_of_events )
567 //( 0, "Done playing %d events.\n", ui_number_of_events );
577 if ( ui_event_counter < ui_number_of_events )
579 next_frame = EventBuffer[ui_event_counter].frame;
581 if ( (FrameCount+PlaybackSpeed) < next_frame )
582 FrameCount = next_frame - PlaybackSpeed;
592 if ( ui_event_counter < ui_number_of_events )
593 FrameCount = EventBuffer[ui_event_counter].frame;
606 void ui_wprintf( UI_WINDOW * wnd, char * format, ... )
611 va_start(args, format );
612 vsprintf(buffer,format,args);
614 gr_set_current_canvas( W_CANVAS );
617 W_TEXT_X = gr_string( W_TEXT_X, W_TEXT_Y, buffer );
621 void ui_wprintf_at( UI_WINDOW * wnd, short x, short y, char * format, ... )
626 va_start(args, format );
627 vsprintf(buffer,format,args);
629 gr_set_current_canvas( W_CANVAS );
632 gr_string( x, y, buffer );