Advertisement
Guest User

button

a guest
Dec 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. local buttons = {}
  2. local buttonProperties = {"x", "y", "width", "height", "text", "textColor", "color", "click"}
  3.  
  4. function newButton(_x, _y, _w, _h, _t, _tc, _c, _cl)
  5.   local args = {_x, _y, _w, _h, _t, _tc, _c, _cl,}
  6.   buttons[#buttons+1] = {}
  7.   for _, key in pairs(buttonProperties) do
  8.     buttons[#buttons][key] = args[_]
  9.   end
  10.   buttons[#buttons].enabled = true
  11.   local _buttonObj = {
  12.     id = #buttons,
  13.     set = function(self, key, value)
  14.       buttons[self.id][key] = value
  15.     end,
  16.     get = function(self, key)
  17.       return buttons[self.id][key]
  18.     end
  19.   }
  20.   return _buttonObj
  21. end
  22.  
  23. function drawButtons(monitor)
  24.   if not monitor == "t" then
  25.     term.redirect(monitor)
  26.   end
  27.   for _, button in pairs(buttons) do
  28.     if button.enabled then
  29.       paintutils.drawFilledBox(button.x, button.y, (button.x + button.width - 1), (button.y + button.height - 1), button.color)
  30.       term.setCursorPos(math.floor(button.x + (button.width / 2) - (#button.text / 2)),math.floor(button.y + (button.height / 2)))
  31.       term.setTextColor(button.textColor)
  32.       term.setBackgroundColor(button.color)
  33.       term.write(button.text)
  34.       --term.blit(button.text, button.textColor, button.color)
  35.     end
  36.   end
  37.   if not monitor == "t" then
  38.     term.native()
  39.   end
  40. end
  41.  
  42. function drawDummyButtons(monitor)
  43.   if not monitor == "t" then
  44.     term.redirect(monitor)
  45.   end
  46.   for _, button in pairs(buttons) do
  47.     if button.enabled then
  48.       term.setCursorPos(button.x, button.y)
  49.       term.blit(button.text, button.textColor, button.color)
  50.     end
  51.   end
  52. end
  53.  
  54. function checkDummy(_x, _y)
  55.   for _, button in pairs(buttons) do
  56.     if button.enabled then
  57.       if _y == button.y then
  58.         if _x >= button.x and _x <= (button.x + #button.text) then
  59.           return button.click
  60.         end
  61.       end
  62.     end
  63.   end
  64. end
  65.  
  66. function checkButtons(_x, _y)
  67.   for _, button in pairs(buttons) do
  68.     if button.enabled then
  69.       if _x >= button.x and _x <= (button.x + button.width) then
  70.         if _y >= button.y and _y <= (button.y + button.height) then
  71.           return button.click
  72.         end
  73.       end
  74.     end
  75.   end
  76.   return nil
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement