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