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.clearArray (t) | Clears the array part of the table. |
| utils.clear (t) | Clears the table. |
| utils.textAtWidth (text, width, font) | Returns the maximum substring (prefix) of a string which width is less than or equal to a given width. |
| utils.elide (text, width, font) | Elides the text and appends an ellipsis at the end if elided. |
| utils.placeAt (block, x, y, w, h, s) | Places the block in a given enclosing cell with some default algorithm. |
| 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.clearArray (t)
-
Clears the array part of the table.
Parameters:
- t A table.
- utils.clear (t)
-
Clears the table.
Parameters:
- t A table.
- utils.textAtWidth (text, width, font)
-
Returns the maximum substring (prefix) of a string which width
is less than or equal to a given width.
NOTE: this function will work only for fonts with monotonically
increasing width once the length of the string increases. In other words,
for any non-empty strings
S1,S2:width(S1 + S2) > width(S1).Parameters:
- text A string. The original text to elide.
- width A non-negative number. The width to fit the text.
- font A font. The target font.
Returns:
-
A string. The maximum prefix of
textwhich width (according to the fontfont) is less or equal towidth. Iftextis an empty string returns an empty string. Ifwidthis 0 returns an empty string. - utils.elide (text, width, font)
-
Elides the text and appends an ellipsis at the end if elided.
Parameters:
- text a string. The original text to elide.
- width a non-negative number. The width to fit the text.
- font a font. The target font.
See also:
Usage:
local t = 'Something went wrong' -- suppose this text is 10 units wide in out font t = utils.elide(t, 4, font) print(t) --> Somet...
- utils.placeAt (block, x, y, w, h, s)
-
Places the block in a given enclosing cell with some default algorithm.
It is unlikely you need this function ever -- it is used internally, but
exposed for the sake of reusability.
Parameters:
- block a block. The block to place.
- x x coordinate of the enclosing cell.
- y y coordinate of the enclosing cell.
- w width of the enclosing cell.
- h height of the enclosing cell.
- s
size of the block which must be used during placement. If
nilthen Block:size is used.
See also:
- 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