taro-ls: delete and insert characters properly in the middle of the string

This commit is contained in:
Iris Lightshard 2023-03-22 00:05:30 -06:00
parent 09b089fe75
commit 09ec048f53
Signed by: nilix
GPG key ID: 3B7FBC22144E6398

View file

@ -218,11 +218,11 @@ JMP2r
@on_key ( -> ) @on_key ( -> )
.textbox/mode LDZ #00 EQU ,&no_text_entry JCN .textbox/mode LDZ #00 EQU ,&no_text_entry JCN
insert_char_textbox handle_textbox
&no_text_entry &no_text_entry
BRK BRK
@insert_char_textbox ( -> ) @handle_textbox ( -> )
.Controller/key DEI DUP #00 EQU ,&no_key JCN .Controller/key DEI DUP #00 EQU ,&no_key JCN
DUP check_enter_or_esc DUP check_enter_or_esc
@ -230,17 +230,13 @@ BRK
POP POP
( handle backspace ) ( handle backspace )
.textbox/cursor LDZ #00 EQU ,&also_done JCN .textbox/cursor LDZ #00 EQU ,&also_done JCN
.textbox/cursor LDZ #01 SUB .textbox/cursor STZ delete_char
.textbox/len LDZ #01 SUB .textbox/len STZ
#00 ;textbox_text .textbox/cursor LDZ #00 SWP ADD2 STA
,&done JMP ,&done JMP
&no_delete &no_delete
DUP #20 LTH OVR #7e GTH ORA ,&no_btn JCN
( TODO: handle inserting character when bone is in middle of string ) ( TODO: handle inserting character when bone is in middle of string )
( or when string is already max length ) ( or when string is already max length )
;textbox_text .textbox/cursor LDZ #00 SWP ADD2 STA insert_char
.textbox/len LDZk INC SWP STZ
#00 ;textbox_text .textbox/len LDZ #00 SWP ADD2 STA
.textbox/cursor LDZk INC SWP STZ
&also_done &also_done
,&done JMP ,&done JMP
&no_key &no_key
@ -261,6 +257,53 @@ BRK
JMP2r JMP2r
@delete_char ( -- )
( if cursor = len, then just decrement both and add null byte )
.textbox/cursor LDZ .textbox/len LDZ NEQ ,&its_complicated JCN
.textbox/cursor LDZ #01 SUB .textbox/cursor STZ
.textbox/len LDZ #01 SUB .textbox/len STZ
#00 ;textbox_text .textbox/cursor LDZ #00 SWP ADD2 STA
JMP2r
&its_complicated
( otherwise loop through characters from cursor to len,
and copy them to their location - 1 )
.textbox/len LDZ #01 ADD .textbox/cursor LDZ &loop EQUk ,&end JCN
DUP #00 SWP ;textbox_text ADD2 LDA STH
DUP #01 SUB STHr SWP #00 SWP ;textbox_text ADD2 STA
INC
,&loop JMP &end POP2
( and then decrement counters and set the null byte )
.textbox/cursor LDZ #01 SUB .textbox/cursor STZ
.textbox/len LDZ #01 SUB .textbox/len STZ
#00 ;textbox_text .textbox/len LDZ #00 SWP ADD2 STA
JMP2r
@insert_char ( key -- )
STH
( if cursor = len, then add the charater, increment both counters, and add a null byte )
.textbox/cursor LDZ .textbox/len LDZ NEQ ,&its_complicated JCN
STHr
;textbox_text .textbox/cursor LDZ #00 SWP ADD2 STA
.textbox/len LDZk INC SWP STZ
#00 ;textbox_text .textbox/len LDZ #00 SWP ADD2 STA
.textbox/cursor LDZk INC SWP STZ
JMP2r
&its_complicated
( otherwise loop through characters from len to cursor,
and copy them to their location + 1 )
.textbox/cursor LDZ .textbox/len LDZ #01 ADD &loop EQUk ,&end JCN
DUP #00 SWP ;textbox_text ADD2 LDA STH
DUP INC STHr SWP #00 SWP ;textbox_text ADD2 STA
#01 SUB
,&loop JMP &end POP2
STHr
( and put the character at the cursor locaton, then increment the counts )
;textbox_text .textbox/cursor LDZ #00 SWP ADD2 STA
.textbox/len LDZk INC SWP STZ
.textbox/cursor LDZk INC SWP STZ
JMP2r
@check_enter_or_esc ( key -- ) @check_enter_or_esc ( key -- )
POP POP
( check mode and either cancel or send appropriate message ) ( check mode and either cancel or send appropriate message )