Advertisement
Fredyman_95

Redstone Controler For OC

Nov 8th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.19 KB | Gaming | 0 0
  1. --[[Created by:
  2. Fredyman_95 aka Carters Toaster]]
  3.  
  4. -- Declarations
  5.  
  6.     local component = require("component")
  7.     local computer = require("computer")
  8.     local event = require("event")
  9.     local term = require("term")
  10.     local sides = require("sides")
  11.     local unicode = require("unicode")
  12.     local gpu = component.gpu
  13.     local screen = component.screen
  14.     local mainloop = true
  15.     local ActiveButtons = {}
  16.     local buttons = {}
  17.     local logo = [[
  18.   ____  ____                    _             _          
  19.  |  _ \/ ___|    ___ ___  _ __ | |_ _ __ ___ | | ___ _ __
  20.  | |_) \___ \   / __/ _ \| '_ \| __| '__/ _ \| |/ _ \ '__|
  21. |  _ < ___) | | (_| (_) | | | | |_| | | (_) | |  __/ |  
  22. |_| \_\____/   \___\___/|_| |_|\__|_|  \___/|_|\___|_|]]
  23.  
  24. -- Requirements check -------------------
  25.    if gpu.maxResolution() < 100 then
  26.      io.stderr:write("Tier 3 GPU and Screen Required")
  27.      computer.beep()
  28.      os.sleep(0.2)
  29.      computer.beep()
  30.      os.exit(1)
  31.    end
  32.  
  33.    if not component.isAvailable("redstone") then
  34.      io.stderr:write("No RedstoneIO Connected.")
  35.      computer.beep()
  36.      os.sleep(0.2)
  37.      computer.beep()
  38.      os.sleep(0.2)
  39.      computer.beep()
  40.      os.exit(1)
  41.    else
  42.      redstone = component.redstone
  43.    end
  44.  
  45. -- End of Requirements check ------------
  46.  
  47. -- Prepare GUI ---
  48.  local function colorText(plainText, colorCode, posX, posY)
  49.    gpu.setForeground(colorCode)
  50.    term.setCursor(posX, posY)
  51.    print(plainText)
  52.    gpu.setForeground(0xFFFFFF)
  53.  end
  54.  
  55.  local function setupRedstoneIO()
  56.    a = 0
  57.    for i, v in pairs(redstone.getInput()) do
  58.      if a == 0 then if v > 0 then buttonCondition0 = true; colorText("ON ", 0x00FF00, 15, 50) else buttonCondition0 = false; colorText("OFF", 0xFF0000, 15, 50) end end
  59.      if a == 1 then if v > 0 then buttonCondition1 = true; colorText("ON ", 0x00FF00, 15, 51) else buttonCondition1 = false; colorText("OFF", 0xFF0000, 15, 51) end end
  60.      if a == 2 then if v > 0 then buttonCondition2 = true; colorText("ON ", 0x00FF00, 15, 52) else buttonCondition2 = false; colorText("OFF", 0xFF0000, 15, 52) end end
  61.      if a == 3 then if v > 0 then buttonCondition3 = true; colorText("ON ", 0x00FF00, 35 ,50) else buttonCondition3 = false; colorText("OFF", 0xFF0000, 35, 50) end end
  62.      if a == 4 then if v > 0 then buttonCondition4 = true; colorText("ON ", 0x00FF00, 35, 51) else buttonCondition4 = false; colorText("OFF", 0xFF0000, 35, 51) end end
  63.      if a == 5 then if v > 0 then buttonCondition5 = true; colorText("ON ", 0x00FF00, 35, 52) else buttonCondition5 = false; colorText("OFF", 0xFF0000, 35, 52) end end
  64.      a = a + 1
  65.    end
  66.  
  67.  end
  68.  
  69.  local function InfoBoard()
  70.      term.setCursor(4, 46)
  71.      for i=4, 54 do
  72.        print("─")
  73.        posx = i + 1
  74.        term.setCursor(posx, 46)
  75.      end
  76.      colorText("STATUS:", 0x006DFF, 26, 48)
  77.      term.setCursor(4, 50)
  78.      print("RS Bottom:")
  79.      term.setCursor(4, 51)
  80.      print("RS Top:")
  81.      term.setCursor(4, 52)
  82.      print("RS North:")
  83.      term.setCursor(25,50)
  84.      print("RS South:")
  85.      term.setCursor(25,51)
  86.      print("RS West:")
  87.      term.setCursor(25,52)
  88.      print("RS East:")
  89.  end
  90.  gpu.setResolution(59,57)
  91.  term.clear()
  92.  colorText(logo, 0xFF0000, 1, 1)
  93.  setupRedstoneIO()
  94.  InfoBoard()
  95.  
  96. -- End of preprare GUI ---
  97.  
  98. -- Buttons Objects ---------------
  99.  
  100.    local Button = {}
  101.    Button.__index = Button
  102.    function Button.new(xPos, yPos, width, height, label, func, border)
  103.      local self = setmetatable({}, Button)
  104.      if xPos < 1 or xPos > term.window.width then xPos = 1 end
  105.      if yPos < 1 or yPos > term.window.height then yPos = 1 end
  106.      if (width-2) < unicode.len(label) then width = unicode.len(label)+2 end
  107.      if height < 3 then height = 3 end
  108.      if border == nil then
  109.        self.border = true
  110.      else
  111.        self.border = border
  112.      end
  113.      self.xPos = xPos
  114.      self.yPos = yPos
  115.      self.width = width
  116.      self.height = height
  117.      self.label = label
  118.      self.func = func
  119.      self.visible = false
  120.      self.disabled = false
  121.      return self
  122.    end
  123.  
  124.    function Button.display(self, x, y)
  125.      table.insert(ActiveButtons, 1, self)
  126.      if (self.width-2) < unicode.len(self.label) then self.width = unicode.len(self.label)+2 end
  127.      if x ~= nil and y ~= nil then
  128.        self.xPos = x
  129.        self.yPos = y
  130.      end
  131.          if self.border then
  132.        gpu.fill(self.xPos+1, self.yPos, self.width-2, 1, "─")
  133.        gpu.fill(self.xPos+1, self.yPos+self.height-1, self.width-2, 1, "─")
  134.        gpu.fill(self.xPos, self.yPos+1, 1, self.height-2, "│")
  135.        gpu.fill(self.xPos+self.width-1, self.yPos+1, 1, self.height-2, "│")
  136.        gpu.set(self.xPos, self.yPos, "┌")
  137.        gpu.set(self.xPos+self.width-1, self.yPos, "┐")
  138.        gpu.set(self.xPos, self.yPos+self.height-1, "└")
  139.        gpu.set(self.xPos+self.width-1, self.yPos+self.height-1, "┘")
  140.      end
  141.      gpu.set(self.xPos+1, self.yPos+1, self.label)
  142.      self.visible = true
  143.    end
  144.  
  145.    function Button.hide(self)
  146.      self.visible = false
  147.      for i,v in ipairs(ActiveButtons) do
  148.        if v == self then table.remove(ActiveButtons, i) end
  149.      end
  150.      if self.border then
  151.        gpu.fill(self.xPos, self.yPos, self.width, self.height, " ")
  152.      else
  153.        gpu.fill(self.xPos+1, self.yPos+1, self.width-2, 1, " ")
  154.      end
  155.    end
  156.  
  157.    function Button.disable(self, bool)
  158.      if bool == nil then
  159.        self.disabled = false
  160.      else
  161.        self.disabled = bool
  162.      end
  163.      if self.disabled then gpu.setForeground(0x0F0F0F) end
  164.      if self.visible then self:display() end
  165.      gpu.setForeground(0xFFFFFF)
  166.    end
  167.  
  168.    function Button.touch(self, x, y)
  169.      local wasTouched = false
  170.      if self.visible and not self.disabled then  
  171.        if self.border then
  172.          if x >= self.xPos and x <= (self.xPos+self.width-1) and y >= self.yPos and y <= (self.yPos+self.height-1) then wasTouched = true end
  173.        else
  174.          if x >= self.xPos+1 and x <= (self.xPos+self.width-2) and y >= self.yPos+1 and y <= (self.yPos+self.height-2) then wasTouched = true end
  175.        end
  176.      end
  177.      if wasTouched then
  178.        gpu.setBackground(0x878787)
  179.        gpu.set(self.xPos+1, self.yPos+1, self.label)
  180.        gpu.setBackground(0x000000)
  181.        if self.visible then gpu.set(self.xPos+1, self.yPos+1, self.label) end
  182.        self.func()
  183.      end
  184.      return wasTouched
  185.    end
  186.  
  187.    function Button.forceTouch(self)
  188.      self:touch(self.xPos+1, self.yPos+1)
  189.    end
  190. -- end of Buttons Objects --------
  191.    
  192. -- Events -------------------------------------------------
  193.  
  194. local Events = {}
  195. Events.interrupted = event.listen("interrupted", function()
  196.    mainloop = false
  197. end)
  198.  
  199. Events.touch = event.listen("touch", function(_, screenAddress, x, y, button, playerName)
  200.  local success, msg = xpcall(function()
  201.    if button == 0 then
  202.      for i,v in ipairs(ActiveButtons) do
  203.        if v:touch(x,y) then break end
  204.      end
  205.    end
  206.  end, debug.traceback)
  207.  if not success then
  208.    ErrorMessage = msg
  209.    mainloop = false
  210.  end
  211. end)
  212. -- end of Events ------------------------------------------
  213.  
  214. -- buttons -----------------------------------------
  215. buttons = {
  216.    Button0 = Button.new(4, 10, 51, 5, "Button - RS Bottom", function()
  217.            computer.beep(300)
  218.            if not buttonCondition0 then
  219.                redstone.setOutput(sides.bottom, 16)
  220.                buttonCondition0 = not buttonCondition0
  221.                colorText("ON ", 0x00FF00, 15, 50)
  222.                os.sleep(1)
  223.            else
  224.                redstone.setOutput(sides.bottom, 0)
  225.                buttonCondition0 = not buttonCondition0
  226.                colorText("OFF", 0xFF0000, 15, 50)
  227.                os.sleep(1)
  228.            end
  229.    end),
  230.  
  231.    Button1 = Button.new(4, 16, 51, 5, "Button - RS Top", function()
  232.            computer.beep(600)
  233.            if not buttonCondition1 then
  234.                redstone.setOutput(sides.top, 16)
  235.                buttonCondition1 = not buttonCondition1
  236.                colorText("ON ", 0x00FF00, 15, 51)
  237.                os.sleep(1)
  238.            else
  239.                redstone.setOutput(sides.top, 0)
  240.                buttonCondition1 = not buttonCondition1
  241.                colorText("OFF", 0xFF0000, 15, 51)
  242.                os.sleep(1)
  243.            end
  244.    end),
  245.  
  246.    Button2 = Button.new(4, 22, 51, 5, "Button - RS North", function()
  247.            computer.beep(900)
  248.            if not buttonCondition2 then
  249.                redstone.setOutput(sides.north, 16)
  250.                buttonCondition2 = not buttonCondition2
  251.                colorText("ON ", 0x00FF00, 15, 52)
  252.                os.sleep(1)
  253.            else
  254.                redstone.setOutput(sides.north, 0)
  255.                buttonCondition2 = not buttonCondition2
  256.                colorText("OFF", 0xFF0000, 15, 52)
  257.                os.sleep(1)
  258.            end
  259.    end),
  260.  
  261.    Button3 = Button.new(4, 28, 51, 5, "Button - RS South", function()
  262.            computer.beep(1200)
  263.            if not buttonCondition3 then
  264.                redstone.setOutput(sides.south, 16)
  265.                buttonCondition3 = not buttonCondition3
  266.                colorText("ON ", 0x00FF00, 35 ,50)
  267.                os.sleep(1)
  268.            else
  269.                redstone.setOutput(sides.south, 0)
  270.                buttonCondition3 = not buttonCondition3
  271.                colorText("OFF", 0xFF0000, 35, 50)
  272.                os.sleep(1)
  273.            end
  274.    end),
  275.  
  276.    Button4 = Button.new(4, 34, 51, 5, "Button - RS West", function()
  277.      computer.beep(1500)
  278.            if not buttonCondition4 then
  279.                redstone.setOutput(sides.west, 16)
  280.                buttonCondition4 = not buttonCondition4
  281.                colorText("ON ", 0x00FF00, 35, 51)
  282.                os.sleep(1)
  283.            else
  284.                redstone.setOutput(sides.west, 0)
  285.                buttonCondition4 = not buttonCondition4
  286.                colorText("OFF", 0xFF0000, 35, 51)
  287.                os.sleep(1)
  288.            end
  289.    end),
  290.  
  291.    Button5 = Button.new(4, 40, 51, 5, "Button - RS East", function()
  292.            computer.beep(1800)
  293.            if not buttonCondition5 then
  294.                redstone.setOutput(sides.east, 16)
  295.                buttonCondition5 = not buttonCondition5
  296.                colorText("ON ", 0x00FF00, 35, 52)
  297.                os.sleep(1)
  298.            else
  299.                redstone.setOutput(sides.east, 0)
  300.                buttonCondition5 = not buttonCondition5
  301.                colorText("OFF", 0xFF0000, 35, 52)
  302.                os.sleep(1)
  303.            end
  304.    end),
  305.  
  306.    Button6 = Button.new(4, 54, 23, 1, "     Turn Off ALL", function()
  307.            computer.beep()
  308.            redstone.setOutput(sides.bottom, 0); buttonCondition0 = false; colorText("OFF", 0xFF0000, 15, 50)
  309.            redstone.setOutput(sides.top, 0); buttonCondition1 = false; colorText("OFF", 0xFF0000, 15, 51)
  310.            redstone.setOutput(sides.north, 0); buttonCondition2 = false; colorText("OFF", 0xFF0000, 15, 52)
  311.            redstone.setOutput(sides.south, 0); buttonCondition3 = false; colorText("OFF", 0xFF0000, 35, 50)
  312.            redstone.setOutput(sides.west, 0); buttonCondition4 = false; colorText("OFF", 0xFF0000, 35, 51)
  313.            redstone.setOutput(sides.east, 0); buttonCondition5 = false; colorText("OFF", 0xFF0000, 35, 52)
  314.            os.sleep(1)
  315.    end),
  316.  
  317.    Button7 = Button.new(32, 54, 23, 1, "      Turn On ALL", function()
  318.            computer.beep()
  319.            redstone.setOutput(sides.bottom, 16); buttonCondition0 = true; colorText("ON ", 0x00FF00, 15, 50)
  320.            redstone.setOutput(sides.top, 16); buttonCondition1 = true; colorText("ON ", 0x00FF00, 15, 51)
  321.            redstone.setOutput(sides.north, 16); buttonCondition2 = true; colorText("ON ", 0x00FF00, 15, 52)
  322.            redstone.setOutput(sides.south, 16); buttonCondition3 = true; colorText("ON ", 0x00FF00, 35 ,50)
  323.            redstone.setOutput(sides.west, 16); buttonCondition4 = true; colorText("ON ", 0x00FF00, 35, 51)
  324.            redstone.setOutput(sides.east, 16); buttonCondition5 = true; colorText("ON ", 0x00FF00, 35, 52)
  325.            os.sleep(1)
  326.    end),
  327.  
  328.    ExitButton = Button.new(45, 50, 10, 1, "  Exit", function()
  329.            computer.beep(2000)
  330.            mainloop = false
  331. end),
  332. }      
  333. -- End of buttons -----------------------------------------
  334.  
  335. -- Functions ----------------------------------------------
  336.  local function showButtons()
  337.      buttons.Button0:display()
  338.      buttons.Button0:disable(false)
  339.      buttons.Button1:display()
  340.      buttons.Button1:disable(false)
  341.      buttons.Button2:display()
  342.      buttons.Button2:disable(false)
  343.      buttons.Button3:display()
  344.      buttons.Button3:disable(false)
  345.      buttons.Button4:display()
  346.      buttons.Button4:disable(false)
  347.      buttons.Button5:display()
  348.      buttons.Button5:disable(false)
  349.      buttons.Button6:display()
  350.      buttons.Button6:disable(false)
  351.      buttons.Button7:display()
  352.      buttons.Button7:disable(false)
  353.      buttons.ExitButton:display()
  354.      buttons.ExitButton:disable(false)
  355.  end
  356.  
  357.  local function hideAllButtons()
  358.      buttons.Button0:hide()
  359.      buttons.Button0:disable(true)
  360.      buttons.Button1:hide()
  361.      buttons.Button1:disable(true)
  362.      buttons.Button2:hide()
  363.      buttons.Button2:disable(true)
  364.      buttons.Button3:hide()
  365.      buttons.Button3:disable(true)
  366.      buttons.Button4:hide()
  367.      buttons.Button4:disable(true)
  368.      buttons.Button5:hide()
  369.      buttons.Button5:disable(true)
  370.      buttons.Button6:hide()
  371.      buttons.Button6:disable(true)
  372.      buttons.Button7:hide()
  373.      buttons.Button7:disable(true)
  374.      buttons.ExitButton:hide()
  375.      buttons.ExitButton:disable(true)
  376.  end
  377.  
  378. -- end of Functions ---------------------------------------
  379.  
  380. showButtons()
  381. screen.setTouchModeInverted(true)
  382. while mainloop do
  383.    os.sleep(0.1)
  384. end
  385. screen.setTouchModeInverted(false)
  386. hideAllButtons()
  387. term.clear()
  388. gpu.setResolution(160,50)
  389. print("All buttons disabled!")
  390. for k,v in pairs(Events) do
  391.    print("Canceling Event Listener: "..k) -- For debug
  392.    event.cancel(v)
  393. end
  394. -- End of program -----------------------------------------
Tags: minecraft
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement