This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
andy7555's profile picture

direwolf20's mobs button api this is coppy of vertion in s6 ep103

Started by andy7555, 24 August 2014 - 09:28 AM
andy7555 #1
Posted 24 August 2014 - 11:28 AM
–Originally coded by Direwolf20, this version has been modified
local mon = peripheral.wrap("back")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)

function standby()
fill("Schutz", colors.yellow, button["Schutz"])
sleep(4)
screen()
end
function load()
local file = fs.open("debitorschutz", "r")
if not file then return; end
loaddata = textutils.unserialize(file.readAll())
for name, active in pairs(loaddata) do
if not button[name]["active"] == active then
button[name]["func"]()
end

end
file.close()
screen()
end
function clearTable()
button = {}
end

function setName(name1, newname)
button[name1]["screenname"] = newname
end
function setTable(name, func, param, xmin, xmax, ymin, ymax, label)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = false
button[name]["param"] = param
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
button[name]["screenname"] = label
end

function active(name1)
for name, data in pairs(button) do
if name == name1 then
return data["active"]
end
end
end
function funcName()
print("You clicked buttonText")
end

function fillTable()
setTable("ButtonText", funcName, 5, 25, 4, 8)
end

local mx, my = mon.getSize()
function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local screenname = bData["screenname"]
– print(screenname)
if screenname == nil then
xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
else
xspot = math.floor((bData["xmax"] - bData["xmin"] - #screenname) /2) +4 -3
end
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(screenname) +1 do
if k == xspot then
mon.write(screenname)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end

function screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then
currColor = colors.lime
elseif on == false then
currColor = colors.red
else
curColor = colors.yellow
end
fill(name, currColor, data)
end
end

function save()
local file = fs.open("debitorschutz", "w")
local savedata = { }
for name,data in pairs(button) do
local on = data["active"]
if on == true then
active = "true"
else
active = "false"
end
savedata[name] = on
end
file.write(textutils.serialize(savedata))
file.close()

end

function toggleButton(name)
button[name]["active"] = not button[name]["active"]
screen()
end

function flash(name)
toggleButton(name)
screen()
sleep(0.15)
toggleButton(name)
screen()
end

function 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
if data["param"] == "" then
data["func"]()
else
data["func"](data["param"])
end
save()
return true
–data["active"] = not data["active"]
–print(name)
end
end
end
return false
end

function heading(text)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1, 1)
mon.write(text)
end

function label(w, h, text)
mon.setCursorPos(w, h)
mon.write(text)
end
andy7555 #2
Posted 24 August 2014 - 12:06 PM
local mon = peripheral.wrap("back")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)

function standby()
fill("Schutz", colors.yellow, button["Schutz"])
sleep(4)
screen()
end
function load()
local file = fs.open("debitorschutz", "r")
if not file then return; end
loaddata = textutils.unserialize(file.readAll())
for name, active in pairs(loaddata) do
if not button[name]["active"] == active then
button[name]["func"]()
end

end
file.close()
screen()
end
function clearTable()
button = {}
end

function setName(name1, newname)
button[name1]["screenname"] = newname
end
function setTable(name, func, param, xmin, xmax, ymin, ymax, label)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = false
button[name]["param"] = param
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
button[name]["screenname"] = label
end

function active(name1)
for name, data in pairs(button) do
if name == name1 then
return data["active"]
end
end
end
function funcName()
print("You clicked buttonText")
end

function fillTable()
setTable("ButtonText", funcName, 5, 25, 4, 8)
end

local mx, my = mon.getSize()
function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local screenname = bData["screenname"]
– print(screenname)
if screenname == nil then
xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
else
xspot = math.floor((bData["xmax"] - bData["xmin"] - #screenname) /2) +4 -3
end
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(screenname) +1 do
if k == xspot then
mon.write(screenname)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end

function screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then
currColor = colors.lime
elseif on == false then
currColor = colors.red
else
curColor = colors.yellow
end
fill(name, currColor, data)
end
end

function save()
local file = fs.open("debitorschutz", "w")
local savedata = { }
for name,data in pairs(button) do
local on = data["active"]
if on == true then
active = "true"
else
active = "false"
end
savedata[name] = on
end
file.write(textutils.serialize(savedata))
file.close()

end

function toggleButton(name)
button[name]["active"] = not button[name]["active"]
screen()
end

function flash(name)
toggleButton(name)
screen()
sleep(0.15)
toggleButton(name)
screen()
end
function 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
if data["param"] == "" then
data["func"]()
else
data["func"](data["param"])
end
save()
return true
–data["active"] = not data["active"]
–print(name)
end
end
end
return false
end

function heading(text)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1, 1)
mon.write(text)
end

function label(w, h, text)
mon.setCursorPos(w, h)
mon.write(text)
end
andy7555 #3
Posted 24 August 2014 - 12:07 PM
os.loadAPI("button")
local m = peripheral.wrap("right")
local rsOut = {}
local c = peripheral.wrap("container_chest_0")
local a = peripheral.wrap("tt_magnet_0")
local s = peripheral.wrap("auto_spawner_0")
m.clear()
local mobArray = {}
local buttonColors = {}
local attractorStatus = ""
local page = 1
local pages = 0
local currMob = ""

function fillMainTable()
getCurrMob()
checkExact()
getAttractorStatus()
m.clear()
button.clearTable()
button.setTable("Lights", lights, "", 4, 17, 2, 4)
button.setTable("Door", door, "", 20, 33, 2, 4)
button.setTable("Grinders", grinders, "", 4, 17, 6, 8)
button.setTable("DrawBridge", drawBridge, "", 20, 33, 6, 8)
button.setTable("Attractor", attractorToggle, "", 4, 17, 10, 12)
button.setTable(attractorStatus, togglePush, "", 20, 33, 10, 12)
button.setTable("Mob Selector", mobSelect, "", 20, 33, 14, 16)
button.setTable("Spawner", spawner, "", 4, 17, 14, 16)
–button.setTable("Refresh", refresh, "", 15, 35, 19, 19)
print("Filled")
button.screen()
for i,j in pairs(buttonColors) do
if not rsOut[j] then button.toggleButton(i) end
end
button.toggleButton("Attractor")
button.label(1, 19, "Current Mob: "..currMob)
button.label(30, 19, "Exact: "..currExact)
end

function getAttractorStatus()
if a.isPulling() then
attractorStatus = "Pulling"
else
attractorStatus = "Pushing"
end
end

function togglePush()
button.flash(attractorStatus)
a.setPulling(not a.isPulling())
fillMainTable()
end

function fillTable()
checkMobs()
checkExact()
getCurrMob()
m.clear()
button.clearTable()
local totalrows = 0
local npp = 12 –names per page
local numNames = 0
local col = 2
local row = 12
local countRow = 1
local currName = 0
for i,j in pairs(mobArray) do
totalrows = totalrows+1
end
pages = math.ceil(totalrows/npp)
print(totalrows)
for name,slot in pairs(mobArray) do
currName = currName + 1
if currName > npp*(page-1) and currName < npp*page+1 then
row = 4+(countRow)
button.setTable(string.sub(name, 0, 17), insertMob, slot, col, col+17 , row, row)
if col == 21 then
col = 2
countRow = countRow + 2
else
col = col+19
end
end
end
button.setTable("Next Page", nextPage, "", 27, 38, 1, 1)
button.setTable("Prev Page", prevPage, "", 2, 13, 1, 1)
button.setTable("Main Menu", goMainMenu, "", 15, 25, 1, 1)
button.setTable("Exact: "..currExact, switchExact, "", 21, 38, 18, 19)
button.setTable("Remove Mob", removeMob, "", 2, 19, 18, 19)
button.screen()
button.label(15,3, "Page: "..tostring(page).." of "..tostring(pages))
button.label(12, 16, "Current Mob: "..currMob)
end

function getCurrMob()
data = s.getStackInSlot(1)
currMob = data.captured
end

function goMainMenu()
fillMainTable()
– refresh()
end

function checkExact()
if s.getSpawnExact() then
currExact = "Yes"
else
currExact = "No"
end
end

function switchExact()
s.setSpawnExact(not s.getSpawnExact())
fillTable()
end

function nextPage()
if page+1 <= pages then
page = page+1
end
fillTable()
sleep(0.25)
end

function prevPage()
if page-1 >= 1 then page = page-1 end
fillTable()
sleep(0.25)
end

function getMobs()
mobArray = {}
for i = 1,27 do
if c.getStackInSlot(i) then
data = c.getStackInSlot(i)
–print(i..":"..data.captured)
mobArray[data.captured] = i
end
end
end

function findEmptySlot()
for i = 1,27 do
if not c.getStackInSlot(i) then
return(i)
end
end
end

function removeMob()
local slot = findEmptySlot()
c.pullItem("down", 1, 1, slot)
end

function insertMob(info)
removeMob()
c.pushItem("down", info, 1, 1)
fillTable()
end

function checkMobs()
getMobs()
–fillTable()
end

function initRS()
rsOut[colors.white] = false
rsOut[colors.orange] = true
rsOut[colors.magenta] = false
rsOut[colors.lightBlue] = true
rsOut[colors.yellow] = false
rsOut[colors.lime] = true
buttonColors["Lights"] = colors.white
buttonColors["Spawner"] = colors.orange
buttonColors["Grinders"] = colors.magenta
buttonColors["DrawBridge"] = colors.lightBlue
buttonColors["Attractor"] = colors.yellow
buttonColors["Door"] = colors.lime
end

function setRS()
local colorOutput = 0
for i,j in pairs(rsOut) do
if j then colorOutput = colorOutput + i end
end
rs.setBundledOutput("left", colorOutput)
end

function lights()
button.toggleButton("Lights")
rsOut[colors.white] = not rsOut[colors.white]
setRS()
end

function door()
button.toggleButton("Door")
rsOut[colors.lime] = not rsOut[colors.lime]
setRS()
end

function spawner()
button.toggleButton("Spawner")
rsOut[colors.orange] = not rsOut[colors.orange]
setRS()
end

function grinders()
button.toggleButton("Grinders")
rsOut[colors.magenta] = not rsOut[colors.magenta]
setRS()
end

function drawBridge()
button.toggleButton("DrawBridge")
rsOut[colors.lightBlue] = not rsOut[colors.lightBlue]
setRS()
end

function attractorToggle()
button.toggleButton("Attractor")
rsOut[colors.yellow] = not rsOut[colors.yellow]
setRS()
end

function mobSelect()
fillTable()
– refresh()
– getClick()
end

function getClick()
local event,side,x,y = os.pullEvent()
if event=="monitor_touch" then
button.checkxy(x,y)
end
end

function refresh()
m.clear()
print("Refreshed")
button.screen()
end

initRS()
setRS()
fillMainTable()
–refresh()
while true do getClick() end
Cranium #4
Posted 24 August 2014 - 06:02 PM
Okay, so I decided to merge your three separate topics into one. Why in the world have you decided to just post the "code" - if it can be called that - instead of posting something useful with it? You might be better off posting this on Pastebin if you're just looking to find someplace to save the code.