62 lines
1.2 KiB
Lua
62 lines
1.2 KiB
Lua
local ui = {}
|
|
|
|
function ui:button(x, y, text)
|
|
local active = false
|
|
local hot = false
|
|
if self.event[1] == "mouse_up" then
|
|
local mx = self.event[3]
|
|
local my = self.event[4]
|
|
if mx >= x and x < mx + #text and my == y then
|
|
active = true
|
|
end
|
|
elseif self.event[1] == "mouse_click" then
|
|
local mx = self.event[3]
|
|
local my = self.event[4]
|
|
if mx >= x and x < mx + #text and my == y then
|
|
hot = true
|
|
end
|
|
end
|
|
|
|
if hot then
|
|
term.setBackgroundColor(colors.lightGray)
|
|
else
|
|
term.setBackgroundColor(colors.gray)
|
|
end
|
|
term.write(text)
|
|
|
|
return active
|
|
end
|
|
|
|
function ui:textbox(width, active, placeholder, text)
|
|
if active then
|
|
if self.event[1] == "char" and #text < width-1 then
|
|
text = text .. self.event[2]
|
|
elseif self.event[1] == "key" and self.event[2] == keys.backspace then
|
|
text = text:sub(1, -2)
|
|
end
|
|
end
|
|
|
|
local x, y = term.getCursorPos()
|
|
paintutils.drawLine(x, y, x+width-1, y, colors.gray)
|
|
term.setCursorPos(x, y)
|
|
|
|
term.setTextColor(colors.white)
|
|
if active then
|
|
term.write(text)
|
|
if os.clock() % 1 < 0.5 then
|
|
term.write("|")
|
|
end
|
|
else
|
|
if text == "" then
|
|
term.setTextColor(colors.lightGray)
|
|
term.write(placeholder)
|
|
else
|
|
term.write(text)
|
|
end
|
|
end
|
|
|
|
return text
|
|
end
|
|
|
|
return ui
|