-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselection.inc
424 lines (375 loc) · 10.9 KB
/
selection.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
;*******************************************************************************
;Copyright 2022-2024, Stefan Jakobsson
;
;Redistribution and use in source and binary forms, with or without modification,
;are permitted provided that the following conditions are met:
;
;1. Redistributions of source code must retain the above copyright notice, this
; list of conditions and the following disclaimer.
;
;2. Redistributions in binary form must reproduce the above copyright notice,
; this list of conditions and the following disclaimer in the documentation
; and/or other materials provided with the distribution.
;
;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
;AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
;FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;*******************************************************************************
;******************************************************************************
;Function name.......: selection_char_count
;Purpose.............: Calculates the number of visible chars on a line that
; is to be printed to the screen; firstly chars visible
; before the selection starts, and secondly visible chars
; that are selected. This function is intended to be
; used by the screen print line and refresh functions
; to decide what colors to output
;Input...............: screen_print_line (24 bits)
; screen_print_col (24 bits)
; selection_start_line (24 bits)
; selection_start_col (24 bits)
; selection_end_line (24 bits)
; selection_end_col (24 bits)
;Returns.............: X: Number of unselected chars at start of line
; Y: Number of selected chars
;Error returns.......: None
.export selection_char_count
.export screen_print_line, screen_print_col
.export selection_start_line, selection_end_line, selection_start_col, selection_end_col
.export screen_width
.proc selection_char_count
;Check if selection is active
lda selection_active
bne :+
jmp no_selection
;Init
: stz flags
;PART I: Check if the line to be printed to screen is within the line where the
; selection starts and ends
;------------------------------------------------------------------------------
;Calculate Selection End Line - Screen Print Line
ldx #0
sec
lda selection_end_line
sbc screen_print_line
beq :+
tax
: lda selection_end_line+1
sbc screen_print_line+1
beq :+
tax
: lda selection_end_line+2
sbc screen_print_line+2
beq :+
tax
: bcs :+ ;Branch if Selection End Line >= Screen Print Line
jmp no_selection ;Selection End Line < Screen Print Line => No selection on this line
: cpx #1 ;Clear flag if Screen Print Line is at Selection End Line
rol flags
;Calculate Screen Print Line - Selection Start Line
ldx #0
sec
lda screen_print_line
sbc selection_start_line
beq :+
tax
: lda screen_print_line+1
sbc selection_start_line+1
beq :+
tax
: lda screen_print_line+2
sbc selection_start_line+2
beq :+
tax
: bcs :+ ;Branch if Screen Print Line >= Selection Start Line
jmp no_selection ;Screen Print Line < Selection Start Line => No selection on this line
: cpx #1 ;Clear flag if Screen Print Line is at Selection Start Line
rol flags ;Bit 0 = clear if Screen Print Line == Selection Start Line
;Bit 1 = clear if Screen Print Line == Selection End Line
;PART II: Calculate number of visible chars on the line to be printed that are selected and
; not selected
;
; Pseudo code:
;
; dleft = selection_start_col - print_col
; if dleft < 0:
; dleft = 0
; dmid = selection_end_col - print_col
; elif dleft > screen_width:
; dleft = screen_width
; dmid = 0
; else:
; dmid = selection_end_col - selection_start_col
;
; #Adjust values for selection start and end lines if an multi-line selection
; if flags & %00000010: #Line to be printed on selection start line, but not selection end line
; dmid = screen_width - dleft
; elif flags & %00000001 #Line to be printed on selection end line, but not selection start line
; dmid = dleft + dmid
; dleft = 0
;
; #Normalize dmid to range 0..screen_width
; if dleft + dmid > screen_width:
; dmid = screen_width - dleft
;
; Where:
; dleft = number of visible chars on the line to be printed before the selection
; dmid = number of visible chars on the line to be printed that are selected
lda flags
; Check if on last but not first line of selection
cmp #%00000001
beq last_not_first
cmp #%00000011
bne left_distance
; Neither first nor last line of selection => The whole line is selected
ldx #0
ldy screen_width
rts
left_distance:
sec
lda selection_start_col
sbc screen_print_col
sta dleft
lda selection_start_col+1
sbc screen_print_col+1
sta dtmp
lda selection_start_col+2
sbc screen_print_col+2
bcc noleft
;Normalize output to range 0..screen_width
ora dtmp
bne :+
lda dleft
cmp screen_width
bcc mid_distance
: ldx screen_width
ldy #0
jmp multiline_adjust
last_not_first:
stz dleft
sec
lda selection_end_col
sbc screen_print_col
sta dmid
lda selection_end_col+1
sbc screen_print_col+1
sta dtmp
lda selection_end_col+2
sbc screen_print_col+2
bcc nomid
ldy dmid
iny
ora dtmp
bne :+
cpy screen_width
bcc :+
ldy screen_width
: ldx #0
rts
nomid:
ldx screen_width
ldy #0
rts
noleft:
stz dleft
sec
lda selection_end_col
sbc screen_print_col
sta dmid
lda selection_end_col+1
sbc screen_print_col+1
sta dtmp
lda selection_end_col+2
sbc screen_print_col+2
bcc no_selection
;Normalize output to range 0..screen_width
inc dmid
ora dtmp
bne :+
lda screen_width
cmp dmid
bcs multiline_adjust
: ldx #0
ldy screen_width
bra multiline_adjust
mid_distance:
sec
lda selection_end_col
sbc selection_start_col
sta dmid
lda selection_end_col+1
sbc selection_start_col+1
sta dtmp
lda selection_end_col+2
sbc selection_start_col+2
;Normalize output to range 0..screen_width
ora dtmp
bne :+
clc
lda dmid
adc dleft
bcs :+
cmp screen_width
bcc multiline_adjust
: sec
lda screen_width
sbc dleft
sta dmid
multiline_adjust:
lda flags
cmp #%00000010 ;On first line, but not last line of selection
bne :+
sec
lda screen_width
sbc dleft
sta dmid
: ldx dleft
ldy dmid
rts
no_selection:
ldx screen_width
ldy #0
rts
flags = tempvars
dleft = tempvars+1
dmid = tempvars+2
dtmp = tempvars+3
.endproc
;******************************************************************************
;Function name.......: selection_mark_origin
;Purpose.............: Marks selection origin at cursor position
;Input...............: Nothing
;Returns.............: Nothing
;Error returns.......: None
.proc selection_mark_origin
lda #1
sta selection_active
ldx #5
: lda mem_cur_col,x
sta selection_origin,x
dex
bpl :-
rts
.endproc
;******************************************************************************
;Function name.......: selection_grow
;Purpose.............: Expands selection to cursor position
;Input...............: Nothing
;Returns.............: Nothing
;Error returns.......: None
.proc selection_grow
; Check if selection is expanding forward or backward
sec
lda mem_cur_line
sbc selection_origin+3
tax
lda mem_cur_line+1
sbc selection_origin+4
beq :+
tax
: lda mem_cur_line+2
sbc selection_origin+5
beq :+
tax
: bcc backward
cpx #0
bne forward
lda mem_cur_col
sbc selection_origin
lda mem_cur_col+1
sbc selection_origin+1
lda mem_cur_col+2
sbc selection_origin+2
bcc backward
forward:
ldx #5
: lda selection_origin,x
sta selection_start_col,x
dex
bpl :-
ldx #5
: lda mem_cur_col,x
sta selection_end_col,x
dex
bpl :-
rts
backward:
ldx #5
: lda mem_cur_col,x
sta selection_start_col,x
dex
bpl :-
ldx #5
: lda selection_origin,x
sta selection_end_col,x
dex
bpl :-
rts
.endproc
;******************************************************************************
;Function name.......: selection_delete
;Purpose.............: Deletes the current selection
;Input...............: Nothing
;Returns.............: Nothing
;Error returns.......: None
.proc selection_delete
; Return if no active selection
lda selection_active
bne :+
rts
; Inactivate selection
: stz selection_active
; Hide cursor
jsr cursor_disable
; Move cursor to end of selection
ldx selection_end_line
ldy selection_end_line+1
lda selection_end_line+2
jsr cmd_goto_line
ldx selection_end_col
ldy selection_end_col+1
lda selection_end_col+2
jsr cmd_goto_col
; Delete char
loop:
jsr cmd_delete
bcs exit
; Check if we are at start of selection
lda selection_start_line
cmp mem_cur_line
bne loop
lda selection_start_line+1
cmp mem_cur_line+1
bne loop
lda selection_start_line+2
cmp mem_cur_line+2
bne loop
lda selection_start_col
cmp mem_cur_col
bne loop
lda selection_start_col+1
cmp mem_cur_col+1
bne loop
lda selection_start_col+2
cmp mem_cur_col+2
bne loop
exit:
; Refresh screen
jsr screen_refresh
; Show cursor
jmp cursor_activate
.endproc
.segment "VARS"
selection_active: .res 1
selection_start_col: .res 3
selection_start_line: .res 3
selection_end_col: .res 3
selection_end_line: .res 3
selection_origin: .res 6
.CODE