Browse code

Drag/drop example

neauoire authored on 16/02/2021 23:34:55
Showing 2 changed files
... ...
@@ -111,7 +111,7 @@ void
111 111
 drawdebugger(Uint32 *dst, Uxn *u)
112 112
 {
113 113
 	Uint8 i;
114
-	for(i = 0; i < 0x10; ++i) { /* memory */
114
+	for(i = 0; i < 0x20; ++i) { /* memory */
115 115
 		Uint8 x = (i % 8) * 3 + 1, y = i / 8 + 1, b = u->ram.dat[i];
116 116
 		drawicn(dst, x * 8, y * 8, icons[(b >> 4) & 0xf], 1, 0);
117 117
 		drawicn(dst, x * 8 + 8, y * 8, icons[b & 0xf], 1, 0);
... ...
@@ -182,12 +182,11 @@ domouse(SDL_Event *event)
182 182
 	devmouse->mem[1] = x & 0xff;
183 183
 	devmouse->mem[2] = (y >> 8) & 0xff;
184 184
 	devmouse->mem[3] = y & 0xff;
185
+	devmouse->mem[4] = event->button.button == SDL_BUTTON_LEFT;
186
+	devmouse->mem[5] = 0x00;
185 187
 	switch(event->type) {
186
-	case SDL_MOUSEBUTTONUP:
187
-		devmouse->mem[4] = 0;
188
-		break;
189
-	case SDL_MOUSEBUTTONDOWN:
190
-		devmouse->mem[4] = event->button.button == SDL_BUTTON_LEFT;
188
+	case SDL_MOUSEBUTTONUP: devmouse->mem[5] = 0x10; break;
189
+	case SDL_MOUSEBUTTONDOWN: devmouse->mem[5] = 0x01; break;
191 190
 	}
192 191
 }
193 192
 
... ...
@@ -254,8 +253,6 @@ screenw(Device *d, Memory *m, Uint8 b)
254 253
 			d->mem[4]);
255 254
 		if(d->mem[5] == 1)
256 255
 			REQDRAW = 1;
257
-		if(d->mem[5] == 2)
258
-			clear(pixels);
259 256
 		d->ptr = 0;
260 257
 	}
261 258
 	(void)m;
... ...
@@ -288,6 +285,8 @@ start(Uxn *u)
288 285
 {
289 286
 	int ticknext = 0;
290 287
 	evaluxn(u, u->vreset);
288
+	if(REQDRAW)
289
+		redraw(pixels, u);
291 290
 	while(1) {
292 291
 		int tick = SDL_GetTicks();
293 292
 		SDL_Event event;
... ...
@@ -4,32 +4,53 @@
4 4
 :dev/w fff9 ( std write port )
5 5
 
6 6
 ;drawx 2 ;drawy 2 ;x 2 ;y 2 ;w 2 ;h 2 ;color 1
7
+;touchx 2 ;touchy 2 ;down 1
7 8
 
8 9
 |0100 @RESET 
9 10
 	
10 11
 	#05 =dev/r ( set dev/read mouse )
11 12
 	#01 =dev/w ( set dev/write to screen ) 
12 13
 
14
+	#02 =color
15
+	#0050 =w #0080 =h ,update JSR
16
+
13 17
 BRK
14 18
 
15 19
 |c000 @FRAME
16 20
 	
17 21
 	( get touch )
18
-	#04 IOR #01 NEQ ,frame-end ROT JMP? POP2
19
-
20
-	( clear )
21
-	#00 =color
22
-	~x ~y #0060 #0040 ,fillrect JSR
22
+	#05 IOR #01 EQU ,on-touch ROT JMP? POP2
23
+	#05 IOR #10 EQU ,on-release ROT JMP? POP2
24
+	#01 ~down EQU ,on-drag ROT JMP? POP2
25
+	BRK
23 26
 
24
-	( fill rect x y w h )
25
-	#01 =color
26
-	#00 IOR2 ( get mouse-x ) =x
27
-	#02 IOR2 ( get mouse-y ) =y
28
-	~x ~y #0060 #0040 ,fillrect JSR
29
-	
30
-	,redraw JSR
27
+	@on-touch
28
+		#01 =down
29
+		#01 =color
30
+		#00 IOR2 =touchx #02 IOR2 =touchy
31
+		,update JSR
32
+	BRK
33
+	@on-release
34
+		#00 =down
35
+		#02 =color
36
+		,update JSR
37
+	BRK
38
+	@on-drag
39
+		( clear last rect )
40
+		#00 =color
41
+		~x ~y ~w ~h ,fillrect JSR
42
+		( draw new rect )
43
+		#03 =color
44
+		#00 IOR2 ~touchx SUBS2 ~x ADDS2 =x
45
+		#02 IOR2 ~touchy SUBS2 ~y ADDS2 =y
46
+		#00 IOR2 =touchx #02 IOR2 =touchy
47
+		,update JSR
48
+	BRK
31 49
 
32
-	@frame-end
50
+	@update
51
+		~x ~y ~w ~h ,fillrect JSR
52
+		,redraw JSR
53
+	BRK
33 54
 
34 55
 BRK
35 56
 
... ...
@@ -58,12 +79,5 @@ BRK
58 79
 	#01 IOW
59 80
 	RTS
60 81
 
61
-@clear
62
-	#0000 IOW2
63
-	#0000 IOW2
64
-	#00 IOW
65
-	#02 IOW
66
-	RTS
67
-
68 82
 |d000 @ERROR BRK 
69 83
 |FFFA .RESET .FRAME .ERROR