This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
pixel-vision-os-input-v1.lua
248 lines (193 loc) · 6.73 KB
/
pixel-vision-os-input-v1.lua
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
function PixelVisionOS:CreateInputArea(rect, text, toolTip, font, colorOffset, spacing)
local data = editorUI:CreateInputArea(rect, text, toolTip, font, colorOffset, spacing)
self:AddInputActions(data)
data.keymap["ctrl-k"] = function(targetData)
if targetData.incsearch == true then
self:TextEditorSearchTextAndNavigate(targetData, targetData.cy)
end
end
data.keymap["shift-ctrl-f"] = function(targetData)
self:TextEditorSearchPreviousFunction(targetData)
end
data.keymap["ctrl-f"] = function(targetData)
self:TextEditorSearchNextFunction(targetData)
end
return data
end
function PixelVisionOS:CreateInputField(rect, text, toolTip, pattern, font, colorOffset, spacing)
local data = editorUI:CreateInputField(rect, text, toolTip, pattern, font, colorOffset, spacing)
self:AddInputActions(data)
return data
end
function PixelVisionOS:CodeEditor(rect, text, toolTip, language)
local data = self:CreateInputArea(rect, text, toolTip)
-- TODO create highlighter
return data
end
function PixelVisionOS:AddInputActions(data)
data.keymap["ctrl-x"] = function(targetData)
-- if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorCutText(targetData)
end
data.keymap["ctrl-c"] = function(targetData)
self:TextEditorCopyText(targetData)
end
data.keymap["ctrl-v"] = function(targetData)
self:TextEditorPasteText(targetData)
end
data.keymap["ctrl-z"] = function(targetData)
-- if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorUndo(targetData)
end
data.keymap["shift-ctrl-z"] = function(targetData)
-- if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorRedo(targetData)
end
data.keymap["ctrl-y"] = function(targetData)
-- if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorRedo(targetData)
end
end
--Copy selection text (Only if selecting)
function PixelVisionOS:TextEditorCopyText(data)
local sxs, sys, sxe, sye = editorUI:TextEditorGetOrderedSelect(data)
if sxs then --If there are any selection
local clipbuffer = {}
for lnum = sys, sye do
local line = data.buffer[lnum]
if lnum == sys and lnum == sye then --Single line selection
line = line:sub(sxs, sxe)
elseif lnum == sys then
line = line:sub(sxs, - 1)
elseif lnum == sye then
line = line:sub(1, sxe)
end
table.insert(clipbuffer, line)
end
local clipdata = table.concat(clipbuffer, "\n")
self:TextEditorClipboard(clipdata)
end
end
--Cut selection text
function PixelVisionOS:TextEditorCutText(data)
if data.sxs then
self:TextEditorCopyText(data)
editorUI:TextEditorDeleteSelection(data)
end
end
-- Paste the text from the clipboard
function PixelVisionOS:TextEditorPasteText(data)
editorUI:TextEditorBeginUndoable(data)
if data.sxs then editorUI:TextEditorDeleteSelection(data) end
local text = self:TextEditorClipboard()
if(text == nil or text == "") then
return
end
text = text:gsub("\t", " ") -- tabs mess up the layout, replace them with spaces
local firstLine = true
for line in string.gmatch(text.."\n", "([^\r\n]*)\r?\n") do
if not firstLine then
editorUI:TextEditorInsertNewLine(data) data.cx = 1
else
firstLine = false
end
editorUI:TextEditorTextInput(data, line)
end
if editorUI:TextEditorCheckPosition(data) then editorUI:TextEditorDrawBuffer(data) else editorUI:TextEditorDrawLine(data) end
-- self:TextEditorDrawLineNum(data)
editorUI:TextEditorEndUndoable(data)
editorUI:TextEditorInvalidateText(data)
end
function PixelVisionOS:TextEditorClipboard(value)
-- print("TextEditorClipboard", value)
-- TODO this should be tied to the OS scope
if(value ~= nil) then
SetClipboardText(value)
-- self.codeEditorClipboardValue = value
end
return GetClipboardText()
end
function PixelVisionOS:TextEditorSearchTextAndNavigate(data, from_line)
for i, t in ipairs(data.buffer)
do
if from_line ~= nil and i > from_line then
if string.find(t, data.searchtxt) then
data.cy = i
data.vy = i
editorUI:TextEditorCheckPosition(data)
editorUI:TextEditorInvalidateBuffer(data)
break
end
end
end
end
function PixelVisionOS:TextEditorSearchPreviousFunction(data)
local highermatch = -1
for i, t in ipairs(data.buffer)
do
if i < data.cy then
if string.find(t, "function ") then
highermatch = i
end
end
end
if highermatch > - 1 then
data.cy = highermatch
data.vy = highermatch
editorUI:TextEditorCheckPosition(data)
-- Force the buffer to redraw
editorUI:TextEditorInvalidateBuffer(data)
end
end
function PixelVisionOS:TextEditorSearchNextFunction(data)
for i, t in ipairs(data.buffer)
do
if i > data.cy then
if string.find(t, "function ") then
data.cy = i
editorUI:TextEditorCheckPosition(data)
-- Force the buffer to redraw
editorUI:TextEditorInvalidateBuffer(data)
break
end
end
end
end
-- Perform an undo. This will pop one entry off the undo
-- stack and restore the editor's contents & cursor state.
function PixelVisionOS:TextEditorUndo(data)
if #data.undoStack == 0 then
-- beep?
return
end
-- pull one entry from the undo stack
local text, state = unpack(table.remove(data.undoStack))
-- push a new entry onto the redo stack
table.insert(data.redoStack, {
editorUI:TextEditorExport(data),
editorUI:TextEditorGetState(data)
})
-- restore the editor contents
editorUI:TextEditorImport(data, text)
-- restore the cursor state
editorUI:TextEditorSetState(data, state)
end
-- Perform a redo. This will pop one entry off the redo
-- stack and restore the editor's contents & cursor state.
function PixelVisionOS:TextEditorRedo(data)
if #data.redoStack == 0 then
-- beep?
return
end
-- pull one entry from the redo stack
local text, state = unpack(table.remove(data.redoStack))
-- push a new entry onto the undo stack
table.insert(data.undoStack, {
editorUI:TextEditorExport(data),
editorUI:TextEditorGetState(data)
})
-- restore the editor contents
editorUI:TextEditorImport(data, text)
-- restore the cursor state
editorUI:TextEditorSetState(data, state)
end