Advertisement
LBPHacker

The most simple Buttons API

Mar 17th, 2013
1,026
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 1 0
  1. --[[ * Buttons API by LBPHacker, 2013
  2.      * You can use it whenever and whereever you want,
  3.        but give credit! (at least something like 'LBP')
  4. ]]
  5.  
  6. local container = {}
  7. local termw, termh = term.getSize()
  8. local defaultColorT = colors.white
  9. local defaultColorB = colors.black
  10.  
  11. local forcedEvent
  12. local getClickEvent = function()
  13.     local clickEvent
  14.     if forcedEvent then
  15.         clickEvent = forcedEvent
  16.     else
  17.         --[[ * Below is a pretty way to determine wheter
  18.                the terminal is redirected to a monitor or not.
  19.              * As you might know, .setTextScale can be found
  20.                in monitors only.
  21.         ]]
  22.         if term.setTextScale then
  23.             clickEvent = "monitor_touch"
  24.         else
  25.             clickEvent = "mouse_click"
  26.         end
  27.     end
  28.     return clickEvent
  29. end
  30.  
  31. register = function(xorg, yorg, width, height, textColor, backgroundColor, text, func)
  32.     local newButtonID = false
  33.     for i = 1, #container do if not container[i].alive and not newButtonID then newButtonID = i end end
  34.     if not newButtonID then
  35.         newButtonID = #container + 1
  36.         container[newButtonID] = {}
  37.     end
  38.     container[newButtonID].alive = true
  39.     container[newButtonID].xorg = xorg
  40.     container[newButtonID].yorg = yorg
  41.     container[newButtonID].width = width
  42.     container[newButtonID].height = height
  43.     container[newButtonID].textColor = textColor
  44.     container[newButtonID].backgroundColor = backgroundColor
  45.     container[newButtonID].text = text
  46.     container[newButtonID].func = func
  47.     container[newButtonID].enabled = true
  48.     container[newButtonID].visible = true
  49.     return newButtonID
  50. end
  51.  
  52. exists = function(buttonID)
  53.     if container[buttonID] then
  54.         return container[buttonID].alive
  55.     else
  56.         return false
  57.     end
  58. end
  59.  
  60. unregister = function(buttonID)
  61.     if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
  62.     container[buttonID].alive = false
  63. end
  64.  
  65. event = function(eventArray)
  66.     if eventArray[1] == getClickEvent() then
  67.         local button, mousex, mousey = eventArray[2], eventArray[3], eventArray[4]
  68.         for i = 1, #container do
  69.             if container[i].alive then
  70.                 if container[i].enabled and mousex > container[i].xorg - 1 and mousey > container[i].yorg - 1 and mousex < container[i].xorg + container[i].width and mousey < container[i].yorg + container[i].height then container[i].func() end
  71.             end
  72.         end
  73.     end
  74. end
  75.  
  76. draw = function()
  77.     term.setBackgroundColor(defaultColorB)
  78.     term.clear()
  79.     for i = 1, #container do
  80.         if container[i].alive and container[i].visible then
  81.             for j = container[i].yorg, container[i].yorg + container[i].height - 1 do
  82.                 term.setCursorPos(container[i].xorg, j)
  83.                 term.setTextColor(container[i].textColor)
  84.                 term.setBackgroundColor(container[i].backgroundColor)
  85.                 term.write(string.rep(" ", container[i].width))
  86.             end
  87.             term.setCursorPos(container[i].xorg + math.floor((container[i].width - #container[i].text) / 2), container[i].yorg + math.floor((container[i].height - 1) / 2))
  88.             term.write(container[i].text)
  89.         end
  90.     end
  91.     term.setTextColor(defaultColorT)
  92.     term.setBackgroundColor(defaultColorB)
  93. end
  94.  
  95. enable = function(buttonID, bEnabled)
  96.     if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
  97.     container[buttonID].enabled = bEnabled
  98. end
  99.  
  100. show = function(buttonID, bVisible)
  101.     if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
  102.     container[buttonID].visible = bVisible
  103. end
  104.  
  105. setPosition = function(buttonID, xorg, yorg, width, height)
  106.     if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
  107.     container[buttonID].xorg = xorg
  108.     container[buttonID].yorg = yorg
  109.     container[buttonID].width = width
  110.     container[buttonID].height = height
  111. end
  112.  
  113. setText = function(buttonID, sText)
  114.     if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
  115.     container[buttonID].text = sText
  116. end
  117.  
  118. setForcedEvent = function(fEvent)
  119.     forcedEvent = fEvent
  120. end
  121.  
  122. setColor = function(buttonID, colorT, colorB)
  123.     if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
  124.     container[buttonID].textColor = colorT
  125.     container[buttonID].backgroundColor = colorB
  126. end
  127.  
  128. setDefaultColor = function(buttonID, colorT, colorB)
  129.     defaultColorT = colorT
  130.     defaultColorB = colorB
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement