Advertisement
Patriik

Untitled

Feb 27th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. local gpu = require("component").gpu
  2. local event = require("event")
  3.  
  4. local objects = {}
  5.  
  6. local function object.new(x, y, w, h,visible,shouldAutoUpdate)
  7. local obj = {
  8. type = "object",
  9. x = x,
  10. y = y,
  11. w = w,
  12. h = h,
  13. visible = visible,
  14. shouldAutoUpdate = shouldAutoUpdate,
  15. update = function() obj.drawNextFrame = true end,
  16. _drawNextFrame = false
  17. }
  18.  
  19. return obj
  20. end
  21.  
  22. local function button(x,y,w,h,label,color,textColor,shouldAutoUpdate,callback)
  23. local obj = object.new(x,y,w,h,true,shouldAutoUpdate)
  24. obj.label = label
  25. obj.color = color
  26. obj.textColor = textColor
  27. obj.callback = callback
  28.  
  29. table.insert(objects,obj)
  30. return #objects
  31. end
  32.  
  33. local function frame(x,y,w,h,color,textColor,text,shouldAutoUpdate)
  34. local obj = object.new(x,y,w,h,true,shouldAutoUpdate)
  35. obj.type = "frame"
  36. obj.color = color
  37. obj.textColor = textColor
  38. obj.text = text
  39. table.insert(objects, obj)
  40. return #objects
  41. end
  42.  
  43. local function draw(bg)
  44. bg = bg or 0x333333
  45. gpu.setBackground(bg)
  46. gpu.setForeground(0xFFFFFF)
  47. gpu.fill(1,1,resx,resy," ")
  48. for i,v in ipairs(objects) do
  49. local object = v
  50.  
  51. if object.shouldAutoUpdate or object._drawNextFrame then
  52. object._drawNextFrame = false
  53. if object.type == "button" and object.visible == true then
  54. gpu.setBackground(object.color)
  55. gpu.setForeground(object.textColor)
  56. gpu.fill(object.x,object.y,object.w,object.h," ")
  57. gpu.set(object.x+math.floor(object.w/2)-math.floor(#object.label/2),object.y+math.floor(object.h/2),object.label)
  58. elseif object.type == "frame" and object.visible == true then
  59. gpu.setBackground(object.color)
  60. gpu.setForeground(object.textColor)
  61. gpu.fill(object.x,object.y,object.w,object.h," ")
  62. gpu.set(object.x+math.floor(object.w/2)-math.floor(#object.text/2),object.y+math.floor(object.h/2),object.text)
  63. end
  64. end
  65. end
  66. end
  67.  
  68. event.listen("touch",function(_,_,x,y)
  69. for i,v in ipairs(objects) do
  70. if v.type == "button" then
  71. if x>=v.x and x<=v.x+v.w-1 and y>=v.y and y<=v.y+v.h-1 then
  72. v.callback()
  73. end
  74. end
  75. end
  76. end)
  77.  
  78. while true do
  79. os.sleep(0.2)
  80.  
  81. draw()
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement