This commit is contained in:
Insality 2025-03-05 21:42:54 +02:00
parent 2c2789f1f9
commit c3b132187a
26 changed files with 338 additions and 574 deletions

View File

@ -16,14 +16,11 @@ function M:init(callback, params)
end
---@param action_id string
---@param action_id hash
---@param action table
---@return boolean
function M:on_input(action_id, action)
if not action.released then
return false
end
if action_id == const.ACTION_BACK or action_id == const.ACTION_BACKSPACE then
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)
return true
end

View File

@ -211,7 +211,7 @@ end
---Get button enabled state.
---By default all Buttons is enabled on creating.
---@return boolean @True, if button is enabled now, False overwise
---@return boolean is_enabled True, if button is enabled now, False overwise
function M:is_enabled()
return not self.disabled
end

View File

@ -1,62 +1,3 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Component to handle drag action on node.
-- Drag have correct handling for multitouch and swap
-- touched while dragging. Drag will be processed even
-- the cursor is outside of node, if drag is already started
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_drag" target="_blank"><b>Example Link</b></a>
-- @module Drag
-- @within BaseComponent
-- @alias druid.drag
--- Drag node
-- @tfield node node
--- Event on touch start callback(self)
-- @tfield event on_touch_start event
--- Event on touch end callback(self)
-- @tfield event on_touch_end event
--- Event on drag start callback(self, touch)
-- @tfield event on_drag_start event
--- on drag progress callback(self, dx, dy, total_x, total_y, touch)
-- @tfield event on_drag Event event
--- Event on drag end callback(self, total_x, total_y, touch)
-- @tfield event on_drag_end event
--- Is component now touching
-- @tfield boolean is_touch
--- Is component now dragging
-- @tfield boolean is_drag
--- Is drag component process vertical dragging. Default - true
-- @tfield boolean can_x
--- Is drag component process horizontal. Default - true
-- @tfield boolean can_y
--- Current touch x position
-- @tfield number x
--- Current touch y position
-- @tfield number y
--- Current touch x screen position
-- @tfield number screen_x
--- Current touch y screen position
-- @tfield number screen_y
--- Touch start position
-- @tfield vector3 touch_start_pos
---
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
@ -150,8 +91,12 @@ local function process_touch(self, touch)
end
--- Return current touch action from action input data
-- If touch_id stored - return exact this touch action
---Return current touch action from action input data
---If touch_id stored - return exact this touch action
---@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
local function find_touch(action_id, action, touch_id)
local act = helper.is_mobile() and const.ACTION_MULTITOUCH or const.ACTION_TOUCH
@ -173,8 +118,11 @@ local function find_touch(action_id, action, touch_id)
end
--- Process on touch release. We should to find, if any other
-- touches exists to switch to another touch.
---Process on touch release. We should to find, if any other
---touches exists to switch to another touch.
---@param self druid.drag
---@param action_id hash Action id from on_input
---@param action table Action from on_input
local function on_touch_release(self, action_id, action)
if #action.touch >= 2 then
-- Find next unpressed touch
@ -199,12 +147,7 @@ local function on_touch_release(self, action_id, action)
end
--- Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
-- @tfield number|nil DRAG_DEADZONE Distance in pixels to start dragging. Default: 10
-- @tfield boolean|nil NO_USE_SCREEN_KOEF If screen aspect ratio affects on drag values. Default: false
---@param style druid.drag.style
function M:on_style_change(style)
self.style = {
DRAG_DEADZONE = style.DRAG_DEADZONE or 10,
@ -289,7 +232,7 @@ end
---@local
---@param action_id string
---@param action_id hash
---@param action table
function M:on_input(action_id, action)
if action_id ~= const.ACTION_TOUCH and action_id ~= const.ACTION_MULTITOUCH then
@ -388,7 +331,7 @@ function M:set_enabled(is_enabled)
end
---Check if Drag component is enabled
---Check if Drag component is capture input
---@return boolean
function M:is_enabled()
return self._is_enabled

View File

@ -1,31 +1,17 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Component to handle hover node interaction
-- @module Hover
-- @within BaseComponent
-- @alias druid.hover
--- Hover node
-- @tfield node node
--- On hover callback(self, state, hover_instance)
-- @tfield event on_hover event
--- On mouse hover callback(self, state, hover_instance)
-- @tfield event on_mouse_hover event
---
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.hover.style
---@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
---@class druid.hover: druid.component
---@field node node
---@field on_hover event
---@field on_mouse_hover event
---@field style table
---@field style druid.hover.style
---@field click_zone node
---@field private _is_hovered boolean|nil
---@field private _is_mouse_hovered boolean|nil
@ -34,7 +20,6 @@ local component = require("druid.component")
local M = component.create("hover")
--- The Hover constructor
---@param node node Gui node
---@param on_hover_callback function Hover callback
---@param on_mouse_hover function On mouse hover callback
@ -61,12 +46,7 @@ function M:on_late_init()
end
--- Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
-- @tfield[opt] string ON_HOVER_CURSOR Mouse hover style on node hover
-- @tfield[opt] string ON_MOUSE_HOVER_CURSOR Mouse hover style on node mouse hover
---@param style druid.hover.style
function M:on_style_change(style)
self.style = {}
self.style.ON_HOVER_CURSOR = style.ON_HOVER_CURSOR or nil
@ -74,6 +54,9 @@ function M:on_style_change(style)
end
---@param action_id hash
---@param action table
---@return boolean
function M:on_input(action_id, action)
if action_id ~= const.ACTION_TOUCH and action_id ~= nil then
return false
@ -113,7 +96,7 @@ function M:on_input_interrupt()
end
--- Set hover state
---Set hover state
---@param state boolean|nil The hover state
function M:set_hover(state)
if self._is_hovered == state then
@ -129,14 +112,14 @@ function M:set_hover(state)
end
--- Return current hover state. True if touch action was on the node at current time
---@return boolean The current hovered state
---Return current hover state. True if touch action was on the node at current time
---@return boolean is_hovered The current hovered state
function M:is_hovered()
return self._is_hovered
end
--- Set mouse hover state
---Set mouse hover state
---@param state boolean|nil The mouse hover state
function M:set_mouse_hover(state)
if self._is_mouse_hovered == state then
@ -152,15 +135,14 @@ function M:set_mouse_hover(state)
end
--- Return current hover state. True if nil action_id (usually desktop mouse) was on the node at current time
---Return current hover state. True if nil action_id (usually desktop mouse) was on the node at current time
---@return boolean The current hovered state
function M:is_mouse_hovered()
return self._is_mouse_hovered
end
--- Strict hover click area. Useful for
-- no click events outside stencil node
---Strict hover click area. Useful for no click events outside stencil node
---@param zone node|string|nil Gui node
function M:set_click_zone(zone)
if not zone then
@ -172,9 +154,9 @@ function M:set_click_zone(zone)
end
--- Set enable state of hover component.
-- If hover is not enabled, it will not generate
-- any hover events
---Set enable state of hover component.
---If hover is not enabled, it will not generate
---any hover events
---@param state boolean|nil The hover enabled state
function M:set_enabled(state)
self._is_enabled = state
@ -190,7 +172,7 @@ function M:set_enabled(state)
end
--- Return current hover enabled state
---Return current hover enabled state
---@return boolean The hover enabled state
function M:is_enabled()
return self._is_enabled

View File

@ -51,7 +51,7 @@ local function inverse_lerp(min, max, current)
end
--- Update vector with next conditions:
---Update vector with next conditions:
-- Field x have to <= field z
-- Field y have to <= field w
local function get_border_vector(vector, offset)
@ -69,7 +69,7 @@ local function get_border_vector(vector, offset)
end
--- Return size from scroll border vector4
---Return size from scroll border vector4
local function get_size_vector(vector)
return vmath.vector3(vector.z - vector.x, vector.w - vector.y, 0)
end
@ -99,7 +99,7 @@ function M:on_style_change(style)
end
--- The Scroll constructor
---The Scroll constructor
---@param view_node string|node GUI view scroll node
---@param content_node string|node GUI content scroll node
function M:init(view_node, content_node)
@ -180,7 +180,7 @@ function M:on_remove()
end
--- Start scroll to target point.
---Start scroll to target point.
---@param point vector3 Target point
---@param is_instant boolean|nil Instant scroll flag
-- @usage scroll:scroll_to(vmath.vector3(0, 50, 0))
@ -213,7 +213,7 @@ function M:scroll_to(point, is_instant)
end
--- Scroll to item in scroll by point index.
---Scroll to item in scroll by point index.
---@param index number Point index
---@param skip_cb boolean|nil If true, skip the point callback
function M:scroll_to_index(index, skip_cb)
@ -235,7 +235,7 @@ function M:scroll_to_index(index, skip_cb)
end
--- Start scroll to target scroll percent
---Start scroll to target scroll percent
---@param percent vector3 target percent
---@param is_instant boolean|nil instant scroll flag
-- @usage scroll:scroll_to_percent(vmath.vector3(0.5, 0, 0))
@ -259,7 +259,7 @@ function M:scroll_to_percent(percent, is_instant)
end
--- Return current scroll progress status.
---Return current scroll progress status.
-- Values will be in [0..1] interval
---@return vector3 New vector with scroll progress values
function M:get_percent()
@ -270,7 +270,7 @@ function M:get_percent()
end
--- Set scroll content size.
---Set scroll content size.
-- 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
@ -286,7 +286,7 @@ function M:set_size(size, offset)
end
--- Set new scroll view size in case the node size was changed.
---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
function M:set_view_size(size)
@ -299,7 +299,7 @@ function M:set_view_size(size)
end
--- Refresh scroll view size
---Refresh scroll view size
function M:update_view_size()
self.view_size = helper.get_scaled_size(self.view_node)
self.view_border = helper.get_border(self.view_node)
@ -309,7 +309,7 @@ function M:update_view_size()
end
--- Enable or disable scroll inert.
---Enable or disable scroll inert.
-- If disabled, scroll through points (if exist)
-- If no points, just simple drag without inertion
---@param state boolean Inert scroll state
@ -321,14 +321,14 @@ function M:set_inert(state)
end
--- Return if scroll have inertion.
---Return if scroll have inertion.
---@return boolean @If scroll have inertion
function M:is_inert()
return self._is_inert
end
--- Set extra size for scroll stretching.
---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
@ -340,14 +340,14 @@ function M:set_extra_stretch_size(stretch_size)
end
--- Return vector of scroll size with width and height.
---Return vector of scroll size with width and height.
---@return vector3 Available scroll size
function M:get_scroll_size()
return self.available_size
end
--- Set points of interest.
---Set points of interest.
-- Scroll will always centered on closer points
---@param points table Array of vector3 points
---@return druid.scroll Current scroll instance
@ -364,7 +364,7 @@ function M:set_points(points)
end
--- Lock or unlock horizontal scroll
---Lock or unlock horizontal scroll
---@param state boolean True, if horizontal scroll is enabled
---@return druid.scroll Current scroll instance
function M:set_horizontal_scroll(state)
@ -374,7 +374,7 @@ function M:set_horizontal_scroll(state)
end
--- Lock or unlock vertical scroll
---Lock or unlock vertical scroll
---@param state boolean True, if vertical scroll is enabled
---@return druid.scroll Current scroll instance
function M:set_vertical_scroll(state)
@ -384,7 +384,7 @@ function M:set_vertical_scroll(state)
end
--- Check node if it visible now on scroll.
---Check node if it visible now on scroll.
-- Extra border is not affected. Return true for elements in extra scroll zone
---@param node node The node to check
---@return boolean True if node in visible scroll area
@ -424,7 +424,7 @@ function M:is_node_in_view(node)
end
--- Bind the grid component (Static or Dynamic) to recalculate
---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
@ -453,7 +453,7 @@ function M:bind_grid(grid)
end
--- Strict drag scroll area. Useful for
---Strict drag scroll area. Useful for
-- restrict events outside stencil node
---@param node node|string Gui node
function M:set_click_zone(node)
@ -560,7 +560,7 @@ function M:_set_scroll_position(position_x, position_y)
end
--- Find closer point of interest
---Find closer point of interest
-- if no inert, scroll to next point by scroll direction
-- if inert, find next point by scroll director
---@private

View File

@ -1,87 +1,12 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Component to handle component's position by row and columns.
-- <b># Overview #</b>
--
-- The Static Grid component allows for positioning components in rows and columns.
-- It provides a static grid layout with constant node sizes, allowing for pre-calculated
-- node positions and the option to include gaps between nodes.
--
-- <b># Notes #</b>
--
-- • In a static grid, the node size remains constant, enabling the calculation of node
-- positions before placement. If you want add gaps between nodes, increase the root prefab size,
-- including the padding and margin.
--
-- • The static grid can automatically shift elements when nodes are added or removed.
--
-- • When a node is added, the grid will set the node's parent to the specified parent_node.
--
-- • You can obtain an array of positions for each element, which can be used to set
-- points of interest in a scroll component.
--
-- • The size of all elements can be retrieved for setting up the size in a scroll component.
--
-- • The grid can be bound to a scroll component for automatic resizing of the scroll content size.
--
-- • The pivot of the parent_node affects the node placement within the grid.
--
-- • A prefab node is used to determine the node size and anchor.
--
-- • You can specify a position_function for animations using the
-- _static_grid:set_position_function(node, pos) callback. The default position function is gui.set_position().
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_grid" target="_blank"><b>Example Link</b></a>
-- @module StaticGrid
-- @within BaseComponent
-- @alias druid.grid
--- On item add callback(self, node, index)
-- @tfield event on_add_item event
--- On item remove callback(self, index)
-- @tfield event on_remove_item event
--- On item add, remove or change in_row callback(self, index|nil)
-- @tfield event on_change_items event
--- On grid clear callback(self)
-- @tfield event on_clear event
--- On update item positions callback(self)
-- @tfield event on_update_positions event
--- Parent gui node
-- @tfield node parent
--- List of all grid nodes
-- @tfield node[] nodes
--- The first index of node in grid
-- @tfield number first_index
--- The last index of node in grid
-- @tfield number last_index
--- Item anchor [0..1]
-- @tfield vector3 anchor
--- Item pivot [-0.5..0.5]
-- @tfield vector3 pivot
--- Item size
-- @tfield vector3 node_size
--- The size of item content
-- @tfield vector4 border
---
local const = require("druid.const")
local event = require("event.event")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.grid.style
---@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
---@class druid.grid: druid.component
---@field on_add_item event
---@field on_remove_item event
@ -98,7 +23,7 @@ local component = require("druid.component")
---@field border vector4
---@field in_row number
---@field style table
local M = component.create("static_grid")
local M = component.create("grid")
local function _extend_border(border, pos, size, pivot)
@ -114,20 +39,15 @@ local function _extend_border(border, pos, size, pivot)
end
--- Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
-- @tfield boolean|nil IS_DYNAMIC_NODE_POSES If true, always center grid content as grid pivot sets. Default: false
-- @tfield boolean|nil IS_ALIGN_LAST_ROW If true, always align last row of the grid as grid pivot sets. Default: false
---@param style druid.grid.style
function M:on_style_change(style)
self.style = {}
self.style.IS_DYNAMIC_NODE_POSES = style.IS_DYNAMIC_NODE_POSES or false
self.style.IS_ALIGN_LAST_ROW = style.IS_ALIGN_LAST_ROW or false
self.style = {
IS_DYNAMIC_NODE_POSES = style.IS_DYNAMIC_NODE_POSES or false,
IS_ALIGN_LAST_ROW = style.IS_ALIGN_LAST_ROW or false,
}
end
--- The StaticGrid constructor
---@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
@ -163,7 +83,7 @@ end
local _temp_pos = vmath.vector3(0)
--- Return pos for grid node index
---Return pos for grid node index
---@param index number The grid element index
---@return vector3 @Node position
function M:get_pos(index)
@ -180,7 +100,7 @@ function M:get_pos(index)
end
--- Return index for grid pos
---Return index for grid pos
---@param pos vector3 The node position in the grid
---@return number The node index
function M:get_index(pos)
@ -199,7 +119,7 @@ function M:get_index(pos)
end
--- Return grid index by node
---Return grid index by node
---@param node node The gui node in the grid
---@return number|nil index The node index
function M:get_index_by_node(node)
@ -218,7 +138,7 @@ function M:on_layout_change()
end
--- Set grid anchor. Default anchor is equal to anchor of grid parent node
---Set grid anchor. Default anchor is equal to anchor of grid parent node
---@param anchor vector3 Anchor
function M:set_anchor(anchor)
self.anchor = anchor
@ -226,7 +146,7 @@ function M:set_anchor(anchor)
end
--- Update grid content
---Update grid content
function M:refresh()
self:_update(true)
end
@ -238,11 +158,6 @@ function M:set_pivot(pivot)
local width = gui.get(self.parent, "size.x")
local height = gui.get(self.parent, "size.y")
--local pos_offset = vmath.vector3(
-- width * (self.pivot.x - prev_pivot.x),
-- height * (self.pivot.y - prev_pivot.y),
-- 0
--)
local position = gui.get_position(self.parent)
position.x = position.x + width * (self.pivot.x - prev_pivot.x)
@ -263,7 +178,7 @@ function M:set_pivot(pivot)
end
--- Add new item to the grid
---Add new item to the grid
---@param item node GUI node
---@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
@ -287,7 +202,7 @@ function M:add(item, index, shift_policy, is_instant)
end
--- Set new items to the grid. All previous items will be removed
---Set new items to the grid. All previous items will be removed
---@param nodes node[] The new grid nodes
-- @tparam[opt=false] boolean is_instant If true, update node positions instantly
function M:set_items(nodes, is_instant)
@ -303,7 +218,7 @@ function M:set_items(nodes, is_instant)
end
--- Remove the item from the grid. Note that gui node will be not deleted
---Remove the item from the grid. Note that gui node will be not deleted
---@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
@ -323,7 +238,7 @@ function M:remove(index, shift_policy, is_instant)
end
--- Return grid content size
---Return grid content size
---@return vector3 The grid content size
function M:get_size()
return vmath.vector3(
@ -355,14 +270,14 @@ function M:get_size_for(count)
end
--- Return grid content borders
---Return grid content borders
---@return vector4 The grid content borders
function M:get_borders()
return self.border
end
--- Return array of all node positions
---Return array of all node positions
---@return vector3[] All grid node positions
function M:get_all_pos()
local result = {}
@ -374,7 +289,7 @@ function M:get_all_pos()
end
--- Change set position function for grid nodes. It will call on
---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
@ -385,7 +300,7 @@ function M:set_position_function(callback)
end
--- Clear grid nodes array. GUI nodes will be not deleted!
---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
function M:clear()
@ -404,7 +319,7 @@ function M:clear()
end
--- Return StaticGrid offset, where StaticGrid content starts.
---Return StaticGrid offset, where StaticGrid content starts.
---@return vector3 The StaticGrid offset
function M:get_offset()
local borders = self:get_borders()
@ -419,7 +334,7 @@ function M:get_offset()
end
--- Set new in_row elements for grid
---Set new in_row elements for grid
---@param in_row number The new in_row value
---@return druid.grid Current grid instance
function M:set_in_row(in_row)
@ -437,7 +352,7 @@ function M:set_in_row(in_row)
end
--- Set new node size for grid
---Set new node size for grid
-- @tparam[opt] number width The new node width
-- @tparam[opt] number height The new node height
---@return druid.grid Current grid instance
@ -461,7 +376,7 @@ function M:set_item_size(width, height)
end
--- Sort grid nodes by custom comparator function
---Sort grid nodes by custom comparator function
---@param comparator function The comparator function. (a, b) -> boolean
---@return druid.grid self Current grid instance
function M:sort_nodes(comparator)
@ -472,7 +387,7 @@ function M:sort_nodes(comparator)
end
--- Update grid inner state
---Update grid inner state
---@param is_instant boolean|nil If true, node position update instantly, otherwise with set_position_function callback
---@private
function M:_update(is_instant)
@ -482,7 +397,7 @@ function M:_update(is_instant)
end
--- Update first and last indexes of grid nodes
---Update first and last indexes of grid nodes
---@private
function M:_update_indexes()
self.first_index = nil
@ -497,7 +412,7 @@ function M:_update_indexes()
end
--- Update grid content borders, recalculate min and max values
---Update grid content borders, recalculate min and max values
---@private
function M:_update_borders()
if not self.first_index then
@ -515,7 +430,7 @@ function M:_update_borders()
end
--- Update grid nodes position
---Update grid nodes position
---@param is_instant boolean|nil If true, node position update instantly, otherwise with set_position_function callback
---@private
function M:_update_pos(is_instant)
@ -537,7 +452,7 @@ function M:_update_pos(is_instant)
end
--- Return elements offset for correct posing nodes. Correct posing at
---Return elements offset for correct posing nodes. Correct posing at
-- parent pivot node (0:0) with adjusting of node sizes and anchoring
---@return vector3 The offset vector
---@private
@ -555,7 +470,7 @@ function M:_get_zero_offset()
end
--- Return offset x for last row in grid. Used to align this row accorting to grid's anchor
---Return offset x for last row in grid. Used to align this row accorting to grid's anchor
---@return number The offset x value
---@private
function M:_get_zero_offset_x(row_index)

View File

@ -1,81 +1,3 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Component for Wrapping GUI Text Nodes: Druid Text
--
-- ## Overview ##
--
-- Druid Text is a component that provides various adjustment modes for text nodes. It allows text to be scaled down to fit within the size of the text node.
--
-- ## Notes ##
--
-- • The text pivot can be changed using the text:set_pivot method.
-- The anchoring will be inside the text node's area size.
--
-- • There are several text adjustment types available. The default is DOWNSCALE.
-- You can change the default adjustment type in the Text style. Refer to the example below to see all available adjustment types:
--
-- - const.TEXT_ADJUST.DOWNSCALE: Changes the text's scale to fit within the text node's size.
--
-- - const.TEXT_ADJUST.TRIM: Trims the text with a postfix (default: "...", can be overridden in styles)
-- to fit within the text node's size.
--
-- - const.TEXT_ADJUST.NO_ADJUST: No adjustment is applied, similar
-- to the default Defold Text Node behavior.
--
-- - const.TEXT_ADJUST.DOWNSCALE_LIMITED: Changes the text's scale
-- with a limited downscale. You can set the minimum scale using the text:set_minimal_scale() function.
--
-- - const.TEXT_ADJUST.SCROLL: Changes the text's pivot to imitate scrolling within the text box.
-- For better effect, use with a stencil node.
--
-- - const.TEXT_ADJUST.SCALE_THEN_SCROLL: Combines two modes: limited downscale first, then scroll.
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=texts_general" target="_blank"><b>Example Link</b></a>
-- @module Text
-- @within BaseComponent
-- @alias druid.text
--- On set text callback(self, text)
-- @tfield event on_set_text event
--- On adjust text size callback(self, new_scale, text_metrics)
-- @tfield event on_update_text_scale event
--- On change pivot callback(self, pivot)
-- @tfield event on_set_pivot event
--- Text node
-- @tfield node node
--- The node id of text node
-- @tfield hash node_id
--- Current text position
-- @tfield vector3 pos
--- The last text value
-- @tfield string last_value
--- Initial text node scale
-- @tfield vector3 start_scale
--- Current text node scale
-- @tfield vector3 scale
--- Initial text node size
-- @tfield vector3 start_size
--- Current text node available are
-- @tfield vector3 text_area
--- Current text size adjust settings
-- @tfield number adjust_type
--- Current text color
-- @tfield vector3 color
---
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
@ -83,14 +5,20 @@ local utf8_lua = require("druid.system.utf8")
local component = require("druid.component")
local utf8 = utf8 or utf8_lua --[[@as utf8]]
---@class druid.text.style
---@field TRIM_POSTFIX string|nil The postfix for TRIM adjust type. Default: ...
---@field DEFAULT_ADJUST string|nil The default adjust type for any text component. Default: DOWNSCALE
---@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
---@class druid.text: druid.component
---@field node node
---@field on_set_text event
---@field on_update_text_scale event
---@field on_set_pivot event
---@field style table
---@field private start_pivot userdata
---@field private start_scale vector3
---@field node node The text node
---@field on_set_text event The event triggered when the text is set, fun(self, text)
---@field on_update_text_scale event The event triggered when the text scale is updated, fun(self, scale, metrics)
---@field on_set_pivot event The event triggered when the text pivot is set, fun(self, pivot)
---@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
local M = component.create("text")
@ -111,7 +39,7 @@ local function update_text_size(self)
end
--- Reset initial scale for text
---Reset initial scale for text
local function reset_default_scale(self)
self.scale.x = self.start_scale.x
self.scale.y = self.start_scale.y
@ -127,7 +55,7 @@ local function is_fit_info_area(self, metrics)
end
--- Setup scale x, but can only be smaller, than start text scale
---Setup scale x, but can only be smaller, than start text scale
local function update_text_area_size(self)
reset_default_scale(self)
@ -305,24 +233,18 @@ local function update_adjust(self)
end
--- Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
-- @tfield string|nil TRIM_POSTFIX The postfix for TRIM adjust type. Default: ...
-- @tfield string|nil DEFAULT_ADJUST The default adjust type for any text component. Default: DOWNSCALE
-- @tfield string|nil ADJUST_STEPS Amount of iterations for text adjust by height. Default: 20
-- @tfield string|nil ADJUST_SCALE_DELTA Scale step on each height adjust step. Default: 0.02
---@param style druid.text.style
function M:on_style_change(style)
self.style = {}
self.style.TRIM_POSTFIX = style.TRIM_POSTFIX or "..."
self.style.DEFAULT_ADJUST = style.DEFAULT_ADJUST or const.TEXT_ADJUST.DOWNSCALE
self.style.ADJUST_STEPS = style.ADJUST_STEPS or 20
self.style.ADJUST_SCALE_DELTA = style.ADJUST_SCALE_DELTA or 0.02
self.style = {
TRIM_POSTFIX = style.TRIM_POSTFIX or "...",
DEFAULT_ADJUST = style.DEFAULT_ADJUST or const.TEXT_ADJUST.DOWNSCALE,
ADJUST_STEPS = style.ADJUST_STEPS or 20,
ADJUST_SCALE_DELTA = style.ADJUST_SCALE_DELTA or 0.02
}
end
--- The Text constructor
---The Text constructor
---@param node string|node Node name or GUI Text Node itself
---@param value string|nil Initial text. Default value is node text from GUI scene. Default: nil
---@param adjust_type string|nil Adjust type for text. By default is DOWNSCALE. Look const.TEXT_ADJUST for reference. Default: DOWNSCALE
@ -357,7 +279,7 @@ function M:on_layout_change()
end
--- Calculate text width with font with respect to trailing space
---Calculate text width with font with respect to trailing space
---@param text string|nil
---@return number Width
---@return number Height
@ -381,7 +303,7 @@ function M:get_text_size(text)
end
--- Get chars count by width
---Get chars count by width
---@param width number
---@return number Chars count
function M:get_text_index_by_width(width)
@ -414,7 +336,7 @@ function M:get_text_index_by_width(width)
end
--- Set text to text field
---Set text to text field
---@deprecated
---@param set_to string Text for node
---@return druid.text Current text instance
@ -443,7 +365,7 @@ function M:get_text()
end
--- Set text area size
---Set text area size
---@param size vector3 The new text area size
---@return druid.text self Current text instance
function M:set_size(size)
@ -457,7 +379,7 @@ function M:set_size(size)
end
--- Set color
---Set color
---@param color vector4 Color for node
---@return druid.text Current text instance
function M:set_color(color)
@ -468,7 +390,7 @@ function M:set_color(color)
end
--- Set alpha
---Set alpha
---@param alpha number Alpha for node
---@return druid.text Current text instance
function M:set_alpha(alpha)
@ -479,7 +401,7 @@ function M:set_alpha(alpha)
end
--- Set scale
---Set scale
---@param scale vector3 Scale for node
---@return druid.text Current text instance
function M:set_scale(scale)
@ -490,7 +412,7 @@ function M:set_scale(scale)
end
--- Set text pivot. Text will re-anchor inside text area
---Set text pivot. Text will re-anchor inside text area
---@param pivot userdata The gui.PIVOT_* constant
---@return druid.text Current text instance
function M:set_pivot(pivot)
@ -537,7 +459,7 @@ function M:set_text_adjust(adjust_type, minimal_scale)
end
--- Set minimal scale for DOWNSCALE_LIMITED or SCALE_THEN_SCROLL adjust types
---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
function M:set_minimal_scale(minimal_scale)
@ -547,7 +469,7 @@ function M:set_minimal_scale(minimal_scale)
end
--- Return current text adjust type
---Return current text adjust type
---@return string adjust_type The current text adjust type
function M:get_text_adjust()
return self.adjust_type

View File

@ -113,10 +113,10 @@ end
---Convert hsb color to rgb color
---@param r number @Red value
---@param g number @Green value
---@param b number @Blue value
---@param alpha number|nil @Alpha value. Default is 1
---@param r number Red value
---@param g number Green value
---@param b number Blue value
---@param alpha number|nil Alpha value. Default is 1
function M.rgb2hsb(r, g, b, alpha)
alpha = alpha or 1
local min, max = math.min(r, g, b), math.max(r, g, b)
@ -143,10 +143,10 @@ end
---Convert hsb color to rgb color
---@param h number @Hue
---@param s number @Saturation
---@param v number @Value
---@param alpha number|nil @Alpha value. Default is 1
---@param h number Hue
---@param s number Saturation
---@param v number Value
---@param alpha number|nil Alpha value. Default is 1
function M.hsb2rgb(h, s, v, alpha)
local r, g, b
local i = math.floor(h * 6)
@ -170,9 +170,9 @@ end
---Convert rgb color to hex color
---@param red number @Red value
---@param green number @Green value
---@param blue number @Blue value
---@param red number Red value
---@param green number Green value
---@param blue number Blue value
function M.rgb2hex(red, green, blue)
local r = string.format("%x", math.floor(red * 255))
local g = string.format("%x", math.floor(green * 255))

View File

@ -241,7 +241,7 @@ function M:get_parent_component()
end
--- Setup component context and his style table
---Setup component context and his style table
---@param druid_instance druid.instance The parent druid instance
---@param context table Druid context. Usually it is self of script
---@param style table Druid style module
@ -272,21 +272,21 @@ function M:setup_component(druid_instance, context, style, instance_class)
end
--- Return true, if input priority was changed
---Return true, if input priority was changed
---@private
function M:_is_input_priority_changed()
return self._component._is_input_priority_changed
end
--- Reset is_input_priority_changed field
---Reset is_input_priority_changed field
---@private
function M:_reset_input_priority_changed()
self._component._is_input_priority_changed = false
end
--- Get current component interests
---Get current component interests
---@return table List of component interests
---@private
function M:__get_interests()
@ -351,7 +351,7 @@ function M:__remove_child(child)
end
--- Return all children components, recursive
---Return all children components, recursive
---@return table Array of childrens if the Druid component instance
function M:get_childrens()
local childrens = {}

View File

@ -114,5 +114,4 @@ M.SIDE = {
Y = "y"
}
return M

View File

@ -1,38 +1,38 @@
-- Copyright (c) 2022 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Druid Rich Input custom component.
---Druid Rich Input custom component.
-- It's wrapper on Input component with cursor and placeholder text
-- @module RichInput
-- @alias druid.rich_input
--- The component druid instance
---The component druid instance
-- @tfield DruidInstance druid DruidInstance
--- Root node
---Root node
-- @tfield node root
--- On input field text change callback(self, input_text)
---On input field text change callback(self, input_text)
-- @tfield Input input Input
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield node cursor
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield node cursor_text
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield vector3 cursor_position
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield druid.text input_text
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield druid.drag drag
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield druid.text placeholder
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield vector3 text_position
---
@ -120,7 +120,7 @@ local function on_unselect(self)
end
--- Update selection
---Update selection
local function update_selection(self)
update_text(self)
end
@ -264,7 +264,7 @@ function M:on_input(action_id, action)
end
--- Set placeholder text
---Set placeholder text
---@param placeholder_text string The placeholder text
function M:set_placeholder(placeholder_text)
self.placeholder:set_text(placeholder_text)
@ -272,13 +272,13 @@ function M:set_placeholder(placeholder_text)
end
--- Select input field
---Select input field
function M:select()
self.input:select()
end
--- Set input field text
---Set input field text
---@param text string The input text
---@return druid.rich_input self Current instance
function M:set_text(text)
@ -289,7 +289,7 @@ function M:set_text(text)
end
--- Set input field font
---Set input field font
---@param font hash The font hash
---@return druid.rich_input self Current instance
function M:set_font(font)
@ -300,13 +300,13 @@ function M:set_font(font)
end
--- Set input field text
---Set input field text
function M:get_text()
return self.input:get_text()
end
--- Set allowed charaters for input field.
---Set allowed charaters for input field.
-- See: https://defold.com/ref/stable/string/
-- ex: [%a%d] for alpha and numeric
---@param characters string Regulax exp. for validate user input

View File

@ -50,7 +50,7 @@ local function compare_words(one, two)
end
--- Get the length of a text ignoring any tags except image tags
---Get the length of a text ignoring any tags except image tags
-- which are treated as having a length of 1
-- @param text String with text or a list of words (from richtext.create)
-- @return Length of text
@ -152,12 +152,12 @@ end
-- Create rich text gui nodes from text
--- @param text string The text to create rich text nodes from
--- @param settings table Optional settings table (refer to documentation for details)
--- @param style druid.rich_text.style
--- @return druid.rich_text.word[]
--- @return druid.rich_text.settings
--- @return druid.rich_text.lines_metrics
---@param text string The text to create rich text nodes from
---@param settings table Optional settings table (refer to documentation for details)
---@param style druid.rich_text.style
---@return druid.rich_text.word[]
---@return druid.rich_text.settings
---@return druid.rich_text.lines_metrics
function M.create(text, settings, style)
assert(text, "You must provide a text")
@ -522,7 +522,7 @@ function M.is_fit_info_area(lines, settings)
end
--- Get all words with a specific tag
---Get all words with a specific tag
-- @param words The words to search (as received from richtext.create)
-- @param tag The tag to search for. Nil to search for words without a tag
-- @return Words matching the tag

View File

@ -107,7 +107,7 @@ local function merge_tags(dst, src)
end
--- Parse the text into individual words
---Parse the text into individual words
-- @param text The text to parse
-- @param default_settings Default settings for each word
-- @param color_aliases Color aliases table
@ -184,7 +184,7 @@ function M.parse(text, default_settings, style)
end
--- Get the length of a text, excluding any tags (except image and spine tags)
---Get the length of a text, excluding any tags (except image and spine tags)
function M.length(text)
return utf8.len(text:gsub("<img.-/>", " "):gsub("<.->", ""))
end

View File

@ -1,6 +1,6 @@
-- Copyright (c) 2022 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Druid Rich Text Custom Component.
---Druid Rich Text Custom Component.
-- <b># Overview #</b>
--
-- This custom component is inspired by <a href="https://github.com/britzl/defold-richtext" target="_blank">defold-richtext</a> by britzl.
@ -62,13 +62,13 @@
-- @within BaseComponent
-- @alias druid.rich_text
--- The component druid instance
---The component druid instance
-- @tfield DruidInstance druid DruidInstance
--- The root node of the Rich Text
---The root node of the Rich Text
-- @tfield node root
--- The text prefab node
---The text prefab node
-- @tfield node text_prefab
--
@ -150,7 +150,7 @@ local rich_text = require("druid.custom.rich_text.module.rt")
local M = component.create("rich_text")
--- The RichText constructor
---The RichText constructor
---@param text_node node|string The text node to make Rich Text
---@param value string|nil The initial text value. Default will be gui.get_text(text_node)
function M:init(text_node, value)
@ -175,7 +175,7 @@ function M:on_layout_change()
end
--- Component style params.
---Component style params.
-- You can override this component styles params in Druid styles table
-- or create your own style
-- @table style
@ -190,7 +190,7 @@ function M:on_style_change(style)
end
--- Set text for Rich Text
---Set text for Rich Text
---@param text string|nil The text to set
---@return druid.rich_text.word[] words
---@return druid.rich_text.lines_metrics line_metrics
@ -252,7 +252,7 @@ function M:set_text(text)
end
--- Get current text
---Get current text
---@return string text
function M:get_text()
return self._last_value
@ -266,7 +266,7 @@ function M:on_remove()
end
--- Clear all created words.
---Clear all created words.
function M:clear()
if self._words then
rich_text.remove(self._words)
@ -276,7 +276,7 @@ function M:clear()
end
--- Get all words, which has a passed tag.
---Get all words, which has a passed tag.
---@param tag string
---@return druid.rich_text.word[] words
function M:tagged(tag)
@ -296,14 +296,14 @@ function M:characters(word)
end
--- Get all current words.
---Get all current words.
---@return druid.rich_text.word[]
function M:get_words()
return self._words
end
--- Get current line metrics
---Get current line metrics
----@return druid.rich_text.lines_metrics
function M:get_line_metric()
return self._line_metrics

View File

@ -1,4 +1,4 @@
--- Container component
---Container component
-- Container setup in GUI
-- parent container - container that contains this container. If not, then it's a window default container or parent node
-- container pivot - the point of the parent container that will be used as a pivot point for positioning
@ -47,7 +47,7 @@ local CORNER_PIVOTS = {
}
--- The Container init
---The Container init
---@param node node Gui node
---@param mode string Layout mode
---@param callback fun(self: druid.container, size: vector3)|nil Callback on size changed
@ -118,7 +118,7 @@ function M:set_pivot(pivot)
end
--- Component style params.
---Component style params.
-- You can override this component styles params in Druid styles table
-- or create your own style
-- @table style
@ -131,7 +131,7 @@ function M:on_style_change(style)
end
--- Set new size of layout node
---Set new size of layout node
---@param width number|nil
---@param height number|nil
---@param anchor_pivot constant|nil If set will keep the corner possition relative to the new size
@ -207,7 +207,7 @@ function M:get_scale()
end
--- Set size for layout node to fit inside it
---Set size for layout node to fit inside it
---@param target_size vector3
---@return druid.container Container
function M:fit_into_size(target_size)
@ -218,7 +218,7 @@ function M:fit_into_size(target_size)
end
--- Set current size for layout node to fit inside it
---Set current size for layout node to fit inside it
---@return druid.container Container
function M:fit_into_window()
return self:fit_into_size(vmath.vector3(gui.get_width(), gui.get_height(), 0))
@ -388,7 +388,7 @@ function M:refresh()
stretch_side_y = parent.size.y - (abs(self.node_offset.y) + abs(self.node_offset.w))
end
---- Size Update (for stretch)
----Size Update (for stretch)
if self.mode == const.LAYOUT_MODE.STRETCH then
self:set_size(
abs(stretch_side_x * self.node_fill_x),
@ -522,7 +522,7 @@ function M:_on_corner_drag(x, y, corner_offset)
end
--- Set node for layout node to fit inside it. Pass nil to reset
---Set node for layout node to fit inside it. Pass nil to reset
---@param node string|node The node_id or gui.get_node(node_id)
---@return druid.container Layout
function M:fit_into_node(node)

View File

@ -1,6 +1,6 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Component to manage data for huge dataset in scroll.
---Component to manage data for huge dataset in scroll.
-- It requires Druid Scroll and Druid Grid (Static or Dynamic) components
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_data_list" target="_blank"><b>Example Link</b></a>
@ -9,22 +9,22 @@
-- @alias druid.data_list
--- The Druid scroll component
---The Druid scroll component
-- @tfield Scroll scroll Scroll
--- The Druid Grid component
---The Druid Grid component
-- @tfield StaticGrid grid StaticGrid}, @{DynamicGrid
--- The current progress of scroll posititon
---The current progress of scroll posititon
-- @tfield number scroll_progress
--- The current top index of visual elements
---The current top index of visual elements
-- @tfield number top_index
--- The current last index of visual elements
---The current last index of visual elements
-- @tfield number last_index
--- Event triggered when scroll progress is changed; event(self, progress_value)
---Event triggered when scroll progress is changed; event(self, progress_value)
-- @tfield event on_scroll_progress_change event
---On DataList visual element created Event callback(self, index, node, instance)
@ -55,7 +55,7 @@ local event = require("event.event")
local M = component.create("data_list")
--- The DataList constructor
---The DataList constructor
---@param scroll druid.scroll The Scroll instance for Data List component
---@param grid druid.grid The StaticGrid} or @{DynamicGrid instance for Data List component
---@param create_function function The create function callback(self, data, index, data_list). Function should return (node, [component])
@ -85,14 +85,14 @@ function M:init(scroll, grid, create_function)
end
--- Druid System on_remove function
---Druid System on_remove function
function M:on_remove()
self:clear()
self.scroll.on_scroll:unsubscribe(self._refresh, self)
end
--- Set refresh function for DataList component
---Set refresh function for DataList component
---@param is_use_cache boolean Use cache version of DataList. Requires make setup of components in on_element_add callback and clean in on_element_remove
---@return druid.data_list Current DataList instance
function M:set_use_cache(is_use_cache)
@ -101,7 +101,7 @@ function M:set_use_cache(is_use_cache)
end
--- Set new data set for DataList component
---Set new data set for DataList component
---@param data table The new data array
---@return druid.data_list Current DataList instance
function M:set_data(data)
@ -112,14 +112,14 @@ function M:set_data(data)
end
--- Return current data from DataList component
---Return current data from DataList component
---@return table The current data array
function M:get_data()
return self._data
end
--- Add element to DataList. Currenly untested
---Add element to DataList. Currenly untested
---@param data table
---@param index number|nil
---@param shift_policy number|nil The constant from const.SHIFT.*
@ -132,7 +132,7 @@ function M:add(data, index, shift_policy)
end
--- Remove element from DataList. Currenly untested
---Remove element from DataList. Currenly untested
---@param index number|nil
---@param shift_policy number|nil The constant from const.SHIFT.*
function M:remove(index, shift_policy)
@ -141,7 +141,7 @@ function M:remove(index, shift_policy)
end
--- Remove element from DataList by data value. Currenly untested
---Remove element from DataList by data value. Currenly untested
---@param data table
---@param shift_policy number|nil The constant from const.SHIFT.*
function M:remove_by_data(data, shift_policy)
@ -153,14 +153,14 @@ function M:remove_by_data(data, shift_policy)
end
--- Clear the DataList and refresh visuals
---Clear the DataList and refresh visuals
function M:clear()
self._data = {}
self:_refresh()
end
--- Return index for data value
---Return index for data value
---@param data table
function M:get_index(data)
for index, value in pairs(self._data) do
@ -173,7 +173,7 @@ function M:get_index(data)
end
--- Return all currenly created nodes in DataList
---Return all currenly created nodes in DataList
---@return node[] List of created nodes
function M:get_created_nodes()
local nodes = {}
@ -186,7 +186,7 @@ function M:get_created_nodes()
end
--- Return all currenly created components in DataList
---Return all currenly created components in DataList
---@return druid.component[] List of created nodes
function M:get_created_components()
local components = {}
@ -199,7 +199,7 @@ function M:get_created_components()
end
--- Instant scroll to element with passed index
---Instant scroll to element with passed index
---@param index number
function M:scroll_to_index(index)
local pos = self.grid:get_pos(index)
@ -207,7 +207,7 @@ function M:scroll_to_index(index)
end
--- Add element at passed index using cache or create new
---Add element at passed index using cache or create new
---@param index number
---@private
function M:_add_at(index)
@ -240,7 +240,7 @@ function M:_add_at(index)
end
--- Remove element from passed index and add it to cache if applicable
---Remove element from passed index and add it to cache if applicable
---@param index number
---@private
function M:_remove_at(index)
@ -270,7 +270,7 @@ end
--- Refresh all elements in DataList
---Refresh all elements in DataList
---@private
function M:_refresh()
self.scroll:set_size(self.grid:get_size_for(#self._data))

View File

@ -1,25 +1,25 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Druid hotkey component
---Druid hotkey component
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_hotkey" target="_blank"><b>Example Link</b></a>
-- @module Hotkey
-- @within BaseComponent
-- @alias druid.hotkey
--- On hotkey released callback(self, argument)
---On hotkey released callback(self, argument)
-- @tfield event on_hotkey_pressed event
--- On hotkey released callback(self, argument)
---On hotkey released callback(self, argument)
-- @tfield event on_hotkey_released event
--- Visual node
---Visual node
-- @tfield node node
--- Button trigger node
---Button trigger node
-- @tfield node|nil click_node
--- Button component from click_node
---Button component from click_node
-- @tfield Button button Button
---
@ -37,7 +37,7 @@ local event = require("event.event")
local M = component.create("hotkey")
--- The Hotkey constructor
---The Hotkey constructor
---@param keys string[]|string The keys to be pressed for trigger callback. Should contains one key and any modificator keys
---@param callback function The callback function
---@param callback_argument any|nil The argument to pass into the callback function
@ -56,7 +56,7 @@ function M:init(keys, callback, callback_argument)
end
--- Component style params.
---Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
@ -71,7 +71,7 @@ function M:on_style_change(style)
end
--- Add hotkey for component callback
---Add hotkey for component callback
---@param keys string[]|hash[]|string|hash that have to be pressed before key pressed to activate
---@param callback_argument any|nil The argument to pass into the callback function
---@return druid.hotkey Current instance
@ -186,7 +186,7 @@ function M:on_input(action_id, action)
end
--- If true, the callback will be triggered on action.repeated
---If true, the callback will be triggered on action.repeated
---@param is_enabled_repeated bool The flag value
---@return druid.hotkey
function M:set_repeat(is_enabled_repeated)

View File

@ -1,6 +1,6 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Druid input text component.
---Druid input text component.
-- Carry on user text input
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_input" target="_blank"><b>Example Link</b></a>
@ -9,73 +9,73 @@
-- @within BaseComponent
-- @alias druid.input
--- On input field select callback(self, input_instance)
---On input field select callback(self, input_instance)
-- @tfield event on_input_select event
--- On input field unselect callback(self, input_text, input_instance)
---On input field unselect callback(self, input_text, input_instance)
-- @tfield event on_input_unselect event
--- On input field text change callback(self, input_text)
---On input field text change callback(self, input_text)
-- @tfield event on_input_text event
--- On input field text change to empty string callback(self, input_text)
---On input field text change to empty string callback(self, input_text)
-- @tfield event on_input_empty event
--- On input field text change to max length string callback(self, input_text)
---On input field text change to max length string callback(self, input_text)
-- @tfield event on_input_full event
--- On trying user input with not allowed character callback(self, params, input_text)
---On trying user input with not allowed character callback(self, params, input_text)
-- @tfield event on_input_wrong event
--- On cursor position change callback(self, cursor_index, start_index, end_index)
---On cursor position change callback(self, cursor_index, start_index, end_index)
-- @tfield event on_select_cursor_change event
--- The cursor index. The index of letter cursor after. Leftmost cursor - 0
---The cursor index. The index of letter cursor after. Leftmost cursor - 0
-- @tfield number cursor_index
--- The selection start index. The index of letter cursor after. Leftmost selection - 0
---The selection start index. The index of letter cursor after. Leftmost selection - 0
-- @tfield number start_index
--- Theselection end index. The index of letter cursor before. Rightmost selection - #text
---Theselection end index. The index of letter cursor before. Rightmost selection - #text
-- @tfield number end_index
--- Text component
---Text component
-- @tfield Text text Text
--- Current input value
---Current input value
-- @tfield string value
--- Previous input value
---Previous input value
-- @tfield string previous_value
--- Current input value with marked text
---Current input value with marked text
-- @tfield string current_value
--- Marked text for input field. Info: https://defold.com/manuals/input-key-and-text/#marked-text
---Marked text for input field. Info: https://defold.com/manuals/input-key-and-text/#marked-text
-- @tfield string marked_value
--- Text width
---Text width
-- @tfield number text_width
--- Marked text width
---Marked text width
-- @tfield number marked_text_width
--- Button component
---Button component
-- @tfield Button button Button
--- Is current input selected now
---Is current input selected now
-- @tfield boolean is_selected
--- Is current input is empty now
---Is current input is empty now
-- @tfield boolean is_empty
--- Max length for input text
---Max length for input text
-- @tfield number|nil max_length
--- Pattern matching for user input
---Pattern matching for user input
-- @tfield string|nil allowerd_characters
--- Gui keyboard type for input field
---Gui keyboard type for input field
-- @tfield number keyboard_type
---
@ -108,7 +108,7 @@ M.ALLOWED_ACTIONS = {
[const.ACTION_ESC] = true,
}
--- Mask text by replacing every character with a mask character
---Mask text by replacing every character with a mask character
---@param text string
---@param mask string
---@return string Masked text
@ -132,7 +132,7 @@ local function clear_and_select(self)
end
--- Component style params.
---Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
@ -155,7 +155,7 @@ function M:on_style_change(style)
end
--- The Input constructor
---The Input constructor
---@param click_node node Node to enabled input component
---@param text_node node|druid.text Text node what will be changed on user input. You can pass text component instead of text node name Text
---@param keyboard_type number|nil Gui keyboard type for input field
@ -326,7 +326,7 @@ function M:get_text_selected()
return utf8.sub(self.value, self.start_index + 1, self.end_index)
end
--- Replace selected text with new text
---Replace selected text with new text
---@param text string The text to replace selected text
---@return string New input text
function M:get_text_selected_replaced(text)
@ -343,7 +343,7 @@ function M:get_text_selected_replaced(text)
end
--- Set text for input field
---Set text for input field
---@param input_text string The string to apply for input field
function M:set_text(input_text)
input_text = tostring(input_text or "")
@ -391,7 +391,7 @@ function M:set_text(input_text)
end
--- Select input field. It will show the keyboard and trigger on_select events
---Select input field. It will show the keyboard and trigger on_select events
function M:select()
gui.reset_keyboard()
self.marked_value = ""
@ -415,7 +415,7 @@ function M:select()
end
--- Remove selection from input. It will hide the keyboard and trigger on_unselect events
---Remove selection from input. It will hide the keyboard and trigger on_unselect events
function M:unselect()
gui.reset_keyboard()
self.marked_value = ""
@ -433,7 +433,7 @@ function M:unselect()
end
--- Return current input field text
---Return current input field text
---@return string The current input field text
function M:get_text()
if self.marked_value ~= "" then
@ -444,7 +444,7 @@ function M:get_text()
end
--- Set maximum length for input field.
---Set maximum length for input field.
-- Pass nil to make input field unliminted (by default)
---@param max_length number Maximum length for input text field
---@return druid.input Current input instance
@ -454,7 +454,7 @@ function M:set_max_length(max_length)
end
--- Set allowed charaters for input field.
---Set allowed charaters for input field.
-- See: https://defold.com/ref/stable/string/
-- ex: [%a%d] for alpha and numeric
---@param characters string Regulax exp. for validate user input
@ -465,7 +465,7 @@ function M:set_allowed_characters(characters)
end
--- Reset current input selection and return previous value
---Reset current input selection and return previous value
---@return druid.input Current input instance
function M:reset_changes()
self:set_text(self.previous_value)
@ -474,7 +474,7 @@ function M:reset_changes()
end
--- Set cursor position in input field
---Set cursor position in input field
---@param cursor_index number|nil Cursor index for cursor position, if nil - will be set to the end of the text
---@param start_index number|nil Start index for cursor position, if nil - will be set to the end of the text
---@param end_index number|nil End index for cursor position, if nil - will be set to the start_index
@ -496,7 +496,7 @@ function M:select_cursor(cursor_index, start_index, end_index)
end
--- Change cursor position by delta
---Change cursor position by delta
---@param delta number side for cursor position, -1 for left, 1 for right
---@param is_add_to_selection boolean (Shift key)
---@param is_move_to_end boolean (Ctrl key)

View File

@ -1,6 +1,6 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Component to wrap over GUI Text nodes with localization helpers
---Component to wrap over GUI Text nodes with localization helpers
--
-- <b># Overview #</b>
--
@ -17,13 +17,13 @@
-- @within BaseComponent
-- @alias druid.lang_text
--- On change text callback
---On change text callback
-- @tfield event on_change event
--- The text component
---The text component
-- @tfield Text text Text
--- Text node
---Text node
-- @tfield node node
---
@ -41,7 +41,7 @@ local component = require("druid.component")
local M = component.create("lang_text")
--- The LangText constructor
---The LangText constructor
---@param node string|node The node_id or gui.get_node(node_id)
---@param locale_id string|nil Default locale id or text from node as default
---@param adjust_type string|nil Adjust type for text. By default is DOWNSCALE. Look const.TEXT_ADJUST for reference
@ -67,7 +67,7 @@ function M:on_language_change()
end
--- Setup raw text to lang_text component
---Setup raw text to lang_text component
---@param text string Text for text node
---@return druid.lang_text Current instance
function M:set_to(text)
@ -79,7 +79,7 @@ function M:set_to(text)
end
--- Setup raw text to lang_text component
---Setup raw text to lang_text component
---@param text string Text for text node
---@return druid.lang_text Current instance
function M:set_text(text)
@ -87,7 +87,7 @@ function M:set_text(text)
end
--- Translate the text by locale_id
---Translate the text by locale_id
---@param locale_id string Locale id
---@param ... string Optional params for string.format
---@return druid.lang_text Current instance
@ -100,7 +100,7 @@ function M:translate(locale_id, ...)
end
--- Format string with new text params on localized text
---Format string with new text params on localized text
---@param ... string Optional params for string.format
---@return druid.lang_text Current instance
function M:format(...)

View File

@ -1,6 +1,6 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Druid component to handle the progress bars.
---Druid component to handle the progress bars.
-- <b># Overview #</b>
--
-- <b># Notes #</b>
@ -19,27 +19,27 @@
-- @within BaseComponent
-- @alias druid.progress
--- On progress bar change callback(self, new_value)
---On progress bar change callback(self, new_value)
-- @tfield event on_change event
--- Progress bar fill node
---Progress bar fill node
-- @tfield node node
--- The progress bar direction.
---The progress bar direction.
--
-- The values are: "x" or "y". (const.SIDE.X or const.SIDE.Y)
-- @tfield string key
--- Current progress bar scale
---Current progress bar scale
-- @tfield vector3 scale
--- Current progress bar size
---Current progress bar size
-- @tfield vector3 size
--- Maximum size of progress bar
---Maximum size of progress bar
-- @tfield number max_size
--- Progress bar slice9 settings
---Progress bar slice9 settings
-- @tfield vector4 slice
---
@ -117,7 +117,7 @@ local function set_bar_to(self, set_to, is_silent)
end
--- Component style params.
---Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
@ -130,7 +130,7 @@ function M:on_style_change(style)
end
--- The Progress constructor
---The Progress constructor
---@param node string|node Node name or GUI Node itself.
---@param key string Progress bar direction: const.SIDE.X or const.SIDE.Y
---@param init_value number|nil Initial value of progress bar. Default: 1
@ -191,19 +191,19 @@ function M:update(dt)
end
--- Fill a progress bar and stop progress animation
---Fill a progress bar and stop progress animation
function M:fill()
set_bar_to(self, 1, true)
end
--- Empty a progress bar
---Empty a progress bar
function M:empty()
set_bar_to(self, 0, true)
end
--- Instant fill progress bar to value
---Instant fill progress bar to value
---@param to number Progress bar value, from 0 to 1
function M:set_to(to)
to = helper.clamp(to, 0, 1)
@ -211,13 +211,13 @@ function M:set_to(to)
end
--- Return current progress bar value
---Return current progress bar value
function M:get()
return self.last_value
end
--- Set points on progress bar to fire the callback
---Set points on progress bar to fire the callback
---@param steps number[] Array of progress bar values
---@param callback function Callback on intersect step value
-- @usage progress:set_steps({0, 0.3, 0.6, 1}, function(self, step) end)
@ -227,7 +227,7 @@ function M:set_steps(steps, callback)
end
--- Start animation of a progress bar
---Start animation of a progress bar
---@param to number value between 0..1
---@param callback function|nil Callback on animation ends
function M:to(to, callback)
@ -245,7 +245,7 @@ function M:to(to, callback)
end
--- Set progress bar max node size
---Set progress bar max node size
---@param max_size vector3 The new node maximum (full) size
---@return druid.progress Progress
function M:set_max_size(max_size)

View File

@ -1,37 +1,37 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
--- Druid slider component
---Druid slider component
--
-- <a href="https://insality.github.io/druid/druid/index.html?example=general_sliders" target="_blank"><b>Example Link</b></a>
-- @module Slider
-- @within BaseComponent
-- @alias druid.slider
--- On change value callback(self, value)
---On change value callback(self, value)
-- @tfield event on_change_value event
--- Slider pin node
---Slider pin node
-- @tfield node node
--- Start pin node position
---Start pin node position
-- @tfield vector3 start_pos
--- Current pin node position
---Current pin node position
-- @tfield vector3 pos
--- Targer pin node position
---Targer pin node position
-- @tfield vector3 target_pos
--- End pin node position
---End pin node position
-- @tfield vector3 end_pos
--- Length between start and end position
---Length between start and end position
-- @tfield vector3 dist
--- Current drag state
---Current drag state
-- @tfield boolean is_drag
--- Current slider value
---Current slider value
-- @tfield number value
---
@ -68,7 +68,7 @@ local function set_position(self, value)
end
--- The Slider constructor
---The Slider constructor
---@param node node Gui pin node
---@param end_pos vector3 The end position of slider
---@param callback function|nil On slider change callback
@ -195,7 +195,7 @@ function M:on_input(action_id, action)
end
--- Set value for slider
---Set value for slider
---@param value number Value from 0 to 1
---@param is_silent boolean|nil Don't trigger event if true
function M:set(value, is_silent)
@ -208,7 +208,7 @@ function M:set(value, is_silent)
end
--- Set slider steps. Pin node will
---Set slider steps. Pin node will
-- apply closest step position
---@param steps number[] Array of steps
-- @usage slider:set_steps({0, 0.2, 0.6, 1})
@ -219,7 +219,7 @@ function M:set_steps(steps)
end
--- Set input zone for slider.
---Set input zone for slider.
-- User can touch any place of node, pin instantly will
-- move at this position and node drag will start.
-- This function require the Defold version 1.3.0+
@ -236,14 +236,14 @@ function M:set_input_node(input_node)
end
--- Set Slider input enabled or disabled
---Set Slider input enabled or disabled
---@param is_enabled boolean
function M:set_enabled(is_enabled)
self._is_enabled = is_enabled
end
--- Check if Slider component is enabled
---Check if Slider component is enabled
---@return boolean
function M:is_enabled()
return self._is_enabled

View File

@ -96,7 +96,7 @@ function M:on_input_interrupt()
end
--- Strict swipe click area. Useful for
---Strict swipe click area. Useful for
-- restrict events outside stencil node
---@param zone node|string|nil Gui node
function M:set_click_zone(zone)

View File

@ -7,7 +7,7 @@ local gui_pick_node = gui.pick_node
---The helper module contains various functions that are used in the Druid library.
---You can use these functions in your projects as well.
---@class druid.system.helper
---@class druid.helper
local M = {}
local POSITION_X = hash("position.x")
@ -44,15 +44,15 @@ local function is_text_node(node)
end
--- Text node or icon node can be nil
---Text node or icon node can be nil
local function get_width(node)
return is_text_node(node) and get_text_width(node) or get_icon_width(node)
end
---Center two nodes.
--Nodes will be center around 0 x position
--text_node will be first (at left side)
---Nodes will be center around 0 x position
---text_node will be first (at left side)
---@param text_node node|nil Gui text node
---@param icon_node node|nil Gui box node
---@param margin number Offset between nodes
@ -63,8 +63,8 @@ end
---Center two nodes.
--Nodes will be center around 0 x position
--icon_node will be first (at left side)
---Nodes will be center around 0 x position
---icon_node will be first (at left side)
---@param icon_node node|nil Gui box node
---@param text_node node|nil Gui text node
---@param margin number|nil Offset between nodes
@ -115,8 +115,8 @@ end
---@param node_id string|node
---@param template string|nil @Full Path to the template
---@param nodes table<hash, node>|nil @Nodes what created with gui.clone_tree
---@param template string|nil Full Path to the template
---@param nodes table<hash, node>|nil Nodes what created with gui.clone_tree
---@return node
function M.get_node(node_id, template, nodes)
if type(node_id) ~= "string" then
@ -177,7 +177,7 @@ function M.step(current, target, step)
end
---Clamp value between min and max
---Clamp value between min and max. Works with nil values and swap min and max if needed.
---@param value number Value
---@param v1 number|nil Min value. If nil, value will be clamped to positive infinity
---@param v2 number|nil Max value If nil, value will be clamped to negative infinity
@ -450,15 +450,20 @@ function M.get_border(node, offset)
end
local TEXT_METRICS_OPTIONS = {
line_break = false,
tracking = 0,
leading = 0,
width = 0,
}
---Get text metric from GUI node.
---@param text_node node
---@return GUITextMetrics
function M.get_text_metrics_from_node(text_node)
local font_resource = gui.get_font_resource(gui.get_font(text_node))
local options = {
tracking = gui.get_tracking(text_node),
line_break = gui.get_line_break(text_node),
}
local options = TEXT_METRICS_OPTIONS
options.tracking = gui.get_tracking(text_node)
options.line_break = gui.get_line_break(text_node)
-- Gather other options only if it used in node
if options.line_break then
@ -466,6 +471,7 @@ function M.get_text_metrics_from_node(text_node)
options.leading = gui.get_leading(text_node)
end
local font_resource = gui.get_font_resource(gui.get_font(text_node))
return resource.get_text_metrics(font_resource, gui.get_text(text_node), options)
end
@ -548,16 +554,16 @@ end
---@class druid.animation_data
---@field frames table<number, table<string, number>> @List of frames with uv coordinates and size
---@field width number @Width of the animation
---@field height number @Height of the animation
---@field fps number @Frames per second
---@field current_frame number @Current frame
---@field node node @Node with flipbook animation
---@field v vector4 @Vector with UV coordinates and size
---@field frames table<number, table<string, number>> List of frames with uv coordinates and size
---@field width number Width of the animation
---@field height number Height of the animation
---@field fps number Frames per second
---@field current_frame number Current frame
---@field node node Node with flipbook animation
---@field v vector4 Vector with UV coordinates and size
---@param node node
---@param atlas_path string @Path to the atlas
---@param atlas_path string Path to the atlas
---@return druid.animation_data
function M.get_animation_data_from_node(node, atlas_path)
local atlas_data = resource.get_atlas(atlas_path)

View File

@ -204,7 +204,7 @@ local function schedule_late_init(self)
end
--- Druid class constructor
---Druid class constructor
---@param context table Druid context. Usually it is self of gui script
---@param style table? Druid style table
function M:initialize(context, style)
@ -247,7 +247,7 @@ function M:new(component, ...)
end
--- Call this in gui_script final function.
---Call this in gui_script final function.
function M:final()
local components = self.components_all
@ -266,7 +266,7 @@ function M:final()
end
--- Remove created component from Druid instance.
---Remove created component from Druid instance.
--
-- Component `on_remove` function will be invoked, if exist.
---@generic T: druid.component
@ -369,7 +369,7 @@ function M:on_input(action_id, action)
end
--- Call this in gui_script on_message function.
---Call this in gui_script on_message function.
---@param message_id hash Message_id from on_message
---@param message table Message from on_message
---@param sender url Sender from on_message
@ -410,7 +410,7 @@ function M:on_window_event(window_event)
end
--- Calls the on_language_change function in all related components
---Calls the on_language_change function in all related components
-- This one called by global druid.on_language_change, but can be
-- call manualy to update all translations
---@private

View File

@ -59,14 +59,14 @@ function M:on_click()
end
--- Set the text property of the checkbox
---Set the text property of the checkbox
---@param text string
function M:set_text_property(text)
self.text_name:set_text(text)
end
--- Set the callback function for when the checkbox value changes
---Set the callback function for when the checkbox value changes
---@param callback function
function M:on_change(callback)
self.on_change_value:subscribe(callback)

View File

@ -47,14 +47,14 @@ function M:set_text_function(callback)
end
--- Sets the text property of the slider
---Sets the text property of the slider
---@param text string
function M:set_text_property(text)
self.text_name:set_text(text)
end
--- Sets the callback function for when the slider value changes
---Sets the callback function for when the slider value changes
---@param callback fun(value:number)
function M:on_change(callback)
self.on_change_value:subscribe(callback)