This commit is contained in:
Insality
2025-03-05 22:06:06 +02:00
parent d6bec60ba9
commit 46223f0bb8
11 changed files with 557 additions and 628 deletions

View File

@@ -15,6 +15,21 @@ local helper = require("druid.helper")
local component = require("druid.component")
local event = require("event.event")
local abs = math.abs
local min = math.min
local max = math.max
local CORNER_PIVOTS = {
gui.PIVOT_NE,
gui.PIVOT_NW,
gui.PIVOT_SE,
gui.PIVOT_SW,
}
---@class druid.container.style
---@field DRAGGABLE_CORNER_SIZE vector3 Size of box node for debug draggable corners
---@field DRAGGABLE_CORNER_COLOR vector4 Color of debug draggable corners
---@class druid.container: druid.component
---@field node node
---@field druid druid.instance
@@ -35,19 +50,7 @@ local event = require("event.event")
---@field _draggable_corners table
local M = component.create("container")
local abs = math.abs
local min = math.min
local max = math.max
local CORNER_PIVOTS = {
gui.PIVOT_NE,
gui.PIVOT_NW,
gui.PIVOT_SE,
gui.PIVOT_SW,
}
---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,16 +121,12 @@ function M:set_pivot(pivot)
end
---Component style params.
-- You can override this component styles params in Druid styles table
-- or create your own style
-- @table style
-- @tfield[opt=vector3(24, 24, 0)] vector3 DRAGGABLE_CORNER_SIZE Size of box node for debug draggable corners
-- @tfield[opt=vector4(1)] vector4 DRAGGABLE_CORNER_COLOR Color of debug draggable corners
---@param style druid.container.style
function M:on_style_change(style)
self.style = {}
self.style.DRAGGABLE_CORNER_SIZE = style.DRAGGABLE_CORNER_SIZE or vmath.vector3(24, 24, 0)
self.style.DRAGGABLE_CORNER_COLOR = style.DRAGGABLE_CORNER_COLOR or vmath.vector4(10)
self.style = {
DRAGGABLE_CORNER_SIZE = style.DRAGGABLE_CORNER_SIZE or vmath.vector3(24, 24, 0),
DRAGGABLE_CORNER_COLOR = style.DRAGGABLE_CORNER_COLOR or vmath.vector4(10)
}
end

View File

@@ -1,85 +1,3 @@
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
---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>
-- @author Part of code from Britzl gooey input component
-- @module Input
-- @within BaseComponent
-- @alias druid.input
---On input field select callback(self, input_instance)
-- @tfield event on_input_select event
---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)
-- @tfield event on_input_text event
---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)
-- @tfield event on_input_full event
---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)
-- @tfield event on_select_cursor_change event
---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
-- @tfield number start_index
---Theselection end index. The index of letter cursor before. Rightmost selection - #text
-- @tfield number end_index
---Text component
-- @tfield Text text Text
---Current input value
-- @tfield string value
---Previous input value
-- @tfield string previous_value
---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
-- @tfield string marked_value
---Text width
-- @tfield number text_width
---Marked text width
-- @tfield number marked_text_width
---Button component
-- @tfield Button button Button
---Is current input selected now
-- @tfield boolean is_selected
---Is current input is empty now
-- @tfield boolean is_empty
---Max length for input text
-- @tfield number|nil max_length
---Pattern matching for user input
-- @tfield string|nil allowerd_characters
---Gui keyboard type for input field
-- @tfield number keyboard_type
---
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
@@ -87,6 +5,14 @@ local component = require("druid.component")
local utf8_lua = require("druid.system.utf8")
local utf8 = utf8 or utf8_lua
---@class druid.input.style
---@field MASK_DEFAULT_CHAR string Default character mask for password input
---@field IS_LONGTAP_ERASE boolean Is long tap will erase current input data
---@field IS_UNSELECT_ON_RESELECT boolean If true, call unselect on select selected input
---@field on_select fun(self: druid.input, button_node: node) Callback on input field selecting
---@field on_unselect fun(self: druid.input, button_node: node) Callback on input field unselecting
---@field on_input_wrong fun(self: druid.input, button_node: node) Callback on wrong user input
---@class druid.input: druid.component
---@field on_input_select event
---@field on_input_unselect event
@@ -132,30 +58,6 @@ local function clear_and_select(self)
end
---Component style params.
-- You can override this component styles params in druid styles table
-- or create your own style
-- @table style
-- @tfield boolean IS_LONGTAP_ERASE Is long tap will erase current input data. Default: false
-- @tfield string MASK_DEFAULT_CHAR Default character mask for password input. Default: *]
-- @tfield boolean IS_UNSELECT_ON_RESELECT If true, call unselect on select selected input. Default: false
-- @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 M:on_style_change(style)
self.style = {}
self.style.IS_LONGTAP_ERASE = style.IS_LONGTAP_ERASE or false
self.style.MASK_DEFAULT_CHAR = style.MASK_DEFAULT_CHAR or "*"
self.style.IS_UNSELECT_ON_RESELECT = style.IS_UNSELECT_ON_RESELECT or false
self.style.on_select = style.on_select or function(_, button_node) end
self.style.on_unselect = style.on_unselect or function(_, button_node) end
self.style.on_input_wrong = style.on_input_wrong or function(_, button_node) end
end
---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
@@ -210,6 +112,20 @@ function M:init(click_node, text_node, keyboard_type)
end
---@param style druid.input.style
function M:on_style_change(style)
self.style = {
IS_LONGTAP_ERASE = style.IS_LONGTAP_ERASE or false,
MASK_DEFAULT_CHAR = style.MASK_DEFAULT_CHAR or "*",
IS_UNSELECT_ON_RESELECT = style.IS_UNSELECT_ON_RESELECT or false,
on_select = style.on_select or function(_, button_node) end,
on_unselect = style.on_unselect or function(_, button_node) end,
on_input_wrong = style.on_input_wrong or function(_, button_node) end,
}
end
function M:on_input(action_id, action)
if not (action_id == nil or M.ALLOWED_ACTIONS[action_id]) then
return false

