b091f95398f4c61e8bd08d3c3c3801e256f77098
[btb/d2x.git] / ui / mouse.c
1 /* $Id: mouse.c,v 1.6 2005-01-25 21:31:45 schaffner Exp $ */
2 /*
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.
13 */
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: mouse.c,v 1.6 2005-01-25 21:31:45 schaffner Exp $";
17 #endif
18
19 #ifdef HAVE_CONFIG_H
20 #include "conf.h"
21 #endif
22
23 #include <stdlib.h>
24
25 #include "u_mem.h"
26 #include "fix.h"
27 #include "pstypes.h"
28 #include "gr.h"
29 #include "mouse.h"
30 #include "timer.h"
31
32 #include "ui.h"
33
34 // 16x16
35
36 #define PTR_W 11
37 #define PTR_H 19
38
39 char ui_converted_mouse_pointer[PTR_W*PTR_H];
40 char ui_mouse_pointer[] =  \
41 //"1111100000000000"\
42 //"1111111111000000"\
43 //"1144411111111111"\
44 //"1144444444111111"\
45 //"1144444444444110"\
46 //"0114444444441100"\
47 //"0114444444411000"\
48 //"0114444444411000"\
49 //"0114444444441100"\
50 //"0114444444444110"\
51 //"0011444444444411"\
52 //"0011444444444411"\
53 //"0011411144444411"\
54 //"0011111111444111"\
55 //"0011100011111110"\
56 //"0011000000111100";
57
58 "10000000000"\
59 "11000000000"\
60 "14100000000"\
61 "14410000000"\
62 "14441000000"\
63 "14444100000"\
64 "14444410000"\
65 "14444441000"\
66 "14444444100"\
67 "14444444410"\
68 "14444411111"\
69 "14114410000"\
70 "11014410000"\
71 "10001441000"\
72 "00001441000"\
73 "00001441000"\
74 "00000144100"\
75 "00000144100"\
76 "00000111100";
77
78 static grs_bitmap * default_pointer;
79
80
81 UI_MOUSE Mouse;
82
83
84 /*
85 int ui_mouse_find_gadget(short n)
86 {
87         int i;
88
89         for (i=1; i<=Window[n].NumItems; i++ )  {
90                 if ((Mouse.x >= (Window[n].Item[i].x1+Window[n].x)) &&
91                         (Mouse.x <= (Window[n].Item[i].x2+Window[n].x)) &&
92                         (Mouse.y >= (Window[n].Item[i].y1+Window[n].y)) &&
93                         (Mouse.y <= (Window[n].Item[i].y2+Window[n].y)) )
94                         return i;
95         }
96         return 0;
97 }
98 */
99
100 void ui_mouse_show()
101 {
102         if (Mouse.hidden==1 )   {
103                 Mouse.hidden = 0;
104                 // Save the background under new pointer
105                 Mouse.bg_saved=1;
106                 Mouse.bg_x = Mouse.x; Mouse.bg_y = Mouse.y;
107                 gr_bm_ubitblt( Mouse.background->bm_w, Mouse.background->bm_h, 0, 0, Mouse.bg_x, Mouse.bg_y, &(grd_curscreen->sc_canvas.cv_bitmap),Mouse.background );
108                 // Draw the new pointer
109                 gr_bm_ubitbltm( Mouse.pointer->bm_w, Mouse.pointer->bm_h, Mouse.x, Mouse.y, 0, 0, Mouse.pointer, &(grd_curscreen->sc_canvas.cv_bitmap)   );
110         }
111 }
112
113 void ui_mouse_hide()
114 {
115         if (Mouse.hidden==0 )   {
116                 Mouse.hidden = 1;
117                 if (Mouse.bg_saved==1)  {
118                         gr_bm_ubitblt( Mouse.background->bm_w, Mouse.background->bm_h, Mouse.bg_x, Mouse.bg_y, 0, 0, Mouse.background,&(grd_curscreen->sc_canvas.cv_bitmap) );
119                         Mouse.bg_saved = 0;
120                 }
121         }
122 }
123
124 void ui_mouse_process()
125 {   int buttons,w,h;
126
127         //if (Mouse.hidden==0)
128         //    mouse_get_pos( &Mouse.x, &Mouse.y );
129         //else
130         //    mouse_set_pos( Mouse.x, Mouse.y );
131
132
133         Mouse.dx = Mouse.new_dx;
134         Mouse.dy = Mouse.new_dy;
135         buttons = Mouse.new_buttons;
136
137         Mouse.x += Mouse.dx;
138         Mouse.y += Mouse.dy;
139
140         w = grd_curscreen->sc_w;
141         h = grd_curscreen->sc_h;
142
143         if (Mouse.x < 0 ) Mouse.x = 0;
144         if (Mouse.y < 0 ) Mouse.y = 0;
145 //      if (Mouse.x > w-Mouse.pointer->bm_w ) Mouse.x = w - Mouse.pointer->bm_w;
146 //      if (Mouse.y > h-Mouse.pointer->bm_h ) Mouse.y = h - Mouse.pointer->bm_h;
147
148         if (Mouse.x > w-3 ) Mouse.x = w - 3;
149         if (Mouse.y > h-3 ) Mouse.y = h - 3;
150
151         if ( (Mouse.bg_x!=Mouse.x) || (Mouse.bg_y!=Mouse.y) )
152                 Mouse.moved = 1;
153
154         if ( (Mouse.bg_x!=Mouse.x) || (Mouse.bg_y!=Mouse.y) || (Mouse.bg_saved==0) )
155         {
156                 // Restore the background under old pointer
157                 if (Mouse.bg_saved==1)  {
158                         gr_bm_ubitblt( Mouse.background->bm_w, Mouse.background->bm_h, Mouse.bg_x, Mouse.bg_y, 0, 0, Mouse.background, &(grd_curscreen->sc_canvas.cv_bitmap)   );
159                 }
160                 Mouse.bg_saved = 0;
161
162                 if (!Mouse.hidden)
163                 {
164                         // Save the background under new pointer
165                         Mouse.bg_saved=1;
166                         Mouse.bg_x = Mouse.x; Mouse.bg_y = Mouse.y;
167
168                         gr_bm_ubitblt( Mouse.background->bm_w, Mouse.background->bm_h, 0, 0, Mouse.bg_x, Mouse.bg_y, &(grd_curscreen->sc_canvas.cv_bitmap),Mouse.background   );
169
170                         // Draw the new pointer
171                         gr_bm_ubitbltm( Mouse.pointer->bm_w, Mouse.pointer->bm_h, Mouse.x, Mouse.y, 0, 0, Mouse.pointer, &(grd_curscreen->sc_canvas.cv_bitmap)   );
172                 }
173         }
174
175         Mouse.b1_last_status = Mouse.b1_status;
176         Mouse.b2_last_status = Mouse.b2_status;
177
178         if ( Mouse.backwards== 0 )
179         {
180                 if (buttons & MOUSE_LBTN )
181                         Mouse.b1_status = BUTTON_PRESSED;
182                 else
183                         Mouse.b1_status = BUTTON_RELEASED;
184                 if (buttons & MOUSE_RBTN )
185                         Mouse.b2_status = BUTTON_PRESSED;
186                 else
187                         Mouse.b2_status = BUTTON_RELEASED;
188         } else {
189                 if (buttons & MOUSE_LBTN )
190                         Mouse.b2_status = BUTTON_PRESSED;
191                 else
192                         Mouse.b2_status = BUTTON_RELEASED;
193                 if (buttons & MOUSE_RBTN )
194                         Mouse.b1_status = BUTTON_PRESSED;
195                 else
196                         Mouse.b1_status = BUTTON_RELEASED;
197         }
198
199         if ((Mouse.b1_status & BUTTON_PRESSED) && (Mouse.b1_last_status & BUTTON_RELEASED) )
200         {
201                 if ((timer_get_fixed_seconds() <= Mouse.time_lastpressed + F1_0/5))  //&& (Mouse.moved==0)
202                         Mouse.b1_status |= BUTTON_DOUBLE_CLICKED;
203
204                 Mouse.moved = 0;
205                 Mouse.time_lastpressed = timer_get_fixed_seconds();
206                 Mouse.b1_status |= BUTTON_JUST_PRESSED;
207
208         }
209         else if ((Mouse.b1_status & BUTTON_RELEASED) && (Mouse.b1_last_status & BUTTON_PRESSED) )
210                 Mouse.b1_status |= BUTTON_JUST_RELEASED;
211
212         if ((Mouse.b2_status & BUTTON_PRESSED) && (Mouse.b2_last_status & BUTTON_RELEASED) )
213                 Mouse.b2_status |= BUTTON_JUST_PRESSED;
214         else if ((Mouse.b2_status & BUTTON_RELEASED) && (Mouse.b2_last_status & BUTTON_PRESSED) )
215                 Mouse.b2_status |= BUTTON_JUST_RELEASED;
216 }
217
218 void ui_mouse_flip_buttons()
219 {   short x;
220
221         x = Mouse.b1_status;
222         Mouse.b1_status = Mouse.b2_status;
223         Mouse.b2_status = x;
224
225         x = Mouse.b1_last_status;
226         Mouse.b1_last_status = Mouse.b2_last_status;
227         Mouse.b2_last_status = x;
228
229         Mouse.backwards ^= 1;
230
231 }
232
233
234 void ui_mouse_init()
235 {
236         int i, w,h;
237
238         //mouse_init();
239
240         w = grd_curscreen->sc_w;
241         h = grd_curscreen->sc_h;
242
243         //mouse_set_limits(0, 0, w - 1, h - 1);
244
245         Mouse.x = w/2;
246         Mouse.y = h/2;
247
248         //mouse_set_pos( w/2, h/2 );
249
250         for (i=0; i < PTR_W*PTR_H; i++ )   {
251                 switch (ui_mouse_pointer[i]) {
252                 case '0':
253                         ui_converted_mouse_pointer[i]=255;
254                         break;
255                 case '1':
256                         ui_converted_mouse_pointer[i]=CBLACK;
257                         break;
258                 case '2':
259                         ui_converted_mouse_pointer[i]=CGREY;
260                         break;
261                 case '3':
262                         ui_converted_mouse_pointer[i]=CWHITE;
263                         break;
264                 case '4':
265                         ui_converted_mouse_pointer[i]=CBRIGHT;
266                         break;
267                 case '5':
268                         ui_converted_mouse_pointer[i]=CRED;
269                         break;
270                 }
271         }
272
273         default_pointer = gr_create_bitmap_raw( PTR_W, PTR_H, ui_converted_mouse_pointer );
274         Mouse.x = Mouse.y = 0;
275         Mouse.backwards = 0;
276         Mouse.hidden = 1;
277         Mouse.b1_status = Mouse.b1_last_status = BUTTON_RELEASED;
278         Mouse.b2_status = Mouse.b2_last_status = BUTTON_RELEASED;
279         Mouse.b3_status = Mouse.b3_last_status = BUTTON_RELEASED;
280         Mouse.bg_x = Mouse.bg_y = 0;
281         Mouse.bg_saved = 0;
282         Mouse.pointer = default_pointer;
283         Mouse.background = gr_create_bitmap( Mouse.pointer->bm_w, Mouse.pointer->bm_h );
284         Mouse.time_lastpressed = 0;
285         Mouse.moved = 0;
286
287 }
288
289 grs_bitmap * ui_mouse_set_pointer( grs_bitmap * new )
290 {
291         grs_bitmap * temp = Mouse.pointer;
292
293         gr_free_bitmap( Mouse.background );
294
295         if (new == NULL ) {
296                 Mouse.pointer = default_pointer;
297         } else {
298                 Mouse.pointer = new;
299         }
300         Mouse.background = gr_create_bitmap( Mouse.pointer->bm_w, Mouse.pointer->bm_h );
301
302         return temp;
303
304 }
305
306 void ui_mouse_close()
307 {
308         gr_free_sub_bitmap(default_pointer);
309
310         gr_free_bitmap(Mouse.background);
311
312 }
313