Browse code

Implemented basic key example

neauoire authored on 25/02/2021 00:40:42
Showing 2 changed files
... ...
@@ -285,6 +285,20 @@ domouse(SDL_Event *event)
285 285
 	}
286 286
 }
287 287
 
288
+void
289
+dotext(SDL_Event *event)
290
+{
291
+	int i;
292
+	if(SDL_GetModState() & KMOD_LCTRL || SDL_GetModState() & KMOD_RCTRL)
293
+		return;
294
+	for(i = 0; i < SDL_TEXTINPUTEVENT_TEXT_SIZE; ++i) {
295
+		char c = event->text.text[i];
296
+		if(c < ' ' || c > '~')
297
+			break;
298
+		devkey->mem[0] = c;
299
+	}
300
+}
301
+
288 302
 void
289 303
 doctrl(SDL_Event *event, int z)
290 304
 {
... ...
@@ -303,6 +317,14 @@ doctrl(SDL_Event *event, int z)
303 317
 	case SDLK_LEFT: flag = 0x40; break;
304 318
 	case SDLK_RIGHT: flag = 0x80; break;
305 319
 	}
320
+	if(z) {
321
+		/* special key controls */
322
+		switch(event->key.keysym.sym) {
323
+		case SDLK_BACKSPACE: devkey->mem[0] = 0x08; break;
324
+		case SDLK_RETURN: devkey->mem[0] = 0x0d; break;
325
+		}
326
+	}
327
+
306 328
 	setflag(&devcontroller->mem[0], flag, z);
307 329
 }
308 330
 
... ...
@@ -393,12 +415,14 @@ start(Uxn *u)
393 415
 		if(tick < ticknext)
394 416
 			SDL_Delay(ticknext - tick);
395 417
 		ticknext = tick + (1000 / FPS);
