This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_controls.lua
266 lines (209 loc) · 6.73 KB
/
default_controls.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
local DefaultControls = {}
DefaultControls.__index = DefaultControls
function DefaultControls.New(CommonUI)
local ctrls = {
CommonUI = CommonUI
}
setmetatable(ctrls, DefaultControls)
return ctrls
end
function DefaultControls:Base()
local control = self.CommonUI:EmptyControl()
control.Redraw = function()
self.CommonUI:DrawAnonymousComponent(self.CommonUI:GetComponent(control.__ID__))
end
return control
end
function DefaultControls:Box()
local control = self:Base()
-- Custom properties
control.Colour = colours.white
-- Overriden functions
control.Fetch = function()
local pixels = {}
for y = control.PosY, control.PosY + control.SizeY - 1 do
for x = control.PosX, control.PosX + control.SizeX - 1 do
pixels[x .. ";" .. y] = self.CommonUI.MakePixel(
control.Colour, control.Colour, " "
)
end
end
return pixels
end
return control
end
function DefaultControls:TextLabel()
local control = self:Box()
-- Custom properties
control.Text = "Label"
control.TextColour = colours.black
-- Overriden functions
local baseFetch = control.Fetch
control.Fetch = function()
local pixels = baseFetch()
local currentX = control.PosX
local currentY = control.PosY
for i = 1, #control.Text do
if currentX >= control.SizeX + control.PosX then
currentX = control.PosX
currentY = currentY + 1
end
if currentY >= control.SizeY + control.PosY then
break
end
local char = control.Text:sub(i, i)
if char == "\n" then
currentX = control.PosX
currentY = currentY + 1
else
pixels[currentX .. ";" .. currentY] = self.CommonUI.MakePixel(
control.Colour, control.TextColour,
char
)
currentX = currentX + 1
end
end
return pixels
end
return control
end
function DefaultControls:Header()
local control = self:TextLabel()
control.PosX = 2
control.PosY = 2
-- Overriden functions
local baseFetch = control.Fetch
control.Fetch = function()
control.PosX = 2
control.PosY = 2
control.SizeX = term.getSize()
control.SizeY = 1
-- terrible performance but it's ok
local box = self:Box()
box.PosX = 1
box.PosY = 1
box.SizeX = control.SizeX
box.SizeY = 3
box.Colour = control.Colour
local pixels = box:Fetch()
for pos, pixel in pairs(baseFetch()) do
pixels[pos] = pixel
end
return pixels
end
return control
end
function DefaultControls:Button()
local control = self:TextLabel()
-- Custom properties
control.OnClick = function() end
control.Text = "Button"
-- Overriden functions
control.OnEvent = function(event)
if event == "gain_focus" then
control.OnClick()
end
end
return control
end
function DefaultControls:BigButton()
local control = self:Button()
-- Overriden functions
local baseFetch = control.Fetch
control.Fetch = function()
-- terrible performance but it's ok
local box = self:Box()
box.PosX = control.PosX - 1
box.PosY = control.PosY - 1
box.SizeX = control.SizeX + 2
box.SizeY = control.SizeY + 2
box.Colour = control.Colour
local pixels = box:Fetch()
for pos, pixel in pairs(baseFetch()) do
pixels[pos] = pixel
end
return pixels
end
return control
end
function DefaultControls:TextBox()
local control = self:Box()
local scroll = 1
control.Text = ""
control.PasswordChar = nil
control.TextColour = colours.black
control.SetScroll = function(pos)
if pos > #control.Text then
pos = #control.Text
elseif pos < 1 then
pos = 1
end
scroll = pos
end
-- Overriden functions
local baseFetch = control.Fetch
control.Fetch = function()
local pixels = baseFetch()
local currentX = control.PosX
local currentY = control.PosY
pixels[currentX .. ";" .. currentY] = self.CommonUI.MakePixel(
control.TextColour, control.Colour,
">"
)
currentX = currentX + 1
for i = scroll, #control.Text do
if currentX >= control.SizeX + control.PosX then
currentX = control.PosX
currentY = currentY + 1
end
if currentY >= control.SizeY + control.PosY then
break
end
local char = control.Text:sub(i, i)
pixels[currentX .. ";" .. currentY] = self.CommonUI.MakePixel(
control.Colour, control.TextColour,
control.PasswordChar and control.PasswordChar:sub(1,1) or char
)
currentX = currentX + 1
end
return pixels
end
local baseRedraw = control.Redraw
control.Redraw = function()
baseRedraw()
term.setCursorBlink(control.IsFocused)
if control.IsFocused then
term.setBackgroundColour(control.Colour)
term.setTextColour(control.TextColour)
term.setCursorPos(control.PosX + #control.Text - scroll + 2, control.PosY)
end
end
control.OnEvent = function(event, ...)
if event == "gain_focus" then
control.Redraw()
elseif event == "lose_focus" then
control.Redraw()
elseif event == "char" then
local c = arg[1]
control.Text = control.Text .. c
if #control.Text:sub(scroll - 1) > control.SizeX then
control.SetScroll(scroll + control.SizeX / 2)
end
control.Redraw()
elseif event == "key" then
local switch = ({
-- Backspace
[259] = function()
if #control.Text > 0 then
control.Text = control.Text:sub(1, #control.Text-1)
control.SetScroll(scroll - 1)
end
control.Redraw()
end
})[arg[1]]
if switch then switch() end
end
end
return control
end
return DefaultControls