Advertisement
HangMan23

Untitled

Jan 30th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. local SGL = require ("SGL")
  2.  
  3. ------------------------------
  4.  
  5. local ButtonLib = {}
  6.  
  7. ------------------------------
  8.  
  9. function ButtonLib.NewButton (x, y, size_x, size_y, color, onClick, drag_react)
  10.  
  11. drag_react = drag_react or false
  12.  
  13. local button = {x = x, y = y, size_x = size_x, size_y = size_y, color = color, defEventProc = ButtonLib.DefaultEventProc, onClick = onClick, dragreact = dragreact}
  14.  
  15. return button
  16.  
  17. end
  18.  
  19. ------------------------------
  20.  
  21. function ButtonLib.AddButton (buttons, button)
  22.  
  23. buttons[#buttons + 1] = button
  24.  
  25. end
  26.  
  27. ------------------------------
  28.  
  29. function ButtonLib.OnMouseTest (button, mouse_x, mouse_y)
  30.  
  31. return mouse_x >= button.x and mouse_x < button.x + button.size_x and mouse_y >= button.y and mouse_y < button.y + button.size_y
  32.  
  33. end
  34.  
  35. ------------------------------
  36.  
  37. function ButtonLib.DrawButton (button)
  38.  
  39. if button.onDraw then
  40.  
  41. button.onDraw ()
  42.  
  43. else
  44.  
  45. SGL.Draw.Rect (button.x, button.y, button.size_x, button.size_y, button.color)
  46.  
  47. end
  48.  
  49. end
  50.  
  51. ------------------------------
  52.  
  53. function ButtonLib.OnTouch (button, event)
  54.  
  55. local _, _, mouse_x, mouse_y = table.unpack (event)
  56.  
  57. if ButtonLib.OnMouseTest (button, mouse_x, mouse_y) then return ButtonLib.OnAction (button) end
  58.  
  59. end
  60.  
  61. ------------------------------
  62.  
  63. function ButtonLib.OnDrag (button, event)
  64.  
  65. if not button.dragreact then return false end
  66.  
  67. return ButtonLib.OnTouch (button, event)
  68.  
  69. end
  70.  
  71. ------------------------------
  72.  
  73. function ButtonLib.OnAction (button)
  74.  
  75. if button.onClick then return button.onClick (button) end
  76.  
  77. end
  78.  
  79. ------------------------------
  80.  
  81. function ButtonLib.DefaultEventProc (button, event)
  82.  
  83. local event_type = event[1]
  84.  
  85. if event_type == "touch" then if ButtonLib.OnTouch (button, event) then return true end
  86. elseif event_type == "drag" then if ButtonLib.OnDrag (button, event) then return true end
  87.  
  88. end
  89.  
  90. end
  91.  
  92. ------------------------------
  93.  
  94. function ButtonLib.ProcessButton (button, event)
  95.  
  96. if button.onEvent then if button.onEvent (button, event) then return true end end
  97.  
  98. if button.defEventProc (button, event) then
  99.  
  100. return true
  101.  
  102. end
  103.  
  104. return false
  105.  
  106. end
  107.  
  108. ------------------------------
  109.  
  110. function ButtonLib.DrawButtons (buttons)
  111.  
  112. for index, button in pairs (buttons) do
  113.  
  114. ButtonLib.DrawButton (button)
  115.  
  116. end
  117.  
  118. end
  119.  
  120. ------------------------------
  121.  
  122. function ButtonLib.ProcessButtons (buttons, event)
  123.  
  124. if not event or not event[1] then return false end
  125.  
  126. for index, button in pairs (buttons) do
  127.  
  128. if ButtonLib.ProcessButton (button, event) then return true end
  129.  
  130. end
  131.  
  132. return false
  133.  
  134. end
  135.  
  136. ------------------------------
  137.  
  138. return ButtonLib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement