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