1 /* $Id: window.c,v 1.6 2005-01-25 19:44:27 schaffner 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.6 2005-01-25 19:44:27 schaffner Exp $";
40 #define W_BACKGROUND (wnd->background )
43 #define W_WIDTH (wnd->width)
44 #define W_HEIGHT (wnd->height)
45 #define W_OLDCANVAS (wnd->oldcanvas)
46 #define W_CANVAS (wnd->canvas)
47 #define W_GADGET (wnd->gadget)
48 #define W_TEXT_X (wnd->text_x)
49 #define W_TEXT_Y (wnd->text_y)
50 #define W_NEXT (wnd->next)
51 #define W_PREV (wnd->prev)
54 UI_WINDOW * CurWindow = NULL;
55 UI_WINDOW * FirstWindow = NULL;
56 UI_WINDOW * LastWindow = NULL;
58 int last_keypress = 0;
60 #define BORDER_WIDTH 8
62 static unsigned int FrameCount = 0;
63 unsigned int ui_event_counter = 0;
64 unsigned int ui_number_of_events = 0;
65 static UI_EVENT * EventBuffer = NULL;
66 static int Record = 0;
67 static int RecordFlags = 0;
69 static short MouseDX=0, MouseDY=0, MouseButtons=0;
71 static unsigned char SavedState[256];
73 static int PlaybackSpeed = 1;
75 extern void ui_draw_frame( short x1, short y1, short x2, short y2 );
77 // 1=1x faster, 2=2x faster, etc
78 void ui_set_playback_speed( int speed )
80 PlaybackSpeed = speed;
83 int ui_record_events( int NumberOfEvents, UI_EVENT * buffer, int Flags )
85 if ( Record > 0 || buffer==NULL ) return 1;
91 ui_number_of_events = NumberOfEvents;
96 int ui_play_events_realtime( int NumberOfEvents, UI_EVENT * buffer )
98 if ( buffer == NULL ) return 1;
100 EventBuffer = buffer;
102 ui_event_counter = 0;
103 ui_number_of_events = NumberOfEvents;
106 keyd_last_released= 0;
107 keyd_last_pressed= 0;
108 for (i=0; i<256; i++ )
109 SavedState[i] = keyd_pressed[i];
115 int ui_play_events_fast( int NumberOfEvents, UI_EVENT * buffer )
118 if ( buffer == NULL ) return 1;
120 EventBuffer = buffer;
122 ui_event_counter = 0;
123 ui_number_of_events = NumberOfEvents;
126 keyd_last_released= 0;
127 keyd_last_pressed= 0;
129 //mprintf( 0, "Before: ", i );
130 for (i=0; i<256; i++ )
132 //if (keyd_pressed[i]) mprintf( 0, "%d ", i );
133 SavedState[i] = keyd_pressed[i];
135 //mprintf( 0, "\n" );
141 // Returns: 0=Normal, 1=Recording, 2=Playback normal, 3=Playback fast
142 int ui_recorder_status()
147 void add_window_to_end( UI_WINDOW * wnd )
151 LastWindow->next = wnd;
158 void add_window_to_beg( UI_WINDOW * wnd )
161 W_NEXT = FirstWindow;
162 FirstWindow->prev = wnd;
170 void add_window_after( UI_WINDOW * w1, UI_WINDOW * w2 )
175 if (w1->next == NULL )
183 UI_WINDOW *sav, *wnd = LastWindow;
188 ui_close_window(wnd);
193 void remove_window( UI_WINDOW * wnd )
196 W_NEXT->prev = W_PREV;
198 W_PREV->next = W_NEXT;
199 if (FirstWindow == wnd )
200 FirstWindow = W_NEXT;
201 if (LastWindow == wnd )
203 W_NEXT = W_PREV = NULL;
208 UI_WINDOW * ui_open_window( short x, short y, short w, short h, int flags )
211 int sw, sh, req_w, req_h;
213 wnd = (UI_WINDOW *)malloc(sizeof(UI_WINDOW));
214 if (wnd==NULL) exit(1);
219 add_window_to_end( wnd );
221 sw = grd_curscreen->sc_w;
222 sh = grd_curscreen->sc_h;
224 mouse_set_limits( 0,0, sw-1, sh-1 );
229 if (flags & WIN_BORDER)
238 if ( (x+w-1) >= sw ) x = sw - w;
240 if ( (y+h-1) >= sh ) y = sh - h;
246 W_OLDCANVAS = grd_curcanv;
248 wnd->keyboard_focus_gadget = NULL;
252 if (flags & WIN_SAVE_BG)
254 W_BACKGROUND = gr_create_bitmap( w, h );
255 gr_bm_ubitblt(w, h, 0, 0, x, y, &(grd_curscreen->sc_canvas.cv_bitmap), W_BACKGROUND );
260 if (flags & WIN_BORDER)
262 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x+BORDER_WIDTH, y+BORDER_WIDTH, req_w, req_h );
263 gr_set_current_canvas( NULL );
264 ui_draw_frame( x, y, x+w-1, y+h-1 );
267 W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x, y, req_w, req_h );
269 gr_set_current_canvas( W_CANVAS );
271 if (flags & WIN_FILLED)
272 ui_draw_box_out( 0, 0, req_w-1, req_h-1 );
274 gr_set_fontcolor( CBLACK, CWHITE );
276 selected_gadget = NULL;
285 void ui_close_window( UI_WINDOW * wnd )
290 ui_gadget_delete_all( wnd );
294 gr_bm_ubitblt(W_WIDTH, W_HEIGHT, W_X, W_Y, 0, 0, W_BACKGROUND, &(grd_curscreen->sc_canvas.cv_bitmap));
295 gr_free_bitmap( W_BACKGROUND );
298 gr_set_current_canvas( NULL );
299 gr_setcolor( CBLACK );
300 gr_rect( W_X, W_Y, W_X+W_WIDTH-1, W_Y+W_HEIGHT-1 );
303 gr_free_sub_canvas( W_CANVAS );
305 gr_set_current_canvas( W_OLDCANVAS );
307 selected_gadget = NULL;
309 remove_window( wnd );
323 //mprintf( 0, "After: " );
324 for (i=0; i<256; i++ )
326 //if (SavedState[i]) mprintf( 0, "%d ", i );
327 keyd_pressed[i] = SavedState[i];
329 //mprintf( 0, "\n" );
336 void ui_reset_idle_seconds()
338 last_event = timer_get_fixed_seconds();
341 int ui_get_idle_seconds()
343 return (timer_get_fixed_seconds() - last_event)/F1_0;
346 void ui_mega_process()
354 mouse_get_delta( &mx, &my );
357 Mouse.new_buttons = mouse_get_btns();
358 last_keypress = key_inkey();
360 if ( Mouse.new_buttons || last_keypress || Mouse.new_dx || Mouse.new_dy ) {
367 if (ui_event_counter==0 )
369 EventBuffer[ui_event_counter].frame = 0;
370 EventBuffer[ui_event_counter].type = 7;
371 EventBuffer[ui_event_counter].data = ui_number_of_events;
376 if (ui_event_counter==1 && (RecordFlags & UI_RECORD_MOUSE) )
378 Mouse.new_buttons = 0;
379 EventBuffer[ui_event_counter].frame = FrameCount;
380 EventBuffer[ui_event_counter].type = 6;
381 EventBuffer[ui_event_counter].data = ((Mouse.y & 0xFFFF) << 16) | (Mouse.x & 0xFFFF);
385 mouse_get_delta( &mx, &my );
388 MouseButtons = mouse_get_btns();
390 Mouse.new_dx = MouseDX;
391 Mouse.new_dy = MouseDY;
393 if ((MouseDX != 0 || MouseDY != 0) && (RecordFlags & UI_RECORD_MOUSE) )
395 if (ui_event_counter < ui_number_of_events-1 )
397 EventBuffer[ui_event_counter].frame = FrameCount;
398 EventBuffer[ui_event_counter].type = 1;
399 EventBuffer[ui_event_counter].data = ((MouseDY & 0xFFFF) << 16) | (MouseDX & 0xFFFF);
401 //mprintf( 0, "EVENT:%d, Mouse moved %d,%d\n", ui_event_counter, MouseDX, MouseDY );
408 if ( (MouseButtons != Mouse.new_buttons) && (RecordFlags & UI_RECORD_MOUSE) )
410 Mouse.new_buttons = MouseButtons;
412 if (ui_event_counter < ui_number_of_events-1 )
414 EventBuffer[ui_event_counter].frame = FrameCount;
415 EventBuffer[ui_event_counter].type = 2;
416 EventBuffer[ui_event_counter].data = MouseButtons;
418 //mprintf( 0, "EVENT:%d, Mouse buttons changed %d\n", ui_event_counter, MouseButtons );
426 if ( keyd_last_pressed && (RecordFlags & UI_RECORD_KEYS) )
429 k = keyd_last_pressed;
430 keyd_last_pressed= 0;
433 if (ui_event_counter < ui_number_of_events-1 )
435 EventBuffer[ui_event_counter].frame = FrameCount;
436 EventBuffer[ui_event_counter].type = 3;
437 EventBuffer[ui_event_counter].data = k;
439 //mprintf( 0, "EVENT:%d, Key %d pressed\n", ui_event_counter, k );
446 if ( keyd_last_released && (RecordFlags & UI_RECORD_KEYS) )
449 k = keyd_last_released;
450 keyd_last_released= 0;
453 if (ui_event_counter < ui_number_of_events-1 )
455 EventBuffer[ui_event_counter].frame = FrameCount;
456 EventBuffer[ui_event_counter].type = 4;
457 EventBuffer[ui_event_counter].data = k;
459 //mprintf( 0, "EVENT:%d, Key %d released\n", ui_event_counter, k );
465 last_keypress = key_inkey();
467 if (last_keypress == KEY_F12 )
469 ui_number_of_events = ui_event_counter;
475 if ((last_keypress != 0) && (RecordFlags & UI_RECORD_KEYS) )
477 if (ui_event_counter < ui_number_of_events-1 )
479 EventBuffer[ui_event_counter].frame = FrameCount;
480 EventBuffer[ui_event_counter].type = 5;
481 EventBuffer[ui_event_counter].data = last_keypress;
483 //mprintf( 0, "EVENT:%d, Keypressed %d\n", ui_event_counter, last_keypress );
496 Mouse.new_buttons = 0;
499 if ( keyd_last_pressed ) {
501 k = keyd_last_pressed;
502 keyd_last_pressed = 0;
507 if ( keyd_last_released )
510 k = keyd_last_released;
511 keyd_last_released = 0;
516 if (key_inkey() == KEY_F12 )
518 //mprintf( 0, "Playing stopped.\n" );
524 if (EventBuffer==NULL) {
530 while( (ui_event_counter < ui_number_of_events) && (EventBuffer[ui_event_counter].frame <= FrameCount) )
532 switch ( EventBuffer[ui_event_counter].type )
534 case 1: // Mouse moved
535 Mouse.new_dx = EventBuffer[ui_event_counter].data & 0xFFFF;
536 Mouse.new_dy = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
538 case 2: // Mouse buttons changed
539 Mouse.new_buttons = EventBuffer[ui_event_counter].data;
541 case 3: // Key moved down
542 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 1;
544 case 4: // Key moved up
545 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 0;
547 case 5: // Key pressed
548 last_keypress = EventBuffer[ui_event_counter].data;
550 case 6: // Initial Mouse X position
551 Mouse.x = EventBuffer[ui_event_counter].data & 0xFFFF;
552 Mouse.y = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
558 if (ui_event_counter >= ui_number_of_events )
562 //( 0, "Done playing %d events.\n", ui_number_of_events );
572 if ( ui_event_counter < ui_number_of_events )
574 next_frame = EventBuffer[ui_event_counter].frame;
576 if ( (FrameCount+PlaybackSpeed) < next_frame )
577 FrameCount = next_frame - PlaybackSpeed;
587 if ( ui_event_counter < ui_number_of_events )
588 FrameCount = EventBuffer[ui_event_counter].frame;
601 void ui_wprintf( UI_WINDOW * wnd, char * format, ... )
606 va_start(args, format );
607 vsprintf(buffer,format,args);
609 gr_set_current_canvas( W_CANVAS );
612 W_TEXT_X = gr_string( W_TEXT_X, W_TEXT_Y, buffer );
616 void ui_wprintf_at( UI_WINDOW * wnd, short x, short y, char * format, ... )
621 va_start(args, format );
622 vsprintf(buffer,format,args);
624 gr_set_current_canvas( W_CANVAS );
627 gr_string( x, y, buffer );