Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Redstone Controler
- Version: 1.2.0.0
- Created by: Fredyman_95 aka Carters Toaster
- Changes:
- - Support for more Redstone Components;
- - Smoother code
- ]]
- -- Declarations -----------------------------------------------------------------------------------
- local component = require("component")
- local computer = require("computer")
- local event = require("event")
- local term = require("term")
- local sides = require("sides")
- local unicode = require("unicode")
- local gpu = component.gpu
- local screen = component.screen
- local mainloop = true
- local sidesTable = {"0", "1", "2", "3", "4", "5"}
- local infoY = {"4", "4", "4", "25", "25", "25"}
- local infoX ={"50", "51", "52", "50", "51", "52"}
- local informations ={"RS Bottom:", "RS Top:", "RS North:", "RS South:", "RS West:", "RS East:"}
- local ActiveButtons = {}
- local buttons = {}
- local rsIO = {}
- local Events = {}
- local logo = [[
- ____ ____ _ _
- | _ \/ ___| ___ ___ _ __ | |_ _ __ ___ | | ___ _ __
- | |_) \___ \ / __/ _ \| '_ \| __| '__/ _ \| |/ _ \ '__|
- | _ < ___) | | (_| (_) | | | | |_| | | (_) | | __/ |
- |_| \_\____/ \___\___/|_| |_|\__|_| \___/|_|\___|_|]]
- -- End of Declarations ----------------------------------------------------------------------------
- -- Requirements check ------------------------------------------------------------------------------
- if component.isAvailable("redstone") then
- numberOfRestoneIO = 0
- for k, v in component.list() do if v == "redstone" then table.insert(rsIO, component.proxy(k)); numberOfRestoneIO = numberOfRestoneIO + 1 end end
- else
- io.stderr:write("No Redstone Cards Or RedstoneIOs Found!")
- for i = 1, 3 do computer.beep(); os.sleep(.2) end
- os.exit(1)
- end
- if gpu.maxResolution() < 100 then
- io.stderr:write("Tier 3 GPU and Screen Required")
- for i = 1, 2 do computer.beep(); os.sleep(.2) end
- os.exit(1)
- end
- -- End of Requirements check ----------------------------------------------------------------------
- -- Special Functions ------------------------------------------------------------------------------
- local function colorText(plainText, colorCode, posX, posY)
- gpu.setForeground(colorCode)
- term.setCursor(posX, posY)
- print(plainText)
- gpu.setForeground(0xFFFFFF)
- end
- local function setupRedstoneIO()
- for k, c in pairs(rsIO) do primaryRedstone = c; break end
- a = 0
- for i, v in pairs(primaryRedstone.getInput()) do
- 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
- 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
- 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
- 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
- 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
- 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
- a = a + 1
- end
- if numberOfRestoneIO > 1 then
- for k, c in pairs(rsIO) do
- if buttonCondition0 then c.setOutput(tonumber(sidesTable[1]),16) else c.setOutput(tonumber(sidesTable[1]), 0) end
- if buttonCondition1 then c.setOutput(tonumber(sidesTable[2]),16) else c.setOutput(tonumber(sidesTable[2]), 0) end
- if buttonCondition2 then c.setOutput(tonumber(sidesTable[3]),16) else c.setOutput(tonumber(sidesTable[3]), 0) end
- if buttonCondition3 then c.setOutput(tonumber(sidesTable[4]),16) else c.setOutput(tonumber(sidesTable[4]), 0) end
- if buttonCondition4 then c.setOutput(tonumber(sidesTable[5]),16) else c.setOutput(tonumber(sidesTable[5]), 0) end
- if buttonCondition5 then c.setOutput(tonumber(sidesTable[6]),16) else c.setOutput(tonumber(sidesTable[6]), 0) end
- end
- end
- end
- -- End of Special Functions -----------------------------------------------------------------------
- -- Common GUI functions ---------------------------------------------------------------------------
- local function InfoBoard()
- term.setCursor(4, 46)
- for i=4, 54 do
- print("─")
- col = i + 1
- term.setCursor(col, 46)
- end
- colorText("STATUS:", 0x006DFF, 26, 48)
- for i=1, 6 do term.setCursor(tonumber(infoY[i]),tonumber(infoX[i])); print(informations[i]) end
- end
- local function showButtons()
- buttons.Button0:display()
- buttons.Button0:disable(false)
- buttons.Button1:display()
- buttons.Button1:disable(false)
- buttons.Button2:display()
- buttons.Button2:disable(false)
- buttons.Button3:display()
- buttons.Button3:disable(false)
- buttons.Button4:display()
- buttons.Button4:disable(false)
- buttons.Button5:display()
- buttons.Button5:disable(false)
- buttons.Button6:display()
- buttons.Button6:disable(false)
- buttons.Button7:display()
- buttons.Button7:disable(false)
- buttons.ExitButton:display()
- buttons.ExitButton:disable(false)
- end
- local function hideAllButtons()
- for k,v in pairs(buttons) do
- k = nil
- end
- buttons = nil
- end
- -- End of Common GUI functions --------------------------------------------------------------------
- -- Prepare GUI ------------------------------------------------------------------------------------
- gpu.setResolution(59,57)
- term.clear()
- colorText(logo, 0xFF0000, 1, 1)
- setupRedstoneIO()
- InfoBoard()
- screen.setTouchModeInverted(true)
- -- End of Prepare GUI -----------------------------------------------------------------------------
- -- Buttons Objects --------------------------------------------------------------------------------
- local Button = {}
- Button.__index = Button
- function Button.new(xPos, yPos, width, height, label, func, border)
- local self = setmetatable({}, Button)
- if xPos < 1 or xPos > term.window.width then xPos = 1 end
- if yPos < 1 or yPos > term.window.height then yPos = 1 end
- if (width-2) < unicode.len(label) then width = unicode.len(label)+2 end
- if height < 3 then height = 3 end
- if border == nil then
- self.border = true
- else
- self.border = border
- end
- self.xPos = xPos
- self.yPos = yPos
- self.width = width
- self.height = height
- self.label = label
- self.func = func
- self.visible = false
- self.disabled = false
- return self
- end
- function Button.display(self, x, y)
- table.insert(ActiveButtons, 1, self)
- if (self.width-2) < unicode.len(self.label) then self.width = unicode.len(self.label)+2 end
- if x ~= nil and y ~= nil then
- self.xPos = x
- self.yPos = y
- end
- if self.border then
- gpu.fill(self.xPos+1, self.yPos, self.width-2, 1, "─")
- gpu.fill(self.xPos+1, self.yPos+self.height-1, self.width-2, 1, "─")
- gpu.fill(self.xPos, self.yPos+1, 1, self.height-2, "│")
- gpu.fill(self.xPos+self.width-1, self.yPos+1, 1, self.height-2, "│")
- gpu.set(self.xPos, self.yPos, "┌")
- gpu.set(self.xPos+self.width-1, self.yPos, "┐")
- gpu.set(self.xPos, self.yPos+self.height-1, "└")
- gpu.set(self.xPos+self.width-1, self.yPos+self.height-1, "┘")
- end
- gpu.set(self.xPos+1, self.yPos+1, self.label)
- self.visible = true
- end
- function Button.hide(self)
- self.visible = false
- for i,v in ipairs(ActiveButtons) do
- if v == self then table.remove(ActiveButtons, i) end
- end
- if self.border then
- gpu.fill(self.xPos, self.yPos, self.width, self.height, " ")
- else
- gpu.fill(self.xPos+1, self.yPos+1, self.width-2, 1, " ")
- end
- end
- function Button.disable(self, bool)
- if bool == nil then
- self.disabled = false
- else
- self.disabled = bool
- end
- if self.disabled then gpu.setForeground(0x0F0F0F) end
- if self.visible then self:display() end
- gpu.setForeground(0xFFFFFF)
- end
- function Button.touch(self, x, y)
- local wasTouched = false
- if self.visible and not self.disabled then
- if self.border then
- 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
- else
- 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
- end
- end
- if wasTouched then
- gpu.setBackground(0x878787)
- gpu.set(self.xPos+1, self.yPos+1, self.label)
- gpu.setBackground(0x000000)
- if self.visible then gpu.set(self.xPos+1, self.yPos+1, self.label) end
- self.func()
- end
- return wasTouched
- end
- function Button.forceTouch(self)
- self:touch(self.xPos+1, self.yPos+1)
- end
- -- End of Buttons Objects -------------------------------------------------------------------------
- -- Events -----------------------------------------------------------------------------------------
- Events.interrupted = event.listen("interrupted", function()
- mainloop = false
- end)
- Events.touch = event.listen("touch", function(_, screenAddress, x, y, button, playerName)
- local success, msg = xpcall(function()
- if button == 0 then
- for i,v in ipairs(ActiveButtons) do
- if v:touch(x,y) then break end
- end
- end
- end, debug.traceback)
- if not success then
- ErrorMessage = msg
- mainloop = false
- end
- end)
- -- End of Events ---------------------------------------------------------------------------------
- -- User Buttons -----------------------------------------------------------------------------------
- buttons = {
- Button0 = Button.new(4, 10, 51, 5, "Button - RS Bottom", function()
- computer.beep(300)
- if not buttonCondition0 then
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[1]),16) end
- buttonCondition0 = not buttonCondition0
- colorText("ON ", 0x00FF00, 15, 50)
- os.sleep(1)
- else
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[1]),0) end
- buttonCondition0 = not buttonCondition0
- colorText("OFF", 0xFF0000, 15, 50)
- os.sleep(1)
- end
- end),
- Button1 = Button.new(4, 16, 51, 5, "Button - RS Top", function()
- computer.beep(600)
- if not buttonCondition1 then
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[2]),16) end
- buttonCondition1 = not buttonCondition1
- colorText("ON ", 0x00FF00, 15, 51)
- os.sleep(1)
- else
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[2]),0) end
- buttonCondition1 = not buttonCondition1
- colorText("OFF", 0xFF0000, 15, 51)
- os.sleep(1)
- end
- end),
- Button2 = Button.new(4, 22, 51, 5, "Button - RS North", function()
- computer.beep(900)
- if not buttonCondition2 then
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[3]),16) end
- buttonCondition2 = not buttonCondition2
- colorText("ON ", 0x00FF00, 15, 52)
- os.sleep(1)
- else
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[3]),0) end
- buttonCondition2 = not buttonCondition2
- colorText("OFF", 0xFF0000, 15, 52)
- os.sleep(1)
- end
- end),
- Button3 = Button.new(4, 28, 51, 5, "Button - RS South", function()
- computer.beep(1200)
- if not buttonCondition3 then
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[4]),16) end
- buttonCondition3 = not buttonCondition3
- colorText("ON ", 0x00FF00, 35 ,50)
- os.sleep(1)
- else
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[4]),0) end
- buttonCondition3 = not buttonCondition3
- colorText("OFF", 0xFF0000, 35, 50)
- os.sleep(1)
- end
- end),
- Button4 = Button.new(4, 34, 51, 5, "Button - RS West", function()
- computer.beep(1500)
- if not buttonCondition4 then
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[5]),16) end
- buttonCondition4 = not buttonCondition4
- colorText("ON ", 0x00FF00, 35, 51)
- os.sleep(1)
- else
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[5]),0) end
- buttonCondition4 = not buttonCondition4
- colorText("OFF", 0xFF0000, 35, 51)
- os.sleep(1)
- end
- end),
- Button5 = Button.new(4, 40, 51, 5, "Button - RS East", function()
- computer.beep(1800)
- if not buttonCondition5 then
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[6]),16) end
- buttonCondition5 = not buttonCondition5
- colorText("ON ", 0x00FF00, 35, 52)
- os.sleep(1)
- else
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[6]),0) end
- buttonCondition5 = not buttonCondition5
- colorText("OFF", 0xFF0000, 35, 52)
- os.sleep(1)
- end
- end),
- Button6 = Button.new(4, 54, 23, 1, " Turn Off ALL", function()
- computer.beep()
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[1]),0) end; buttonCondition0 = false; colorText("OFF", 0xFF0000, 15, 50)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[2]),0) end; buttonCondition1 = false; colorText("OFF", 0xFF0000, 15, 51)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[3]),0) end; buttonCondition2 = false; colorText("OFF", 0xFF0000, 15, 52)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[4]),0) end; buttonCondition3 = false; colorText("OFF", 0xFF0000, 35, 50)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[5]),0) end; buttonCondition4 = false; colorText("OFF", 0xFF0000, 35, 51)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[6]),0) end; buttonCondition5 = false; colorText("OFF", 0xFF0000, 35, 52)
- os.sleep(.25)
- end),
- Button7 = Button.new(32, 54, 23, 1, " Turn On ALL", function()
- computer.beep()
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[1]),16) end; buttonCondition0 = true; colorText("ON ", 0x00FF00, 15, 50)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[2]),16) end; buttonCondition1 = true; colorText("ON ", 0x00FF00, 15, 51)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[3]),16) end; buttonCondition2 = true; colorText("ON ", 0x00FF00, 15, 52)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[4]),16) end; buttonCondition3 = true; colorText("ON ", 0x00FF00, 35 ,50)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[5]),16) end; buttonCondition4 = true; colorText("ON ", 0x00FF00, 35, 51)
- for k, c in pairs(rsIO) do c.setOutput(tonumber(sidesTable[6]),16) end; buttonCondition5 = true; colorText("ON ", 0x00FF00, 35, 52)
- os.sleep(.25)
- end),
- ExitButton = Button.new(45, 50, 10, 1, " Exit", function()
- computer.beep(2000)
- mainloop = false
- end),
- }
- -- End of User Buttons ----------------------------------------------------------------------------
- -- Main Loop --------------------------------------------------------------------------------------
- showButtons()
- while mainloop do
- os.sleep(0.1)
- end
- -- End of Main Loop -------------------------------------------------------------------------------
- -- Closing procedure ------------------------------------------------------------------------------
- screen.setTouchModeInverted(false)
- hideAllButtons()
- term.clear()
- gpu.setResolution(160,50)
- print("All buttons disabled!")
- for k,v in pairs(Events) do
- print("Canceling Event Listener: "..k) -- For debug
- event.cancel(v)
- end
- -- End of Closing procedure -----------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement