Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Stručný příklad tlačítka na monitoru
- local monitor = peripheral.find("monitor") -- najde připojený monitor
- monitor.setTextScale(1)
- monitor.setBackgroundColour(colours.black)
- monitor.clear()
- -- Souřadnice tlačítka (x1,y1) - (x2,y2)
- local button = {
- x1 = 5, y1 = 3,
- x2 = 15, y2 = 5,
- label = "HELLO"
- }
- -- Funkce na vykreslení tlačítka
- local function drawButton()
- monitor.setBackgroundColour(colours.blue)
- for y = button.y1, button.y2 do
- monitor.setCursorPos(button.x1, y)
- monitor.write(string.rep(" ", button.x2 - button.x1 + 1))
- end
- local textX = button.x1 + math.floor((button.x2 - button.x1 - #button.label) / 2)
- local textY = math.floor((button.y1 + button.y2) / 2)
- monitor.setCursorPos(textX, textY)
- monitor.setTextColour(colours.white)
- monitor.write(button.label)
- end
- -- Funkce která zkontroluje, jestli klik byl v oblasti tlačítka
- local function isInside(x, y)
- return x >= button.x1 and x <= button.x2 and y >= button.y1 and y <= button.y2
- end
- drawButton()
- monitor.setCursorPos(1,1)
- monitor.setTextColour(colours.yellow)
- monitor.write("Klikni na tlacitko!")
- -- Hlavní smyčka
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- if isInside(x, y) then
- monitor.setBackgroundColour(colours.green)
- monitor.clear()
- monitor.setCursorPos(5,3)
- monitor.setTextColour(colours.white)
- monitor.write("Ahoj! 👋")
- sleep(2)
- drawButton()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment