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