From 330dabda09fcc81bcc17825482783c190972e9ed Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 20 Mar 2025 09:46:13 +0200 Subject: [PATCH] Update --- druid/base/text.lua | 26 +++++++++++------------- druid/const.lua | 3 +-- druid/helper.lua | 5 ----- druid/styles/default/style.lua | 2 +- druid/system/druid_instance.lua | 7 +++++-- druid/widget/node_repeat/node_repeat.lua | 13 ++++++++---- example/examples/basic/examples_list.lua | 20 +++++++++--------- wiki/widgets.md | 10 ++++++--- 8 files changed, 45 insertions(+), 41 deletions(-) diff --git a/druid/base/text.lua b/druid/base/text.lua index a079ede..f84423f 100755 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -77,7 +77,7 @@ end function M:on_style_change(style) self.style = { TRIM_POSTFIX = style.TRIM_POSTFIX or "...", - DEFAULT_ADJUST = style.DEFAULT_ADJUST or const.TEXT_ADJUST.DOWNSCALE, + DEFAULT_ADJUST = style.DEFAULT_ADJUST or "downscale", ADJUST_STEPS = style.ADJUST_STEPS or 20, ADJUST_SCALE_DELTA = style.ADJUST_SCALE_DELTA or 0.02 } @@ -256,9 +256,7 @@ end ---Set text adjust, refresh the current text visuals, if needed ----Values are: "downscale", "trim", "no_adjust", "downscale_limited", ----"scroll", "scale_then_scroll", "trim_left", "scale_then_trim", "scale_then_trim_left" ----@param adjust_type string|nil See const.TEXT_ADJUST. If pass nil - use current adjust type +---@param adjust_type druid.text.adjust_type|nil The adjust type to set, values: "downscale", "trim", "no_adjust", "downscale_limited", "scroll", "scale_then_scroll", "trim_left", "scale_then_trim", "scale_then_trim_left" ---@param minimal_scale number|nil To remove minimal scale, use `text:set_minimal_scale(nil)`, if pass nil - not change minimal scale ---@return druid.text self Current text instance function M:set_text_adjust(adjust_type, minimal_scale) @@ -270,7 +268,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 self Current text instance function M:set_minimal_scale(minimal_scale) @@ -466,42 +464,42 @@ end ---@private function M:_update_adjust() - if not self.adjust_type or self.adjust_type == const.TEXT_ADJUST.NO_ADJUST then + if not self.adjust_type or self.adjust_type == "no_adjust" then self:_reset_default_scale() return end - if self.adjust_type == const.TEXT_ADJUST.DOWNSCALE then + if self.adjust_type == "downscale" then self:_update_text_area_size() end - if self.adjust_type == const.TEXT_ADJUST.TRIM then + if self.adjust_type == "trim" then self:_update_text_with_trim(self.style.TRIM_POSTFIX) end - if self.adjust_type == const.TEXT_ADJUST.TRIM_LEFT then + if self.adjust_type == "trim_left" then self:_update_text_with_trim_left(self.style.TRIM_POSTFIX) end - if self.adjust_type == const.TEXT_ADJUST.DOWNSCALE_LIMITED then + if self.adjust_type == "downscale_limited" then self:_update_text_area_size() end - if self.adjust_type == const.TEXT_ADJUST.SCROLL then + if self.adjust_type == "scroll" then self:_update_text_with_anchor_shift() end - if self.adjust_type == const.TEXT_ADJUST.SCALE_THEN_SCROLL then + if self.adjust_type == "scale_then_scroll" then self:_update_text_area_size() self:_update_text_with_anchor_shift() end - if self.adjust_type == const.TEXT_ADJUST.SCALE_THEN_TRIM then + if self.adjust_type == "scale_then_trim" then self:_update_text_area_size() self:_update_text_with_trim(self.style.TRIM_POSTFIX) end - if self.adjust_type == const.TEXT_ADJUST.SCALE_THEN_TRIM_LEFT then + if self.adjust_type == "scale_then_trim_left" then self:_update_text_area_size() self:_update_text_with_trim_left(self.style.TRIM_POSTFIX) end diff --git a/druid/const.lua b/druid/const.lua index 64f3643..0363871 100755 --- a/druid/const.lua +++ b/druid/const.lua @@ -79,8 +79,7 @@ M.LAYOUT_MODE = { STRETCH = gui.ADJUST_STRETCH, } -M.SYS_INFO = sys.get_sys_info() -M.CURRENT_SYSTEM_NAME = M.SYS_INFO.system_name +M.CURRENT_SYSTEM_NAME = sys.get_sys_info().system_name M.OS = { ANDROID = "Android", diff --git a/druid/helper.lua b/druid/helper.lua index 28d60cc..842f2f7 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -14,11 +14,6 @@ local POSITION_X = hash("position.x") local SCALE_X = hash("scale.x") local SIZE_X = hash("size.x") -M.PROP_SIZE_X = hash("size.x") -M.PROP_SIZE_Y = hash("size.y") -M.PROP_SCALE_X = hash("scale.x") -M.PROP_SCALE_Y = hash("scale.y") - local function get_text_width(text_node) if text_node then local text_metrics = M.get_text_metrics_from_node(text_node) diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 7398c74..d7931ed 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -130,7 +130,7 @@ M["input"] = { M["text"] = { TRIM_POSTFIX = "...", - DEFAULT_ADJUST = const.TEXT_ADJUST.DOWNSCALE + DEFAULT_ADJUST = "downscale" } diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 5b35acb..5c1c910 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -491,12 +491,15 @@ end ---@generic T: druid.component ---@param widget T The widget class to create ---@param template string|nil The template name used by widget ----@param nodes table|node|nil The nodes table from gui.clone_tree or prefab node to use for clone +---@param nodes table|node|string|nil The nodes table from gui.clone_tree or prefab node to use for clone or node id to clone ---@vararg any Additional arguments to pass to the widget's init function ---@return T widget The new ready to use widget function M:new_widget(widget, template, nodes, ...) local instance = create_widget(self, widget) + if type(nodes) == "string" then + nodes = gui.get_node(nodes) + end if type(nodes) == "userdata" then nodes = gui.clone_tree(nodes) --[[@as table]] end @@ -612,7 +615,7 @@ local lang_text = require("druid.extended.lang_text") ---Create LangText component ---@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 node. Default: const.TEXT_ADJUST.DOWNSCALE +---@param adjust_type string|nil Adjust type for text node. Default: "downscale" ---@return druid.lang_text lang_text The new lang text component function M:new_lang_text(node, locale_id, adjust_type) return self:new(lang_text, node, locale_id, adjust_type) diff --git a/druid/widget/node_repeat/node_repeat.lua b/druid/widget/node_repeat/node_repeat.lua index 95e5d9e..adcb0a7 100644 --- a/druid/widget/node_repeat/node_repeat.lua +++ b/druid/widget/node_repeat/node_repeat.lua @@ -8,6 +8,11 @@ local event_queue = require("druid.event_queue") ---@field time number local M = {} +M.PROP_SIZE_X = hash("size.x") +M.PROP_SIZE_Y = hash("size.y") +M.PROP_SCALE_X = hash("scale.x") +M.PROP_SCALE_Y = hash("scale.y") + function M:init(node) self.node = self:get_node(node) self.animation = nil @@ -42,10 +47,10 @@ function M:get_repeat() if not self.is_inited then return 1, 1 end - local size_x = gui.get(self.node, helper.PROP_SIZE_X) - local size_y = gui.get(self.node, helper.PROP_SIZE_Y) - local scale_x = gui.get(self.node, helper.PROP_SCALE_X) - local scale_y = gui.get(self.node, helper.PROP_SCALE_Y) + local size_x = gui.get(self.node, M.PROP_SIZE_X) + local size_y = gui.get(self.node, M.PROP_SIZE_Y) + local scale_x = gui.get(self.node, M.PROP_SCALE_X) + local scale_y = gui.get(self.node, M.PROP_SCALE_Y) local repeat_x = (size_x / self.animation.width) / scale_x local repeat_y = (size_y / self.animation.height) / scale_y diff --git a/example/examples/basic/examples_list.lua b/example/examples/basic/examples_list.lua index f9482d6..41dcfb1 100644 --- a/example/examples/basic/examples_list.lua +++ b/example/examples/basic/examples_list.lua @@ -73,11 +73,11 @@ function M.get_examples() local adjust_index = 1 local adjust_types = { - const.TEXT_ADJUST.DOWNSCALE, - const.TEXT_ADJUST.DOWNSCALE_LIMITED, - --const.TEXT_ADJUST.SCALE_THEN_SCROLL, -- works bad with container for some reason - --const.TEXT_ADJUST.SCROLL, -- works bad with container for some reason - const.TEXT_ADJUST.TRIM, + "downscale", + "downscale_limited", + --"scale_then_scroll", -- works bad with container for some reason + --"scroll", -- works bad with container for some reason + "trim", } properties_panel:add_button("ui_adjust_next", function() adjust_index = adjust_index + 1 @@ -131,11 +131,11 @@ function M.get_examples() local adjust_index = 1 local adjust_types = { - const.TEXT_ADJUST.DOWNSCALE, - const.TEXT_ADJUST.DOWNSCALE_LIMITED, - --const.TEXT_ADJUST.SCALE_THEN_SCROLL, -- works bad with container for some reason - --const.TEXT_ADJUST.SCROLL, -- works bad with container for some reason - const.TEXT_ADJUST.TRIM, + "downscale", + "downscale_limited", + --"scale_then_scroll", -- works bad with container for some reason + --"scroll", -- works bad with container for some reason + "trim", } properties_panel:add_button("ui_adjust_next", function() adjust_index = adjust_index + 1 diff --git a/wiki/widgets.md b/wiki/widgets.md index 492b26f..f19a6e3 100644 --- a/wiki/widgets.md +++ b/wiki/widgets.md @@ -11,6 +11,7 @@ Let's see a basic custom component template: ```lua local component = require("druid.component") +---@class my_component: druid.component local M = component.create("my_component") function M:init(template, nodes, output_string) @@ -25,6 +26,9 @@ end Basic components are created with the `druid:new()` function: ```lua +local template = "my_component" -- The template name on GUI scene, if nil will take nodes directly by gui.get_node() +local nodes = gui.clone_tree(gui.get_node("my_component/root")) -- We can clone component nodes and init over cloned nodes + local my_component = druid:new("my_component", template, nodes, "Hello world!") ``` @@ -64,7 +68,7 @@ Let's start from the beginning. Widgets usually consist of 2 parts: 1. GUI scene 2. Widget Lua module -Make a GUI scene of your widget (user portrait avatar panel, shop window, game panel menu, etc). Design it as you wish, but it's recommended to add one `root` node with the name `root` and make all your other nodes children of this node. This makes working with the widget much easier. +Make a GUI scene of your widget (user portrait avatar panel, shop window, game panel menu, etc). Design it as you wish, but it's recommended to add one `root` node with the id `root` and make all your other nodes children of this node. This makes working with the widget much easier. Also ofter this root will represent the widget size, so it's recommended to set it's size to the desired size of the widget. Let's create a new widget by creating a new file next to our GUI scene file: @@ -101,8 +105,8 @@ function init(self) -- In case we want to clone it and use several times we can pass the nodes table local array_of_widgets = {} for index = 1, 10 do - local nodes = gui.clone_tree(self.my_widget.root) - local widget = self.druid:new_widget(my_widget, "my_widget", nodes) + -- For widgets now we can use a root node directly instead of manually cloning the nodes + local widget = self.druid:new_widget(my_widget, "my_widget", "my_widget/root") table.insert(array_of_widgets, widget) end end