From 337090e74cca4ef113b853aaf17673d4d4fec924 Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 15 Jul 2023 12:35:05 +0300 Subject: [PATCH] Update docs, little optimizations, inline style anims --- README.md | 2 +- druid/component.lua | 8 ++---- druid/custom/rich_text/module/rt.lua | 4 +-- druid/druid.lua | 8 ++---- druid/extended/lang_text.lua | 2 +- druid/helper.lua | 20 ++++++++++++++ druid/styles/default/anims.lua | 40 ---------------------------- druid/styles/default/style.lua | 35 ++++++++++-------------- druid/system/druid_instance.lua | 26 +++++++++--------- 9 files changed, 53 insertions(+), 92 deletions(-) delete mode 100644 druid/styles/default/anims.lua diff --git a/README.md b/README.md index 5853c68..caa5a5b 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Here is full **Druid** components list. ### Extended components -> Extended components before usage should be registered in **Druid** with `druid.register()` function. +> Extended components before usage should be registered in **Druid** with [`druid.register()`](https://insality.github.io/druid/modules/Druid.html#druid.register) function. > On usage of unregistered **Druid** component the next log will be shown in the console. ``` local data_list = require("druid.extended.data_list") diff --git a/druid/component.lua b/druid/component.lua index fb29b40..dd4578f 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -126,8 +126,7 @@ function BaseComponent.get_template(self) end ---- Set current component nodes --- +--- Set current component nodes. -- Use if your component nodes was cloned with `gui.clone_tree` and you got the node tree. -- @function component:set_nodes -- @tparam BaseComponent self @{BaseComponent} @@ -501,10 +500,7 @@ function BaseComponent.get_childrens(self) local children = self._meta.children[i] table.insert(childrens, children) - local recursive_childrens = children:get_childrens() - for j = 1, #recursive_childrens do - table.insert(childrens, recursive_childrens[j]) - end + helper.add_array(childrens, children:get_childrens()) end return childrens diff --git a/druid/custom/rich_text/module/rt.lua b/druid/custom/rich_text/module/rt.lua index 5b82a09..69ee1c7 100755 --- a/druid/custom/rich_text/module/rt.lua +++ b/druid/custom/rich_text/module/rt.lua @@ -190,9 +190,7 @@ function M.create(text, settings) local words = {} for index = 1, #lines do - for jindex = 1, #lines[index] do - table.insert(words, lines[index][jindex]) - end + helper.add_array(words, lines[index]) end return words, settings, lines_metrics diff --git a/druid/druid.lua b/druid/druid.lua index fe90baf..4226953 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -172,15 +172,11 @@ function M.on_window_callback(event) for i = 1, #instances do msg.post(instances[i].url, base_component.ON_FOCUS_LOST) end - end - - if event == window.WINDOW_EVENT_FOCUS_GAINED then + elseif event == window.WINDOW_EVENT_FOCUS_GAINED then for i = 1, #instances do msg.post(instances[i].url, base_component.ON_FOCUS_GAINED) end - end - - if event == window.WINDOW_EVENT_RESIZED then + elseif event == window.WINDOW_EVENT_RESIZED then for i = 1, #instances do msg.post(instances[i].url, base_component.ON_WINDOW_RESIZED) end diff --git a/druid/extended/lang_text.lua b/druid/extended/lang_text.lua index 6b066c5..bf7eab1 100755 --- a/druid/extended/lang_text.lua +++ b/druid/extended/lang_text.lua @@ -32,7 +32,7 @@ local component = require("druid.component") local LangText = component.create("lang_text") ---- @{LangText} constructor +--- The @{LangText} constructor -- @tparam LangText self @{LangText} -- @tparam string|node node Node name or GUI Text Node itself -- @tparam string locale_id Default locale id or text from node as default diff --git a/druid/helper.lua b/druid/helper.lua index 4816881..f091675 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -251,6 +251,26 @@ function M.deepcopy(orig_table) end +--- Add all elements from source array to the target array +-- @function helper.add_array +-- @tparam table target Array to put elements from source +-- @tparam[opt] table source The source array to get elements from +-- @treturn array The target array +function M.add_array(target, source) + assert(target) + + if not source then + return target + end + + for index = 1, #source do + table.insert(target, source[index]) + end + + return target +end + + --- Get node size adjusted by scale -- @function helper.get_scaled_size -- @tparam node node GUI node diff --git a/druid/styles/default/anims.lua b/druid/styles/default/anims.lua deleted file mode 100644 index 6f13a30..0000000 --- a/druid/styles/default/anims.lua +++ /dev/null @@ -1,40 +0,0 @@ --- Copyright (c) 2021 Maksim Tuprikov . This code is licensed under MIT license - -local M = {} - - -local function scale_to(self, node, to, callback, time, delay, easing) - easing = easing or gui.EASING_INSINE - time = time or M.SCALE_ANIMATION_TIME - delay = delay or 0 - time = time or 0.10 - gui.animate(node, gui.PROP_SCALE, to, easing, time, delay, - function() - if callback then - callback(self, node) - end - end - ) -end - - -function M.back_scale_animation(self, node, target_scale) - scale_to(self, node, target_scale) -end - - -function M.tap_scale_animation(self, node, target_scale) - scale_to(self, node, target_scale, - function() - M.back_scale_animation(self, node, self.start_scale) - end - ) -end - - -function M.hover_scale(self, target, time) - gui.animate(self.anim_node, "scale", target, gui.EASING_OUTSINE, time) -end - - -return M diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 3c8a543..3b074c1 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -2,11 +2,20 @@ local const = require("druid.const") local settings = require("druid.system.settings") -local anims = require("druid.styles.default.anims") local M = {} +local function button_hover_scale(node, target, time) + gui.animate(node, "scale", target, gui.EASING_OUTSINE, time) +end + +local function button_tap_anim(node, tap_scale, start_scale) + gui.animate(node, gui.PROP_SCALE, tap_scale, gui.EASING_INSINE, 0.1, 0, function() + gui.animate(node, gui.PROP_SCALE, start_scale, gui.EASING_INSINE, 0.1) + end) +end + M["button"] = { HOVER_SCALE = vmath.vector3(0.02, 0.02, 1), HOVER_MOUSE_SCALE = vmath.vector3(0.01, 0.01, 1), @@ -24,19 +33,19 @@ M["button"] = { local scale_to = self.start_scale + M.button.HOVER_SCALE local target_scale = state and scale_to or self.start_scale - anims.hover_scale(self, target_scale, M.button.HOVER_TIME) + button_hover_scale(node, target_scale, M.button.HOVER_TIME) end, on_mouse_hover = function(self, node, state) local scale_to = self.start_scale + M.button.HOVER_MOUSE_SCALE local target_scale = state and scale_to or self.start_scale - anims.hover_scale(self, target_scale, M.button.HOVER_TIME) + button_hover_scale(node, target_scale, M.button.HOVER_TIME) end, on_click = function(self, node) local scale_to = self.start_scale + M.button.SCALE_CHANGE - anims.tap_scale_animation(self, node, scale_to) + button_tap_anim(node, scale_to, self.start_scale) settings.play_sound(M.button.BTN_SOUND) end, @@ -167,23 +176,7 @@ M["hotkey"] = { M["rich_text"] = { COLORS = { white = "#FFFFFF", - black = "#000000", - red = "#FF0000", - green = "#00FF00", - blue = "#0000FF", - yellow = "#FFFF00", - magenta = "#FF00FF", - cyan = "#00FFFF", - gray = "#808080", - dark_gray = "#404040", - light_gray = "#C0C0C0", - orange = "#FFA500", - pink = "#FFC0CB", - purple = "#800080", - brown = "#A52A2A", - olive = "#808000", - teal = "#008080", - navy = "#000080", + black = "#000000" } } diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index aba14e2..5876a56 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -115,7 +115,7 @@ local function set_input_state(self, is_input_inited) end --- a and b - two Druid components +-- The a and b - two Druid components -- @local local function sort_input_comparator(a, b) local a_priority = a:get_input_priority() @@ -478,20 +478,19 @@ end -- component will be not processed on input step -- @tparam DruidInstance self -- @tparam[opt=nil] table|Component whitelist_components The array of component to whitelist +-- @treturn self @{DruidInstance} function DruidInstance.set_whitelist(self, whitelist_components) if whitelist_components and whitelist_components.isInstanceOf then whitelist_components = { whitelist_components } end for i = 1, #whitelist_components do - local component = whitelist_components[i] - local childrens = component:get_childrens() - for j = 1, #childrens do - table.insert(whitelist_components, childrens[j]) - end + helper.add_array(whitelist_components, whitelist_components[i]:get_childrens()) end self._input_whitelist = whitelist_components + + return self end @@ -501,20 +500,19 @@ end -- component will be not processed on input step -- @tparam DruidInstance self @{DruidInstance} -- @tparam[opt=nil] table|Component blacklist_components The array of component to blacklist +-- @treturn self @{DruidInstance} function DruidInstance.set_blacklist(self, blacklist_components) if blacklist_components and blacklist_components.isInstanceOf then blacklist_components = { blacklist_components } end for i = 1, #blacklist_components do - local component = blacklist_components[i] - local childrens = component:get_childrens() - for j = 1, #childrens do - table.insert(blacklist_components, childrens[j]) - end + helper.add_array(blacklist_components, blacklist_components[i]:get_childrens()) end self._input_blacklist = blacklist_components + + return self end @@ -800,9 +798,9 @@ end -- As a template please check rich_text.gui layout. -- @tparam DruidInstance self -- @tparam[opt] string template Template name if used --- @tparam[opt] table nodes Nodes table from gui.clone_tree --- @treturn Hotkey @{RichText} component -function DruidInstance.new_hotkey(self, template, nodes) +-- @tparam[opt] table nodes Nodes table from gui.clone_tree +-- @treturn RichText @{RichText} component +function DruidInstance.new_rich_text(self, template, nodes) return helper.require_component_message("rich_text", "custom") end