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
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