418
+		devkey->mem[0] = 0x00; /* TODO: cleanup */
396 419
 		while(SDL_PollEvent(&event) != 0) {
397 420
 			switch(event.type) {
398 421
 			case SDL_QUIT: quit(); break;
399 422
 			case SDL_MOUSEBUTTONUP:
400 423
 			case SDL_MOUSEBUTTONDOWN:
401 424
 			case SDL_MOUSEMOTION: domouse(&event); break;
425
+			case SDL_TEXTINPUT: dotext(&event); break;
402 426
 			case SDL_KEYDOWN: doctrl(&event, 1); break;
403 427
 			case SDL_KEYUP: doctrl(&event, 0); break;
404 428
 			case SDL_WINDOWEVENT:
405 429
new file mode 100644
... ...
@@ -0,0 +1,233 @@
1
+( mouse )
2
+
3
+:dev/r fff8 ( std read port )
4
+:dev/w fff9 ( std write port )
5
+
6
+&Point2d { x 2 y 2 }
7
+&Window2d { x1 2 y1 2 x2 2 y2 2 }
8
+
9
+;pos Point2d
10
+;mouse Point2d
11
+;scenter Point2d
12
+;win Window2d
13
+
14
+( drawing ) ;color 1 ;x1 2 ;x2 2 ;y1 2 ;y2 2 ;i 2
15
+;state 1 ;textlen 2
16
+
17
+|0100 @RESET 
18
+
19
+	#01 =dev/r ( read screen for size )
20
+	#02 =dev/w ( write to screen )
21
+
22
+	#08 =color
23
+	,paint-pattern JSR
24
+
25
+	#01 =dev/w ( write to screen )
26
+
27
+	#00 IOR2 #0002 DIV2 =scenter.x
28
+	#02 IOR2 #0002 DIV2 =scenter.y
29
+
30
+	~scenter.x #0050 SUB2 
31
+	~scenter.y #0030 SUB2 
32
+	~scenter.x #0050 ADD2 
33
+	~scenter.y #0030 ADD2 ,paint-window JSR
34
+
35
+	#05 =dev/r ( set dev/read mouse )
36
+	#02 =dev/w ( set dev/write to sprite ) 
37
+
38
+	#09 =color
39
+
40
+BRK
41
+
42
+|c000 @FRAME
43
+
44
+	( clear last cursor )
45
+	#10 ,clear_icn ~mouse.x ~mouse.y ,draw-sprite JSR
46
+	( record mouse positions )
47
+	#05 =dev/r ( set dev/read mouse )
48
+	#00 IOR2 =mouse.x 
49
+	#02 IOR2 =mouse.y
50
+	#11 =state
51
+
52
+	#04 IOR #01 NEQ ,no-touch ROT JMP? POP2
53
+		#13 =state
54
+	@no-touch
55
+
56
+	( clear window )
57
+
58
+	#04 =dev/r ( set dev/read mouse )
59
+	#00 IOR #00 EQU ,key-end ROT JMP? POP2
60
+
61
+		( is backspace )
62
+		#00 IOR #08 NEQ ,no-key-back ROT JMP? POP2
63
+			#00 ,body ~textlen ADD2 STR
64
+			~textlen #0001 SUB2 =textlen
65
+			,redraw-body-label JSR
66
+			,key-end JMP
67
+		@no-key-back
68
+
69
+		~textlen #0001 ADD2 =textlen
70
+		#00 IOR ,body ~textlen ADD2 STR
71
+		,redraw-body-label JSR
72
+	@key-end
73
+
74
+	( draw mouse )
75
+	~state ,cursor_icn ~mouse.x ~mouse.y ,draw-sprite JSR
76
+
77
+BRK
78
+
79
+@redraw-body-label
80
+
81
+	#02 =color
82
+	#01 =dev/w ( write to screen )
83
+	~win.x1 #0004 ADD2 ~win.y1 #0004 ADD2 ~win.x2 #0010 SUB2 ~win.y2 #0010 SUB2 ,fill-rect JSR
84
+	#02 =dev/w ( write to sprite )
85
+	#06 =color
86
+	,body ~win.x1 #0018 ADD2 ~scenter.y ,draw-label JSR
87
+
88
+RTS
89
+
90
+@paint-pattern ( nil )
91
+
92
+	#01 =dev/r ( read screen for size )
93
+	#02 =dev/w ( write to sprite )
94
+
95
+	#0000 
96
+	@paint-pattern-loop-hor
97
+	#0000
98
+	@paint-pattern-loop
99
+		( draw ) OVR2 IOW2 DUP2 IOW2 ,pattern IOW2 ~color IOW
100
+		( incr ) #0008 ADD2 DUP2
101
+		#00 IOR2 LTH2 ,paint-pattern-loop ROT JMP? POP2
102
+		POP2
103
+	( incr ) #0008 ADD2 DUP2
104
+	#02 IOR2 LTH2 ,paint-pattern-loop-hor ROT JMP? POP2
105
+	POP2
106
+
107
+RTS
108
+
109
+@paint-window ( name wx1 wy1 wx2 wy2 )
110
+
111
+	=win.y2 =win.x2 =win.y1 =win.x1
112
+
113
+	( Draw shadow )
114
+	#01 =color
115
+	~win.x2 ~win.y1 #0003 ADD2 ~win.x2 #0003 ADD2 ~win.y2 #0003 ADD2 ,fill-rect JSR
116
+	~win.x1 #0003 ADD2 ~win.y2 ~win.x2 #0003 ADD2 ~win.y2 #0003 ADD2 ,fill-rect JSR
117
+	( Fill background )
118
+	#02 =color
119
+	~win.x1 ~win.y1 ~win.x2 ~win.y2 ,fill-rect JSR
120
+	( draw outline )
121
+	#01 =color
122
+	~win.x1 ~win.y1 ~win.x2 ~win.y2 ,line-rect JSR
123
+	#01 =color
124
+	~win.x1 #0002 ADD2 ~win.y1 #0002 ADD2 ~win.x2 #0002 SUB2 ~win.y2 #0002 SUB2 ,line-rect JSR
125
+
126
+RTS
127
+
128
+@draw-label ( text x1 y1 )
129
+
130
+	=pos.y =pos.x
131
+	@draw-label-loop
132
+
133
+		( draw ) ~pos.x ~pos.y IOW2 IOW2 DUP2 LDR #00 SWP #0008 MUL2 ,font ADD2 IOW2 ~color IOW
134
+		( incr ) #0001 ADD2
135
+		( incr ) ~pos.x #0008 ADD2 =pos.x
136
+
137
+		( detect linebreaks )
138
+		DUP2 LDR #0d NEQ ,no-return ROT JMP? POP2
139
+			#0048 =pos.x
140
+		( incr ) ~pos.y #0008 ADD2 =pos.y
141
+		@no-return
142
+
143
+		DUP2 LDR #00 NEQ ,draw-label-loop ROT JMP? POP2
144
+	POP2
145
+
146
+RTS
147
+
148
+@fill-rect ( x1 y1 x2 y2 )
149
+	=y2 =x2 ( stash x1 y1 ) =y1 DUP2 WSR2 =x1
150
+	@fill-rect-ver
151
+		RSW2 DUP2 =x1 WSR2
152
+		@fill-rect-hor
153
+			( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW
154
+			( incr ) ~x1 #0001 ADD2 DUP2 =x1  
155
+			~x2 LTH2 ,fill-rect-hor ROT JMP? POP2
156
+		~y1 #0001 ADD2 DUP2 =y1
157
+		~y2 LTH2 ,fill-rect-ver ROT JMP? POP2
158
+	RSW2 POP2
159
+RTS
160
+
161
+@line-rect ( x1 y1 x2 y2 )
162
+	=y2 =x2 ( stash x1 y1 ) DUP2 WSR2 =y1 DUP2 WSR2 =x1
163
+	@line-rect-hor
164
+		( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW
165
+		( draw ) ~x1 ~y2 IOW2 IOW2 ~color IOW
166
+		( incr ) ~x1 #0001 ADD2 DUP2 =x1  
167
+		~x2 #0001 ADD2 LTH2 ,line-rect-hor ROT JMP? POP2
168
+	( restore x1 y1 ) RSW2 =x1 RSW2 =y1
169
+	@line-rect-ver
170
+		( incr ) ~y1 #0001 ADD2 DUP2 =y1  
171
+		( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW
172
+		( draw ) ~x2 ~y1 IOW2 IOW2 ~color IOW
173
+		~y2 #0001 SUB2 LTH2 ,line-rect-ver ROT JMP? POP2
174
+RTS
175
+
176
+@draw-sprite
177
+	IOW2 ( y byte )
178
+	IOW2 ( x byte )
179
+	IOW2 ( sprite address )
180
+	IOW ( layer-color )
181
+	RTS
182
+
183
+@pattern     [ 4281 1824 2418 8142 ]
184
+
185
+@clear_icn   [ 0000 0000 0000 0000 ]
186
+@cursor_icn  [ 80c0 e0f0 f8e0 1000 ]
187
+
188
+@mouse0_icn  [ 7c82 92ee 8282 4438 ]
189
+@mouse1_icn  [ 7cf2 f2ee 8282 4438 ]
190
+@mouse2_icn  [ 7c9e 9eee 8282 4438 ]
191
+@mouse12_icn [ 7cfe feee 8282 4438 ]
192
+
193
+@font ( spectrum-zx font ) 
194
+[
195
+	0000 0000 0000 0000 0000 2400 7e3c 0000 0000 2400 3c42 0000 0000 6c7c 7c38 1000
196
+	0010 387c 7c38 1000 0038 387c 6c10 3800 0010 387c 7c10 3800 0000 0018 1800 0000
197
+	007e 4242 4242 7e00 0000 1824 2418 0000 0018 2442 4224 1800 001e 063a 4a48 3000
198
+	0038 446c 107c 1000 000c 0808 0838 3800 003e 2222 2266 6600 0000 0822 0022 0800
199
+	0000 1018 1c18 1000 0000 0818 3818 0800 0008 1c00 001c 0800 0028 2828 2800 2800
200
+	003e 4a4a 3a0a 0a00 000c 3046 620c 3000 0000 0000 0000 ffff 0010 3800 3810 0038
201
+	0008 1c2a 0808 0800 0008 0808 2a1c 0800 0000 0804 7e04 0800 0000 1020 7e20 1000
202
+	0000 4040 7e00 0000 0000 0024 6624 0000 0000 1038 7c00 0000 0000 007c 3810 0000
203
+	0000 0000 0000 0000 0008 0808 0800 0800 0014 1400 0000 0000 0024 7e24 247e 2400
204
+	0008 1e28 1c0a 3c08 0042 0408 1020 4200 0030 4832 4c44 3a00 0008 1000 0000 0000
205
+	0004 0808 0808 0400 0010 0808 0808 1000 0000 1408 3e08 1400 0000 0808 3e08 0800
206
+	0000 0000 0008 0810 0000 0000 3c00 0000 0000 0000 0000 0800 0000 0204 0810 2000
207
+	003c 464a 5262 3c00 0018 2808 0808 3e00 003c 4202 3c40 7e00 003c 421c 0242 3c00
208
+	0008 1828 487e 0800 007e 407c 0242 3c00 003c 407c 4242 3c00 007e 0204 0810 1000
209
+	003c 423c 4242 3c00 003c 4242 3e02 3c00 0000 0008 0000 0800 0000 0800 0008 0810
210
+	0000 0810 2010 0800 0000 003e 003e 0000 0000 1008 0408 1000 003c 4202 0c00 0800
211
+	003c 425a 5442 3c00 0018 2442 7e42 4200 007c 427c 4242 7c00 003c 4240 4042 3c00
212
+	0078 4442 4244 7800 007e 407c 4040 7e00 003e 4040 7c40 4000 003c 4240 4e42 3c00
213
+	0042 427e 4242 4200 003e 0808 0808 3e00 0002 0202 4242 3c00 0044 4870 4844 4200
214
+	0040 4040 4040 7e00 0042 665a 4242 4200 0042 6252 4a46 4200 003c 4242 4242 3c00
215
+	007c 4242 7c40 4000 003c 4242 524a 3c00 007c 4242 7c44 4200 003c 403c 0242 3c00
216
+	00fe 1010 1010 1000 0042 4242 4242 3c00 0042 4242 4224 1800 0042 4242 5a66 4200
217
+	0042 2418 1824 4200 0082 4428 1010 1000 007e 0408 1020 7e00 000c 0808 0808 0c00
218
+	0040 2010 0804 0200 0018 0808 0808 1800 0008 1422 0000 0000 0000 0000 0000 7e00
219
+	0008 0400 0000 0000 0000 1c02 1e22 1e00 0020 203c 2222 3c00 0000 1e20 2020 1e00
220
+	0002 021e 2222 1e00 0000 1c22 3c20 1e00 000c 101c 1010 1000 0000 1c22 221e 021c
221
+	0020 202c 3222 2200 0008 0018 0808 0400 0008 0008 0808 4830 0020 2428 3028 2400
222
+	0010 1010 1010 0c00 0000 6854 5454 5400 0000 5864 4444 4400 0000 3844 4444 3800
223
+	0000 7844 4478 4040 0000 3c44 443c 0406 0000 2c30 2020 2000 0000 3840 3804 7800
224
+	0010 103c 1010 0c00 0000 4444 4444 3800 0000 4444 2828 1000 0000 4454 5454 2800
225
+	0000 4428 1028 4400 0000 4444 443c 0438 0000 7c08 1020 7c00 000c 0810 1008 0c00
226
+	0008 0808 0808 0800 0030 1008 0810 3000 0000 0032 4c00 0000 3c42 99a1 a199 423c
227
+]
228
+
229
+@body          [  ]
230
+
231
+|d000 @ERROR BRK 
232
+|FFF0 [ f0ff f00f f00f ] ( palette )
233
+|FFFA .RESET .FRAME .ERROR