termanater13

ButtonAPI v1.0

Mar 3rd, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.28 KB | None | 0 0
  1. -- Button API
  2. -- Version: 1.0
  3. -- Tested in: computercraft 1.58 (Minecraft 1.6.4)
  4. -- By: Termanater13 (of the computercraft forums)
  5. -- Link: http://www.computercraft.info/forums2/index.php?/topic/17346-button-api/
  6. -- Description: Turn your advanced monitor into a touch screen with
  7. --      with fully customizable buttons and labels.
  8. -- Support: if you are having any issues with This API, use the link
  9. --      and follow the directions there.
  10. -- Credits: Direwolf20 for the button API he made that made me want
  11. --      to make my own, the display code is this is based on his code
  12. --      since ones I tried would not work.
  13.  
  14. -- set variables for API
  15. local buttonData = {}
  16. local buttonFunc = {}
  17. local mon = {}
  18. local chk = {}
  19. new = {}
  20. e = {}
  21. e.s={}
  22. get = {}
  23. -- initioal settings for buttonSetting
  24. local buttonSetting = {}
  25. buttonSetting.back = {}
  26. buttonSetting.back.on = colors.lime
  27. buttonSetting.back.off = colors.red
  28. buttonSetting.back.non = colors.black
  29. buttonSetting.text = {}
  30. buttonSetting.text.on = colors.white
  31. buttonSetting.text.off = colors.white
  32. buttonSetting.text.non = colors.white
  33. buttonSetting.scale = 1
  34. buttonSetting.size = {}
  35. buttonSetting.size.x = nil
  36. buttonSetting.size.y = nil
  37. -- set Side for the button to be displayed
  38. function wrap (side)
  39.   mon = peripheral.wrap(side)
  40.   buttonSetting.size.x, buttonSetting.size.y = mon.getSize()
  41. end
  42. -- find coordinates middle value
  43. local function fx (l,h,t)
  44.   local n = string.len(t)
  45.   local f = h-l-n
  46.   if (f%2) == 0 then
  47.     return (f/2)
  48.     else
  49.         return (math.floor(f/2)+1)
  50.   end
  51. end
  52. local function fy (l,h)
  53.   return math.floor((h+l)/2)
  54. end
  55. -- check functions
  56. function chk.color(color)
  57.   if type(color) == "number" then
  58.     for i = 0,15,1 do
  59.       if color == (2^i) then
  60.         return color
  61.       end
  62.     end
  63.   elseif type(color) == "string" then
  64.     color = string.lower(color)
  65.     if color == "lightgray" or color == "light grey" then
  66.       color = "lightGray"
  67.     end
  68.     if color == "lightblue" or color == "light blue" then
  69.       color = "lightBlue"
  70.     end
  71.     if colors[color] then
  72.       return colors[color]
  73.     end
  74.   else
  75.     return false
  76.   end
  77. end
  78. -- make a new button
  79. function new.b (id,text,xmin,xmax,ymin,ymax,func,argpass)
  80.   buttonData[id]={}
  81.   buttonData[id].x={}
  82.   buttonData[id].x.min = xmin
  83.   buttonData[id].x.max = xmax
  84.   buttonData[id].y={}
  85.   buttonData[id].y.min = ymin
  86.   buttonData[id].y.max = ymax
  87.   buttonData[id].text = {}
  88.   buttonData[id].text.on = nil
  89.   buttonData[id].text.off = nil
  90.   buttonData[id].text.show = text
  91.   buttonData[id].back = {}
  92.   buttonData[id].back.on = nil
  93.   buttonData[id].back.off = nil
  94.   buttonData[id].display = true
  95.   buttonData[id].click = true
  96.   buttonData[id].hidden = false
  97.   buttonData[id].label = "B"
  98.   buttonData[id].status = false
  99.   buttonFunc[id] = {}
  100.   buttonFunc[id].func = func or nil
  101.   buttonFunc[id].argpass = argpass or nil
  102. end
  103. function new.l (id,text,x,y,func,argpass)
  104.   buttonData[id]={}
  105.   buttonData[id].x={}
  106.   buttonData[id].x.min = x
  107.   buttonData[id].x.max = x + string.len(text)
  108.   buttonData[id].y={}
  109.   buttonData[id].y.min = y
  110.   buttonData[id].y.max = y
  111.   buttonData[id].text = {}
  112.   buttonData[id].text.on = nil
  113.   buttonData[id].text.off = nil
  114.   buttonData[id].text.show = text
  115.   buttonData[id].back = {}
  116.   buttonData[id].back.on = nil
  117.   buttonData[id].back.off = nil
  118.   buttonData[id].display = true
  119.   buttonData[id].click = false
  120.   buttonData[id].hidden = false
  121.   buttonData[id].label = "L"
  122.   buttonData[id].status = false
  123.   buttonFunc[id] = {}
  124.   buttonFunc[id].func = func or nil
  125.   buttonFunc[id].argpass = argpass or nil
  126. end
  127. -- edit commands to directly set the values
  128. function e.color (id, T, on, off)
  129.   T = string.lower(T)
  130.   if T == "back" or T == "background" then
  131.     buttonData[id].back.on = chk.color(on)
  132.     buttonData[id].back.off = chk.color(off)
  133.   elseif T == "text" then
  134.     buttonData[id].text.on = chk.color(on)
  135.     buttonData[id].text.off = chk.color(off)
  136.   end
  137. end
  138. function e.x (id, T, value)
  139.   if type(T) == "number" then
  140.     if T > value then
  141.       value, T = T, value
  142.     end
  143.     buttonData[id].x.min = T
  144.     buttonData[id].x.max = value
  145.   elseif type(T) == "string" then
  146.     if string.lower(T) == "min" then
  147.       buttonData[id].x.min = value
  148.     elseif string.lower(T) == "max" then
  149.       buttonData[id].x.max = value
  150.     end
  151.   end
  152. end
  153. function e.y (id, T, value)
  154.   if type(T) == "number" then
  155.     if T > value then
  156.       value, T = T, value
  157.     end
  158.     buttonData[id].x.min = T
  159.     buttonData[id].x.max = value
  160.   elseif type(T) == "string" then
  161.     if string.lower(T) == "min" then
  162.       buttonData[id].x.min = value
  163.     elseif string.lower(T) == "max" then
  164.       buttonData[id].x.max = value
  165.     end
  166.   end
  167. end
  168. function e.text (id, value)
  169.   if type(value) == "string" then
  170.     buttonData[id].text.show = value
  171.   end
  172. end
  173. function e.label (id, value)
  174.   value = string.lower(value)
  175.   if value == "b" or value == "button" then
  176.     buttonData[id].label = "B"
  177.   elseif value == "l" or value == "label" then
  178.     buttonData[id].label = "L"
  179.   end
  180. end
  181. function e.func (id, value)
  182.   if type(value) == "function" then
  183.     buttonFunc[id].func = value
  184.   end
  185. end
  186. function e.argpass(id, value)
  187.   buttonFunc[id].argpass = argpass
  188. end
  189. -- edit commands to set or toggle values
  190. function e.display (id, value)
  191.   if type(value) == "string" then
  192.     if string.lower(value) == "on" or string.lower(value) == "yes" then
  193.       value = true
  194.     elseif string.lower(value) == "off" or string.lower(value) == "no" then
  195.       value = false
  196.     end
  197.   end
  198.   if value == nil or type(value) ~= "boolean" then
  199.     buttonData[id].display = not buttonData[id].display
  200.   elseif type(value) == "boolean" then
  201.     buttonData[id].display = value
  202.   end
  203. end
  204. function e.click (id, value)
  205.   if type(value) == "string" then
  206.     if string.lower(value) == "on" or string.lower(value) == "yes" then
  207.       value = true
  208.     elseif string.lower(value) == "off" or string.lower(value) == "no" then
  209.       value = false
  210.     end
  211.   end
  212.   if value == nil or type(value) ~= "boolean" then
  213.     buttonData[id].click = not buttonData[id].click
  214.   elseif type(value) == "boolean" then
  215.     buttonData[id].click = value
  216.   end
  217. end
  218. function e.status (id, value)
  219.   if type(value) == "string" then
  220.     if string.lower(value) == "on" or string.lower(value) == "yes" then
  221.       value = true
  222.     elseif string.lower(value) == "off" or string.lower(value) == "no" then
  223.       value = false
  224.     end
  225.   end
  226.   if value == nil or type(value) ~= "boolean" then
  227.     buttonData[id].status = not buttonData[id].status
  228.   elseif type(value) == "boolean" then
  229.     buttonData[id].status = value
  230.   end
  231. end
  232. function e.hidden (id, value)
  233.   if type(value) == "string" then
  234.     if string.lower(value) == "on" or string.lower(value) == "yes" then
  235.       value = true
  236.     elseif string.lower(value) == "off" or string.lower(value) == "no" then
  237.       value = false
  238.     end
  239.   end
  240.   if value == nil or type(value) ~= "boolean" then
  241.     buttonData[id].hidden = not buttonData[id].hidden
  242.   elseif type(value) == "boolean" then
  243.     buttonData[id].hidden = value
  244.   end
  245. end
  246. -- used to edit the settings
  247. function e.s.color(T, S, color)
  248.   T = string.lower(T)
  249.   S = string.lower(S)
  250.   local test = {}
  251.   if T == "back" or T == "background" or T == "text" then
  252.     test.t = true
  253.     if T == "background" then
  254.       T = "back"
  255.     end
  256.   end
  257.   if S == "on" or S == "Off" or S == "non" then
  258.     test.s = true
  259.   end
  260.   if test.t and test.s and chk.color(color) then
  261.     buttonSetting[T][S] = chk.color(color)
  262.   end
  263. end
  264. function e.s.scale(scale)
  265.   buttonSetting.scale = scale
  266.   mon.setTextScale(scale)
  267. end
  268. -- used to get data for buttons
  269. function get.b (id)
  270.   if id == nil then
  271.     return buttonData
  272.   else
  273.     return buttonData[id]
  274.   end
  275. end
  276. get.l = get.b
  277. function get.s ()
  278.   return buttonSetting
  279. end
  280. function get.f (id)
  281.   return buttonFunc[id]
  282. end
  283. -- color select
  284. local function colorselect(value, T)
  285.   if value.status then
  286.     if value.label == "B" then
  287.       if value[T].on == nil then
  288.         return buttonSetting[T].on
  289.       else
  290.         return value[T].on
  291.       end
  292.     elseif value.label == "L" then
  293.       if value[T].on == nil then
  294.         return buttonSetting[T].non
  295.       else
  296.         return value[T].on
  297.       end
  298.     end  
  299.   else
  300.     if value.label == "B" then
  301.       if value[T].off == nil then
  302.         return buttonSetting[T].off
  303.       else
  304.         return value[T].off
  305.       end
  306.     elseif value.label == "L" then
  307.       if value[T].off == nil then
  308.         return buttonSetting[T].non
  309.       else
  310.         return value[T].off
  311.       end
  312.     end
  313.   end
  314. end
  315. -- display code
  316. function display ()
  317.   mon.setBackgroundColor(buttonSetting.back.non)
  318.   mon.setTextColor(buttonSetting.text.non)
  319.   mon.clear()
  320.   for key, value in pairs(buttonData) do
  321.     if value.display then
  322.       ys = fy(value.y.min,value.y.max)
  323.       xs = fx(value.x.min,value.x.max,value.text.show)
  324.       mon.setBackgroundColor(colorselect(value, "back"))
  325.       mon.setTextColor(colorselect(value, "text"))
  326.       for y = value.y.min, value.y.max, 1 do
  327.         mon.setCursorPos(value.x.min, y)
  328.         if y == ys then
  329.           for x = 0, (value.x.max-value.x.min-string.len(value.text.show)+1), 1 do
  330.             if x == xs then
  331.               mon.write(value.text.show)
  332.             else
  333.               mon.write(" ")
  334.             end
  335.           end
  336.         else
  337.           for x = 0, (value.x.max-value.x.min), 1 do
  338.             mon.write(" ")
  339.           end
  340.         end
  341.       end
  342.     end
  343.   end
  344. end
  345. -- check if button is clicked
  346. function check(x,y)
  347.   for key, value in pairs(buttonData) do
  348.     if value.y.min <= y and y <= value.y.max then
  349.       if value.x.min <= x and x <= value.x.max then
  350.         if ((value.hidden and value.click) or (value.display and value.click)) and buttonFunc[key].func ~= nil then
  351.           if buttonFunc[key].argpass ~= nil then
  352.             buttonFunc[key].func(buttonFunc[key].argpass)
  353.           else
  354.             buttonFunc[key].func()
  355.           end
  356.         end
  357.       end
  358.     end
  359.   end
  360. end
  361. -- cycle command (not recommended but works)
  362. function cycle ()
  363.   local cychk = {}
  364.   while true do
  365.     display()
  366.     cychk["event"],cychk["side"],cychk["x"],cychk["y"] = os.pullEvent("monitor_touch")
  367.     check(cychk["x"],cychk["y"])
  368.   end
  369. end
Advertisement
Add Comment
Please, Sign In to add comment