Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local SGL = require ("SGL")
- ------------------------------
- local ButtonLib = {}
- ------------------------------
- function ButtonLib.NewButton (x, y, size_x, size_y, color, onClick, drag_react)
- drag_react = drag_react or false
- local button = {x = x, y = y, size_x = size_x, size_y = size_y, color = color, defEventProc = ButtonLib.DefaultEventProc, onClick = onClick, dragreact = dragreact}
- return button
- end
- ------------------------------
- function ButtonLib.AddButton (buttons, button)
- buttons[#buttons + 1] = button
- end
- ------------------------------
- function ButtonLib.OnMouseTest (button, mouse_x, mouse_y)
- 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
- end
- ------------------------------
- function ButtonLib.DrawButton (button)
- if button.onDraw then
- button.onDraw ()
- else
- SGL.Draw.Rect (button.x, button.y, button.size_x, button.size_y, button.color)
- end
- end
- ------------------------------
- function ButtonLib.OnTouch (button, event)
- local _, _, mouse_x, mouse_y = table.unpack (event)
- if ButtonLib.OnMouseTest (button, mouse_x, mouse_y) then return ButtonLib.OnAction (button) end
- end
- ------------------------------
- function ButtonLib.OnDrag (button, event)
- if not button.dragreact then return false end
- return ButtonLib.OnTouch (button, event)
- end
- ------------------------------
- function ButtonLib.OnAction (button)
- if button.onClick then return button.onClick (button) end
- end
- ------------------------------
- function ButtonLib.DefaultEventProc (button, event)
- local event_type = event[1]
- if event_type == "touch" then if ButtonLib.OnTouch (button, event) then return true end
- elseif event_type == "drag" then if ButtonLib.OnDrag (button, event) then return true end
- end
- end
- ------------------------------
- function ButtonLib.ProcessButton (button, event)
- if button.onEvent then if button.onEvent (button, event) then return true end end
- if button.defEventProc (button, event) then
- return true
- end
- return false
- end
- ------------------------------
- function ButtonLib.DrawButtons (buttons)
- for index, button in pairs (buttons) do
- ButtonLib.DrawButton (button)
- end
- end
- ------------------------------
- function ButtonLib.ProcessButtons (buttons, event)
- if not event or not event[1] then return false end
- for index, button in pairs (buttons) do
- if ButtonLib.ProcessButton (button, event) then return true end
- end
- return false
- end
- ------------------------------
- return ButtonLib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement