Advertisement
Telinc1

button

Mar 3rd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. --- Button API for ComputerCraft.
  2. -- @author direwolf20, refactored by Telinc1.
  3.  
  4. local monitor = peripheral.find("monitor")
  5. monitor.setTextScale(1)
  6. monitor.setTextColor(colors.white)
  7. monitor.setBackgroundColor(colors.black)
  8.  
  9. local buttons = {}
  10.  
  11. --- Add a button to the button table.
  12. -- @param name The name of the button (also used as its label).
  13. -- @param callback The function to call on clicking the button.
  14. -- @param x The X position of the button.
  15. -- @param y The Y position of the button.
  16. -- @param width The width of the button.
  17. -- @param height The height of the button.
  18. function addButton(name, callback, x, y, width, height)
  19.     buttons[name] = {}
  20.     buttons[name]["callback"] = callback
  21.     buttons[name]["active"] = false
  22.     buttons[name]["left"] = x
  23.     buttons[name]["top"] = y
  24.     buttons[name]["right"] = x + width
  25.     buttons[name]["bottom"] = y + height
  26. end
  27.  
  28. --- Remove a button from the button table.
  29. -- @param name The name of the button to remove.
  30. function removeButton(name)
  31.     buttons[name] = nil
  32. end
  33.  
  34. --- Remove all buttons from the table and clear the monitor.
  35. function clearButtons()
  36.     buttons = {}
  37.     monitor.clear()
  38. end
  39.  
  40. --- Draw a piece of button-styled text with the specified background color.
  41. -- @param text The label of the button.
  42. -- @param color The color of the button.
  43. -- @param data The button's data, used for its positioning.
  44. function draw(text, color, data)
  45.     monitor.setBackgroundColor(color)
  46.  
  47.     local textX = math.floor((data["right"] - data["left"] - string.len(text)) / 2) + 1
  48.     local textY = math.floor((data["top"] + data["bottom"]) / 2)
  49.  
  50.     for j = data["top"], data["bottom"] do
  51.         monitor.setCursorPos(data["left"], j)
  52.  
  53.         if j == textY then
  54.             for k = 0, data["right"] - data["left"] - string.len(text) + 1 do
  55.                 if k == textX then
  56.                     monitor.write(text)
  57.                 else
  58.                     monitor.write(" ")
  59.                 end
  60.             end
  61.         else
  62.             for i = data["left"], data["right"] do
  63.                 monitor.write(" ")
  64.             end
  65.         end
  66.     end
  67.  
  68.     monitor.setBackgroundColor(colors.black)
  69. end
  70.  
  71. --- Draw all the buttons to the monitor.
  72. function refresh()
  73.     local currentColor
  74.  
  75.     for name, data in pairs(buttons) do
  76.         if data["active"] == true then currentColor = colors.lime else currentColor = colors.red end
  77.         draw(name, currentColor, data)
  78.     end
  79. end
  80.  
  81. --- Toggle a button's active state and refresh the screen.
  82. -- @param name The button to toggle.
  83. function toggle(name)
  84.    button[name]["active"] = not buttons[name]["active"]
  85.    refresh()
  86. end
  87.  
  88. --- Flip a button's active state for 0.15 seconds.
  89. -- @param name The button to flash.
  90. function flash(name)
  91.    toggle(name)
  92.    sleep(0.15)
  93.    toggle(name)
  94. end
  95.  
  96. --- Check (x, y) for a button and execute its callback.
  97. -- @param x The X coordinate of the point.
  98. -- @param y The Y coordinate of the point.
  99. function checkXY(x, y)
  100.     for name, data in pairs(buttons) do
  101.         if y >= data["top"] and y <= data["bottom"] then
  102.             if x >= data["left"] and x <= data["right"] then
  103.                 data["callback"]()
  104.                 return true
  105.             end
  106.         end
  107.     end
  108.  
  109.     return false
  110. end
  111.  
  112. --- Draw a centered heading to the monitor.
  113. -- @param text The heading to draw.
  114. function heading(text)
  115.    width, height = monitor.getSize()
  116.  
  117.    monitor.setCursorPos((width - string.len(text)) / 2 + 1, 1)
  118.    monitor.write(text)
  119. end
  120.  
  121. --- Draw a label to the monitor.
  122. -- @param x The X position of the label.
  123. -- @param y The Y position of the label.
  124. -- @param text The label to draw.
  125. function label(x, y, text)
  126.    monitor.setCursorPos(x, y)
  127.    monitor.write(text)
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement