Skip to content

Instantly share code, notes, and snippets.

@Keridos
Created April 28, 2015 23:03
Show Gist options
  • Save Keridos/c85a90312fccb2e30d18 to your computer and use it in GitHub Desktop.
Save Keridos/c85a90312fccb2e30d18 to your computer and use it in GitHub Desktop.
local API = {}
local button={}
local component = require("component")
local colors = require("colors")
local term = require("term")
local mon = component.gpu
local w, h = mon.getResolution()
local Green = 0x00AA00
local Red = 0xAA0000
local Black = 0x000000
buttonStatus = nil
function API.clear()
mon.setBackground(Black)
mon.fill(1, 1, w, h, " ")
end
function API.clearTable()
button = {}
API.clear()
end
function API.setTable(name, func, arg, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["arg"] = arg
button[name]["active"] = false
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function API.fill(text, color, bData)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
local oldColor = mon.setBackground(color)
mon.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
mon.set(xspot, yspot, text)
mon.setBackground(oldColor)
end
function API.screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = Green else currColor = Red end
API.fill(name, currColor, data)
end
end
function API.toggleButton(name)
button[name]["active"] = not button[name]["active"]
buttonStatus = button[name]["active"]
API.screen()
end
function API.getButtonStatus(name)
if button[name]["active"] == nil then return false end
return button[name]["active"]
end
function API.flash(name,length)
API.toggleButton(name)
API.screen()
os.sleep(length)
API.toggleButton(name)
API.screen()
end
function API.checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"](data["arg"])
return true
end
end
end
return false
end
function API.heading(text)
w, h = mon.getResolution()
term.setCursor((w-string.len(text))/2+1, 1)
term.write(text)
end
function API.label(w, h, text)
term.setCursor(w, h)
term.write(text)
end
return API
local API = require("buttonAPI")
local event = require("event")
local computer = require("computer")
local term = require("term")
local component = require("component")
local gpu = component.gpu
local rs = component.redstone
local colors = require("colors")
local side = require("sides")
local mobtable = {}
function fillTable()
--API.setTable("Creeper", toggleSpawn, "Creeper", 3,5,3,10)
--API.setTable("Enderman", toggleSpawn, "Enderman", 7,5,3,10)
--API.setTable("Wither Skeleton", toggleSpawn, "Wither Skeleton", 10,20,8,10)
--API.setTable("Blaze", toggleSpawn, "Blaze", 22,32,8,10)
--API.setTable("Spider", toggleSpawn, "Spider", 12,14,19,29)
API.screen()
end
function setupTable()
mobtable["Creeper"] = 0
mobtable["Enderman"] = 1
mobtable["Wither Skeleton"] = 2
mobtable["Blaze"] = 3
mobtable["Spider"] = 4
end
function enableSpawn(mob)
rs.setBundledOutput(0,mobtable(mob),15)
end
function disableSpawn(mob)
rs.setBundledOutput(0,mobtable(mob),0)
end
function toggleSpawn(mob)
API.toggleButton(mob)
if API.getButtonStatus(mob) then
enableSpawn(mob)
else
disableSpawn(mob)
end
API.screen()
end
function getClick()
local _, _, x, y = event.pull(1,touch)
if x == nil or y == nil then
local h, w = gpu.getResolution()
gpu.set(h, w, ".")
gpu.set(h, w, " ")
else
API.checkxy(x,y)
end
end
function init()
term.setCursorBlink(false)
gpu.setResolution(80, 25)
API.clear()
fillTable()
--API.heading("Button API Demo! Created in CC by DW20, ported to OC by MoparDan!")
--API.label(1,24,"A sample Label.")
end
function main()
init()
setupTable()
while true do
getClick()
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment