Posted 10 July 2013 - 01:33 AM
Im trying to right an API to add buttons to a monitor similar to Direwolf20's button API and as I get close to getting it done I get a "bios:338: [string "button"]:2: unexpected symbol" error and on line 2 of my code looks fine to me and I have no idea whats wrong. it worked untill I added a setting saving part to the variable.
its in spoiler tags due to its length and I have no Idea what part could be causing it.
its in spoiler tags due to its length and I have no Idea what part could be causing it.
Spoiler
local Buttonapi = {}
local Buttonapi["data"] = {}
local Buttonapi["setting"] = {}
--this function is called to setup the
--button api for the current instance
--************************************
--need a way to save setup data
function setup(option,setting)
if option == "wrap" then
mon = peripheral.wrap(setting)
Buttonapi["setting"]["wrap"] = setting
elseif option == "textscale" then
mon.setTextScale(setting)
Buttonapi["setting"]["textscale"] = setting
elseif option == "textcolor" then
mon.setTextColor(setting)
Buttonapi["setting"]["textcolor"] = setting
elseif option == "backgroundcolor" then
mon.setBackgroundColor(setting)
Buttonapi["setting"]["backgroundcolor"] = setting
elseif option == "basic" then
if setting == nil then
mon = peripheral.wrap("top")
Buttonapi["setting"]["wrap"] = "top"
else
mon = peripheral.wrap(setting)
Buttonapi["setting"]["wrap"] = setting
end
mon.setTextScale(1)
mon.setTextColor(colors.white)
mon.setBackgroundColor(colors.black)
Buttonapi["setting"]["textscale"] = 1
Buttonapi["setting"]["textcolor"] = colors.white
Buttonapi["setting"]["backgroundcolor"] = colors.black
else
print("Option not reconized. Please use (option, setting) for function. possible options are wrap, textscale, textcolor, backgroundcolor, basic.")
end
end
--adds a color checking function for
--easyer coding with api, you supply a
--name or number for the color rather
--than using CC's defult color api.
--This makes it more new user freindly.
function colorchk(color, defultcolor)
if type(color) == "number" then
for i = 0, 15, 1 do
if (2^i) == color then
return color
else
return defultcolor
end
end
elseif type(color) == "string" then
if color == "white" then
return colors.white
elseif color == "orange" then
return colors.orange
elseif color == "magenta" then
return colors.magenta
elseif color == "lightblue" then
return colors.lightBlue
elseif color == "yellow" then
return colors.yellow
elseif color == "lime" then
return colors.lime
elseif color == "pink" then
return colors.pink
elseif color == "gray" then
return colors.gray
elseif color == "lightgray" then
return colors.lightGray
elseif color == "cyan" then
return colors.cyan
elseif color == "purple" then
return colors.purple
elseif color == "blue" then
return colors.blue
elseif color == "brown" then
return colors.brown
elseif color == "green" then
return colors.green
elseif color == "red" then
return colors.red
elseif color == "black" then
return colors.black
end
else
return defultcolor
end
end
--sets the api call that creats a new
--button in the table.
function new(name, usefunc, xmin, xmax, ymin, ymax, neColor, enColor)
Buttonapi["data"][name]={}
Buttonapi["data"][name]["usefunc"] = usefunc
Buttonapi["data"][name]["xmin"] = xmin
Buttonapi["data"][name]["xmax"] = xmax
Buttonapi["data"][name]["ymin"] = ymin
Buttonapi["data"][name]["ymax"] = ymax
Buttonapi["data"][name]["active"] = false
Buttonapi["data"][name]["disabled"] = colorchk(neColor, colors.red)
Buttonapi["data"][name]["enabled"] = colorchk(enColor, colors.lime)
end
--toggles atctive statuse of the button
function toggle(name)
Buttonapi["data"][name]["active"] = not Buttonapi["data"][name]["active"]
display(name)
end
--finds where to place text based on x
local function findx(xl,xh,tl)
ts=xh-xl-tl
if (ts % 2) == 0 then
return (ts/2)
else
return (math.floor(ts/2)+1)
end
end
--finds where to place text based on y
local function findy(yl,yh)
return (math.floor((yl+yh)/2))
end
--displayes th buttons created
--needs to be looked over throws errors
function display()
for key, value in pairs(Buttonapi["data"]) do
if value["active"] then
mon.setBackgroundColor(value["enabled"])
else
mon.setBackgroundColor(value["disabled"])
end
xspot = findx(value["xmin"], value["xmax"], string.len(key))
yspot = findy(value["ymin"], value["ymax"])
for y = value["ymin"], value["ymax"] do
mon.setCursorPos(value["xmin"],y)
if y == yspot then
for x = 0, (value["xmax"]-value["xmin"]-string.len(key)+1) do
if x == xspot then
mon.write(key)
else
mon.write(" ")
end
end
else
for x = 0, (value["xmax"]-value["xmin"]) do
mon.write(" ")
end
end
end
--reset background color here
end
end
--checks the x and y of place clicked
--to call the right function
function check(x,y)
for key, value in pairs(Buttonapi["data"]) do
if y>=value["ymin"] and y<=value["ymax"] then
if x>=value["xmin"] and x<=value["xmax"] then
value["usefunc"]()
return true
end
end
end
return false
end
function heading(text)
w, h = mon.getSize()
l = string.len(text)
mon.setCursorPos(math.ceil((w-l)/2)+1,1)
mon.write(text)
end
function text(x, y, text)
mon.setCursorPos(x,y)
mon.write(text)
end