-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_input_string.ks
205 lines (190 loc) · 6.27 KB
/
lib_input_string.ks
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
// This file is distributed under the terms of the MIT license, (c) the KSLib team
@LAZYGLOBAL off.
if exists("1:/lib_file_exists.ks") {
runpath("1:/lib_file_exists").
} else {
runpath("0:/lib_file_exists").
}
function input_string
{
parameter
line, // top edge of the keyboard
x0, y0, // where to display the input on screen
hidden, // set to true to display "*" instead of character
help. //true or false (the info above the keyboard).
////////////////// internal functions ////////////////////////////////
function dep_check {
parameter fileName.
local fileList is list().
local found is false.
list files in fileList.
for file in fileList {
if file = fileName {
set found to true.
}
}
return found.
}
function refresh_board // this is done as 1 large print as drawing the boxes individually lags.
{
if shift = 0 {
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line).
print "| ` | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | - | = |" at (0,line+1).
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+2).
print "| q | w | e | r | t | y | u | i | o | p | [ | ] |Ent|" at (0,line+3).
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+4).
print "| a | s | d | f | g | h | j | k | l | ; | ' | # |Cap|" at (0,line+5).
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+6).
print "| \ | z | x | c | v | b | n | m | , | . | / | |" at (0,line+7).
print "+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+8).
} else if shift = 1 {
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line).
print "|N/A| ! | "+quote+" |N/A| $ | % | ^ | & | * | ( | ) | _ | + |" at (0,line+1).
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+2).
print "| Q | W | E | R | T | Y | U | I | O | P | { | } |Ent|" at (0,line+3).
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+4).
print "| A | S | D | F | G | H | J | K | L | : | @ | ~ |Cap|" at (0,line+5).
print "+---+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+6).
print "| | | Z | X | C | V | B | N | M | < | > | ? | |" at (0,line+7).
print "+---+---+---+---+---+---+---+---+---+---+---+---+" at (0,line+8).
}
refresh_position("N/A",0).
}
function refresh_position
{
parameter dir, increment.
if dir = "row" {
set row to mod(row+increment+keyboard[shift]:length, keyboard[shift]:length).
set col to mod(col+keyboard[shift][row]:length, keyboard[shift][row]:length).
} else if dir = "col" {
set col to mod(col+increment+keyboard[shift][row]:length, keyboard[shift][row]:length).
}
print "+---+" at (4*oldCol,2*oldRow+line).
print "|" at (4*oldCol,2*oldRow+line+1).
print "|" at (4*(oldCol+1),2*oldRow+line+1).
print "+---+" at (4*oldCol,2*oldRow+line+2).
print "#===#" at (4*col,2*row+line).
print "H" at (4*col,2*row+line+1).
print "H" at (4*(col+1),2*row+line+1).
print "#===#" at (4*col,2*row+line+2).
set oldrow to row.
set oldCol to col.
}
function string_add {
if keyboard[shift][row][col] = "Cap" {
set shift to mod(shift+1,2).
refresh_board().
} else if keyboard[shift][row][col] = "Ent" {
set Enter to True.
} else {
string:add(keyboard[shift][row][col]).
if hidden {
print "*" at (x0+string:length+2,y0).
} else {
print keyboard[shift][row][col] at (x0+string:length+2,y0).
}
}
}
function string_remove {
if not string:empty {
string:remove(string:length-1).
print " " at (x0+string:length+3,y0).
}
}
/////////////function body/////////////////////////////////////
local col is 0.
local row is 0.
local oldCol is 0.
local oldRow is 0.
local shift is 0.
local controlMap is ship:control.
local oldTop is controlMap:pilottop.
local oldStar is controlMap:pilotstarboard.
local oldFore is controlMap:pilotfore.
local Enter is False.
local string is list().
local returnString is "".
local char is 0.
local oldT is 0.
local blink is false.
local quote is " ".
if file_exists(spec_char.ksm) {
run spec_char.
}.
local keyboard is list(
list( //lower case.
list("`","1","2","3","4","5","6","7","8","9","0","-","="),
list("q","w","e","r","t","y","u","i","o","p","[","]","Ent"),
list("a","s","d","f","g","h","j","k","l",";","'","#","Cap"),
list("\","z","x","c","v","b","n","m",",",".","/"," ")
),
list( //upper case.
list("¬","!",quote,"£","$","%","^","&","*","(",")","_","+"),
list("Q","W","E","R","T","Y","U","I","O","P","{","}","Ent"),
list("A","S","D","F","G","H","J","K","L",":","@","~","Cap"),
list("|","Z","X","C","V","B","N","M","<",">","?"," ")
)
).
local oldHeight is terminal:height.
local oldWidth is terminal:width.
if oldwidth < 53 {
set terminal:width to 53.
}
if oldHeight < line+9 {
set terminal:height to line+9.
}
refresh_board().
if help {
Print "Navigate using the IJKL keys. Use the H and N" at (3,line-3).
Print "keys to add and remove characters respectively." at (3,line-2).
}.
until Enter
{
if oldt + 0.3 < time {
if blink {
print " " at (x0+string:length+3,y0).
toggle blink.
} else {
print "_" at (x0+string:length+3,y0).
toggle blink.
}
set oldT to time.
}.
if (controlMap:PILOTTOP <> oldTop) or (controlMap:PILOTSTARBOARD <> oldStar) or (controlMap:PILOTFORE <> oldFore)
{
if help {
print " " at (3,line-3).
print " " at (3,line-2).
set help to false.
}
if controlMap:pilottop > 0
{
refresh_position("row",+1).
} else if controlMap:pilottop < 0 {
refresh_position("row", -1).
}
if controlMap:pilotstarboard > 0 {
refresh_position("col", +1).
} else if controlMap:pilotstarboard < 0 {
refresh_position("col", -1).
}
if controlMap:pilotfore > 0 {
string_add().
} else if controlMap:pilotfore < 0 {
string_remove().
}
SET oldTop TO controlMap:PILOTTOP.
SET oldStar TO controlMap:PILOTSTARBOARD.
SET oldFore TO controlMap:PILOTFORE.
}
}
print " " at (x0+string:length+3,y0).
set terminal:width to oldWidth.
set terminal:height to oldHeight.
set char to string:iterator.
char:reset.
until not char:next {
set returnString to returnString+ char:value.
}
return returnstring.
}