Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- API version: 1.5 Stable (match to computercraft version for best compatibility)
- -- Created by: Termanater13 on the computercraft forums
- -- Based on: Direwolf20 Button API
- -- Planned features:
- -- Rednet functionality
- -- network intigration(need Computercraft 1.51)
- -- multi monitor support
- -- changing text on buttons, labels, and headers to fit needs of user.
- -- real multi page support that has cross page button support.
- --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.
- local function colorchk(color, defultcolor)
- -- checks to see if color given is a number
- if type(color) == "number" then
- for i = 0, 15, 1 do
- if (2^i) == color then
- return color
- end
- end
- return defultcolor
- -- if color is not a number it compairs the string values for the color
- elseif type(color) == "string" then
- if colors[color] then
- return colors[color]
- elseif colours[color] then
- return colours[color]
- else
- return defultcolor
- end
- -- sets color to background color if it fails
- else
- return defultcolor
- end
- end
- -- sets the buttonapi varibale used in the api.
- -- Note to end user: making the same varible in your code will breake the api
- local Buttonapi = {}
- local ButtonApiData = {}
- local ButtonApiSetting = {}
- --this function is called to setup the
- --button api for the current instance
- -- and save the setup settings
- function setup(option,setting)
- -- tells the api what side the monitor is on.
- if option == "wrap" then
- ButtonApiSetting["wrap"] = setting
- mon = peripheral.wrap(ButtonApiSetting["wrap"])
- -- sets the text scale of monitor
- elseif option == "textScale" then
- ButtonApiSetting["textScale"] = setting
- mon.setTextScale(ButtonApiSetting["textScale"])
- -- sets text color of the monitor
- -- this is the only place it is set
- elseif option == "textColor" then
- ButtonApiSetting["textColor"] = colorchk(setting,colors.white)
- mon.setTextColor(ButtonApiSetting["textColor"])
- -- sets the background color of the monitor
- elseif option == "backgroundColor" then
- ButtonApiSetting["backgroundColor"] = colorchk(setting,colors.black)
- mon.setBackgroundColor(ButtonApiSetting["backgroundColor"])
- --Sets button Defult color on state
- elseif option == "defultOn" then
- ButtonApiSetting["defultOn"] = colorchk(setting,colors.lime)
- --Sets button Defult color off state
- elseif option == "defultOff" then
- ButtonApiSetting["defultOff"] = colorchk(setting,colors.red)
- -- sets basic settings. if setting argumemt is empty top is assumed to be location
- elseif option == "basic" then
- if setting == nil then
- ButtonApiSetting["wrap"] = "top"
- mon = peripheral.wrap(ButtonApiSetting["wrap"])
- else
- ButtonApiSetting["wrap"] = setting
- mon = peripheral.wrap(ButtonApiSetting["wrap"])
- end
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- ButtonApiSetting["textScale"] = 1
- ButtonApiSetting["textcolor"] = colors.white
- ButtonApiSetting["backgroundColor"] = colors.black
- ButtonApiSetting["defultOn"] = colors.lime
- ButtonApiSetting["defultOff"] = colors.red
- else
- print("Option "..option.." not reconized. Please use (option, setting) for function. possible options are wrap, textScale, textColor, backgroundColor, basic.")
- end
- end
- --sets the api call that creats a new
- --button in the table.
- function new(name, usefunc, xmin, xmax, ymin, ymax, offColor, onColor)
- ButtonApiData[name]={}
- ButtonApiData[name]["usefunc"] = usefunc
- ButtonApiData[name]["xmin"] = xmin
- ButtonApiData[name]["xmax"] = xmax
- ButtonApiData[name]["ymin"] = ymin
- ButtonApiData[name]["ymax"] = ymax
- ButtonApiData[name]["active"] = false
- ButtonApiData[name]["disabled"] = colorchk(offColor, ButtonApiSetting["defultOff"])
- ButtonApiData[name]["enabled"] = colorchk(onColor, ButtonApiSetting["defultOn"])
- end
- --toggles atctive statuse of the button
- function toggle(name)
- ButtonApiData[name]["active"] = not ButtonApiData[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(ButtonApiData) do
- -- sets button background color based on its state
- if value["active"] then
- mon.setBackgroundColor(value["enabled"])
- else
- mon.setBackgroundColor(value["disabled"])
- end
- -- finds the spot to print the text so it is centerd
- 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 verticly centered run the following
- 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
- -- any other vertical spot run this
- for x = 0, (value["xmax"]-value["xmin"]) do
- mon.write(" ")
- end
- end
- end
- -- resets backgroundd color to user set background color
- mon.setBackgroundColor(ButtonApiSetting["backgroundColor"])
- end
- end
- --checks the x and y of place clicked
- --to call the right function
- function check(x,y)
- for key, value in pairs(ButtonApiData) 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
- -- allows the addition of a heading to the monitor in top center
- function heading(text)
- w, h = mon.getSize()
- l = string.len(text)
- mon.setCursorPos(math.ceil((w-l)/2)+1,1)
- mon.write(text)
- end
- -- allowes placment of text anywhere
- function text(x, y, text)
- mon.setCursorPos(x,y)
- mon.write(text)
- end
- -- allows for tex scale to be increased
- function textScaleUp(upby)
- if upby == nil then
- ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] + 0.5
- else
- if type(upby) == "number" then
- ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] + upby
- else
- ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] + 0.5
- end
- end
- mon.setTextScale(ButtonApiSetting["textScale"])
- print("scale: "..ButtonApiSetting["textScale"])
- end
- -- allows for the decrease of text scale
- function textScaleDown(downby)
- if downby == nil then
- ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] - 0.5
- else
- if type(downby) == "number" then
- ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] - downby
- else
- ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] - 0.5
- end
- end
- mon.setTextScale(ButtonApiSetting["textScale"])
- print("scale: "..ButtonApiSetting["textScale"])
- end
- --this is used for clearing the memory so new pages of buttons can be used.
- function clearButtons()
- mon.clear()
- ButtonApiData = {}
- end
- -- this function will allow for the user to call info on the button
- function info(action, data1, data2)
- local returndata = {}
- -- allows for the retriving of data based on the button name
- if action == "name" then
- if data1 == nil or data1 == "all" then
- -- all data
- for key1, value1 in pairs(ButtonApiData) do
- returndata[key] = {}
- for key2, value2 in pairs(value1) do
- if key2 ~= "usefunc" then
- returndata[key1][key2] = value
- end
- end
- end
- return returndata
- elseif ButtonApiData[data1] then
- -- retrives data on a spicific button
- if data2 == nil or data2 == "all" then
- -- all data of a button
- for key, value in pairs(ButtonApiData[data1]) do
- if key ~= "usefunc" then
- returndata[key] = value
- end
- end
- return returndata
- elseif ButtonApiData[data1][data2] then
- -- returns a spacific button value
- return ButtonApiData[data1][data2]
- end
- else
- return false
- end
- -- allows the ability to retrive data based on if it is active or not
- elseif action == "status" then
- if data1 == nil or data1 == "all" then
- -- retreives button status
- for key1, value1 in pairs(buttonApiData) do
- for key2, value2 in pairs(value1) do
- if key2 == "active" then
- returndata[key1] = value2
- end
- end
- end
- elseif ButtonApiData[data1] then
- -- returns the data fo the button if it is active or not
- return ButtonApiData[data1]["active"]
- elseif data == status then
- if data2 == "active" or data2 == "on" or data2 == "enabled" or data2 == true then
- -- returns all active buttons
- for key, value in pairs(ButtonApiData) do
- if ButtonApiData[key]["active"] then
- returndata[key] = ButtonApiData[key]["active"]
- end
- end
- return returndata
- elseif data2 == "deactive" or data2 == "off" or data2 == "disabled" or data2 == false then
- -- returns all enactive buttons
- for key, value in pairs(ButtonApiData) do
- if (not ButtonApiData[key]["active"]) then
- returndata[key] = ButtonApiData[key]["active"]
- end
- end
- return returndata
- end
- else
- return false
- end
- -- allows for the retrival of current settings
- elseif action == "setting" then
- if data1 == nil or data1 == "all" then
- -- returns current setting of all settings
- for key, value in pairs(ButtonApiSetting) do
- returndata[key] = value
- end
- return returndata
- elseif ButtonApiSetting[data1] then
- -- returns the current setting's setting
- return ButtonApiSetting[data1]
- else
- return false
- end
- else
- return false
- end
- end
- -- function allowas for the saving of the data and settings
- function save(page, setting)
- -- set local var to manipulate the save data
- local var = {}
- -- check if api is loaded
- if termAPI then
- -- error check to still allow one page saving
- if type(page) == "number" then
- if fs.exists("ButtonData") then
- var = termAPI.load("ButtonData")
- end
- var[page] = ButtonApiData
- termAPI.save("ButtonData", var)
- if setting == "yes" then
- termAPI.save("ButtonSetting", ButtonApiSetting)
- end
- return true
- else
- print("ERROR SAVE 001: check function aurguments.")
- return false
- end
- else
- print("ERROR API 404: termAPI needed to save data, please download and load api and try again.")
- return false
- end
- end
- -- loades data from files
- function load(page, funcbutt, buttover, setload, funcsett, settover)
- local var = {}
- if termAPI then
- if type(page) == "number" then
- if fs.exists("ButtonData") and buttover ~= "yes" then
- var = termAPI.load("ButtonData")
- ButtonApiData = var[page]
- else
- funcbutt()
- end
- if setload == "yes" then
- if fs.exists("ButtonSetting") and settover ~= "yes" then
- var = termAPI.load("ButtonSetting")
- for key, value in pairs(var) do
- setup(key,value)
- end
- else
- funcsett()
- end
- else
- end
- else
- print("ERROR LOAD 001: check function aurguments.")
- return false
- end
- else
- print("ERROR API 404: termAPI needed to load data, please download and load api and try again. func ran as fall back")
- funcbutt()
- funcsett()
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment