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