Update annotations: dynamic_grid, input, lang_text, progress, radio_roup, slider, timer

This commit is contained in:
Insality
2020-10-12 00:18:37 +03:00
parent bea8e3b329
commit 56d42d6949
9 changed files with 348 additions and 332 deletions

View File

@@ -1,24 +1,41 @@
--- Component to handle placing components in row
-- @module druid.dynamic_grid
-- @module DynamicGrid
-- @within BaseComponent
-- @alias druid.dynamic_grid
--- Component events
-- @table Events
-- @tfield druid_event on_add_item On item add callback
-- @tfield druid_event on_remove_item On item remove callback
-- @tfield druid_event on_change_items On item add or remove callback
-- @tfield druid_event on_clear On grid clear callback
-- @tfield druid_event on_update_positions On update item positions callback
--- On item add callback(self, node, index)
-- @tfield druid_event on_add_item
--- On item remove callback(self, index)
-- @tfield druid_event on_remove_item
--- On item add or remove callback(self, index)
-- @tfield druid_event on_change_items
--- On grid clear callback(self)
-- @tfield druid_event on_clear
--- On update item positions callback(self)
-- @tfield druid_event on_update_positions
--- 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 size
-- @tfield vector3 node_size
--- The size of item content
-- @tfield vector4 border
--- Component fields
-- @table Fields
-- @tfield node parent Parent gui node
-- @tfield node[] nodes List of all grid nodes
-- @tfield number first_index The first index of node in grid
-- @tfield number last_index The last index of node in grid
-- @tfield vector3 node_size Item size
-- @tfield vector4 border The size of item content
local const = require("druid.const")
local Event = require("druid.event")
@@ -44,9 +61,9 @@ local AVAILABLE_PIVOTS = {
--- Component init function
-- @function dynamic_grid:init
-- @tparam DynamicGrid self
-- @tparam node parent The gui node parent, where items will be placed
function DynamicGrid:init(parent)
function DynamicGrid.init(self, parent)
self.parent = self:get_node(parent)
local parent_pivot = gui.get_pivot(self.parent)
@@ -69,17 +86,18 @@ function DynamicGrid:init(parent)
end
function DynamicGrid:on_layout_change()
function DynamicGrid.on_layout_change(self)
self:_update(true)
end
--- Return pos for grid node index
-- @function dynamic_grid:get_pos
-- @tparam DynamicGrid self
-- @tparam number index The grid element index
-- @tparam node node The node to be placed
-- @tparam[opt] number origin_index Index of nearby node
-- @treturn vector3 Node position
function DynamicGrid:get_pos(index, node, origin_index)
function DynamicGrid.get_pos(self, index, node, origin_index)
local origin_node = self.nodes[origin_index]
-- If anchor node is not exist, check around nodes
@@ -115,11 +133,11 @@ end
--- Add new node to the grid
-- @function dynamic_grid:add
-- @tparam DynamicGrid self
-- @tparam node node Gui node
-- @tparam[opt] number index The node position. By default add as last node
-- @tparam[opt=false] bool is_shift_left If true, shift all nodes to the left, otherwise shift nodes to the right
function DynamicGrid:add(node, index, is_shift_left)
function DynamicGrid.add(self, node, index, is_shift_left)
local delta = is_shift_left and -1 or 1
-- By default add node at end
@@ -157,10 +175,10 @@ end
--- Remove the item from the grid. Note that gui node will be not deleted
-- @function dynamic_grid:remove
-- @tparam DynamicGrid self
-- @tparam number index The grid node index to remove
-- @tparam[opt=false] bool is_shift_left If true, shift all nodes to the left, otherwise shift nodes to the right
function DynamicGrid:remove(index, is_shift_left)
function DynamicGrid.remove(self, index, is_shift_left)
local delta = is_shift_left and -1 or 1
assert(self.nodes[index], "No grid item at given index " .. index)
@@ -180,15 +198,16 @@ function DynamicGrid:remove(index, is_shift_left)
-- Sync grid data
self:_update()
self.on_add_item:trigger(self:get_context(), index)
self.on_remove_item:trigger(self:get_context(), index)
self.on_change_items:trigger(self:get_context(), index)
end
--- Return grid content size
-- @function dynamic_grid:get_size
-- @tparam DynamicGrid self
-- @tparam vector3 border
-- @treturn vector3 The grid content size
function DynamicGrid:get_size(border)
function DynamicGrid.get_size(self, border)
border = border or self.border
return vmath.vector3(
border.z - border.x,
@@ -198,10 +217,10 @@ end
--- Return grid index by node
-- @function dynamic_grid:get_index_by_node
-- @tparam DynamicGrid self
-- @tparam node node The gui node in the grid
-- @treturn number The node index
function DynamicGrid:get_index_by_node(node)
function DynamicGrid.get_index_by_node(self, node)
for index, node_info in pairs(self.nodes) do
if node == node_info.node then
return index
@@ -213,9 +232,9 @@ end
--- Return array of all node positions
-- @function dynamic_grid:get_all_pos
-- @tparam DynamicGrid self
-- @treturn vector3[] All grid node positions
function DynamicGrid:get_all_pos()
function DynamicGrid.get_all_pos(self)
local result = {}
for i, node in pairs(self.nodes) do
table.insert(result, gui.get_position(node))
@@ -227,10 +246,10 @@ end
--- Change set position function for grid nodes. It will call on
-- update poses on grid elements. Default: gui.set_position
-- @function dynamic_grid:set_position_function
-- @tparam DynamicGrid self
-- @tparam function callback Function on node set position
-- @treturn druid.dynamic_grid Current grid instance
function DynamicGrid:set_position_function(callback)
function DynamicGrid.set_position_function(self, callback)
self._set_position_function = callback or gui.set_position
return self
end
@@ -238,16 +257,19 @@ end
--- Clear grid nodes array. GUI nodes will be not deleted!
-- If you want to delete GUI nodes, use dynamic_grid.nodes array before grid:clear
-- @function dynamic_grid:clear
-- @tparam DynamicGrid self
-- @treturn druid.dynamic_grid Current grid instance
function DynamicGrid:clear()
function DynamicGrid.clear(self)
self.nodes = {}
self:_update()
self.on_clear:trigger(self:get_context())
return self
end
function DynamicGrid:_add_node(node, index, origin_index)
function DynamicGrid._add_node(self, node, index, origin_index)
self.nodes[index] = {
node = node,
pos = self:get_pos(index, node, origin_index),
@@ -262,10 +284,10 @@ end
--- Update grid inner state
-- @function dynamic_grid:_update
-- @tparam DynamicGrid self
-- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback
-- @local
function DynamicGrid:_update(is_instant)
function DynamicGrid._update(self, is_instant)
self:_update_indexes()
self:_update_borders()
self:_update_pos(is_instant)
@@ -273,9 +295,9 @@ end
--- Update first and last indexes of grid nodes
-- @function dynamic_grid:_update_indexes
-- @tparam DynamicGrid self
-- @local
function DynamicGrid:_update_indexes()
function DynamicGrid._update_indexes(self)
self.first_index = nil
self.last_index = nil
for index in pairs(self.nodes) do
@@ -289,9 +311,9 @@ end
--- Update grid content borders, recalculate min and max values
-- @function dynamic_grid:_update_borders
-- @tparam DynamicGrid self
-- @local
function DynamicGrid:_update_borders()
function DynamicGrid._update_borders(self)
if not self.first_index then
self.border = vmath.vector4(0)
return
@@ -318,10 +340,10 @@ end
--- Update grid nodes position
-- @function dynamic_grid:_update_indexes
-- @tparam DynamicGrid self
-- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback
-- @local
function DynamicGrid:_update_pos(is_instant)
function DynamicGrid._update_pos(self, is_instant)
local offset = self:_get_zero_offset()
for index, node in pairs(self.nodes) do
@@ -336,7 +358,7 @@ function DynamicGrid:_update_pos(is_instant)
end
function DynamicGrid:_get_next_node_pos(origin_node_index, new_node, place_side)
function DynamicGrid._get_next_node_pos(self, origin_node_index, new_node, place_side)
local node = self.nodes[origin_node_index]
local new_node_size = self:_get_node_size(new_node)
@@ -355,17 +377,17 @@ function DynamicGrid:_get_next_node_pos(origin_node_index, new_node, place_side)
end
function DynamicGrid:_get_node_size(node)
function DynamicGrid._get_node_size(self, node)
return vmath.mul_per_elem(gui.get_size(node), gui.get_scale(node))
end
--- Return elements offset for correct posing nodes. Correct posing at
-- parent pivot node (0:0) with adjusting of node sizes and anchoring
-- @function dynamic_grid:_get_zero_offset
-- @tparam DynamicGrid self
-- @treturn vector3 The offset vector
-- @local
function DynamicGrid:_get_zero_offset()
function DynamicGrid._get_zero_offset(self)
-- zero offset: center pos - border size * anchor
return vmath.vector3(
-((self.border.x + self.border.z)/2 + (self.border.z - self.border.x) * self.pivot.x),
@@ -375,7 +397,7 @@ end
--- Return side vector to correct node shifting
function DynamicGrid:_get_side_vector(side, is_forward)
function DynamicGrid._get_side_vector(self, side, is_forward)
if side == const.SIDE.X then
return is_forward and SIDE_VECTORS.RIGHT or SIDE_VECTORS.LEFT
end

View File

@@ -1,28 +1,49 @@
--- Druid input text component.
-- Carry on user text input
-- @author Part of code from Britzl gooey input component
-- @module druid.input
-- @module Input
-- @within BaseComponent
-- @alias druid.input
--- Component events
-- @table Events
-- @tfield druid_event on_input_select (self, button_node) On input field select callback
-- @tfield druid_event on_input_unselect (self, button_node) On input field unselect callback
-- @tfield druid_event on_input_text (self, input_text) On input field text change callback
-- @tfield druid_event on_input_empty (self, input_text) On input field text change to empty string callback
-- @tfield druid_event on_input_full (self, input_text) On input field text change to max length string callback
-- @tfield druid_event on_input_wrong (self, params, button_instance) On trying user input with not allowed character callback
--- On input field select callback(self, button_node)
-- @tfield druid_event on_input_select
--- On input field unselect callback(self, button_node)
-- @tfield druid_event on_input_unselect
--- On input field text change callback(self, input_text)
-- @tfield druid_event on_input_text
--- On input field text change to empty string callback(self, input_text)
-- @tfield druid_event on_input_empty
--- On input field text change to max length string callback(self, input_text)
-- @tfield druid_event on_input_full
--- On trying user input with not allowed character callback(self, params, button_instance)
-- @tfield druid_event on_input_wrong
--- Text component
-- @tfield druid.text text
--- Button component
-- @tfield druid.button button
--- Is current input selected now
-- @tfield bool is_selected
--- Is current input is empty now
-- @tfield bool is_empty
--- Max length for input text
-- @tfield[opt] number max_length
--- Pattern matching for user input
-- @tfield[opt] string allowerd_characters
--- Gui keyboard type for input field
-- @tfield number keyboard_type
--- Component fields
-- @table Fields
-- @tfield druid.text text Text component
-- @tfield druid.button button Button component
-- @tfield bool is_selected Is current input selected now
-- @tfield bool is_empty Is current input is empty now
-- @tfield[opt] number max_length Max length for input text
-- @tfield[opt] string allowerd_characters Pattern matching for user input
-- @tfield number keyboard_type Gui keyboard type for input field
local Event = require("druid.event")
local const = require("druid.const")
@@ -92,14 +113,14 @@ end
--- Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table Style
-- @table style
-- @tfield[opt=false] bool IS_LONGTAP_ERASE Is long tap will erase current input data
-- @tfield[opt=*] string MASK_DEFAULT_CHAR Default character mask for password input
-- @tfield function on_select (self, button_node) Callback on input field selecting
-- @tfield function on_unselect (self, button_node) Callback on input field unselecting
-- @tfield function on_input_wrong (self, button_node) Callback on wrong user input
-- @tfield table button_style Custom button style for input node
function Input:on_style_change(style)
function Input.on_style_change(self, style)
self.style = {}
self.style.IS_LONGTAP_ERASE = style.IS_LONGTAP_ERASE or false
@@ -117,7 +138,7 @@ function Input:on_style_change(style)
end
function Input:init(click_node, text_node, keyboard_type)
function Input.init(self, click_node, text_node, keyboard_type)
self.druid = self:get_druid(self)
self.text = self.druid:new_text(text_node)
@@ -151,7 +172,7 @@ function Input:init(click_node, text_node, keyboard_type)
end
function Input:on_input(action_id, action)
function Input.on_input(self, action_id, action)
if self.selected then
local input_text = nil
if action_id == const.ACTION_TEXT then
@@ -215,20 +236,20 @@ function Input:on_input(action_id, action)
end
function Input:on_focus_lost()
function Input.on_focus_lost(self)
unselect(self)
end
function Input:on_input_interrupt()
function Input.on_input_interrupt(self)
-- unselect(self)
end
--- Set text for input field
-- @function input:set_text
-- @tparam Input self
-- @tparam string input_text The string to apply for input field
function Input:set_text(input_text)
function Input.set_text(self, input_text)
-- Case when update with marked text
if input_text then
self.value = input_text
@@ -273,19 +294,19 @@ end
--- Return current input field text
-- @function input:get_text
-- @tparam Input self
-- @treturn string The current input field text
function Input:get_text()
function Input.get_text(self)
return self.value .. self.marked_value
end
--- Set maximum length for input field.
-- Pass nil to make input field unliminted (by default)
-- @function input:set_max_length
-- @tparam Input self
-- @tparam number max_length Maximum length for input text field
-- @treturn druid.input Current input instance
function Input:set_max_length(max_length)
function Input.set_max_length(self, max_length)
self.max_length = max_length
return self
end
@@ -294,18 +315,18 @@ end
--- Set allowed charaters for input field.
-- See: https://defold.com/ref/stable/string/
-- ex: [%a%d] for alpha and numeric
-- @function input:set_allowerd_characters
-- @tparam Input self
-- @tparam string characters Regulax exp. for validate user input
-- @treturn druid.input Current input instance
function Input:set_allowed_characters(characters)
function Input.set_allowed_characters(self, characters)
self.allowed_characters = characters
return self
end
--- Reset current input selection and return previous value
-- @function input:reset_changes
function Input:reset_changes()
-- @tparam Input self
function Input.reset_changes(self)
self:set_text(self.previous_value)
unselect(self)
end

View File

@@ -1,16 +1,15 @@
--- Component to handle all GUI texts
-- Good working with localization system
-- @module druid.lang_text
-- @module LangText
-- @within BaseComponent
-- @alias druid.lang_text
--- Component events
-- @table Events
-- @tfield druid_event on_change On change text callback
--- On change text callback
-- @tfield druid_event on_change
--- The text component
-- @tfield Text text
--- Component fields
-- @table Fields
-- @tfield druid.text text The text component
local Event = require("druid.event")
local const = require("druid.const")
@@ -21,11 +20,11 @@ local LangText = component.create("lang_text", { const.ON_LANGUAGE_CHANGE })
--- Component init function
-- @function lang_text:init
-- @tparam LangText self
-- @tparam node node The text node
-- @tparam string locale_id Default locale id
-- @tparam bool no_adjust If true, will not correct text size
function LangText:init(node, locale_id, no_adjust)
function LangText.init(self, node, locale_id, no_adjust)
self.druid = self:get_druid()
self.text = self.druid:new_text(node, locale_id, no_adjust)
self.last_locale_args = {}
@@ -38,7 +37,7 @@ function LangText:init(node, locale_id, no_adjust)
end
function LangText:on_language_change()
function LangText.on_language_change(self)
if self.last_locale then
self:translate(self.last_locale, unpack(self.last_locale_args))
end
@@ -46,9 +45,9 @@ end
--- Setup raw text to lang_text component
-- @function lang_text:set_to
-- @tparam LangText self
-- @tparam string text Text for text node
function LangText:set_to(text)
function LangText.set_to(self, text)
self.last_locale = false
self.text:set_to(text)
self.on_change:trigger()
@@ -56,9 +55,9 @@ end
--- Translate the text by locale_id
-- @function lang_text:translate
-- @tparam LangText self
-- @tparam string locale_id Locale id
function LangText:translate(locale_id, ...)
function LangText.translate(self, locale_id, ...)
self.last_locale_args = {...}
self.last_locale = locale_id or self.last_locale
self.text:set_to(settings.get_text(self.last_locale, ...))

View File

@@ -1,21 +1,30 @@
--- Basic progress bar component.
-- For correct progress bar init it should be in max size from gui
-- @module druid.progress
-- @module Progress
-- @within BaseComponent
-- @alias druid.progress
--- Component events
-- @table Events
-- @tfield druid_event on_change On progress bar change callback
--- On progress bar change callback(self, new_value)
-- @tfield druid_event on_change
--- Progress bar fill node
-- @tfield node node
--- The progress bar direction
-- @tfield string key
--- Current progress bar scale
-- @tfield vector3 scale
--- Current progress bar size
-- @tfield vector3 size
--- Maximum size of progress bar
-- @tfield number max_size
--- Progress bar slice9 settings
-- @tfield vector4 slice
--- Component fields
-- @table Fields
-- @tfield node node Progress bar fill node
-- @tfield string key The progress bar direction
-- @tfield vector3 scale Current progress bar scale
-- @tfield vector3 size Current progress bar size
-- @tfield number max_size Maximum size of progress bar
-- @tfield vector4 slice Progress bar slice9 settings
local Event = require("druid.event")
local const = require("druid.const")
@@ -70,10 +79,10 @@ end
--- Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table Style
-- @table style
-- @tfield[opt=5] number SPEED Progress bas fill rate. More -> faster
-- @tfield[opt=0.005] number MIN_DELTA Minimum step to fill progress bar
function Progress:on_style_change(style)
function Progress.on_style_change(self, style)
self.style = {}
self.style.SPEED = style.SPEED or 5
self.style.MIN_DELTA = style.MIN_DELTA or 0.005
@@ -81,11 +90,11 @@ end
--- Component init function
-- @function progress:init
-- @tparam Progress self
-- @tparam string|node node Progress bar fill node or node name
-- @tparam string key Progress bar direction: const.SIDE.X or const.SIDE.Y
-- @tparam[opt=1] number init_value Initial value of progress bar
function Progress:init(node, key, init_value)
function Progress.init(self, node, key, init_value)
assert(key == const.SIDE.X or const.SIDE.Y, "Progress bar key should be 'x' or 'y'")
self.prop = hash("scale."..key)
@@ -108,12 +117,12 @@ function Progress:init(node, key, init_value)
end
function Progress:on_layout_change()
function Progress.on_layout_change(self)
self:set_to(self.last_value)
end
function Progress:update(dt)
function Progress.update(self, dt)
if self.target then
local prev_value = self.last_value
local step = math.abs(self.last_value - self.target) * (self.style.SPEED*dt)
@@ -134,50 +143,50 @@ end
--- Fill a progress bar and stop progress animation
-- @function progress:fill
function Progress:fill()
-- @tparam Progress self
function Progress.fill(self)
set_bar_to(self, 1, true)
end
--- Empty a progress bar
-- @function progress:empty
function Progress:empty()
-- @tparam Progress self
function Progress.empty(self)
set_bar_to(self, 0, true)
end
--- Instant fill progress bar to value
-- @function progress:set_to
-- @tparam Progress self
-- @tparam number to Progress bar value, from 0 to 1
function Progress:set_to(to)
function Progress.set_to(self, to)
set_bar_to(self, to)
end
--- Return current progress bar value
-- @function progress:get
function Progress:get()
-- @tparam Progress self
function Progress.get(self)
return self.last_value
end
--- Set points on progress bar to fire the callback
-- @function progress:set_steps
-- @tparam Progress self
-- @tparam number[] steps Array of progress bar values
-- @tparam function callback Callback on intersect step value
-- @usage progress:set_steps({0, 0.3, 0.6, 1}, function(self, step) end)
function Progress:set_steps(steps, callback)
function Progress.set_steps(self, steps, callback)
self.steps = steps
self.step_callback = callback
end
--- Start animation of a progress bar
-- @function progress:to
-- @tparam Progress self
-- @tparam number to value between 0..1
-- @tparam[opt] function callback Callback on animation ends
function Progress:to(to, callback)
function Progress.to(self, to, callback)
to = helper.clamp(to, 0, 1)
-- cause of float error
local value = helper.round(to, 5)

View File

@@ -1,15 +1,14 @@
--- Radio group module
-- @module druid.radio_group
-- @module RadioGroup
-- @within BaseComponent
-- @alias druid.radio_group
--- Component events
-- @table Events
-- @tfield druid_event on_radio_click On any checkbox click
--- On any checkbox click
-- @tfield druid_event on_radio_click
--- Array of checkbox components
-- @tfield Checkbox[] checkboxes
--- Component fields
-- @table Fields
-- @tfield table checkboxes Array of checkbox components
local Event = require("druid.event")
local component = require("druid.component")
@@ -27,11 +26,11 @@ end
--- Component init function
-- @function radio_group:init
-- @tparam node[] node Array of gui node
-- @tparam RadioGroup self
-- @tparam node[] nodes Array of gui node
-- @tparam function callback Radio callback
-- @tparam[opt=node] node[] click node Array of trigger nodes, by default equals to nodes
function RadioGroup:init(nodes, callback, click_nodes)
-- @tparam[opt=node] node[] click_nodes Array of trigger nodes, by default equals to nodes
function RadioGroup.init(self, nodes, callback, click_nodes)
self.druid = self:get_druid()
self.checkboxes = {}
@@ -49,17 +48,17 @@ end
--- Set radio group state
-- @function radio_group:set_state
-- @tparam RadioGroup self
-- @tparam number index Index in radio group
function RadioGroup:set_state(index)
function RadioGroup.set_state(self, index)
on_checkbox_click(self, index)
end
--- Return radio group state
-- @function radio_group:get_state
-- @tparam RadioGroup self
-- @treturn number Index in radio group
function RadioGroup:get_state()
function RadioGroup.get_state(self)
local result = -1
for i = 1, #self.checkboxes do

View File

@@ -1,22 +1,35 @@
--- Druid slider component
-- @module druid.slider
-- @module Slider
-- @within BaseComponent
-- @alias druid.slider
--- Component events
-- @table Events
-- @tfield druid_event on_change_value On change value callback
--- On change value callback(self, value)
-- @tfield druid_event on_change_value
--- Slider pin node
-- @tfield node node
--- Start pin node position
-- @tfield vector3 start_pos
--- Current pin node position
-- @tfield vector3 pos
--- Targer pin node position
-- @tfield vector3 target_pos
--- End pin node position
-- @tfield vector3 end_pos
--- Length between start and end position
-- @tfield number dist
--- Current drag state
-- @tfield bool is_drag
--- Current slider value
-- @tfield number value
--- Component fields
-- @table Fields
-- @tfield node node Slider pin node
-- @tfield vector3 start_pos Start pin node position
-- @tfield vector3 pos Current pin node position
-- @tfield vector3 target_pos Targer pin node position
-- @tfield vector3 end_pos End pin node position
-- @tfield number dist Length between start and end position
-- @tfield bool is_drag Current drag state
-- @tfield number value Current slider value
local Event = require("druid.event")
@@ -39,11 +52,11 @@ end
--- Component init function
-- @function slider:init
-- @tparam Slider self
-- @tparam node node Gui pin node
-- @tparam vector3 end_pos The end position of slider
-- @tparam[opt] function callback On slider change callback
function Slider:init(node, end_pos, callback)
function Slider.init(self, node, end_pos, callback)
self.node = self:get_node(node)
self.start_pos = gui.get_position(self.node)
@@ -61,12 +74,12 @@ function Slider:init(node, end_pos, callback)
end
function Slider:on_layout_change()
function Slider.on_layout_change(self, )
self:set(self.value, true)
end
function Slider:on_input(action_id, action)
function Slider.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
return false
end
@@ -132,10 +145,10 @@ end
--- Set value for slider
-- @function slider:set
-- @tparam Slider self
-- @tparam number value Value from 0 to 1
-- @tparam[opt] bool is_silent Don't trigger event if true
function Slider:set(value, is_silent)
function Slider.set(self, value, is_silent)
value = helper.clamp(value, 0, 1)
set_position(self, value)
self.value = value
@@ -147,10 +160,10 @@ end
--- Set slider steps. Pin node will
-- apply closest step position
-- @function slider:set_steps
-- @tparam Slider self
-- @tparam number[] steps Array of steps
-- @usage slider:set_steps({0, 0.2, 0.6, 1})
function Slider:set_steps(steps)
function Slider.set_steps(self, steps)
self.steps = steps
end

View File

@@ -1,22 +1,31 @@
--- Component to handle GUI timers.
-- Timer updating by game delta time. If game is not focused -
-- timer will be not updated.
-- @module druid.timer
-- @module Timer
-- @within BaseComponent
-- @alias druid.timer
--- Component events
-- @table Events
-- @tfield druid_event on_tick On timer tick callback. Fire every second
-- @tfield druid_event on_set_enabled On timer change enabled state callback
-- @tfield druid_event on_timer_end On timer end callback
--- On timer tick. Fire every second callback(self, value)
-- @tfield druid_event on_tick
--- On timer change enabled state callback(self, is_enabled)
-- @tfield druid_event on_set_enabled
--- On timer end callback
-- @tfield druid_event on_timer_end(self, Timer)
--- Trigger node
-- @tfield node node
--- Initial timer value
-- @tfield number from
--- Target timer value
-- @tfield number target
--- Current timer value
-- @tfield number value
--- Component fields
-- @table Fields
-- @tfield node node Trigger node
-- @tfield number from Initial timer value
-- @tfield number target Target timer value
-- @tfield number value Current timer value
local Event = require("druid.event")
local const = require("druid.const")
@@ -28,12 +37,12 @@ local Timer = component.create("timer", { const.ON_UPDATE })
--- Component init function
-- @function timer:init
-- @tparam Timer self
-- @tparam node node Gui text node
-- @tparam number seconds_from Start timer value in seconds
-- @tparam[opt=0] number seconds_to End timer value in seconds
-- @tparam[opt] function callback Function on timer end
function Timer:init(node, seconds_from, seconds_to, callback)
function Timer.init(self, node, seconds_from, seconds_to, callback)
self.node = self:get_node(node)
seconds_from = math.max(seconds_from, 0)
seconds_to = math.max(seconds_to or 0, 0)
@@ -54,7 +63,7 @@ function Timer:init(node, seconds_from, seconds_to, callback)
end
function Timer:update(dt)
function Timer.update(self, dt)
if not self.is_on then
return
end
@@ -77,18 +86,18 @@ function Timer:update(dt)
end
--- Set text to text field
-- @function timer:set_to
-- @tparam Timer self
-- @tparam number set_to Value in seconds
function Timer:set_to(set_to)
function Timer.set_to(self, set_to)
self.last_value = set_to
gui.set_text(self.node, formats.second_string_min(set_to))
end
--- Called when update
-- @function timer:set_state
-- @tparam Timer self
-- @tparam bool is_on Timer enable state
function Timer:set_state(is_on)
function Timer.set_state(self, is_on)
self.is_on = is_on
self.on_set_enabled:trigger(self:get_context(), is_on)
@@ -96,10 +105,10 @@ end
--- Set time interval
-- @function timer:set_interval
-- @tparam Timer self
-- @tparam number from Start time in seconds
-- @tparam number to Target time in seconds
function Timer:set_interval(from, to)
function Timer.set_interval(self, from, to)
self.from = from
self.value = from
self.temp = 0