Advertisement
Fredyman_95

Redstone Controler For OC - Multiple Components

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