This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.lua
227 lines (180 loc) · 4.81 KB
/
core.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
local monitor = require("util.monitor_util")
require("util.draw_util")
require("config.core")
local tabs = {}
local currentTab
local hasErrorScreen = false
local running = true
local timerId
function initialize()
monitor.clear()
monitor.setCursorBlink(false)
if system_type == "tablet" then
os.setComputerLabel("System")
tabs = require("tablet/tablet_tab")
draw()
else
os.setComputerLabel("System - " .. system_type)
local tab = require("system/system_" .. system_type)
table.insert(tabs, tab)
openTab(tab)
end
peripheral.find("modem", rednet.open)
startEventLoop()
end
function close()
closeTab()
print("Goodnight")
end
function draw()
local width, height = monitor.getSize()
local buttonWidth = width / 2
local buttonHeight = height / (#tabs / 2)
for step, tab in ipairs(tabs) do
monitor.setBackgroundColor(tab.backgroundColor)
local offsetX = getOffsetX(step, buttonWidth)
local offsetY = getOffsetY(step, buttonHeight)
for x = 1, buttonWidth do
for y = 1, buttonHeight do
monitor.setCursorPos(offsetX + x, offsetY + y)
monitor.write(" ")
end
end
local textSize = string.len(tab.name)
local textRow = math.floor((buttonHeight + 1) / 2)
local offsetText = math.ceil((buttonWidth + 1 - textSize) / 2)
monitor.setCursorPos(offsetX + offsetText, offsetY + textRow)
monitor.setTextColor(tab.textColor)
monitor.write(tab.name)
end
end
function getOffsetX(step, width)
if step % 2 == 0 then
return width
end
return 0
end
function getOffsetY(step, height)
return math.floor((step - 1) / 2) * height
end
function openTab(tab)
if tab then
closeTab()
drawBrackground()
drawTabTitle(tab)
currentTab = tab
tab.openTab()
end
end
function closeTab()
if currentTab then
xpcall(currentTab.closeTab, handleError)
currentTab = nil
draw()
end
end
function handleButton(button, x, y)
if button == 3 and system_type == "tablet" then
if currentTab then
closeTab()
end
return
elseif currentTab then
currentTab.handleButton(button, x, y)
return
end
local width, height = monitor.getSize()
local tabSize = #tabs
local buttonWidth = (width / 2)
local buttonHeight = (height / (tabSize / 2))
for step, tab in ipairs(tabs) do
local offsetX = getOffsetX(step, buttonWidth)
local offsetY = getOffsetY(step, buttonHeight)
if x >= offsetX and x <= offsetX + buttonWidth then
if y >= offsetY and y <= offsetY + buttonHeight then
monitor.setCursorPos(10, 10)
xpcall(function() openTab(tab) end, handleError)
end
end
end
end
function handleRednetMessage(sender, message, protocol)
for index, value in ipairs(tabs) do
if (value.network) then
if (value.network.isListening(message, protocol)) then
value.handleRednetMessage(sender, message, protocol)
end
end
end
end
function handleTick()
for index, value in ipairs(tabs) do
if (value.handleTick) then
value.handleTick()
end
end
end
function handleError(error)
if hasErrorScreen then
return
end
hasErrorScreen = true
closeTab()
drawBrackground(colors.red)
local width, height = monitor.getSize()
local text = "An error occurred"
local textSize = string.len(text)
local offset = math.ceil((width - textSize) / 2)
monitor.setCursorPos(offset, 2)
monitor.setBackgroundColor(colors.red)
monitor.setTextColor(colors.white)
monitor.write(text)
local textMaxWidth = width - 6
local textHeightOffset = 5
for i = textHeightOffset, height do
monitor.setCursorPos(2, i)
local currentTextWidth = (i - textHeightOffset) * textMaxWidth
monitor.write(string.sub(error, currentTextWidth, currentTextWidth + textMaxWidth - 1))
end
for i = 1, 10 do
monitor.setCursorPos(width - 1, 2)
monitor.setTextColor(colors.white)
monitor.write(10 - i)
sleep(1)
end
hasErrorScreen = false
-- os.reboot()
draw()
end
function startEventLoop()
timerId = os.startTimer(1)
while true do
local event = {os.pullEvent()}
local eventName = event[1]
if eventName == "terminate" then
close()
break
end
xpcall(function ()
if hasErrorScreen then
elseif eventName == "timer" then
if (event[2] == timerId) then
os.cancelTimer(timerId)
timerId = os.startTimer(1)
handleTick()
end
elseif eventName == "rednet_message" then
handleRednetMessage(event[2], event[3], event[4])
elseif eventName == "mouse_click" then
if (monitor == term) then
handleButton(event[2], event[3], event[4])
else
print("Only monitor!")
end
elseif eventName == "monitor_touch" then
handleButton(2, event[3], event[4])
end
end, handleError)
end
end
initialize()