View File

@@ -75,16 +75,6 @@ local function set_bar_to(self, set_to, is_silent)
end
---@param style druid.progress.style
function M:on_style_change(style)
self.style = {
SPEED = style.SPEED or 5,
MIN_DELTA = style.MIN_DELTA or 0.005,
}
end
---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
@@ -114,6 +104,15 @@ function M:init(node, key, init_value)
end
---@param style druid.progress.style
function M:on_style_change(style)
self.style = {
SPEED = style.SPEED or 5,
MIN_DELTA = style.MIN_DELTA or 0.005,
}
end
function M:on_layout_change()
self:set_to(self.last_value)
end
@@ -124,10 +123,11 @@ function M:on_remove()
end
---@param dt number Delta time
function M:update(dt)
if self.target then
local prev_value = self.last_value
local step = math.abs(self.last_value - self.target) * (self.style.SPEED*dt)
local step = math.abs(self.last_value - self.target) * (self.style.SPEED * dt)
step = math.max(step, self.style.MIN_DELTA)
self:set_to(helper.step(self.last_value, self.target, step))

View File

@@ -18,17 +18,6 @@ local component = require("druid.component")
local M = component.create("slider", const.PRIORITY_INPUT_HIGH)
local function on_change_value(self)
self.on_change_value:trigger(self:get_context(), self.value)
end
local function set_position(self, value)
value = helper.clamp(value, 0, 1)
gui.set_position(self.node, self.start_pos + self.dist * value)
end
---The Slider constructor
---@param node node Gui pin node
---@param end_pos vector3 The end position of slider
@@ -141,11 +130,11 @@ function M:on_input(action_id, action)
end
if prev_value ~= self.value then
on_change_value(self)
self:_on_change_value()
end
end
set_position(self, self.value)
self:_set_position(self.value)
end
if action.released then
@@ -161,10 +150,10 @@ end
---@param is_silent boolean|nil Don't trigger event if true
function M:set(value, is_silent)
value = helper.clamp(value, 0, 1)
set_position(self, value)
self:_set_position(value)
self.value = value
if not is_silent then
on_change_value(self)
self:_on_change_value()
end
end
@@ -210,4 +199,17 @@ function M:is_enabled()
end
---@private
function M:_on_change_value()
self.on_change_value:trigger(self:get_context(), self.value)
end
---@private
function M:_set_position(value)
value = helper.clamp(value, 0, 1)
gui.set_position(self.node, self.start_pos + self.dist * value)
end
return M

View File

@@ -21,16 +21,6 @@ local component = require("druid.component")
local M = component.create("swipe")
---@param style druid.swipe.style
function M:on_style_change(style)
self.style = {
SWIPE_TIME = style.SWIPE_TIME or 0.4,
SWIPE_THRESHOLD = style.SWIPE_THRESHOLD or 50,
SWIPE_TRIGGER_ON_MOVE = style.SWIPE_TRIGGER_ON_MOVE or false,
}
end
---@param node_or_node_id node|string
---@param on_swipe_callback function
function M:init(node_or_node_id, on_swipe_callback)
@@ -55,6 +45,16 @@ function M:on_late_init()
end
---@param style druid.swipe.style
function M:on_style_change(style)
self.style = {
SWIPE_TIME = style.SWIPE_TIME or 0.4,
SWIPE_THRESHOLD = style.SWIPE_THRESHOLD or 50,
SWIPE_TRIGGER_ON_MOVE = style.SWIPE_TRIGGER_ON_MOVE or false,
}
end
---@param action_id hash
---@param action action
function M:on_input(action_id, action)
@@ -154,6 +154,4 @@ function M:_check_swipe(action)
end
return M

View File

@@ -21,7 +21,6 @@ local function second_string_min(sec)
end
---The Timer constructor
---@param node node Gui text node
---@param seconds_from number|nil Start timer value in seconds
---@param seconds_to number|nil End timer value in seconds