Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Сенсорные кнопки для управления контроллером красного камня
- -- https://computercraft.ru/topic/3271-programma-dve-knopki/?do=findComment&comment=42097
- -- https://i.imgur.com/hzCskre.png
- local event = require("event")
- local component = require("component")
- local gpu = component.gpu
- local red = component.isAvailable("redstone") and component.redstone or error("нет контроллера красного камня")
- local Buttons = {}
- local function CreateButton(x, y, w, h, bgc, fgc, text, callback)
- local obgc = gpu.getBackground()
- local ofgc = gpu.getForeground()
- gpu.setBackground(bgc)
- gpu.setForeground(fgc)
- gpu.fill(x, y, w, h, " ")
- gpu.set(x+w/2-string.len(text)/2, y+h/2, text)
- table.insert(Buttons, {xmi = x,ymi = y,xma = x+w,yma = y+h,cb = callback})
- gpu.setBackground(obgc)
- gpu.setForeground(ofgc)
- end
- local function CheckButtonPress(x, y)
- for _, button in ipairs(Buttons) do
- if (x>=button.xmi) and (x<=button.xma) then
- if (y>=button.ymi) and (y<=button.yma) then
- button.cb(x, y)
- break
- end
- end
- end
- end
- gpu.setResolution(18,23)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1,1,18,23," ")
- CreateButton(1, 1, 18, 3, 0x646464, 0xbebebe, "Top", function()
- SignalTop = not SignalTop
- red.setOutput(1, SignalTop and 15 or 0)
- end)
- CreateButton(1, 5, 18, 3, 0x646464, 0xbebebe, "Bottom", function()
- SignalBottom = not SignalBottom
- red.setOutput(0, SignalBottom and 15 or 0)
- end)
- CreateButton(1, 9, 18, 3, 0x646464, 0xbebebe, "Left", function()
- SignalLeft = not SignalLeft
- red.setOutput(5, SignalLeft and 15 or 0)
- end)
- CreateButton(1, 13, 18, 3, 0x646464, 0xbebebe, "Right", function()
- SignalRight = not SignalRight
- red.setOutput(4, SignalRight and 15 or 0)
- end)
- CreateButton(1, 17, 18, 3, 0x646464, 0xbebebe, "Back", function()
- SignalBack = not SignalBack
- red.setOutput(2, SignalBack and 15 or 0)
- end)
- CreateButton(1, 21, 18, 3, 0x646464, 0xbebebe, "Front", function()
- SignalFront = not SignalFront
- red.setOutput(3, SignalFront and 15 or 0)
- end)
- while true do
- local _, _, x, y = event.pull("touch")
- CheckButtonPress(x, y)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement