Posted 29 March 2016 - 05:41 AM
So Im trying to build a button API to use for my CC projects, I found one that Direwolf20 made and edited it to try and make it work but whenever I call the API and try to use it it returns nil.
Here's the API:
and here is me trying to use it:
Please Help!
Here's the API:
local Button = {}
local FillColor = colors.cyan
local ActiveColor = colors.blue
local TextColor = colors.white
function SetTable(name, func, x, y, width, height)
Button[name] = {}
Button[name]["Func"] = func
Button[name]["Active"] = false
Button[name]["X"] = x
Button[name]["Y"] = y
Button[name]["Width"] = width
Button[name]["Height"] = height
end
function ClearTable()
Button = {}
end
function SetFillColor(color)
if (colors.combine(color,color)) then
FillColor = color
end
end
function SetTextColor(color)
if (colors.combine(color,color)) then
TextColor = color
end
end
function DrawButton(text,color,bData)
term.setBackgroundColor(color)
term.setTextColor(TextColor)
local textCenter = string.len(text)/2
local yCenter = bData["Y"]+bData["Height"]/2+1
local xCenter = bData["X"]+bData["Width"]/2-textCenter-1
for y = bData["Y"],bData["Height"] do
term.setCursorPos(bData["X"],y)
if y == yCenter then
for x = 0, bData["X"]+bData["Width"]-textCenter-1 do
if x==xCenter then
term.write(text)
else
term.write(" ")
end
end
else
for i = bData["X"],bData["Width"] do
term.write(" ")
end
end
end
end
function DrawtoScreen()
for name,data in ipairs(Button) do
local on = data["Active"]
if on == true then
FillColor = FillColor
else
FillColor = ActiveColor
end
DrawButton(name,FillColor,data)
end
end
function ToggleButton(name)
Button[name]["Active"] = not Button[name]["Active"]
DrawtoScreen()
end
function PressButton(name)
ToggleButton(name)
DrawtoScreen()
sleep(0.15)
ToggleButton(name)
DrawtoScreen()
end
function GetClick()
local event,side,x,y = os.pullEvent("mouse_click")
for name,data in ipairs(Button) do
if x >= data["X"] and x <= data["Width"] and y >= data["Y"] and y <= data["Height"] then
data["Func"]()
return true
end
end
end
and here is me trying to use it:
--Desktop
os.loadAPI("Button")
slc = 0
titleBarColor = 256
titleBarTextColor = 32768
function titleBar()
term.setCursorPos(1,1)
term.setBackgroundColor(titleBarColor)
term.setTextColor(titleBarTextColor)
term.clearLine()
titleBarButtons()
end
function start()
return true
end
function drawDesktop()
term.clear()
titleBar()
Button.SetTable("Name", start, 2, 2, 10, 4)
Button.DrawtoScreen()
end
drawDesktop()
Please Help!