Update docs

This commit is contained in:
Insality
2025-03-18 21:23:30 +02:00
parent 5348561d68
commit 1bc916e187
25 changed files with 937 additions and 380 deletions

View File

@@ -2,23 +2,24 @@ local event = require("event.event")
local const = require("druid.const")
local component = require("druid.component")
---The component that handles the back handler action, like backspace or android back button
---@class druid.back_handler: druid.component
---@field on_back event Trigger on back handler action, fun(self, params)
---@field params any|nil Custom args to pass in the callback
local M = component.create("back_handler")
---@param callback function|nil
---@param params any|nil
---@param callback function|nil The callback to call when the back handler is triggered
---@param params any? Custom args to pass in the callback
function M:init(callback, params)
self.params = params
self.on_back = event.create(callback)
end
---@param action_id hash
---@param action table
---@return boolean
---@param action_id hash The action id
---@param action table The action table
---@return boolean is_consumed True if the input was consumed
function M:on_input(action_id, action)
if action.released and (action_id == const.ACTION_BACK or action_id == const.ACTION_BACKSPACE) then
self.on_back:trigger(self:get_context(), self.params)

View File

@@ -7,15 +7,16 @@ local component = require("druid.component")
local M = component.create("blocker")
---@param node node
---@param node node|string The node to use as a blocker
function M:init(node)
self.node = self:get_node(node)
self._is_enabled = gui.is_enabled(self.node, true)
end
---@param action_id string
---@param action table
---@param action_id string The action id
---@param action table The action table
---@return boolean is_consumed True if the input was consumed
function M:on_input(action_id, action)
if action_id ~= const.ACTION_TOUCH and
action_id ~= const.ACTION_MULTITOUCH and
@@ -40,8 +41,8 @@ end
---Set blocker enabled state
---@param state boolean
---@return druid.blocker self
---@param state boolean The new enabled state
---@return druid.blocker self The blocker instance
function M:set_enabled(state)
self._is_enabled = state
@@ -50,7 +51,7 @@ end
---Get blocker enabled state
---@return boolean
---@return boolean is_enabled True if the blocker is enabled
function M:is_enabled()
return self._is_enabled
end

View File

