Module Utils
Different utility functions
Functions
| utils.round (x) | Round function. |
| utils.inside (x0, y0, x, y, w, h) | Tells whether or not a given point lies inside an axis-aligned rectangle. |
| utils.FSM (t) | Creates a finite state machine. |
Functions
- utils.round (x)
-
Round function.
Parameters:
- x A number.
Returns:
math.floor(x + 0.5) - utils.inside (x0, y0, x, y, w, h)
-
Tells whether or not a given point lies inside an axis-aligned rectangle.
Parameters:
- x0 The point's x-coordinate.
- y0 The point's y-coordinate.
- x x-coordinate of the top-left corner of the rectangle.
- y y-coordinate of the top-left corner of the rectangle.
- w Width of the rectangle.
- h Height of the rectangle.
Returns:
-
Whether the point
(x0, y0)lies inside the rectangle(x, y, w, h). - utils.FSM (t)
-
Creates a finite state machine.
Parameters:
- t
A table containing events, states and transitions.
events: an array of event IDs. Technically can be of any type that is allowed for a table key, but it is more convenient to use strings.states: an array of state IDs. Technically can be of any type that is allowed for a table key, but it is more convenient to use strings.transitions: a table of the following form.transitions = { eventID = { stateID_from = { state = stateID_to, action = f }, stateID_from = { state = stateID_to, action = f }, ... }, eventID = { stateID_from = { state = stateID_to, action = f }, }, ... initial = stateID, }where
eventIDdenotes the event that causes the transition,stateID_fromandstateID_tois the current and the next state respectively, andactionis a function that is called each time this transition is active. Also it must contain the initial state under theinitialkey. Events are processed via theprocessmethod:fsm:process(event). Below you can see an FSM which is used for managing mouse events in this library (which diagram can be seen in the Block:new section):
Usage:
local m = self.mouse utils.FSM { events = { 'press', 'inside', 'outside', 'release', }, states = { 'idle', 'hovered', 'pressed' }, transitions = { ['press'] = { ['hovered'] = { state = 'pressed', action = f(m.onPress) } }, ['inside'] = { ['idle'] = { state = 'hovered', action = f(m.onEnter) } }, ['outside'] = { ['pressed'] = { state = 'idle', action = f(m.onExit) }, ['hovered'] = { state = 'idle', action = f(m.onExit) }, }, ['release'] = { ['pressed'] = { state = 'hovered', action = f(m.onClick) } }, }, initial = 'idle' }
- t