I just sat here working on a complete new idea.
I abandoned all ideas of adjusting tables, But I found a way to save states accross reloads. (even menu changes)…
Here is the new Button API code
mon = peripheral.wrap("right")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
function clearTable()
button = {}
mon.clear()
end
function setTable(name, func, xmin, xmax, ymin, ymax, state)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = state or false
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function funcName()
print("You clicked buttonText")
end
function fillTable()
setTable("ButtonText", funcName, 5, 25, 4, 8)
end
function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
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(text) +1 do
if k == xspot then
mon.write(text)
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 else currColor = colors.red end
fill(name, currColor, data)
end
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
data["func"]()
return true
--data["active"] = not data["active"]
--print(name)
end
end
end
return false
end
function heading(text)
w, h = mon.getSize()
mon.setCursorPos(math.floor((w-string.len(text))/2+1), 1)
mon.write(text)
end
function label(w, h, text)
mon.setCursorPos(w, h)
mon.write(text)
end
Here is the new Menu Code.
os.loadAPI("button")
--[[ VARIABLES ]]--
local mon = peripheral.wrap("right")
local CowBool = "false"
--[[ FUNCTIONS ]]--
function FarmButtons()
button.setTable("Cow Farm", CowFarm, 10, 20, 3, 5, CowBool) -- pass new var to api.
button.setTable("Blaze Farm", BlazeFarm, 22, 32, 3, 5)
button.setTable("Back", back, 10, 17, 15, 17)
button.screen()
end
function MainButtons()
button.setTable("Farm Menu", FarmMenu, 10, 20, 3, 5)
button.setTable("Test2", main2, 22, 32, 3, 5)
button.setTable("Test3", main3, 10, 20, 8, 10)
button.setTable("Test4", main4, 22, 32, 8, 10)
button.screen()
end
function getClick()
event, side, x, y = os.pullEvent("monitor_touch")
button.checkxy(x,y)
end
function CheckCowBool()
if fs.exists("buttonstates") then -- File exists catch
local filer = fs.open("buttonstates", "r")
ReadCow = filer.readLine()
CowBool = tostring(ReadCow) == "true"
filer.close()
if ReadCow == "true" then
rs.setBundledOutput("bottom", colors.white)
end
end
end
function FarmMenu() --Clears the past(main) menu and runs/displays the "Farms" menu
button.flash("Farm Menu")
sleep(0.1)
button.clearTable()
FarmButtons()
button.heading("Farms")
end
function main2()
sleep(0.8) --Do Stuff
end
function main3()
sleep(0.8)
--Do Stuff
end
function main4()
sleep(0.8)
--Do stuff
end
function back()
button.flash("Back")
button.clearTable()
MainButtons()
button.heading("N1ght Network")
end
function CowFarm()
local CowBool = not CowBool -- why do 5 line if's, if one line not works.
local filew = fs.open("buttonstates", "w")
filew.writeLine(CowBool)
filew.close()
button.toggleButton("Cow Farm")
if CowBool then
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom")+colors.white)
else
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom")-colors.white)
end
end
function BlazeFarm()
sleep(0.8) --Toggle the BlazeFarm
end
--[[ MAIN ]]--
CheckCowBool()
mon.clear()
MainButtons()
button.heading("N1ght Network")
while true do
getClick()
end
I just spent the last hour making it work as it says on the tin. :D/>
(to carry a new state to another button you need a new varible and read from file.) then when the setTable( … ) is called just add the boolean state caller to the end of the table eg. CowBool like I already did. (the table insert will handle nil and true values, the default value being false).
Have fun :)/>