This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.lua
More file actions
37 lines (36 loc) · 1.4 KB
/
utils.lua
File metadata and controls
37 lines (36 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---@module 'lib.gui'
local gui = require "lib.gui"
---@module 'lib.safety'
local safety = require "lib.safety"
---@module 'lib.wait'
local wait = require "lib.wait"
---@module 'lib.state'
local state = require "lib.state"
local utils = {}
---A skeleton for creating repetative switch functions
---@param button Button any class which inherits from Button
---@param func function|nil an option function to be ran right before the state switches
---@param state_container {state : table}
function utils.onClickSwitch(button, func, state_container)
safety.ensureInstanceType(button, gui.Base, "button")
safety.ensureTable(state_container, "state_container")
if (not (func == nil)) then -- nilable variable
safety.ensureFunction(func, "func")
end
safety.ensureTable(state_container, "state_container.state")
if (not button.initButton) then -- Check that base is a button
error("Button must be a button")
end
button:onClick(function(pt, button_id, presses)
if (button:contains(pt) and button_id == 1) then -- if button 1 clicked within button's bounds
wait(0.05, -- Wait to prevent double clicking and rubberband hell
function()
if (func) then -- Allow user to call code before the switch, perhaps to reload the state
func(state_container)
end
state.switch(state_container.state) -- switch to new state
end)
end
end)
end
return utils -- Return utils table