Advertisement
PayZuni

button

May 11th, 2021
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.49 KB | None | 0 0
  1. local API = {}
  2. local button = {}
  3.  
  4. local component = require("component")
  5. local gpu = component.gpu
  6.  
  7. function draw(x, y, width, height, colorB, colorF, text)
  8.     local oldBackground = gpu.setBackground(colorB)
  9.     local textLen = string.len(text)
  10.     if(textLen > (width - 2)) then
  11.         text = string.sub(text , 1, width)
  12.     end
  13.     gpu.fill(x, y, width, height, " ")
  14.     local oldForeground = gpu.setForeground(colorF)
  15.     gpu.set(x + math.floor(width / 2) - (textLen / 2), y + math.floor(height / 2), text)
  16.     gpu.setBackground(oldBackground)
  17.     gpu.setForeground(oldForeground)
  18. end
  19.  
  20. function toggleColor(buttonName)
  21.     local foreground
  22.     local background
  23.     if button[buttonName]["state"] then
  24.         foreground = button[buttonName]["textC1"]
  25.         background = button[buttonName]["color1"]
  26.     else
  27.         foreground = button[buttonName]["textC2"]
  28.         background = button[buttonName]["color2"]
  29.     end
  30.    
  31.     draw(button[buttonName]["x"], button[buttonName]["y"], button[buttonName]["width"], button[buttonName]["height"], background, foreground, buttonName)
  32. end
  33.  
  34. function API.add(name, callback, x, y, width, height, color1, color2, textC1, textC2, toggle, state)
  35.     button[name] = {}
  36.     button[name]["callback"] = callback
  37.     button[name]["x"] = x
  38.     button[name]["y"] = y
  39.     button[name]["width"] = width
  40.     button[name]["height"] = height
  41.     button[name]["color1"] = color1
  42.     button[name]["color2"] = color2
  43.     button[name]["textC1"] = textC1
  44.     button[name]["textC2"] = textC2
  45.     button[name]["active"] = state
  46.     button[name]["toggle"] = toggle
  47.  
  48.     draw(x, y, width, height, color1, textC1, name);
  49. end
  50.  
  51. function API.getButtonState(name)
  52.     return button[name]["state"]
  53. end
  54.  
  55. -- Check for any button presses
  56. function API.touch(x, y)
  57.     for name, data in pairs(button) do
  58.         local btnX = button[name]["x"]
  59.         local btnY = button[name]["y"]
  60.         local btnWidth = button[name]["width"]
  61.         local btnHeight = button[name]["height"]
  62.  
  63.         if x < btnX then goto continue end
  64.         if y < btnY then goto continue end
  65.         if x > (btnX + btnWidth) then goto continue end
  66.         if y > (btnY + btnHeight) then goto continue end
  67.  
  68.         if button[name]["callback"](name) then
  69.             if button[name]["toggle"] then
  70.                 toggleColor(name)
  71.                 button[name]["state"] = not button[name]["state"];
  72.             end
  73.         end
  74.         break;
  75.         ::continue::
  76.     end
  77. end
  78.  
  79. return API;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement