Advertisement
Guest User

button

a guest
Sep 16th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. --wrap the monitor as a peripheral
  2. mon = peripheral.wrap("left")
  3.  
  4. mainBackground = colors.black
  5. mainText = colors.white
  6.  
  7. --table that stores the buttons
  8. buttons = {}
  9.  
  10. --define a new button
  11. function newButton(lbl,txt,x,y,text,bg,border)--newButton(string,string,integer,integer,colors api,colors api,boolean)
  12.     if type(lbl) ~= "string" then
  13.         error("function newButton expects a string to label the button as its first argument")
  14.     elseif type(txt) ~= "string" then
  15.         error("function newButton expects a string to tell it what text to display as its second argument")
  16.     elseif type(x) ~= "number" then
  17.         error("function newButton expects an integer to determine the x coordinate of the button as its third argument")
  18.     elseif type(y) ~= "number" then
  19.         error("function newButton expects an integer to determine the y coordinate of the button as its forth argument")
  20.     elseif type(text) ~= "number" then
  21.         error("function newButton expects an integer or colors/colours API value to determine the color of the text as its fifth argument")
  22.     elseif type(bg) ~= "number" then
  23.         error("function newButton expects an integer or colors/colours API value to determine the background color of the button as its sixth argument")
  24.     elseif type(border) ~= "boolean" then
  25.         error("function newButton expects a boolean value to determine wether the button has a border as its seventh argument")
  26.     elseif lbl == "all" then
  27.         error("all cannot be used as the label for a button because it is used in other functions")
  28.     elseif lbl == "timed out" then
  29.         error("timed out cannot be used as the label for a button because it is used in other functions")
  30.     end
  31.     buttons[lbl] = {txt,x,y,false,lbl,text,bg,border}
  32. end
  33.  
  34. --internal function to draw a button
  35. local function draw(button)
  36.     buttons[button][4] = true
  37.     mon.setBackgroundColor(buttons[button][7])
  38.     mon.setTextColor(buttons[button][6])
  39.     mon.setCursorPos(buttons[button][2],buttons[button][3] + 1)
  40.     mon.write(" "..buttons[button][1].." ")
  41.    
  42.     if buttons[button][8] then
  43.         mon.setCursorPos(buttons[button][2],buttons[button][3])
  44.        
  45.         for i=1,string.len(buttons[button][1]) + 2 do
  46.             mon.write(" ")
  47.         end
  48.        
  49.         mon.setCursorPos(buttons[button][2],buttons[button][3] + 2)
  50.        
  51.         for i=1,string.len(buttons[button][1]) + 2 do
  52.             mon.write(" ")
  53.         end
  54.     end
  55. end
  56.  
  57. --internal function to erase a button
  58. local function erase(button)
  59.     buttons[button][4] = false
  60.     mon.setBackgroundColor(mainBackground)
  61.     mon.setTextColor(mainText)
  62.    
  63.     mon.setCursorPos(buttons[button][2],buttons[button][3] + 1)
  64.    
  65.     for i=1,string.len(buttons[button][1]) + 2 do
  66.         mon.write(" ")
  67.     end
  68.    
  69.     if buttons[button][8] then
  70.         mon.setCursorPos(buttons[button][2],buttons[button][3] + 2)
  71.        
  72.         for i=1,string.len(buttons[button][1]) + 2 do
  73.             mon.write(" ")
  74.         end
  75.            
  76.         mon.setCursorPos(buttons[button][2],buttons[button][3])
  77.        
  78.         for i=1,string.len(buttons[button][1]) + 2 do
  79.             mon.write(" ")
  80.         end
  81.     end
  82. end
  83.  
  84. --draw either a button or all buttons
  85. function drawButton(button)
  86.     if type(button) ~= "string" then
  87.         error("function drawButton expects the string label of the button you would like to draw or the string all to draw all buttons")
  88.     end
  89.     if button == "all" then
  90.         for b in pairs(buttons) do
  91.             draw(b)
  92.         end
  93.     else
  94.         if buttons[button] ~= nil then
  95.             draw(button)
  96.         else
  97.             error("button: "..button.." does not exist")
  98.         end
  99.     end
  100. end
  101.  
  102. --erase either a button or all buttons
  103. function eraseButton(button)
  104.     if type(button) ~= "string" then
  105.         error("function eraseButton expects the string label of the button you would like to erase or the string all to erase all buttons")
  106.     end
  107.     if button == "all" then
  108.         for b in pairs(buttons) do
  109.             erase(b)
  110.         end
  111.     else
  112.         if buttons[button] ~= nil then
  113.             erase(button)
  114.         else
  115.             error("button: "..button.." does not exist")
  116.         end
  117.     end
  118. end
  119.  
  120. --clear the monitor and erase all buttons
  121. function monDefault()
  122.     mon.setBackgroundColor(mainBackground)
  123.     mon.setTextColor(mainText)
  124.     eraseButton("all")
  125.     mon.clear()
  126.     mon.setCursorPos(1,1)
  127. end
  128.  
  129. function getClick(timeout)
  130.     t = type(timeout)
  131.     if t ~= "number" and t ~= "nil" then
  132.         error("function getClick expects a number to determine the time before timing out or a blank/nil to represent no time limit")
  133.     end
  134.     if t == "number" then
  135.         timeo = os.startTimer(timeout)
  136.     end
  137.     while true do
  138.         local event,id,xPos,yPos = os.pullEvent()
  139.         if event == "timer" and id == timeo then
  140.             return "timed out"
  141.         elseif event == "monitor_touch" then
  142.             for button in pairs(buttons) do
  143.                 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
  144.                     return button
  145.                 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
  146.                     return button
  147.                 end
  148.             end
  149.         end
  150.     end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement