termanater13

button API not Finished

Jul 28th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.29 KB | None | 0 0
  1. -- API version: 1.5 Stable (match to computercraft version for best compatibility)
  2. -- Created by: Termanater13 on the computercraft forums
  3. -- Based on: Direwolf20 Button API
  4.  
  5. -- Planned features:
  6. -- Rednet functionality
  7. -- network intigration(need Computercraft 1.51)
  8. -- multi monitor support
  9. -- changing text on buttons, labels, and headers to fit needs of user.
  10. -- real multi page support that has cross page button support.
  11.  
  12. --adds a color checking function for
  13. --easyer coding with api, you supply a
  14. --name or number for the color rather
  15. --than using CC's defult color api.
  16. --This makes it more new user freindly.
  17. local function colorchk(color, defultcolor)
  18.     -- checks to see if color given is a number
  19.     if type(color) == "number" then
  20.         for i = 0, 15, 1 do
  21.             if (2^i) == color then
  22.                 return color
  23.             end
  24.         end
  25.         return defultcolor
  26.     -- if color is not a number it compairs the string values for the color
  27.     elseif type(color) == "string" then
  28.         if colors[color] then
  29.             return colors[color]
  30.         elseif colours[color] then
  31.             return colours[color]
  32.         else
  33.             return defultcolor
  34.         end
  35.     -- sets color to background color if it fails
  36.     else
  37.         return defultcolor
  38.     end
  39. end
  40. -- sets the buttonapi varibale used in the api.
  41. -- Note to end user: making the same varible in your code will breake the api
  42. local Buttonapi = {}
  43. local ButtonApiData = {}
  44. local ButtonApiSetting = {}
  45. --this function is called to setup the
  46. --button api for the current  instance
  47. -- and save the setup settings
  48. function setup(option,setting)
  49.     -- tells the api what side the monitor is on.
  50.     if option == "wrap" then
  51.         ButtonApiSetting["wrap"] = setting
  52.         mon = peripheral.wrap(ButtonApiSetting["wrap"])
  53.     -- sets the text scale of monitor
  54.     elseif option == "textScale" then
  55.         ButtonApiSetting["textScale"] = setting
  56.         mon.setTextScale(ButtonApiSetting["textScale"])
  57.     -- sets text color of the monitor
  58.     -- this is the only place it is set
  59.     elseif option == "textColor" then
  60.         ButtonApiSetting["textColor"] = colorchk(setting,colors.white)
  61.         mon.setTextColor(ButtonApiSetting["textColor"])
  62.     -- sets the background color of the monitor
  63.     elseif option == "backgroundColor" then
  64.         ButtonApiSetting["backgroundColor"] = colorchk(setting,colors.black)
  65.         mon.setBackgroundColor(ButtonApiSetting["backgroundColor"])
  66.     --Sets button Defult color on state
  67.     elseif option == "defultOn" then
  68.         ButtonApiSetting["defultOn"] = colorchk(setting,colors.lime)
  69.     --Sets button Defult color off state
  70.     elseif option == "defultOff" then
  71.         ButtonApiSetting["defultOff"] = colorchk(setting,colors.red)
  72.     -- sets basic settings. if setting argumemt is empty top is assumed to be location
  73.     elseif option == "basic" then
  74.         if setting == nil then
  75.             ButtonApiSetting["wrap"] = "top"
  76.             mon = peripheral.wrap(ButtonApiSetting["wrap"])
  77.         else
  78.             ButtonApiSetting["wrap"] = setting
  79.             mon = peripheral.wrap(ButtonApiSetting["wrap"])
  80.         end
  81.         mon.setTextScale(1)
  82.         mon.setTextColor(colors.white)
  83.         mon.setBackgroundColor(colors.black)
  84.         ButtonApiSetting["textScale"] = 1
  85.         ButtonApiSetting["textcolor"] = colors.white
  86.         ButtonApiSetting["backgroundColor"] = colors.black
  87.         ButtonApiSetting["defultOn"] = colors.lime
  88.         ButtonApiSetting["defultOff"] = colors.red
  89.     else
  90.         print("Option "..option.." not reconized. Please use (option, setting) for function. possible options are wrap, textScale, textColor, backgroundColor, basic.")
  91.     end
  92. end
  93. --sets the api call that creats a new
  94. --button in the table.
  95. function new(name, usefunc, xmin, xmax, ymin, ymax, offColor, onColor)
  96.     ButtonApiData[name]={}
  97.     ButtonApiData[name]["usefunc"] = usefunc
  98.     ButtonApiData[name]["xmin"] = xmin
  99.     ButtonApiData[name]["xmax"] = xmax
  100.     ButtonApiData[name]["ymin"] = ymin
  101.     ButtonApiData[name]["ymax"] = ymax
  102.     ButtonApiData[name]["active"] = false
  103.     ButtonApiData[name]["disabled"] = colorchk(offColor, ButtonApiSetting["defultOff"])
  104.     ButtonApiData[name]["enabled"] = colorchk(onColor, ButtonApiSetting["defultOn"])
  105. end
  106. --toggles atctive statuse of the button
  107. function toggle(name)
  108.     ButtonApiData[name]["active"] = not ButtonApiData[name]["active"]
  109.     display(name)
  110. end
  111. --finds where to place text based on x
  112. local function findx(xl,xh,tl)
  113.     ts=xh-xl-tl
  114.     if (ts % 2) == 0 then
  115.         return (ts/2)
  116.     else
  117.         return (math.floor(ts/2)+1)
  118.     end
  119. end
  120. --finds where to place text based on y
  121. local function findy(yl,yh)
  122.     return (math.floor((yl+yh)/2))
  123. end
  124. --displayes th buttons created
  125. --needs to be looked over throws errors
  126. function display()
  127.     for key, value in pairs(ButtonApiData) do
  128.         -- sets button background color based on its state
  129.         if value["active"] then
  130.             mon.setBackgroundColor(value["enabled"])
  131.         else
  132.             mon.setBackgroundColor(value["disabled"])
  133.         end
  134.         -- finds the spot to print the text so it is centerd
  135.         xspot = findx(value["xmin"], value["xmax"], string.len(key))
  136.         yspot = findy(value["ymin"], value["ymax"])
  137.         for y = value["ymin"], value["ymax"] do
  138.             mon.setCursorPos(value["xmin"],y)
  139.             -- if verticly centered run the following
  140.             if y == yspot then
  141.                 for x = 0, (value["xmax"]-value["xmin"]-string.len(key)+1) do
  142.                     if x == xspot then
  143.                         mon.write(key)
  144.                     else
  145.                         mon.write(" ")
  146.                     end
  147.                 end
  148.             else
  149.             -- any other vertical spot run this
  150.                 for x = 0, (value["xmax"]-value["xmin"]) do
  151.                     mon.write(" ")
  152.                 end
  153.             end
  154.         end
  155.         -- resets backgroundd color to user set background color
  156.         mon.setBackgroundColor(ButtonApiSetting["backgroundColor"])
  157.     end
  158. end
  159. --checks the x and y of place clicked
  160. --to call the right function
  161. function check(x,y)
  162.     for key, value in pairs(ButtonApiData) do
  163.         if y>=value["ymin"] and y<=value["ymax"] then
  164.             if x>=value["xmin"] and x<=value["xmax"] then
  165.                 value["usefunc"]()
  166.                 return true
  167.             end
  168.         end
  169.     end
  170.     return false
  171. end
  172. -- allows the addition of a heading to the monitor in top center
  173. function heading(text)
  174.     w, h = mon.getSize()
  175.     l = string.len(text)
  176.     mon.setCursorPos(math.ceil((w-l)/2)+1,1)
  177.     mon.write(text)
  178. end
  179. -- allowes placment of text anywhere
  180. function text(x, y, text)
  181.     mon.setCursorPos(x,y)
  182.     mon.write(text)
  183. end
  184. -- allows for tex scale to be increased
  185. function textScaleUp(upby)
  186.     if upby == nil then
  187.         ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] + 0.5
  188.     else
  189.         if type(upby) == "number" then
  190.             ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] + upby
  191.         else
  192.             ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] + 0.5
  193.         end
  194.     end
  195.     mon.setTextScale(ButtonApiSetting["textScale"])
  196.     print("scale: "..ButtonApiSetting["textScale"])
  197. end
  198. -- allows for the decrease of text scale
  199. function textScaleDown(downby)
  200.     if downby == nil then
  201.         ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] - 0.5
  202.     else
  203.         if type(downby) == "number" then
  204.             ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] - downby
  205.         else
  206.             ButtonApiSetting["textScale"] = ButtonApiSetting["textScale"] - 0.5
  207.         end
  208.     end
  209.     mon.setTextScale(ButtonApiSetting["textScale"])
  210.     print("scale: "..ButtonApiSetting["textScale"])
  211. end
  212.  
  213. --this is used for clearing the memory so new pages of buttons can be used.
  214. function clearButtons()
  215.     mon.clear()
  216.     ButtonApiData = {}
  217. end
  218.  
  219. -- this function will allow for the user to call info on the button
  220. function info(action, data1, data2)
  221. local returndata = {}
  222.     -- allows for the retriving of data based on the button name
  223.     if action == "name" then
  224.         if data1 == nil or data1 == "all" then
  225.             -- all data
  226.             for key1, value1 in pairs(ButtonApiData) do
  227.                 returndata[key] = {}
  228.                 for key2, value2 in pairs(value1) do
  229.                     if key2 ~= "usefunc" then
  230.                         returndata[key1][key2] = value
  231.                     end
  232.                 end
  233.             end
  234.             return returndata
  235.         elseif ButtonApiData[data1] then
  236.             -- retrives data on a spicific button
  237.             if data2 == nil or data2 == "all" then
  238.                 -- all data of a button
  239.                 for key, value in pairs(ButtonApiData[data1]) do
  240.                     if key ~= "usefunc" then
  241.                         returndata[key] = value
  242.                     end
  243.                 end
  244.                 return returndata
  245.             elseif ButtonApiData[data1][data2] then
  246.                 -- returns a spacific button value
  247.                 return ButtonApiData[data1][data2]
  248.             end
  249.         else
  250.             return false
  251.         end
  252.     -- allows the ability to retrive data based on if it is active or not
  253.     elseif action == "status" then
  254.         if data1 == nil or data1 == "all" then
  255.             -- retreives button status
  256.             for key1, value1 in pairs(buttonApiData) do
  257.                 for key2, value2 in pairs(value1) do
  258.                     if key2 == "active" then
  259.                         returndata[key1] = value2
  260.                     end
  261.                 end
  262.             end
  263.         elseif ButtonApiData[data1] then
  264.             -- returns the data fo the button if it is active or not
  265.             return ButtonApiData[data1]["active"]
  266.         elseif data == status then
  267.             if data2 == "active" or data2 == "on" or data2 == "enabled" or data2 == true then
  268.                 -- returns all active buttons
  269.                 for key, value in pairs(ButtonApiData) do
  270.                     if ButtonApiData[key]["active"] then
  271.                         returndata[key] = ButtonApiData[key]["active"]
  272.                     end
  273.                 end
  274.                 return returndata
  275.             elseif data2 == "deactive" or data2 == "off" or data2 == "disabled" or data2 == false then
  276.                 -- returns all enactive buttons
  277.                 for key, value in pairs(ButtonApiData) do
  278.                     if (not ButtonApiData[key]["active"]) then
  279.                         returndata[key] = ButtonApiData[key]["active"]
  280.                     end
  281.                 end
  282.                 return returndata
  283.             end
  284.         else
  285.             return false
  286.         end
  287.     -- allows for the retrival of current settings
  288.     elseif action == "setting" then
  289.         if data1 == nil or data1 == "all" then
  290.             -- returns current setting of all settings
  291.             for key, value in pairs(ButtonApiSetting) do
  292.                 returndata[key] = value
  293.             end
  294.             return returndata
  295.         elseif ButtonApiSetting[data1] then
  296.             -- returns the current setting's setting
  297.             return ButtonApiSetting[data1]
  298.         else
  299.             return false
  300.         end
  301.     else
  302.         return false
  303.     end
  304. end
  305. -- function allowas for the saving of the data and settings
  306. function save(page, setting)
  307. -- set local var to manipulate the save data
  308. local var = {}
  309.     -- check if api is loaded
  310.     if termAPI then
  311.         -- error check to still allow one page saving
  312.         if type(page) == "number" then
  313.             if fs.exists("ButtonData") then
  314.                 var = termAPI.load("ButtonData")
  315.             end
  316.             var[page] = ButtonApiData
  317.             termAPI.save("ButtonData", var)
  318.             if setting == "yes" then
  319.                 termAPI.save("ButtonSetting", ButtonApiSetting)
  320.             end
  321.             return true
  322.         else
  323.             print("ERROR SAVE 001: check function aurguments.")
  324.             return false
  325.         end
  326.     else
  327.         print("ERROR API 404: termAPI needed to save data, please download and load api and try again.")
  328.         return false
  329.     end
  330. end
  331. -- loades data from files
  332. function load(page, funcbutt, buttover, setload, funcsett, settover)
  333. local var = {}
  334.     if termAPI then
  335.         if type(page) == "number" then
  336.             if fs.exists("ButtonData") and buttover ~= "yes" then
  337.                 var = termAPI.load("ButtonData")
  338.                 ButtonApiData = var[page]
  339.             else
  340.                 funcbutt()
  341.             end
  342.             if setload == "yes" then
  343.                 if fs.exists("ButtonSetting") and settover ~= "yes" then
  344.                     var = termAPI.load("ButtonSetting")
  345.                     for key, value in pairs(var) do
  346.                         setup(key,value)
  347.                     end
  348.                 else
  349.                     funcsett()
  350.                 end
  351.             else
  352.            
  353.             end
  354.         else
  355.             print("ERROR LOAD 001: check function aurguments.")
  356.             return false
  357.         end
  358.     else
  359.         print("ERROR API 404: termAPI needed to load data, please download and load api and try again. func ran as fall back")
  360.         funcbutt()
  361.         funcsett()
  362.         return false
  363.     end
  364. end
Advertisement
Add Comment
Please, Sign In to add comment