Advertisement
Guest User

button

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