Guest User

button

a guest
Sep 22nd, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. mon = peripheral.wrap("left")
  2.  
  3. mainBackground = colors.black
  4. mainText = colors.white
  5.  
  6. buttons = {}
  7.  
  8. local xs,ys = mon.getSize()
  9.  
  10. function define(lbl,txt,x,y,text,bg,border)
  11.     if type(lbl) == "table" then
  12.         error("a button cannot be labeled with a table")
  13.     elseif type(txt) ~= "string" then
  14.         error("function  expects a string to tell it what text to display as its second argument")
  15.     elseif type(x) ~= "number" then
  16.         error("function define expects an integer to determine the x coordinate of the button as its third argument")
  17.     elseif type(y) ~= "number" then
  18.         error("function define expects an integer to determine the y coordinate of the button as its forth argument")
  19.     elseif type(text) ~= "number" then
  20.         error("function define expects an integer or colors/colours API value to determine the color of the text as its fifth argument")
  21.     elseif type(bg) ~= "number" then
  22.         error("function define expects an integer or colors/colours API value to determine the background color of the button as its sixth argument")
  23.     elseif type(border) ~= "boolean" then
  24.         error("function define expects a boolean value to determine wether the button has a border as its seventh argument")
  25.     elseif lbl == "all" then
  26.         error("all cannot be used as the label for a button because it is used in other functions")
  27.     elseif lbl == "timed out" then
  28.         error("timed out cannot be used as the label for a button because it is used in other functions")
  29.     elseif x < 1 or y < 1 then
  30.         error("the x and y coordinates must be positive")
  31.     elseif x > xs + 1 or y > ys + 1 then
  32.         error("the button must be on the screen")
  33.     end
  34.     buttons[lbl] = {txt,math.floor(x),math.floor(y),false,lbl,text,bg,border}
  35. end
  36.  
  37. function bg(x,y,xSize,ySize,color)
  38.     mon.setBackgroundColor(color)
  39.     for i=y,y + ySize - 1 do
  40.         mon.setCursorPos(x,i)
  41.         for j=1,xSize do
  42.             mon.write(" ")
  43.         end
  44.     end
  45. end
  46.  
  47. function drawButton(button)
  48.     buttons[button][4] = true
  49.     mon.setBackgroundColor(buttons[button][7])
  50.     mon.setTextColor(buttons[button][6])
  51.     if buttons[button][8] then
  52.         bg(buttons[button][2],buttons[button][3],string.len(buttons[button][1]) + 2,3,buttons[button][7])
  53.         mon.setCursorPos(buttons[button][2] + 1,buttons[button][3] + 1)
  54.         mon.write(buttons[button][1])
  55.     else
  56.         mon.setCursorPos(buttons[button][2],buttons[button][3])
  57.         mon.write(buttons[button][1])
  58.     end
  59. end
  60.  
  61. function eraseButton(button)
  62.     buttons[button][4] = false
  63.     mon.setBackgroundColor(mainBackground)
  64.     mon.setTextColor(mainText)
  65.     if buttons[button][8] then
  66.         for j=0,2 do
  67.             mon.setCursorPos(buttons[button][2],buttons[button][3] + j)
  68.             for i=1,string.len(buttons[button][1]) + 2 do
  69.                 mon.write(" ")
  70.             end
  71.         end
  72.     else
  73.         for i=1,string.len(buttons[button][1]) do
  74.             mon.write(" ")
  75.         end
  76.     end
  77. end
  78.  
  79. function draw(button)
  80.     if button == "all" then
  81.         for b in pairs(buttons) do
  82.             drawButton(b)
  83.         end
  84.     elseif type(button) == "table" then
  85.         for k,b in ipairs(button) do
  86.             drawButton(b)
  87.         end
  88.     else
  89.         drawButton(button)
  90.     end
  91. end
  92.  
  93. function erase(button)
  94.     if button == "all" then
  95.         for b in pairs(buttons) do
  96.             eraseButton(b)
  97.         end
  98.     elseif type(button) == "table" then
  99.         for k,b in ipairs(button) do
  100.             eraseButton(b)
  101.         end
  102.     else
  103.         eraseButton(button)
  104.     end
  105. end
  106.  
  107. function default()
  108.     mon.setBackgroundColor(mainBackground)
  109.     mon.setTextColor(mainText)
  110.     erase("all")
  111.     mon.clear()
  112.     mon.setCursorPos(1,1)
  113. end
  114.  
  115. function click(timeout)
  116.     local t = type(timeout)
  117.     if t ~= "number" and t ~= "nil" then
  118.         error("function getClick expects a number to determine the time before timing out or a blank/nil to represent no time limit")
  119.     end
  120.     if t == "number" then
  121.         timeo = os.startTimer(timeout)
  122.     end
  123.     while true do
  124.         local event,id,xPos,yPos = os.pullEvent()
  125.         if event == "timer" and id == timeo then
  126.             return "timed out"
  127.         elseif event == "monitor_touch" then
  128.             for button in pairs(buttons) do
  129.                 if buttons[button][8] and xPos >= buttons[button][2] and xPos <= buttons[button][2] + string.len(buttons[button][1]) + 1 and yPos >= buttons[button][3] and yPos <= buttons[button][3] + 2 and buttons[button][4] then
  130.                     return button
  131.                 elseif (not buttons[button][8]) and xPos >= buttons[button][2] and xPos <= buttons[button][2] + string.len(buttons[button][1]) - 1 and yPos == buttons[button][3] and buttons[button][4] then
  132.                     return button
  133.                 end
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139. function deactivate(button)
  140.     if button == "all" then
  141.         for b in pairs(buttons) do
  142.             buttons[b][4] = false
  143.         end
  144.     elseif type(button) == "table" then
  145.         for k,b in ipairs(button) do
  146.             buttons[b][4] = false
  147.         end
  148.     else
  149.         buttons[button][4] = false
  150.     end
  151. end
  152.  
  153. function delete(button)
  154.     if button == "all" then
  155.         for b in pairs(buttons) do
  156.             erase(b)
  157.         end
  158.         buttons = {}
  159.     elseif type(button) == "table" then
  160.         for k,b in ipairs(button) do
  161.             erase(b)
  162.             table.remove(buttons,b)
  163.         end
  164.     else
  165.         erase(button)
  166.         table.remove(buttons,button)
  167.     end
  168. end
  169.  
  170. function relabel(old,new)
  171.     button[new] = button[old]
  172.     delete(old)
  173. end
  174.  
  175. function text(button,txt)
  176.     buttons[button][1] = txt
  177. end
  178.  
  179. function move(button,x,y)
  180.     buttons[button][2] = math.floor(x)
  181.     buttons[button][3] = math.floor(y)
  182. end
  183.  
  184. function activate(button)
  185.     if button == "all" then
  186.         for b in pairs(buttons) do
  187.             buttons[b][4] = true
  188.         end
  189.     elseif type(button) == "table" then
  190.         for k,b in ipairs(button) do
  191.             buttons[b][4] = true
  192.         end
  193.     else
  194.         buttons[button][4] = true
  195.     end
  196. end
  197.  
  198. print("Button API (by minecraftwarlock) loaded")
Advertisement
Add Comment
Please, Sign In to add comment