Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local buttons = {}
- buttons.event = {}
- button.items = {
- [1] = {
- text = "test";
- minX = 1;
- maxX = 10;
- minY = 5;
- maxX = 15;
- toggle = true;
- pressTime = 0;
- func = printStuff;
- }
- }
- function buttons.draw()
- for i = 1,#buttons.items do
- term.setTextColor(1)
- if buttons.items[i].active then
- term.setBackgroundColor(colors.green)
- else
- term.setBackgroundColor(colors.red)
- end
- buttons.xSpot = math.floor(math.abs(((buttons.items[i].maxX-buttons.items[i].minX)-string.len(buttons.items[i].text))/2))
- buttons.ySpot = math.floor(math.abs((buttons.items[i].maxY-buttons.items[i].maxY)/2))
- for y = buttons.items[i].minY,buttons.items[i].maxY do
- term.setCursorPos(buttons.items[i].minX,y)
- if y == buttons.ySpot then
- for x = buttons.items[i].minX,buttons.items[i].maxX do
- if x == buttons.xSpot then
- term.write(" ")
- else
- term.write(buttons.items[i].text)
- end
- end
- end
- end
- end
- end
- function buttons.checkClick(sX,sY)
- for i = 1,#buttons.items do
- if buttons.items[i].minX <= sX
- and buttons.items[i].maxX >= sX
- and buttons.items[i].minY <= sY
- and buttons.items[i].maxY >= sY then
- return i
- end
- end
- end
- function buttons.run(sIndex)
- buttons.func = buttons.items[sIndex].func
- buttons.func()
- end
- --Main loop
- while true do
- buttons.draw()
- buttons.event[1],buttons.event[2],buttons.event[3],buttons.event[4] = os.pullEvent()
- if buttons.event[1] == "mouse_click" then
- buttons.index = checkClick(buttons.event[3],buttons.event[4])
- if buttons.items[buttons.index].active then
- if buttons.items[buttons.index].toggle then
- buttons.items[buttons.index].active = false
- end
- else
- buttons.run(buttons.index)
- buttons.items[buttons.index].active = true
- if not buttons.items[buttons.index].toggle then
- buttons.items[buttons.index].timer = os.startTimer(buttons.items[buttons.index].pressTime)
- end
- end
- elseif buttons.event[1] == "timer" then
- for i = 1,#buttons.items do
- if buttons.items[i].timer == buttons.event[2]
- buttons.index = i
- end
- end
- buttons.items[buttons.index].active = false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment