-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.lua
More file actions
503 lines (426 loc) · 16 KB
/
ui.lua
File metadata and controls
503 lines (426 loc) · 16 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
------------------------------------
-- MONITORS
------------------------------------
------------------------------------
-- BUTTONS CODE
------------------------------------
-- Credit: I watched Direwolf20's Button API
-- video before writing this, and this is based
-- off of his implementation. I have generalized
-- it so I can use it alongside the window API
-- to compartmentalize the output screen.
------------------------------------
function makeButton(handler, name, text, func, bgcolor, x, y, w, h, txtcolor)
txtcolor = txtcolor or colors.white
handler[name] = {}
handler[name].text = text
handler[name].func = func
handler[name].txtcolor = txtcolor
handler[name].bgcolor = bgcolor
handler[name].x = x
handler[name].y = y
handler[name].w = w
handler[name].h = h
handler[name].active = true
end
function drawButtons(handler, output)
for name, data in pairs(handler) do
if (data.active) then
drawButton(data, output)
end
end
end
function drawButton(data, output)
local oldbg = output.getBackgroundColor()
local oldtc = output.getTextColor()
local midy = math.floor(data.y + data.h / 2)
output.setBackgroundColor(data.bgcolor)
output.setTextColor(data.txtcolor)
if (data.bgcolor == data.txtcolor) then
output.setTextColor(oldbg)
end
-- Mode for a single line text entry
if (not data.text:find("\n")) then
for j = data.y, data.y + data.h - 1 do
output.setCursorPos(data.x, j)
if (j == midy) then
for i = 1, data.w - #(data.text) + 1 do
if (i == math.floor((data.w - #(data.text)) / 2) + 1) then
output.write(data.text)
else
output.write(" ")
end
end
else
for i = 1, data.w do
output.write(" ")
end
end
end
else -- Mode for a multi-line text entry
local numLines = countC(data.text, "\n") + 1
local lines = {}
local count = 1
for line in string.gmatch(data.text, "([^\n]+)") do
lines[count] = line
count = count + 1
end
for j = data.y, data.y + data.h - 1 do
output.setCursorPos(data.x, j)
-- this part is a mess, I am so sorry (I spent like 30 minutes messing with numbers until this worked)
if (j >= midy - math.ceil(numLines / 2) and j <= midy + math.floor(numLines / 2) - 1) then
local line_index = j - (midy - math.ceil(numLines / 2)) + 1
for i = 1, data.w - #(lines[line_index]) + 1 do
if (i == math.floor((data.w - #(lines[line_index])) / 2) + 1) then
output.write(lines[line_index])
else
output.write(" ")
end
end
else
for i = 1, data.w do
output.write(" ")
end
end
end
end
output.setTextColor(oldtc)
output.setBackgroundColor(oldbg)
end
-- TODO: add filter to check for click on a specific monitor
-- Calls the function associated with a button and passes it
-- the output terminal, and relative x and y of the touch.
function checkButtons(handler, output)
local event, side, x, y = os.pullEvent("monitor_touch")
for name, data in pairs(handler) do
if (x >= data.x and x <= data.x + data.w - 1) then
if (y >= data.y and y <= data.y + data.h - 1) then
data.func(output, x - data.x, y - data.y)
end
end
end
end
function demoButtons()
mon = peripheral.find("monitor")
mon.clear()
buttons = {}
mon.setTextColor(colors.white)
mon.setBackgroundColor(colors.black)
local function test(output, relx, rely)
term.redirect(output)
print(string.format("Clicked button at %d, %d.", relx, rely))
end
makeButton(buttons, "1", "Demo", test, colors.green, 2, 2, 9, 3)
makeButton(buttons, "error", "oh no!", test, colors.red, 20, 5, 8, 3)
makeButton(buttons, "party", "Party time!", test, colors.pink, 40, 5, 19, 5)
drawButtons(buttons, mon)
while true do
checkButtons(buttons, term.native())
end
end
------------------------------------
-- UTILITY FUNCTIONS
------------------------------------
function countC(s, c)
local count = 0
for i in string.gmatch(s, c) do
count = count + 1
end
return count
end
------------------------------------
-- MENUS
------------------------------------
-- Deactivates the menu when the q key is pressed.
-- Note: This may not completely exit the program if
-- menu_init() was called inside something else.
function checkQuit(menu, output)
event, key = os.pullEvent("key_up")
if (key == keys.q) then
quit(menu, output)
end
end
function quit(menu, output)
menu.active = false
output.clear()
term.clear()
term.setCursorPos(1,1)
end
-- This output argument can be used ANYWHERE in the following code,
-- make a 'local output' if you want a different one
function menu_init(output)
---- HELPER STUFF ----
----------------------
-- first class functions !!
-- Returns a function that swaps from one menu to another
function swap_menu_f(cur, other)
return function (output)
cur.close(output)
other.open(output)
end
end
-- Returns a function that will open the given menu.
-- Before updating the screen, it will call extra(output)
function generic_open(menu, extra)
return function (output)
menu.active = true
if (extra) then
extra(output)
end
menu.update(output)
-- wait for quit or button press
while menu.active do
parallel.waitForAny(
function () checkQuit(menu, output) end,
-- Instead of 'output', you can set a different screen
-- where button output will appear, like term.native()
-- for logging purposes.
function () checkButtons(menu.buttons, output) end
)
end
end
end
function generic_update(menu, extra)
return function (output)
output.setTextColor(colors.white)
output.setBackgroundColor(colors.black)
output.clear()
drawButtons(menu.buttons, output)
if (menu.header) then
menu.header()
end
if (menu.footer) then
menu.footer()
end
if (extra) then
extra(output)
end
end
end
function generic_header(text, tcolor, bcolor)
return function ()
local oldbg = output.getBackgroundColor()
local oldtc = output.getTextColor()
local width, height = output.getSize()
local startx = math.floor(width / 2 - (#text / 2))
output.setBackgroundColor(bcolor or colors.black)
output.setTextColor(tcolor or colors.white)
output.setCursorPos(1, 1)
for i = 1, width do
output.write(" ")
end
output.setCursorPos(startx, 1)
output.write(text)
output.setBackgroundColor(oldbg)
output.setTextColor(oldtc)
end
end
function generic_footer(text, tcolor, bcolor)
return function ()
local oldbg = output.getBackgroundColor()
local oldtc = output.getTextColor()
local width, height = output.getSize()
local startx = math.floor(width / 2 - (#text / 2))
output.setBackgroundColor(bcolor or colors.black)
output.setTextColor(tcolor or colors.white)
output.setCursorPos(1, height)
for i = 1, width do
output.write(" ")
end
output.setCursorPos(startx, height)
output.write(text)
output.setBackgroundColor(oldbg)
output.setTextColor(oldtc)
end
end
function generic_close(menu)
return function (output)
menu.active = false
output.clear()
end
end
-------------------
---- MENU INIT ----
main_menu = {}
aim_menu = {}
setup_menu = {}
-------------------
---- MAIN MENU ----
main_menu.header = generic_header("Cannon Controller", colors.red)
main_menu.footer = generic_footer("Version: Bromeliad", colors.yellow)
main_menu.active = false
main_menu.buttons = {}
local btns = main_menu.buttons
makeButton(btns, "setup", "Setup", function () print("setup") end,
colors.lime, 6, 9, 9, 3)
makeButton(btns, "aim", "Aim", swap_menu_f(main_menu, aim_menu),
colors.red, 17, 8, 7, 5)
makeButton(btns, "history", "History", function () print("history") end,
colors.lightBlue, 26, 9, 9, 3)
makeButton(btns, "quit", "Quit", function () quit(main_menu, output) end,
colors.gray, 2, 16, 6, 3)
main_menu.open = generic_open(main_menu)
main_menu.close = generic_close(main_menu)
main_menu.update = generic_update(main_menu)
------------------
---- AIM MENU ----
aim_menu.active = false
aim_menu.buttons = {}
aim_menu.selected = nil
aim_menu.aim_mode = "exact"
aim_menu.target = vector.new(0, 0, 0)
btns = aim_menu.buttons
local function num_panel(o, x, y)
if (aim_menu.selected) then
local btn = aim_menu.buttons[aim_menu.selected]
local num = aim_menu.target[aim_menu.selected]
local add = math.floor(x / 2 + 0.5) + 3 * (y - 1)
if (add > 9) then
add = 0
end
if (#tostring(num) < 7) then
num = num * 10 + (num >= 0 and add or (add * -1))
end
aim_menu.target[aim_menu.selected] = num
btn.text = string.sub(btn.text, 1, 2) ..
string.rep(" ", 7 - #tostring(num)) .. num
aim_menu.update(output)
end
end
local function clear_f(o)
if (aim_menu.selected) then
local btn = aim_menu.buttons[aim_menu.selected]
aim_menu.target[aim_menu.selected] = 0
btn.text = string.sub(btn.text, 1, 2) .. string.rep(" ", 6) .. 0
aim_menu.update(output)
end
end
local function backspace_f(o)
if (aim_menu.selected) then
local btn = aim_menu.buttons[aim_menu.selected]
num = aim_menu.target[aim_menu.selected]
if (num >= 0) then
num = math.floor(aim_menu.target[aim_menu.selected] / 10)
else
num = math.ceil(aim_menu.target[aim_menu.selected] / 10)
end
aim_menu.target[aim_menu.selected] = num
btn.text = string.sub(btn.text, 1, 2) ..
string.rep(" ", 7 - #tostring(num)) .. num
aim_menu.update(output)
end
end
local function negate_f(o)
if (aim_menu.selected) then
local btn = aim_menu.buttons[aim_menu.selected]
num = aim_menu.target[aim_menu.selected] * -1
aim_menu.target[aim_menu.selected] = num
btn.text = string.sub(btn.text, 1, 2) ..
string.rep(" ", 7 - #tostring(num)) .. num
aim_menu.update(output)
end
end
local function exact_f(o)
aim_menu.aim_mode = "exact"
aim_menu.buttons.exact.bgcolor = colors.white
aim_menu.buttons.relative.bgcolor = colors.gray
aim_menu.update(output)
end
local function relative_f(o)
aim_menu.aim_mode = "relative"
aim_menu.buttons.relative.bgcolor = colors.white
aim_menu.buttons.exact.bgcolor = colors.gray
aim_menu.update(output)
end
-- This is where the magic happens
local function aim_f(o)
x, y, z = aim_menu.target.x, aim_menu.target.y, aim_menu.target.z
shell.execute("aim.lua", tostring(x), tostring(y), tostring(z),
"0", aim_menu.aim_mode or "exact")
local failed = false
-- Wait 1 second to see if the cannon failed to aim.
parallel.waitForAny(
function ()
sleep(1)
end,
function ()
local done = false
while (not done) do
event, result, dist = os.pullEvent()
if(event == "cannon_aim_failure") then
print("Couldn't aim at that target.")
print("Fake shot landed " .. dist ..
(result == 1 and " past it." or " short."))
done = true
end
failed = true -- does Lua scope work like this?
end
end
)
-- Then wait for the cannon to aim successfully
if (not failed) then
local done = false
while (not done) do
event, result, dist = os.pullEvent()
if(event == "cannon_aim_success") then
print("Cannon aimed at target.")
done = true
end
end
end
end
local function select_f(coord)
return function (o)
local allbtns =
{aim_menu.buttons.x, aim_menu.buttons.y, aim_menu.buttons.z}
for i, b in ipairs(allbtns) do
b.txtcolor = colors.white
end
aim_menu.buttons[coord].txtcolor = colors.black
aim_menu.selected = coord
aim_menu.update(output)
end
end
makeButton(btns, "relative", "Relative", relative_f,
colors.gray, 8, 3, 10, 3)
makeButton(btns, "exact", "Exact", exact_f,
colors.gray, 23, 3, 9, 3)
makeButton(btns, "x", "X: 0", select_f("x"),
colors.red, 3, 7, 11, 3)
makeButton(btns, "y", "Y: 0", select_f("y"),
colors.green, 15, 7, 11, 3)
makeButton(btns, "z", "Z: 0", select_f("z"),
colors.blue, 27, 7, 11, 3)
makeButton(btns, "numbers", "1 2 3\n4 5 6\n7 8 9\n 0 ", num_panel,
colors.white, 17, 11, 7, 6)
makeButton(btns, "negate", "Negate", negate_f,
colors.gray, 25, 11, 8, 1)
makeButton(btns, "backsp", "Backspace", backspace_f,
colors.gray, 25, 13, 11, 1)
makeButton(btns, "clear", "Clear", clear_f,
colors.gray, 25, 15, 7, 1)
makeButton(btns, "fire", "Aim at\nTarget", aim_f,
colors.orange, 5, 12, 10, 4)
makeButton(btns, "back", "Back", swap_menu_f(aim_menu, main_menu),
colors.red, 1, 18, 4, 3)
aim_menu.open = generic_open(aim_menu,
function (o)
aim_menu.selected = nil
aim_menu.aim_mode = nil
aim_menu.buttons.relative.bgcolor = colors.gray
aim_menu.buttons.exact.bgcolor = colors.gray
end)
aim_menu.close = generic_close(aim_menu)
aim_menu.update = generic_update(aim_menu)
------------------------
---- OPEN MAIN MENU ----
main_menu.open(output)
end
------------------------------------
-- MAIN PROGRAM CODE
------------------------------------
-- TODO: Add terminal mode that can take keyboard input
mon = peripheral.find("monitor")
print("Press 'q' to quit.")
menu_init(mon)
-- demoButtons()