serafim7

redstone control [OpenComputers]

Jul 4th, 2020
1,345
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Сенсорные кнопки для управления контроллером красного камня
  2. -- https://computercraft.ru/topic/3271-programma-dve-knopki/?do=findComment&comment=42097
  3. -- https://i.imgur.com/hzCskre.png
  4.  
  5. local event = require("event")
  6. local component = require("component")
  7. local gpu = component.gpu
  8. local red = component.isAvailable("redstone") and component.redstone or error("нет контроллера красного камня")
  9. local Buttons = {}
  10.  
  11. local function CreateButton(x, y, w, h, bgc, fgc, text, callback)
  12.   local obgc = gpu.getBackground()
  13.   local ofgc = gpu.getForeground()
  14.   gpu.setBackground(bgc)
  15.   gpu.setForeground(fgc)
  16.   gpu.fill(x, y, w, h, " ")
  17.   gpu.set(x+w/2-string.len(text)/2, y+h/2, text)
  18.   table.insert(Buttons, {xmi = x,ymi = y,xma = x+w,yma = y+h,cb = callback})
  19.   gpu.setBackground(obgc)
  20.   gpu.setForeground(ofgc)
  21. end
  22.  
  23. local function CheckButtonPress(x, y)
  24.   for _, button in ipairs(Buttons) do
  25.     if (x>=button.xmi) and (x<=button.xma) then
  26.       if (y>=button.ymi) and (y<=button.yma) then
  27.         button.cb(x, y)
  28.         break
  29.      end
  30.    end
  31.  end
  32. end
  33.  
  34. gpu.setResolution(18,23)
  35. gpu.setBackground(0x000000)
  36. gpu.setForeground(0xFFFFFF)
  37. gpu.fill(1,1,18,23," ")
  38.  
  39. CreateButton(1, 1, 18, 3, 0x646464, 0xbebebe, "Top", function()
  40.   SignalTop = not SignalTop
  41.   red.setOutput(1, SignalTop and 15 or 0)
  42. end)
  43. CreateButton(1, 5, 18, 3, 0x646464, 0xbebebe, "Bottom", function()
  44.   SignalBottom = not SignalBottom
  45.   red.setOutput(0, SignalBottom and 15 or 0)
  46. end)
  47. CreateButton(1, 9, 18, 3, 0x646464, 0xbebebe, "Left", function()
  48.   SignalLeft = not SignalLeft
  49.   red.setOutput(5, SignalLeft and 15 or 0)
  50. end)
  51. CreateButton(1, 13, 18, 3, 0x646464, 0xbebebe, "Right", function()
  52.   SignalRight = not SignalRight
  53.   red.setOutput(4, SignalRight and 15 or 0)
  54. end)
  55. CreateButton(1, 17, 18, 3, 0x646464, 0xbebebe, "Back", function()
  56.   SignalBack = not SignalBack
  57.   red.setOutput(2, SignalBack and 15 or 0)
  58. end)
  59. CreateButton(1, 21, 18, 3, 0x646464, 0xbebebe, "Front", function()
  60.   SignalFront = not SignalFront
  61.   red.setOutput(3, SignalFront and 15 or 0)
  62. end)
  63.  
  64. while true do
  65.   local _, _, x, y = event.pull("touch")
  66.   CheckButtonPress(x, y)
  67. end
RAW Paste Data