From 3ba51f3e9dcb4b67567a7794a098b39c72214a00 Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 18 Jul 2020 21:42:51 +0300 Subject: [PATCH 1/5] #79 fix remove in on_input, add grid page example --- druid/system/druid_instance.lua | 22 +- example/gui/main/main.gui | 760 +++++++++++++++++++++++++++++++ example/gui/main/main.gui_script | 3 + example/lang.lua | 2 + example/page/grid_page.lua | 57 +++ 5 files changed, 842 insertions(+), 2 deletions(-) create mode 100644 example/page/grid_page.lua diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index c06b282..0b1191c 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -135,6 +135,8 @@ function Druid.initialize(self, context, style) self._context = context self._style = style or settings.default_style self._deleted = false + self._is_input_processing = false + self._late_remove = {} self.url = msg.url() self.components = {} @@ -182,10 +184,15 @@ end -- @function druid:remove -- @tparam Component component Component instance function Druid.remove(self, component) + if self._is_input_processing then + table.insert(self._late_remove, component) + return + end + local all_components = self.components[const.ALL] -- Recursive remove all children of component - for i = 1, #all_components do + for i = #all_components, 1, -1 do local inst = all_components[i] if inst:is_child_of(component) then self:remove(inst) @@ -197,7 +204,7 @@ function Druid.remove(self, component) if component.on_remove then component:on_remove() end - table.remove(self, i) + table.remove(all_components, i) end end @@ -230,6 +237,8 @@ end -- @tparam hash action_id Action_id from on_input -- @tparam table action Action from on_input function Druid.on_input(self, action_id, action) + self._is_input_processing = true + local is_input_consumed = false is_input_consumed = process_input(action_id, action, @@ -238,6 +247,15 @@ function Druid.on_input(self, action_id, action) is_input_consumed = process_input(action_id, action, self.components[const.ON_INPUT], is_input_consumed) + self._is_input_processing = false + + if #self._late_remove > 0 then + for i = 1, #self._late_remove do + self:remove(self._late_remove[i]) + end + self._late_remove = {} + end + return is_input_consumed end diff --git a/example/gui/main/main.gui b/example/gui/main/main.gui index da41fdc..ba77057 100644 --- a/example/gui/main/main.gui +++ b/example/gui/main/main.gui @@ -9693,6 +9693,766 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } +nodes { + position { + x: 3600.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 1.0 + y: 1.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/empty" + id: "grid_page" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_STRETCH + parent: "C_Anchor" + layer: "image" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_AUTO +} +nodes { + position { + x: 0.0 + y: 200.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 400.0 + y: 400.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.8 + y: 1.0 + z: 0.8 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "grid_nodes" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_N + adjust_mode: ADJUST_MODE_FIT + parent: "grid_page" + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: -150.0 + y: 250.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "button_add" + parent: "grid_page" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/example/templates/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 130.0 + y: 60.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/button_blue" + id: "button_add/button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "button_add" + layer: "image" + inherit_alpha: true + slice9 { + x: 15.0 + y: 15.0 + z: 15.0 + w: 15.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 7.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 0.7 + y: 0.7 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Add" + font: "game" + id: "button_add/text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 0.101960786 + y: 0.2 + z: 0.6 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "button_add/button" + layer: "text" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 0.0 + shadow_alpha: 0.78 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: 0.0 + y: 250.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "button_remove" + parent: "grid_page" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/example/templates/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 130.0 + y: 60.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/button_blue" + id: "button_remove/button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "button_remove" + layer: "image" + inherit_alpha: true + slice9 { + x: 15.0 + y: 15.0 + z: 15.0 + w: 15.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 7.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 0.7 + y: 0.7 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Remove" + font: "game" + id: "button_remove/text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 0.101960786 + y: 0.2 + z: 0.6 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "button_remove/button" + layer: "text" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 0.0 + shadow_alpha: 0.78 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: 150.0 + y: 250.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "button_clear" + parent: "grid_page" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/example/templates/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 130.0 + y: 60.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/button_blue" + id: "button_clear/button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "button_clear" + layer: "image" + inherit_alpha: true + slice9 { + x: 15.0 + y: 15.0 + z: 15.0 + w: 15.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 7.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 0.7 + y: 0.7 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Clear" + font: "game" + id: "button_clear/text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 0.101960786 + y: 0.2 + z: 0.6 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "button_clear/button" + layer: "text" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 0.0 + shadow_alpha: 0.78 + overridden_fields: 8 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: -160.0 + y: 160.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 80.0 + y: 80.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/empty" + id: "grid_nodes_prefab" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "grid_page" + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.5 + y: 1.5 + z: 1.0 + w: 1.0 + } + size { + x: 36.0 + y: 36.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/slider_move" + id: "grid_nodes_dot" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "grid_nodes_prefab" + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_AUTO +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 0.75 + y: 0.75 + z: 1.0 + w: 1.0 + } + size { + x: 50.0 + y: 50.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "1" + font: "game" + id: "grid_nodes_text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "grid_nodes_dot" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 0.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} nodes { position { x: 0.0 diff --git a/example/gui/main/main.gui_script b/example/gui/main/main.gui_script index 3cf9b6a..8973264 100644 --- a/example/gui/main/main.gui_script +++ b/example/gui/main/main.gui_script @@ -9,6 +9,7 @@ local button_page = require("example.page.button_page") local scroll_page = require("example.page.scroll_page") local slider_page = require("example.page.slider_page") local input_page = require("example.page.input_page") +local grid_page = require("example.page.grid_page") local pages = { "main_page", @@ -17,6 +18,7 @@ local pages = { "scroll_page", "slider_page", "input_page", + "grid_page", } local function on_control_button(self, delta) @@ -70,6 +72,7 @@ function init(self) scroll_page.setup_page(self) slider_page.setup_page(self) input_page.setup_page(self) + grid_page.setup_page(self) init_top_panel(self) diff --git a/example/lang.lua b/example/lang.lua index f0c15e1..c75dcbb 100644 --- a/example/lang.lua +++ b/example/lang.lua @@ -9,6 +9,7 @@ local en = { scroll_page = "Scroll page", slider_page = "Slider page", input_page = "Input page", + grid_page = "Grid page", ui_section_button = "Button", ui_section_text = "Text", ui_section_timer = "Timer", @@ -27,6 +28,7 @@ local ru = { scroll_page = "Скролл", slider_page = "Слайдеры", input_page = "Текст. ввод", + grid_page = "Сетка", ui_section_button = "Кнопка", ui_section_text = "Текст", ui_section_timer = "Таймер", diff --git a/example/page/grid_page.lua b/example/page/grid_page.lua new file mode 100644 index 0000000..db6199b --- /dev/null +++ b/example/page/grid_page.lua @@ -0,0 +1,57 @@ +local M = {} + + +local function add_node(self) + local prefab = gui.get_node("grid_nodes_prefab") + local cloned = gui.clone_tree(prefab) + gui.set_enabled(cloned["grid_nodes_prefab"], true) + local index = #self.grid_nodes + 1 + gui.set_text(cloned["grid_nodes_text"], index) + + local button = self.druid:new_button(cloned["grid_nodes_prefab"], function() + print(index) + end) + table.insert(self.grid_node_buttons, button) + + self.grid_nodes:add(cloned["grid_nodes_prefab"]) +end + + +local function clear_nodes(self) + local nodes = self.grid_nodes.nodes + for i = 1, #nodes do + gui.delete_node(nodes[i]) + end + + for i = 1, #self.grid_node_buttons do + self.druid:remove(self.grid_node_buttons[i]) + end + self.grid_node_buttons = {} + + self.grid_nodes:clear() +end + + +local function remove_node(self) + -- Remove is not implemented yet +end + + +function M.setup_page(self) + self.grid_nodes = self.druid:new_grid("grid_nodes", "grid_nodes_prefab", 5) + self.grid_node_buttons = {} + gui.set_enabled(gui.get_node("grid_nodes_prefab"), false) + + for i = 1, 15 do + add_node(self) + end + + self.druid:new_button("button_add/button", add_node) + self.druid:new_button("button_clear/button", clear_nodes) + + local remove_button = self.druid:new_button("button_remove/button", remove_node) + gui.set_enabled(remove_button.node, false) +end + + +return M From a561892fb6a3c9a8adb23eb285e774eaa25f8c05 Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 18 Jul 2020 21:47:15 +0300 Subject: [PATCH 2/5] #80 fix hover function call set_enabled --- druid/base/hover.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/druid/base/hover.lua b/druid/base/hover.lua index 4fe3c33..a625307 100644 --- a/druid/base/hover.lua +++ b/druid/base/hover.lua @@ -65,7 +65,7 @@ end function M.on_input_interrupt(self) - M.set_hover(self, false) + self:set_hover(false) end @@ -109,10 +109,10 @@ function M.set_enabled(self, state) if not state then if self._is_hovered then - M.set_hover(false) + self:set_hover(false) end if self._is_mouse_hovered then - M.set_mouse_hover(false) + self:set_mouse_hover(false) end end end From f182439f2aef09c72acc23a1466122ec610799a8 Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 18 Jul 2020 22:16:24 +0300 Subject: [PATCH 3/5] #76 add params for lang text string.format --- druid/base/lang_text.lua | 8 +++++--- druid/system/settings.lua | 3 ++- example/init.script | 4 ++-- example/lang.lua | 10 ++++++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/druid/base/lang_text.lua b/druid/base/lang_text.lua index f6a90ce..e12aff8 100644 --- a/druid/base/lang_text.lua +++ b/druid/base/lang_text.lua @@ -26,6 +26,7 @@ local M = component.create("lang_text", { const.ON_LANGUAGE_CHANGE }) function M.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 = {} self.on_change = Event() @@ -37,7 +38,7 @@ end function M.on_language_change(self) if self.last_locale then - M.translate(self) + M.translate(self, self.last_locale, unpack(self.last_locale_args)) end end @@ -55,9 +56,10 @@ end --- Translate the text by locale_id -- @function lang_text:translate -- @tparam string locale_id Locale id -function M.translate(self, locale_id) +function M.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)) + self.text:set_to(settings.get_text(self.last_locale, ...)) end diff --git a/druid/system/settings.lua b/druid/system/settings.lua index 0dfe1a1..039dd60 100644 --- a/druid/system/settings.lua +++ b/druid/system/settings.lua @@ -6,7 +6,8 @@ local M = {} M.default_style = nil -function M.get_text(name) + +function M.get_text(name, ...) return "[Druid]: locales not inited" end diff --git a/example/init.script b/example/init.script index b2bfc47..4b097cd 100644 --- a/example/init.script +++ b/example/init.script @@ -7,8 +7,8 @@ local function setup_druid() sound.play("kenney:/sound#" .. name) end) - druid.set_text_function(function(lang_id) - return lang.get_locale(lang_id) + druid.set_text_function(function(lang_id, ...) + return lang.get_locale(lang_id, ...) end) druid.on_language_change() diff --git a/example/lang.lua b/example/lang.lua index c75dcbb..245f211 100644 --- a/example/lang.lua +++ b/example/lang.lua @@ -44,8 +44,14 @@ local ru = { local data = en -function M.get_locale(lang_id) - return data[lang_id] or lang_id +function M.get_locale(lang_id, ...) + local localized_text = data[lang_id] or lang_id + + if #{...} > 0 then + localized_text = string.format(localized_text, ...) + end + + return localized_text end From 40b283760817092346a8e87bd7e654c6566f0cb2 Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 19 Jul 2020 13:10:54 +0300 Subject: [PATCH 4/5] Add changelog --- docs_md/changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs_md/changelog.md b/docs_md/changelog.md index 1010700..7f397dc 100644 --- a/docs_md/changelog.md +++ b/docs_md/changelog.md @@ -99,5 +99,8 @@ Druid 0.5.0: - **Fix #71:** Blocker: blocker now correct block mouse hover event - **Fix #72:** Fix `return nil` in some `on_input` functions - **Fix #74:** Fix typo: strech -> stretch. Scroll function `set_extra_stretch_size` renamed +- **Fix #76:** Add params for lang text localization component +- **Fix #79:** Fix druid:remove inside on_input callback +- **Fix #80:** Fix hover set_enable typo function call - Add `component.tempalte.lua` as template for Druid custom component From 04c39f1ce2305b2d6dc852b83d2f16bbac0a2504 Mon Sep 17 00:00:00 2001 From: Insality Date: Tue, 8 Sep 2020 23:40:05 +0300 Subject: [PATCH 5/5] Add on layout change initial support --- druid/base/checkbox.lua | 12 +++++++----- druid/base/grid.lua | 9 +++++++-- druid/base/progress.lua | 7 ++++++- druid/base/scroll.lua | 7 ++++++- druid/base/slider.lua | 7 ++++++- druid/base/text.lua | 7 ++++++- druid/const.lua | 3 ++- example/gui/main/main.gui | 6 ++++++ 8 files changed, 46 insertions(+), 12 deletions(-) diff --git a/druid/base/checkbox.lua b/druid/base/checkbox.lua index 28307a6..7129ab8 100644 --- a/druid/base/checkbox.lua +++ b/druid/base/checkbox.lua @@ -11,10 +11,11 @@ -- @tfield[opt=node] node click_node Button trigger node -- @tfield druid.button button Button component from click_node +local const = require("druid.const") local Event = require("druid.event") local component = require("druid.component") -local M = component.create("checkbox") +local M = component.create("checkbox", { const.ON_LAYOUT_CHANGE }) local function on_click(self) @@ -53,15 +54,16 @@ function M.init(self, node, callback, click_node) end +function M.on_layout_change(self) + self:set_state(self.state, true) +end + + --- Set checkbox state -- @function checkbox:set_state -- @tparam bool state Checkbox state -- @tparam bool is_silent Don't trigger on_change_state if true function M.set_state(self, state, is_silent) - if self.state == state then - return - end - self.state = state self.style.on_change_state(self, self.node, state) diff --git a/druid/base/grid.lua b/druid/base/grid.lua index c295a4a..c509f9d 100644 --- a/druid/base/grid.lua +++ b/druid/base/grid.lua @@ -19,11 +19,12 @@ -- @tfield vector4 border The size of item content -- @tfield vector3 border_offer The border offset for correct anchor calculations +local const = require("druid.const") local Event = require("druid.event") local helper = require("druid.helper") local component = require("druid.component") -local M = component.create("grid") +local M = component.create("grid", { const.ON_LAYOUT_CHANGE }) --- Component init function @@ -87,7 +88,7 @@ local function get_pos(self, index) end -local function update_pos(self) +local function update_pos(self, is_instant) for i = 1, #self.nodes do local node = self.nodes[i] gui.set_position(node, get_pos(self, i)) @@ -97,6 +98,10 @@ local function update_pos(self) end +function M.on_layout_change(self) + update_pos(self, true) +end + --- Set grid items offset, the distance between items -- @function grid:set_offset diff --git a/druid/base/progress.lua b/druid/base/progress.lua index b1f3687..e9df28f 100644 --- a/druid/base/progress.lua +++ b/druid/base/progress.lua @@ -20,7 +20,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local M = component.create("progress", { const.ON_UPDATE }) +local M = component.create("progress", { const.ON_UPDATE, const.ON_LAYOUT_CHANGE }) local function check_steps(self, from, to, exactly) @@ -106,6 +106,11 @@ function M.init(self, node, key, init_value) end +function M.on_layout_change(self) + self:set_to(self.last_value) +end + + function M.update(self, dt) if self.target then local prev_value = self.last_value diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 35ed50e..3da85f3 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -32,7 +32,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local M = component.create("scroll", { const.ON_UPDATE }) +local M = component.create("scroll", { const.ON_UPDATE, const.ON_LAYOUT_CHANGE }) local function inverse_lerp(min, max, current) @@ -376,6 +376,11 @@ function M.init(self, view_node, content_node) end +function M.on_layout_change(self) + gui.set_position(self.content_node, self.position) +end + + function M.update(self, dt) if self.drag.is_drag then update_hand_scroll(self, dt) diff --git a/druid/base/slider.lua b/druid/base/slider.lua index e995e42..616e4e4 100644 --- a/druid/base/slider.lua +++ b/druid/base/slider.lua @@ -22,7 +22,7 @@ local helper = require("druid.helper") local const = require("druid.const") local component = require("druid.component") -local M = component.create("slider", { const.ON_INPUT_HIGH }) +local M = component.create("slider", { const.ON_INPUT_HIGH, const.ON_LAYOUT_CHANGE }) local function on_change_value(self) @@ -59,6 +59,11 @@ function M.init(self, node, end_pos, callback) end +function M.on_layout_change(self) + self:set(self.value, true) +end + + function M.on_input(self, action_id, action) if action_id ~= const.ACTION_TOUCH then return false diff --git a/druid/base/text.lua b/druid/base/text.lua index a6c7f2e..923c77f 100644 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -24,7 +24,7 @@ local Event = require("druid.event") local const = require("druid.const") local component = require("druid.component") -local M = component.create("text") +local M = component.create("text", { const.ON_LAYOUT_CHANGE }) local function update_text_size(self) @@ -107,6 +107,11 @@ function M.init(self, node, value, no_adjust) end +function M.on_layout_change(self) + self:set_to(self.last_value) +end + + --- Calculate text width with font with respect to trailing space -- @function text:get_text_width -- @tparam[opt] string text diff --git a/druid/const.lua b/druid/const.lua index 83ed071..cad9f8a 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -33,7 +33,7 @@ M.ON_MESSAGE = hash("on_message") M.ON_INPUT_HIGH = hash("on_input_high") M.ON_FOCUS_LOST = hash("on_focus_lost") M.ON_FOCUS_GAINED = hash("on_focus_gained") -M.ON_LAYOUT_CHANGE = hash("on_layout_change") +M.ON_LAYOUT_CHANGE = hash("layout_changed") M.ON_LANGUAGE_CHANGE = hash("on_language_change") @@ -63,6 +63,7 @@ M.PIVOTS = { } +-- Value is method name of component M.SPECIFIC_UI_MESSAGES = { [M.ON_FOCUS_LOST] = "on_focus_lost", [M.ON_FOCUS_GAINED] = "on_focus_gained", diff --git a/example/gui/main/main.gui b/example/gui/main/main.gui index ba77057..d5a321f 100644 --- a/example/gui/main/main.gui +++ b/example/gui/main/main.gui @@ -10961,5 +10961,11 @@ layers { name: "text_top" } material: "/builtins/materials/gui.material" +layouts { + name: "Landscape" +} +layouts { + name: "Portrait" +} adjust_reference: ADJUST_REFERENCE_PARENT max_nodes: 512