keypadOS/src/ui.lua

126 lines
2.8 KiB
Lua
Raw Normal View History

2024-08-18 00:36:52 +00:00
local utils = require("utils.lua")
2024-08-18 00:34:13 +00:00
local basalt = require("basalt")
local config = require("config.lua")
local updater = require("updater.lua")
--- @type Monitor
2024-08-18 00:36:52 +00:00
local monitor = utils.Cast(peripheral.find("monitor"))
2024-08-18 00:34:13 +00:00
--- @type drive
2024-08-18 00:36:52 +00:00
local drive = utils.Cast(peripheral.find("drive"))
2024-08-18 00:34:13 +00:00
--- @class Ui
--- @field pin string
--- @field main any
--- @field pinLabel any
--- @field enterButton any
--- @field resetEverything function
--- @field unlockDoor function
--- @field addToPin function
--- @param ui Ui
local function unlockDoor(ui)
if drive.isDiskPresent() then
if drive.getDiskLabel() == config.correctPin then
ui.pin = config.correctPin
drive:ejectDisk()
end
end
basalt.debug("test")
if ui.pin == config.correctPin then
ui.enterButton:setBackground(colors.green)
ui.pinLabel:setText("Welcome")
redstone.setOutput("front", true)
if drive.isDiskPresent() then
if drive.getDiskLabel() == nil then
drive.setDiskLabel(config.correctPin)
ui.pinLabel:setText("Crd set")
drive:ejectDisk()
end
end
else
ui.pinLabel:setText("Wrong")
ui.enterButton:setBackground(colors.red)
end
ui.main:addThread()
:start(ui.resetEverything)
end
--- @param ui Ui
local function resetEverything(ui)
sleep(2)
ui.pin = ""
ui.pinLabel:setText("")
redstone.setOutput("front", false)
ui.enterButton:setBackground(colors.blue)
end
--- @param ui Ui
local function addToPin(ui, i)
if #ui.pin >= 5 then
return
end
ui.pin = ui.pin .. tostring(i)
ui.pinLabel:setText(ui.pin)
end
function InitUi()
local ui = {
resetEverything,
unlockDoor,
addToPin,
main = basalt.addMonitor(monitor),
}
ui.main:setMonitor(monitor)
ui.pinLabel = ui.main:addLabel()
:setText("")
:setFontSize(1)
ui.enterButton = ui.main:addButton()
:setText(">>>>")
:setBackground(colors.blue)
:setPosition(6,3)
:setSize(1.5,3.2)
:onClick(function()
unlockDoor(ui)
end)
local btnX = 1
local btnY = 3
for i = 1, 9 do
ui.main:addButton()
:setPosition(btnX, btnY)
:setText(tostring(i))
:setSize(2,1)
:onClick(function()
addToPin(ui, i)
end)
btnX = btnX + 2
if btnX >= 6 then
btnY = btnY + 1
btnX = 1
end
end
local status, _ = pcall(function ()
parallel.waitForAll(basalt.autoUpdate, updater.updateChecker)
end)
if not status then
monitor.clear()
fs.delete("basalt.lua")
fs.delete("startup.lua")
fs.copy("backup.lua", "startup.lua")
os.reboot()
end
end
return {
InitUi,
}