@@ -38,6 +38,7 @@ local component = require("druid.component")
local M = component.create("button")
---The constructor for the button component
---@param node_or_node_id node|string Node name or GUI Node itself
---@param callback fun()|nil Callback on button click
---@param custom_args any|nil Custom args for any Button event
@@ -76,6 +77,7 @@ function M:init(node_or_node_id, callback, custom_args, anim_node)
end
---@private
---@param style druid.button.style
function M:on_style_change(style)
self.style = {
@@ -92,7 +94,7 @@ function M:on_style_change(style)
end
---@return druid.button self
---@return druid.button self The current button instance
function M:set_animations_disabled()
self.style.on_click = function() end
self.style.on_click_disabled = function() end
@@ -114,9 +116,9 @@ function M:on_late_init()
end
---@param action_id hash
---@param action table
---@return boolean
---@param action_id hash The action id
---@param action table The action table
---@return boolean is_consumed True if the input was consumed
function M:on_input(action_id, action)
if not self:_is_input_match(action_id) then
return false
@@ -209,7 +211,7 @@ end
---The style.on_set_enabled will be triggered.
---Disabled button is not clickable.
---@param state boolean|nil Enabled state
---@return druid.button self
---@return druid.button self The current button instance
function M:set_enabled(state)
self.disabled = not state
self.hover:set_enabled(state)
@@ -231,7 +233,7 @@ end
---Useful to restrict click outside out stencil node or scrollable content.
---If button node placed inside stencil node, it will be automatically set to this stencil node.
---@param zone node|string|nil Gui node
---@return druid.button self
---@return druid.button self The current button instance
function M:set_click_zone(zone)
self.click_zone = zone and self:get_node(zone) or nil
self.hover:set_click_zone(zone)
@@ -242,7 +244,7 @@ end
---Set key name to trigger this button by keyboard.
---@param key hash|string The action_id of the input key. Example: "key_space"
---@return druid.button self
---@return druid.button self The current button instance
function M:set_key_trigger(key)
if type(key) == "string" then
self.key_trigger = hash(key)
@@ -264,7 +266,7 @@ end
---Set function for additional check for button click availability
---@param check_function function|nil Should return true or false. If true - button can be pressed.
---@param failure_callback function|nil Function will be called on button click, if check function return false
---@return druid.button self
---@return druid.button self The current button instance
function M:set_check_function(check_function, failure_callback)
self._check_function = check_function
self._failure_callback = failure_callback
@@ -280,7 +282,7 @@ end
---
---If the game is not HTML, html mode will be not enabled
---@param is_web_mode boolean|nil If true - button will be called inside html5 callback
---@return druid.button self
---@return druid.button self The current button instance
function M:set_web_user_interaction(is_web_mode)
self._is_html5_mode = not not (is_web_mode and html5)
return self
@@ -288,8 +290,8 @@ end
---@param action_id hash
---@return boolean
---@param action_id hash The action id
---@return boolean is_match True if the input matches the button
function M:_is_input_match(action_id)
if action_id == const.ACTION_TOUCH or action_id == const.ACTION_MULTITOUCH then
return true
@@ -303,13 +305,13 @@ function M:_is_input_match(action_id)
end
---@param hover_state boolean
---@param hover_state boolean True if the hover state is active
function M:_on_button_hover(hover_state)
self.style.on_hover(self, self.anim_node, hover_state)
end
---@param hover_state boolean
---@param hover_state boolean True if the hover state is active
function M:_on_button_mouse_hover(hover_state)
self.style.on_mouse_hover(self, self.anim_node, hover_state)
end
@@ -408,5 +410,4 @@ function M:_on_button_release()
end
return M

View File

@@ -7,35 +7,37 @@ local component = require("druid.component")
---@field DRAG_DEADZONE number Distance in pixels to start dragging. Default: 10
---@field NO_USE_SCREEN_KOEF boolean If screen aspect ratio affects on drag values. Default: false
---A component that allows you to subscribe to drag events over a node
---@class druid.drag: druid.component
---@field node node
---@field on_touch_start event
---@field on_touch_end event
---@field on_drag_start event
---@field on_drag event
---@field on_drag_end event
---@field style druid.drag.style
---@field click_zone node|nil
---@field is_touch boolean
---@field is_drag boolean
---@field can_x boolean
---@field can_y boolean
---@field dx number
---@field dy number
---@field touch_id number
---@field x number
---@field y number
---@field screen_x number
---@field screen_y number
---@field touch_start_pos vector3
---@field private _is_enabled boolean
---@field private _x_koef number
---@field private _y_koef number
---@field node node The node to subscribe to drag events over
---@field on_touch_start event fun(self, touch) The event triggered when a touch starts
---@field on_touch_end event fun(self, touch) The event triggered when a touch ends
---@field on_drag_start event fun(self, touch) The event triggered when a drag starts
---@field on_drag event fun(self, touch) The event triggered when a drag occurs
---@field on_drag_end event fun(self, touch) The event triggered when a drag ends
---@field style druid.drag.style The style of Drag component
---@field click_zone node|nil The click zone of Drag component
---@field is_touch boolean True if a touch is active
---@field is_drag boolean True if a drag is active
---@field can_x boolean True if Drag can move horizontally
---@field can_y boolean True if Drag can move vertically
---@field dx number The horizontal drag distance
---@field dy number The vertical drag distance
---@field touch_id number The touch id
---@field x number The current x position
---@field y number The current y position
---@field screen_x number The current screen x position
---@field screen_y number The current screen y position
---@field touch_start_pos vector3 The touch start position
---@field private _is_enabled boolean True if Drag component is enabled
---@field private _x_koef number The x koef
---@field private _y_koef number The y koef
local M = component.create("drag", const.PRIORITY_INPUT_HIGH)
---@param node_or_node_id node|string
---@param on_drag_callback function
---The constructor for Drag component
---@param node_or_node_id node|string The node to subscribe to drag events over
---@param on_drag_callback fun(self, touch) The callback to call when a drag occurs
function M:init(node_or_node_id, on_drag_callback)
self.druid = self:get_druid()
self.node = self:get_node(node_or_node_id)
@@ -70,7 +72,7 @@ function M:init(node_or_node_id, on_drag_callback)
end
---@param style druid.drag.style
---@param style druid.drag.style The style of Drag component
function M:on_style_change(style)
self.style = {
DRAG_DEADZONE = style.DRAG_DEADZONE or 10,
@@ -80,7 +82,7 @@ end
---Set Drag component enabled state.
---@param is_enabled boolean
---@param is_enabled boolean True if Drag component is enabled
function M:set_drag_cursors(is_enabled)
if defos and is_enabled then
self.hover.style.ON_HOVER_CURSOR = defos.CURSOR_CROSSHAIR
@@ -117,8 +119,9 @@ function M:on_input_interrupt()
end
---@param action_id hash
---@param action table
---@param action_id hash Action id from on_input
---@param action table Action from on_input
---@return boolean is_consumed True if the input was consumed
function M:on_input(action_id, action)
if action_id ~= const.ACTION_TOUCH and action_id ~= const.ACTION_MULTITOUCH then
return false
@@ -197,7 +200,7 @@ end
---Set Drag click zone
---@param node node|string|nil
---@param node node|string|nil Node or node id
---@return druid.drag self Current instance
function M:set_click_zone(node)
self.click_zone = node and self:get_node(node) or nil
@@ -217,7 +220,7 @@ end
---Check if Drag component is capture input
---@return boolean
---@return boolean is_enabled True if Drag component is enabled
function M:is_enabled()
return self._is_enabled
end
@@ -263,7 +266,7 @@ function M:_end_touch(touch)
end
---@param touch touch
---@param touch touch Touch action
function M:_process_touch(touch)
if not self.can_x then
self.touch_start_pos.x = touch.x
@@ -287,7 +290,7 @@ end
---@param action_id hash Action id from on_input
---@param action table Action from on_input
---@param touch_id number Touch id
---@return table|nil Touch action
---@return table|nil touch Touch action
function M:_find_touch(action_id, action, touch_id)
local act = helper.is_mobile() and const.ACTION_MULTITOUCH or const.ACTION_TOUCH

View File

@@ -7,16 +7,17 @@ local component = require("druid.component")
---@field ON_HOVER_CURSOR string|nil Mouse hover style on node hover
---@field ON_MOUSE_HOVER_CURSOR string|nil Mouse hover style on node mouse hover
---The component for handling hover events on a node
---@class druid.hover: druid.component
---@field node node
---@field on_hover event fun(self: druid.hover, is_hover: boolean)
---@field on_mouse_hover event fun(self: druid.hover, is_hover: boolean)
---@field style druid.hover.style
---@field click_zone node
---@field private _is_hovered boolean|nil
---@field private _is_mouse_hovered boolean|nil
---@field private _is_enabled boolean|nil
---@field private _is_mobile boolean
---@field node node Gui node
---@field on_hover event fun(self: druid.hover, is_hover: boolean) Hover event
---@field on_mouse_hover event fun(self: druid.hover, is_hover: boolean) Mouse hover event
---@field style druid.hover.style Style of the hover component
---@field click_zone node Click zone of the hover component
---@field private _is_hovered boolean|nil True if the node is hovered
---@field private _is_mouse_hovered boolean|nil True if the node is mouse hovered
---@field private _is_enabled boolean|nil True if the hover component is enabled
---@field private _is_mobile boolean True if the platform is mobile
local M = component.create("hover")

View File

@@ -242,7 +242,7 @@ end
-- It will change content gui node size
---@param size vector3 The new size for content node
---@param offset vector3|nil Offset value to set, where content is starts
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_size(size, offset)
if offset then
self._offset = offset
@@ -256,7 +256,7 @@ end
---Set new scroll view size in case the node size was changed.
---@param size vector3 The new size for view node
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_view_size(size)
gui.set_size(self.view_node, size)
self.view_size = size
@@ -267,7 +267,8 @@ function M:set_view_size(size)
end
---Refresh scroll view size
---Refresh scroll view size, used when view node size is changed
---@return druid.scroll self Current scroll instance
function M:update_view_size()
self.view_size = helper.get_scaled_size(self.view_node)
self.view_border = helper.get_border(self.view_node)
@@ -281,7 +282,7 @@ end
-- If disabled, scroll through points (if exist)
-- If no points, just simple drag without inertion
---@param state boolean Inert scroll state
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_inert(state)
self._is_inert = state
@@ -299,7 +300,7 @@ end
---Set extra size for scroll stretching
-- Set 0 to disable stretching effect
---@param stretch_size number|nil Size in pixels of additional scroll area
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_extra_stretch_size(stretch_size)
self.style.EXTRA_STRETCH_SIZE = stretch_size or 0
self:_update_size()
@@ -318,7 +319,7 @@ end
---Set points of interest.
-- Scroll will always centered on closer points
---@param points table Array of vector3 points
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_points(points)
self.points = points
@@ -334,7 +335,7 @@ end
---Lock or unlock horizontal scroll
---@param state boolean True, if horizontal scroll is enabled
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_horizontal_scroll(state)
self._is_horizontal_scroll = state
self.drag.can_x = self.available_size.x > 0 and state or false
@@ -344,7 +345,7 @@ end
---Lock or unlock vertical scroll
---@param state boolean True, if vertical scroll is enabled
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:set_vertical_scroll(state)
self._is_vertical_scroll = state
self.drag.can_y = self.available_size.y > 0 and state or false
@@ -395,7 +396,7 @@ end
---Bind the grid component (Static or Dynamic) to recalculate
-- scroll size on grid changes
---@param grid druid.grid|nil Druid grid component
---@return druid.scroll Current scroll instance
---@return druid.scroll self Current scroll instance
function M:bind_grid(grid)
if self._grid_on_change then
self._grid_on_change:unsubscribe(self._grid_on_change_callback)

View File

@@ -7,25 +7,27 @@ local component = require("druid.component")
---@field IS_DYNAMIC_NODE_POSES boolean|nil If true, always center grid content as grid pivot sets. Default: false
---@field IS_ALIGN_LAST_ROW boolean|nil If true, always align last row of the grid as grid pivot sets. Default: false
---The component for manage the nodes position in the grid with various options
---@class druid.grid: druid.component
---@field on_add_item event
---@field on_remove_item event
---@field on_change_items event
---@field on_clear event
---@field on_update_positions event
---@field parent node
---@field nodes node[]
---@field first_index number
---@field last_index number
---@field anchor vector3
---@field pivot vector3
---@field node_size vector3
---@field border vector4
---@field in_row number
---@field style table
---@field on_add_item event Trigger on add item event, fun(self, item, index)
---@field on_remove_item event Trigger on remove item event, fun(self, index)
---@field on_change_items event Trigger on change items event, fun(self, index)
---@field on_clear event Trigger on clear event, fun(self)
---@field on_update_positions event Trigger on update positions event, fun(self)
---@field parent node Parent node
---@field nodes node[] Nodes array
---@field first_index number First index
---@field last_index number Last index
---@field anchor vector3 Anchor
---@field pivot vector3 Pivot
---@field node_size vector3 Node size
---@field border vector4 Border
---@field in_row number In row
---@field style druid.grid.style Style
local M = component.create("grid")
---The constructor for the grid component
---@param parent string|node The GUI Node container, where grid's items will be placed
---@param element node Element prefab. Need to get it size
---@param in_row number|nil How many nodes in row can be placed. By default 1
@@ -87,9 +89,9 @@ function M:get_pos(index)
end
---Return index for grid pos
---Return grid index by position
---@param pos vector3 The node position in the grid
---@return number The node index
---@return number index The node index
function M:get_index(pos)
-- Offset to left-top corner from node pivot
local node_offset_x = self.node_size.x * (-0.5 + self.node_pivot.x)
@@ -133,12 +135,18 @@ function M:set_anchor(anchor)
end
---Update grid content
---Instantly update the grid content
---@return druid.grid self Current grid instance
function M:refresh()
self:_update(true)
return self
end
---Set grid pivot
---@param pivot constant The new pivot
---@return druid.grid self Current grid instance
function M:set_pivot(pivot)
local prev_pivot = helper.get_pivot_offset(gui.get_pivot(self.parent))
self.pivot = helper.get_pivot_offset(pivot)
@@ -162,6 +170,8 @@ function M:set_pivot(pivot)
)
self:_update(true)
return self
end
@@ -170,6 +180,7 @@ end
---@param index number|nil The item position. By default add as last item
---@param shift_policy number|nil How shift nodes, if required. Default: const.SHIFT.RIGHT
---@param is_instant boolean|nil If true, update node positions instantly
---@return druid.grid self Current grid instance
function M:add(item, index, shift_policy, is_instant)
index = index or ((self.last_index or 0) + 1)
@@ -186,12 +197,15 @@ function M:add(item, index, shift_policy, is_instant)
self.on_add_item:trigger(self:get_context(), item, index)
self.on_change_items:trigger(self:get_context(), index)
return self
end
---Set new items to the grid. All previous items will be removed
---@param nodes node[] The new grid nodes
---@param is_instant boolean|nil If true, update node positions instantly
---@return druid.grid self Current grid instance
function M:set_items(nodes, is_instant)
self.nodes = nodes
for index = 1, #nodes do
@@ -202,6 +216,8 @@ function M:set_items(nodes, is_instant)
self:_update(is_instant)
self.on_change_items:trigger(self:get_context())
return self
end
@@ -209,7 +225,7 @@ end
---@param index number The grid node index to remove
---@param shift_policy number|nil How shift nodes, if required. Default: const.SHIFT.RIGHT
---@param is_instant boolean|nil If true, update node positions instantly
---@return node The deleted gui node from grid
---@return node node The deleted gui node from grid
function M:remove(index, shift_policy, is_instant)
assert(self.nodes[index], "No grid item at given index " .. index)
@@ -226,7 +242,7 @@ end
---Return grid content size
---@return vector3 The grid content size
---@return vector3 size The grid content size
function M:get_size()
return vmath.vector3(
self.border.z - self.border.x,
@@ -235,6 +251,9 @@ function M:get_size()
end
---Return grid content size for given count of nodes
---@param count number The count of nodes
---@return vector3 size The grid content size
function M:get_size_for(count)
if not count or count == 0 then
return vmath.vector3(0)
@@ -258,14 +277,14 @@ end
---Return grid content borders
---@return vector4 The grid content borders
---@return vector4 borders The grid content borders
function M:get_borders()
return self.border
end
---Return array of all node positions
---@return vector3[] All grid node positions
---@return vector3[] positions All grid node positions
function M:get_all_pos()
local result = {}
for i, node in pairs(self.nodes) do
@@ -279,7 +298,7 @@ end
---Change set position function for grid nodes. It will call on
-- update poses on grid elements. Default: gui.set_position
---@param callback function Function on node set position
---@return druid.grid Current grid instance
---@return druid.grid self Current grid instance
function M:set_position_function(callback)
self._set_position_function = callback or gui.set_position
@@ -289,7 +308,7 @@ end
---Clear grid nodes array. GUI nodes will be not deleted!
-- If you want to delete GUI nodes, use static_grid.nodes array before grid:clear
---@return druid.grid Current grid instance
---@return druid.grid self Current grid instance
function M:clear()
self.border.x = 0
self.border.y = 0
@@ -307,7 +326,7 @@ end
---Return StaticGrid offset, where StaticGrid content starts.
---@return vector3 The StaticGrid offset
---@return vector3 offset The StaticGrid offset
function M:get_offset()
local borders = self:get_borders()
local size = self:get_size()
@@ -323,7 +342,7 @@ end
---Set new in_row elements for grid
---@param in_row number The new in_row value
---@return druid.grid Current grid instance
---@return druid.grid self Current grid instance
function M:set_in_row(in_row)
self.in_row = in_row
self._grid_horizonal_offset = self.node_size.x * (self.in_row - 1) * self.anchor.x
@@ -342,7 +361,7 @@ end
---Set new node size for grid
---@param width number|nil The new node width
---@param height number|nil The new node height
---@return druid.grid Current grid instance
---@return druid.grid self Current grid instance
function M:set_item_size(width, height)
if width then
self.node_size.x = width

View File

@@ -11,6 +11,7 @@ local utf8 = utf8 or utf8_lua --[[@as utf8]]
---@field ADJUST_STEPS number|nil Amount of iterations for text adjust by height. Default: 20
---@field ADJUST_SCALE_DELTA number|nil Scale step on each height adjust step. Default: 0.02
---The component to handle text behaviour over a GUI Text node, mainly used to automatically adjust text size to fit the text area
---@class druid.text: druid.component
---@field node node The text node
---@field on_set_text event The event triggered when the text is set, fun(self, text)
@@ -19,7 +20,7 @@ local utf8 = utf8 or utf8_lua --[[@as utf8]]
---@field style druid.text.style The style of the text
---@field private start_pivot userdata The start pivot of the text
---@field private start_scale vector3 The start scale of the text
---@field private scale vector3
---@field private scale vector3 The current scale of the text
local M = component.create("text")
@@ -49,7 +50,6 @@ function M:init(node, value, adjust_type)
self.on_update_text_scale = event.create()
self:set_text(value or gui.get_text(self.node))
return self
end
@@ -64,16 +64,15 @@ function M:on_style_change(style)
end
function M:on_layout_change()
self:set_text(self.last_value)
end
---Calculate text width with font with respect to trailing space
---@param text string|nil
---@return number Width
---@return number Height
---@param text string|nil The text to calculate the size of, if nil - use current text
---@return number width The text width
---@return number height The text height
function M:get_text_size(text)
text = text or self.last_value
local font_name = gui.get_font(self.node)
@@ -95,8 +94,8 @@ end
---Get chars count by width
---@param width number
---@return number Chars count
---@param width number The width to get the chars count of
---@return number index The chars count
function M:get_text_index_by_width(width)
local text = self.last_value
local font_name = gui.get_font(self.node)
@@ -130,7 +129,7 @@ end
---Set text to text field
---@deprecated
---@param set_to string Text for node
---@return druid.text Current text instance
---@return druid.text self Current text instance
function M:set_to(set_to)
set_to = tostring(set_to or "")
@@ -172,7 +171,7 @@ end
---Set color
---@param color vector4 Color for node
---@return druid.text Current text instance
---@return druid.text self Current text instance
function M:set_color(color)
self.color = color
gui.set_color(self.node, color)
@@ -183,7 +182,7 @@ end
---Set alpha
---@param alpha number Alpha for node
---@return druid.text Current text instance
---@return druid.text self Current text instance
function M:set_alpha(alpha)
self.color.w = alpha
gui.set_color(self.node, self.color)
@@ -194,7 +193,7 @@ end
---Set scale
---@param scale vector3 Scale for node
---@return druid.text Current text instance
---@return druid.text self Current text instance
function M:set_scale(scale)
self.last_scale = scale
gui.set_scale(self.node, scale)
@@ -205,7 +204,7 @@ end
---Set text pivot. Text will re-anchor inside text area
---@param pivot userdata The gui.PIVOT_* constant
---@return druid.text Current text instance
---@return druid.text self Current text instance
function M:set_pivot(pivot)
local prev_pivot = gui.get_pivot(self.node)
local prev_offset = const.PIVOTS[prev_pivot]
@@ -252,7 +251,7 @@ end
---Set minimal scale for DOWNSCALE_LIMITED or SCALE_THEN_SCROLL adjust types
---@param minimal_scale number If pass nil - not use minimal scale
---@return druid.text Current text instance
---@return druid.text self Current text instance
function M:set_minimal_scale(minimal_scale)
self._minimal_scale = minimal_scale
@@ -412,6 +411,7 @@ function M:_update_text_with_trim(trim_postfix)
end
end
---@private
---@param trim_postfix string
function M:_update_text_with_trim_left(trim_postfix)