-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui
309 lines (302 loc) · 12.8 KB
/
gui
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
-- Viewer GUI API
-- Used for drawing the GUI
function logfile(input)
if not fs.exists("/logs") then
fs.makeDir("/logs")
else
local file = fs.open("/logs/viewer.log", "a")
file.write("[GUI] "..input.."\n")
file.close()
end
end
function clearScreen()
-- Clear the current screen, wiping for complete redraw
gui.logfile("Unloading old settings in preparation for screen refresh...")
os.unloadAPI("settings")
os.loadAPI("settings")
gui.logfile("Settings reloaded!")
term.setBackgroundColor(settings.gui.backgroundColor)
term.clear()
end
local function drawMenuBar()
-- Draw main title/info bar
local termX, termY = term.getSize()
term.setCursorPos(1, settings.gui.menuLocation == "top" and termY or 1)
term.setBackgroundColor(settings.gui.infoBarColor)
term.setTextColor(settings.gui.infoTextColor)
for i = 1, termX do
term.setCursorPos(i, settings.gui.menuLocation == "top" and termY or 1)
write(" ")
end
term.setCursorPos(1, settings.gui.menuLocation == "top" and termY or 1)
write("Viewer v"..settings.main.version)
-- Draw menu bar
term.setCursorPos(1, settings.gui.menuLocation == "top" and 1 or termY)
term.setBackgroundColor(settings.gui.menuBarColor)
for i = 1, termX do
term.setCursorPos(i, settings.gui.menuLocation == "top" and 1 or termY)
write(" ")
end
term.setCursorPos(termX, 1)
write("X")
end
function drawList(list, num)
local termX, termY = term.getSize()
for i = 3, termY - 1 do
term.setTextColor(settings.gui.textColor)
term.setBackgroundColor(settings.gui.backgroundColor)
term.setCursorPos(1, i)
write(string.rep(" ", 15))
end
term.setCursorPos(1, 2)
term.setTextColor(settings.gui.textColor)
term.setBackgroundColor(settings.gui.backgroundColor)
write("Peripherals Information")
term.setTextColor(settings.gui.tuiColor)
term.setCursorPos(15, 2)
write("|")
write(string.rep("-", 51))
for i = 2, termY - 1 do
term.setCursorPos(15, i)
if i == 2 then
term.setBackgroundColor(settings.gui.backgroundColor)
term.setTextColor(settings.gui.tuiColor)
write("|")
elseif i == 3 then
term.setBackgroundColor(settings.gui.backgroundColor)
term.setTextColor(settings.gui.tuiColor)
write("+")
elseif i == 4 then
term.setBackgroundColor(settings.gui.scrollbarColor)
term.setTextColor(settings.gui.scrollbarTextColor)
write("^")
elseif i == termY - 1 then
term.setBackgroundColor(settings.gui.scrollbarColor)
term.setTextColor(settings.gui.scrollbarTextColor)
write("v")
else
term.setBackgroundColor(settings.gui.scrollbarColor)
term.setTextColor(settings.gui.scrollbarTextColor)
write("|")
end
end
term.setBackgroundColor(settings.gui.menuBarColor)
term.setTextColor(settings.gui.menuTextColor)
term.setCursorPos(6, settings.gui.menuLocation == "top" and 1 or termY)
write(string.rep(" ", 9))
term.setCursorPos(1, settings.gui.menuLocation == "top" and 1 or termY)
write("Page "..math.ceil(num / 15).."/"..math.ceil(#list / 15))
for i = 1, 15 do
if #list < 15 then
term.setTextColor(settings.gui.textColor)
term.setBackgroundColor(settings.gui.backgroundColor)
term.setCursorPos(1, i + 3)
if string.len((list[i] and list[i].label) and list[i].label or "") > 11 then
write(list[i] and string.sub(list[i].label, 1, 11).."..." or "")
else
write(list[i] and list[i].label or "")
end
term.setBackgroundColor(settings.gui.highlightBackgroundColor)
term.setTextColor(settings.gui.highlightTextColor)
term.setCursorPos(1, num + 3)
if string.len(list[num].label) > 11 then
write(string.sub(list[num].label, 1, 11).."...")
else
write(list[num].label)
end
else
if num > 15 then
term.setTextColor(settings.gui.textColor)
term.setBackgroundColor(settings.gui.backgroundColor)
term.setCursorPos(1, i + 3)
if string.len((list[i + 15] and list[i + 15].label) and list[i + 15].label or "") > 11 then
write(list[i + 15] and string.sub(list[i + 15].label, 1, 11).."..." or "")
else
write(list[i + 15] and list[i + 15].label or "")
end
term.setBackgroundColor(settings.gui.highlightBackgroundColor)
term.setTextColor(settings.gui.highlightTextColor)
term.setCursorPos(1, num - 12)
if string.len(list[num].label) > 11 then
write(string.sub(list[num].label, 1, 11).."...")
else
write(list[num].label)
end
else
term.setTextColor(settings.gui.textColor)
term.setBackgroundColor(settings.gui.backgroundColor)
term.setCursorPos(1, i > 15 and (i - 12) or (i + 3))
if string.len((list[i] and list[i].label) and list[i].label or "") > 11 then
write(list[i] and string.sub(list[i].label, 1, 11).."..." or "")
else
write(list[i] and list[i].label or "")
end
term.setBackgroundColor(settings.gui.highlightBackgroundColor)
term.setTextColor(settings.gui.highlightTextColor)
term.setCursorPos(1, num + 3)
if string.len(list[num].label) > 11 then
write(string.sub(list[num].label, 1, 11).."...")
else
write(list[num].label)
end
end
end
end
end
function drawInfo(list, num, page, fluidNum)
local termX, termY = term.getSize()
term.setCursorPos(17, settings.gui.menuLocation == "top" and 1 or termY)
term.setBackgroundColor(settings.gui.menuBarColor)
term.setTextColor(settings.gui.menuTextColor)
write("Page: "..string.upper(string.sub(page, 1, 1))..string.sub(page, 2))
term.setCursorPos(17, 4)
term.setTextColor(settings.gui.textColor)
term.setBackgroundColor(settings.gui.backgroundColor)
if string.len(list[num].name) <= 24 then --24 max
write("Location: "..list[num].name)
else
write("Location: "..string.sub(list[num].name, 1, 22).."...")
end
term.setCursorPos(17, 5)
if alias[list[num].label] ~= nil and string.len(alias[list[num].label]) <= 28 then --28 max
write("Type: "..alias[list[num].label])
elseif string.len(list[num].label) <= 28 then
write("Type: "..list[num].label)
else
write("Type: "..string.sub((alias[list[num].label] or list[num].label), 1, 25).."...")
end
if type(list[num][page]) == "table" then
local iNum = 1
term.setCursorPos(17, 6 + iNum)
write(page == "fluids" and ("Fluids: "..#list[num][page]) or "")
for i, v in pairs(list[num][page]) do
term.setCursorPos(17, 6 + iNum)
local opt = string.upper(string.sub(i, 1, 1))..string.sub(i, 2)..": "
if type(v) == "table" then
if page == "fluids" then
term.setCursorPos(17, 6 + 2)
write("Selected: "..list[num][page][fluidNum].contents.rawName)
local aNum = 1
for a, b in pairs(list[num][page][fluidNum]) do
if type(list[num][page][fluidNum][a]) == "table" then
term.setCursorPos(17, 6 + 2 + aNum)
write(string.upper(string.sub(a, 1, 1))..string.sub(a, 2)..": ")
local cNum = 1
for c, d in pairs(list[num][page][fluidNum][a]) do
term.setCursorPos(19, 6 + 2 + aNum + cNum)
write(string.upper(string.sub(c, 1, 1))..string.sub(c, 2)..": "..d)
cNum = cNum + 1
end
else--if type(list[num][page][1][a]) == "string" then
term.setCursorPos(17, 6 + 2 + aNum)
write(string.upper(string.sub(a, 1, 1))..string.sub(a, 2)..": "..b)
end
aNum = aNum + 1
end
elseif string.len(i..": "..textutils.serialize(v)) <= 34 then -- 34 max
write(string.gsub(opt, "_", " "))
write(textutils.serialize(v))
else
write(string.gsub(opt, "_", " "))
write("View in new page")
-- Find some way to either view the data on this, or dump info to a file
-- Need to add a way to handle inventory data or additional nested tables
end
else
if string.len(opt..v) <= 34 then -- 34 max
write(string.gsub(opt, "_", " "))
write(v)
else
write(string.gsub(opt, "_", " "))
write(string.sub(v, 1, 31 - #opt).."...")
-- Find some way to either view the data on this, or dump info to a file
-- Need to add a way to handle inventory data or additional nested tables
end
end
iNum = iNum + 1
end
else
term.setCursorPos(17, 7)
local opt = string.upper(string.sub(page, 1, 1))..string.sub(page, 2)..": "
if list[num][page] then
write(opt..list[num][page])
else
-- No info to display
end
end
-- Need to find a way to list the information per page
-- A bit annoying to do this, considering how I construct the list table
-- Construct the page info
-- When listing the methods, use string.gsub to iterate through and replace the commas with "\n"
-- Each list item apart from the name and label should be a page of its own
-- Menu bar should show "Commands" that can be clicked on in order to execute some of those peripheral commands. Would require use of pcall due to likely errors.
end
function drawPopup(text, opt)
local termX, termY = term.getSize()
-- Determine max text width
local maxWidth = 1
-- Determine height of the options
local height = #text
-- Determine width of options
for _, v in ipairs(text) do
local width = #v
if width > maxWidth then
maxWidth = width
end
end
-- Determine placement of box bounds
local startX = ((termX / 2) - (maxWidth / 2))
local endX = (termX / 2) + (maxWidth / 2) + 1
local startY = math.floor(((termY / 2) - (height / 2)) - 1)
local endY = math.floor((termY / 2) + (height / 2))
-- Change text/background color
for i = startX, endX do
for v = startY, endY do
-- Draw box
term.setBackgroundColor(v == startY and settings.gui.popupTitleBackground or settings.gui.popupBackground)
term.setCursorPos(i, v)
write(" ")
end
end
term.setCursorPos(endX, startY)
term.setTextColor(settings.gui.popupTitleText)
term.setBackgroundColor(settings.gui.popupTitleBackground)
write("X")
for i = 1, #text do
-- Write options
local textStart = (termX / 2) - (#text[i] / 2) + 1
term.setCursorPos(textStart, i == 1 and startY or (startY + i))
term.setBackgroundColor(i == 1 and settings.gui.popupTitleBackground or settings.gui.popupBackground)
term.setTextColor(i == 1 and settings.gui.popupTitleText or settings.gui.popupText)
write(string.upper(string.sub(text[i], 1, 1))..string.sub(text[i], 2))
end
while true do
local events = {os.pullEvent()}
if events[1] == "mouse_click" and events[2] == 1 then
if events[3] == endX and events[4] == startY then
-- Close popup window
return opt
else
for i = 1, #text do
local textStart = (termX / 2) - (#text[i] / 2) + 1
local textEnd = textStart + #text[i]
textY = startY + 1 + i
if events[3] >= textStart and events[3] <= textEnd and events[4] == textY then
-- Select option
local opt = text[i + 1]
return opt
end
end
end
else
-- Repeat until option selected
end
end
end
function drawScreen(list, num, page, fluidNum)
-- Main GUI assembly
clearScreen()
drawMenuBar()
drawList(list, num)
drawInfo(list, num, page, fluidNum)
end