mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 18:12:19 +02:00
Update annotations P.1
This commit is contained in:
@@ -10,25 +10,25 @@
|
||||
-- @alias druid.input
|
||||
|
||||
--- On input field select callback(self, input_instance)
|
||||
-- @tfield DruidEvent on_input_select @{DruidEvent}
|
||||
-- @tfield DruidEvent on_input_select DruidEvent
|
||||
|
||||
--- On input field unselect callback(self, input_text, input_instance)
|
||||
-- @tfield DruidEvent on_input_unselect @{DruidEvent}
|
||||
-- @tfield DruidEvent on_input_unselect DruidEvent
|
||||
|
||||
--- On input field text change callback(self, input_text)
|
||||
-- @tfield DruidEvent on_input_text @{DruidEvent}
|
||||
-- @tfield DruidEvent on_input_text DruidEvent
|
||||
|
||||
--- On input field text change to empty string callback(self, input_text)
|
||||
-- @tfield DruidEvent on_input_empty @{DruidEvent}
|
||||
-- @tfield DruidEvent on_input_empty DruidEvent
|
||||
|
||||
--- On input field text change to max length string callback(self, input_text)
|
||||
-- @tfield DruidEvent on_input_full @{DruidEvent}
|
||||
-- @tfield DruidEvent on_input_full DruidEvent
|
||||
|
||||
--- On trying user input with not allowed character callback(self, params, input_text)
|
||||
-- @tfield DruidEvent on_input_wrong @{DruidEvent}
|
||||
-- @tfield DruidEvent on_input_wrong DruidEvent
|
||||
|
||||
--- On cursor position change callback(self, cursor_index, start_index, end_index)
|
||||
-- @tfield DruidEvent on_select_cursor_change @{DruidEvent}
|
||||
-- @tfield DruidEvent on_select_cursor_change DruidEvent
|
||||
|
||||
--- The cursor index. The index of letter cursor after. Leftmost cursor - 0
|
||||
-- @tfield number cursor_index
|
||||
@@ -40,7 +40,7 @@
|
||||
-- @tfield number end_index
|
||||
|
||||
--- Text component
|
||||
-- @tfield Text text @{Text}
|
||||
-- @tfield Text text Text
|
||||
|
||||
--- Current input value
|
||||
-- @tfield string value
|
||||
@@ -61,7 +61,7 @@
|
||||
-- @tfield number marked_text_width
|
||||
|
||||
--- Button component
|
||||
-- @tfield Button button @{Button}
|
||||
-- @tfield Button button Button
|
||||
|
||||
--- Is current input selected now
|
||||
-- @tfield boolean is_selected
|
||||
@@ -87,9 +87,19 @@ local component = require("druid.component")
|
||||
local utf8_lua = require("druid.system.utf8")
|
||||
local utf8 = utf8 or utf8_lua
|
||||
|
||||
local Input = component.create("input")
|
||||
---@class druid.input: druid.base_component
|
||||
---@field on_input_select druid.event
|
||||
---@field on_input_unselect druid.event
|
||||
---@field on_input_text druid.event
|
||||
---@field on_input_empty druid.event
|
||||
---@field on_input_full druid.event
|
||||
---@field on_input_wrong druid.event
|
||||
---@field on_select_cursor_change druid.event
|
||||
---@field style table
|
||||
---@field text druid.text
|
||||
local M = component.create("input")
|
||||
|
||||
Input.ALLOWED_ACTIONS = {
|
||||
M.ALLOWED_ACTIONS = {
|
||||
[const.ACTION_TOUCH] = true,
|
||||
[const.ACTION_TEXT] = true,
|
||||
[const.ACTION_MARKED_TEXT] = true,
|
||||
@@ -132,7 +142,7 @@ end
|
||||
-- @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
|
||||
function Input.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
|
||||
self.style.IS_LONGTAP_ERASE = style.IS_LONGTAP_ERASE or false
|
||||
@@ -145,12 +155,12 @@ function Input.on_style_change(self, style)
|
||||
end
|
||||
|
||||
|
||||
--- The @{Input} constructor
|
||||
-- @tparam Input self @{Input}
|
||||
--- The Input constructor
|
||||
-- @tparam Input self Input
|
||||
-- @tparam node click_node Node to enabled input component
|
||||
-- @tparam node|Text text_node Text node what will be changed on user input. You can pass text component instead of text node name @{Text}
|
||||
-- @tparam node|Text text_node Text node what will be changed on user input. You can pass text component instead of text node name Text
|
||||
-- @tparam number|nil keyboard_type Gui keyboard type for input field
|
||||
function Input.init(self, click_node, text_node, keyboard_type)
|
||||
function M:init(click_node, text_node, keyboard_type)
|
||||
self.druid = self:get_druid()
|
||||
|
||||
if type(text_node) == "table" then
|
||||
@@ -201,8 +211,8 @@ function Input.init(self, click_node, text_node, keyboard_type)
|
||||
end
|
||||
|
||||
|
||||
function Input.on_input(self, action_id, action)
|
||||
if not (action_id == nil or Input.ALLOWED_ACTIONS[action_id]) then
|
||||
function M:on_input(action_id, action)
|
||||
if not (action_id == nil or M.ALLOWED_ACTIONS[action_id]) then
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -299,17 +309,17 @@ function Input.on_input(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
function Input.on_focus_lost(self)
|
||||
function M:on_focus_lost()
|
||||
self:unselect()
|
||||
end
|
||||
|
||||
|
||||
function Input.on_input_interrupt(self)
|
||||
function M:on_input_interrupt()
|
||||
--self:unselect()
|
||||
end
|
||||
|
||||
|
||||
function Input.get_text_selected(self)
|
||||
function M:get_text_selected()
|
||||
if self.start_index == self.end_index then
|
||||
return self.value
|
||||
end
|
||||
@@ -318,10 +328,10 @@ function Input.get_text_selected(self)
|
||||
end
|
||||
|
||||
--- Replace selected text with new text
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @tparam string text The text to replace selected text
|
||||
-- @treturn string New input text
|
||||
function Input.get_text_selected_replaced(self, text)
|
||||
function M:get_text_selected_replaced(text)
|
||||
local left_part = utf8.sub(self.value, 1, self.start_index)
|
||||
local right_part = utf8.sub(self.value, self.end_index + 1, utf8.len(self.value))
|
||||
local result = left_part .. text .. right_part
|
||||
@@ -336,9 +346,9 @@ end
|
||||
|
||||
|
||||
--- Set text for input field
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @tparam string input_text The string to apply for input field
|
||||
function Input.set_text(self, input_text)
|
||||
function M:set_text(input_text)
|
||||
input_text = tostring(input_text or "")
|
||||
|
||||
-- Case when update with marked text
|
||||
@@ -385,8 +395,8 @@ end
|
||||
|
||||
|
||||
--- Select input field. It will show the keyboard and trigger on_select events
|
||||
-- @tparam Input self @{Input}
|
||||
function Input.select(self)
|
||||
-- @tparam Input self Input
|
||||
function M:select()
|
||||
gui.reset_keyboard()
|
||||
self.marked_value = ""
|
||||
if not self.is_selected then
|
||||
@@ -410,8 +420,8 @@ end
|
||||
|
||||
|
||||
--- Remove selection from input. It will hide the keyboard and trigger on_unselect events
|
||||
-- @tparam Input self @{Input}
|
||||
function Input.unselect(self)
|
||||
-- @tparam Input self Input
|
||||
function M:unselect()
|
||||
gui.reset_keyboard()
|
||||
self.marked_value = ""
|
||||
self.value = self.current_value
|
||||
@@ -429,9 +439,9 @@ end
|
||||
|
||||
|
||||
--- Return current input field text
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @treturn string The current input field text
|
||||
function Input.get_text(self)
|
||||
function M:get_text()
|
||||
if self.marked_value ~= "" then
|
||||
return self.value .. self.marked_value
|
||||
end
|
||||
@@ -442,10 +452,10 @@ end
|
||||
|
||||
--- Set maximum length for input field.
|
||||
-- Pass nil to make input field unliminted (by default)
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @tparam number max_length Maximum length for input text field
|
||||
-- @treturn druid.input Current input instance
|
||||
function Input.set_max_length(self, max_length)
|
||||
function M:set_max_length(max_length)
|
||||
self.max_length = max_length
|
||||
return self
|
||||
end
|
||||
@@ -454,19 +464,19 @@ end
|
||||
--- Set allowed charaters for input field.
|
||||
-- See: https://defold.com/ref/stable/string/
|
||||
-- ex: [%a%d] for alpha and numeric
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @tparam string characters Regulax exp. for validate user input
|
||||
-- @treturn druid.input Current input instance
|
||||
function Input.set_allowed_characters(self, characters)
|
||||
function M:set_allowed_characters(characters)
|
||||
self.allowed_characters = characters
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Reset current input selection and return previous value
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @treturn druid.input Current input instance
|
||||
function Input.reset_changes(self)
|
||||
function M:reset_changes()
|
||||
self:set_text(self.previous_value)
|
||||
self:unselect()
|
||||
return self
|
||||
@@ -474,12 +484,12 @@ end
|
||||
|
||||
|
||||
--- Set cursor position in input field
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @tparam number|nil cursor_index Cursor index for cursor position, if nil - will be set to the end of the text
|
||||
-- @tparam number|nil start_index Start index for cursor position, if nil - will be set to the end of the text
|
||||
-- @tparam number|nil end_index End index for cursor position, if nil - will be set to the start_index
|
||||
-- @treturn druid.input Current input instance
|
||||
function Input.select_cursor(self, cursor_index, start_index, end_index)
|
||||
function M:select_cursor(cursor_index, start_index, end_index)
|
||||
local len = utf8.len(self.value)
|
||||
|
||||
self.cursor_index = cursor_index or len
|
||||
@@ -497,11 +507,11 @@ end
|
||||
|
||||
|
||||
--- Change cursor position by delta
|
||||
-- @tparam Input self @{Input}
|
||||
-- @tparam Input self Input
|
||||
-- @tparam number delta side for cursor position, -1 for left, 1 for right
|
||||
-- @tparam boolean is_add_to_selection (Shift key)
|
||||
-- @tparam boolean is_move_to_end (Ctrl key)
|
||||
function Input.move_selection(self, delta, is_add_to_selection, is_move_to_end)
|
||||
function M:move_selection(delta, is_add_to_selection, is_move_to_end)
|
||||
local len = utf8.len(self.value)
|
||||
local cursor_index = self.cursor_index
|
||||
local start_index, end_index -- if nil, the selection will be 0 at cursor position
|
||||
@@ -559,4 +569,4 @@ function Input.move_selection(self, delta, is_add_to_selection, is_move_to_end)
|
||||
end
|
||||
|
||||
|
||||
return Input
|
||||
return M
|
||||
|
Reference in New Issue
Block a user