From 834d0714e0da4ed5e4727459da9bd730f62fe4a1 Mon Sep 17 00:00:00 2001 From: Insality Date: Mon, 9 Nov 2020 01:11:22 +0300 Subject: [PATCH] Fix static poses calculation --- druid/base/static_grid.lua | 49 +++-- example/gui/main/main.gui | 340 +++++++++++++++++++++++++++------ example/page/grid_page.lua | 16 +- example/page/infinity_page.lua | 58 ++++-- 4 files changed, 358 insertions(+), 105 deletions(-) diff --git a/druid/base/static_grid.lua b/druid/base/static_grid.lua index f965a14..7dd090f 100644 --- a/druid/base/static_grid.lua +++ b/druid/base/static_grid.lua @@ -79,10 +79,16 @@ function StaticGrid.init(self, parent, element, in_row) self._prefab = self:get_node(element) self.node_size = gui.get_size(self._prefab) self.node_pivot = const.PIVOTS[gui.get_pivot(self._prefab)] - self.grid_zero_y = self.node_size.y * self.pivot.y -- Y pos at first grid line + + self._grid_horizonal_offset = self.node_size.x * (self.in_row - 1) * self.anchor.x + self._zero_offset = vmath.vector3( + self.node_size.x * self.node_pivot.x - self.node_size.x * self.pivot.x - self._grid_horizonal_offset, + self.node_size.y * self.node_pivot.y - self.node_size.y * self.pivot.y, + 0) self.border = vmath.vector4(0) -- Current grid content size + self.on_add_item = Event() self.on_remove_item = Event() self.on_change_items = Event() @@ -102,8 +108,8 @@ function StaticGrid.get_pos(self, index) local row = math.ceil(index / self.in_row) - 1 local col = (index - row * self.in_row) - 1 - _temp_pos.x = col * self.node_size.x - _temp_pos.y = -row * self.node_size.y + _temp_pos.x = col * self.node_size.x + self._zero_offset.x + _temp_pos.y = -row * self.node_size.y + self._zero_offset.y _temp_pos.z = 0 return _temp_pos @@ -185,7 +191,7 @@ function StaticGrid.add(self, item, index, shift_policy) self:_update_indexes() self:_update_borders() - gui.set_position(item, self:get_pos(index) + self:_get_zero_offset()) + gui.set_position(item, self:get_pos(index)) self:_update_pos() @@ -239,7 +245,7 @@ end function StaticGrid.get_size_for(self, count) - if count == 0 then + if not count or count == 0 then return vmath.vector3(0) end @@ -248,8 +254,10 @@ function StaticGrid.get_size_for(self, count) local size = self.node_size local pivot = self.node_pivot _extend_border(border, self:get_pos(1), size, pivot) - _extend_border(border, self:get_pos(self.in_row), size, pivot) _extend_border(border, self:get_pos(count), size, pivot) + if count >= self.in_row then + _extend_border(border, self:get_pos(self.in_row), size, pivot) + end return vmath.vector3( border.z - border.x, @@ -310,26 +318,17 @@ function StaticGrid.clear(self) end ---- Return elements offset for correct posing nodes. Correct posing at --- parent pivot node (0:0) with adjusting of node sizes and anchoring --- @tparam StaticGrid self --- @treturn vector3 The offset vector --- @local -function StaticGrid._get_zero_offset(self) - -- zero offset: center pos - border size * anchor - return vmath.vector3( - -((self.border.x + self.border.z)/2 + (self.border.z - self.border.x) * self.pivot.x), - -((self.grid_zero_y + self.border.w)/2 + (self.grid_zero_y - self.border.w) * self.pivot.y), - 0 - ) -end - - -- return vector where content borders starts function StaticGrid:get_offset() - local zero_offset = self:_get_zero_offset() local borders = self:get_borders() - return vmath.vector3(0, zero_offset.y + borders.y, 0) + local size = self:get_size() + + local offset = vmath.vector3( + (borders.z + borders.x)/2 + size.x * self.pivot.x, + (borders.y + borders.w)/2 + size.y * self.pivot.y, + 0) + + return offset end @@ -384,12 +383,8 @@ end -- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback -- @local function StaticGrid._update_pos(self, is_instant) - local zero_offset = self:_get_zero_offset() - for i, node in pairs(self.nodes) do local pos = self:get_pos(i) - pos.x = pos.x + zero_offset.x - pos.y = pos.y + zero_offset.y if is_instant then gui.set_position(node, pos) diff --git a/example/gui/main/main.gui b/example/gui/main/main.gui index 7e191c3..04e4f78 100644 --- a/example/gui/main/main.gui +++ b/example/gui/main/main.gui @@ -11793,7 +11793,7 @@ nodes { } nodes { position { - x: 0.0 + x: -150.0 y: -644.0 z: 0.0 w: 1.0 @@ -11811,7 +11811,7 @@ nodes { w: 1.0 } size { - x: 400.0 + x: 250.0 y: 250.0 z: 0.0 w: 1.0 @@ -11866,7 +11866,7 @@ nodes { w: 1.0 } size { - x: 400.0 + x: 250.0 y: 250.0 z: 0.0 w: 1.0 @@ -11879,7 +11879,7 @@ nodes { } type: TYPE_BOX blend_mode: BLEND_MODE_ALPHA - texture: "kenney/empty" + texture: "" id: "infinity_scroll_content" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE @@ -11903,8 +11903,8 @@ nodes { } nodes { position { - x: 0.0 - y: -684.0 + x: 150.0 + y: -644.0 z: 0.0 w: 1.0 } @@ -11921,35 +11921,35 @@ nodes { w: 1.0 } size { - x: 300.0 - y: 60.0 + x: 250.0 + y: 100.0 z: 0.0 w: 1.0 } color { - x: 1.0 + x: 0.8 y: 1.0 z: 1.0 w: 1.0 } type: TYPE_BOX blend_mode: BLEND_MODE_ALPHA - texture: "kenney/button_blue" - id: "infinity_prefab" + texture: "" + id: "infinity_scroll_stencil_hor" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_N adjust_mode: ADJUST_MODE_FIT parent: "infinity_page_content" layer: "" inherit_alpha: true slice9 { - x: 20.0 + x: 0.0 y: 0.0 - z: 20.0 + z: 0.0 w: 0.0 } - clipping_mode: CLIPPING_MODE_NONE + clipping_mode: CLIPPING_MODE_STENCIL clipping_visible: true clipping_inverted: false alpha: 1.0 @@ -11958,8 +11958,8 @@ nodes { } nodes { position { - x: 0.0 - y: 4.0 + x: -125.0 + y: -50.0 z: 0.0 w: 1.0 } @@ -11970,54 +11970,46 @@ nodes { w: 1.0 } scale { - x: 0.75 - y: 0.75 + x: 1.0 + y: 1.0 z: 1.0 w: 1.0 } size { - x: 380.0 - y: 50.0 + x: 250.0 + y: 100.0 z: 0.0 w: 1.0 } color { - x: 1.0 + x: 0.8 y: 1.0 - z: 1.0 - w: 1.0 - } - type: TYPE_TEXT - blend_mode: BLEND_MODE_ALPHA - text: "Infinity element 1" - font: "game" - id: "infinity_text" - xanchor: XANCHOR_NONE - yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER - outline { - x: 0.3019608 - y: 0.4 z: 0.8 w: 1.0 } - shadow { - x: 1.0 - y: 1.0 - z: 1.0 - w: 1.0 - } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "infinity_scroll_content_hor" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_W adjust_mode: ADJUST_MODE_FIT - line_break: false - parent: "infinity_prefab" + parent: "infinity_scroll_stencil_hor" 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 - outline_alpha: 1.0 - shadow_alpha: 0.0 template_node_child: false - text_leading: 1.0 - text_tracking: 0.0 + size_mode: SIZE_MODE_MANUAL } nodes { position { @@ -12204,7 +12196,7 @@ nodes { w: 1.0 } size { - x: 380.0 + x: 60.0 y: 50.0 z: 0.0 w: 1.0 @@ -12249,7 +12241,7 @@ nodes { } nodes { position { - x: 0.0 + x: -150.0 y: -176.0 z: 0.0 w: 1.0 @@ -12267,8 +12259,8 @@ nodes { w: 1.0 } size { - x: 400.0 - y: 400.0 + x: 250.0 + y: 350.0 z: 0.0 w: 1.0 } @@ -12322,8 +12314,8 @@ nodes { w: 1.0 } size { - x: 400.0 - y: 400.0 + x: 250.0 + y: 350.0 z: 0.0 w: 1.0 } @@ -12359,8 +12351,8 @@ nodes { } nodes { position { - x: 0.0 - y: -216.0 + x: 150.0 + y: -176.0 z: 0.0 w: 1.0 } @@ -12377,7 +12369,117 @@ nodes { w: 1.0 } size { - x: 300.0 + x: 250.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.8 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "infinity_scroll_stencil_dynamic_hor" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_N + adjust_mode: ADJUST_MODE_FIT + parent: "infinity_page_content" + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_STENCIL + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: -125.0 + y: -50.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: 250.0 + y: 100.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: "kenney/empty" + id: "infinity_scroll_content_dynamic_hor" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_W + adjust_mode: ADJUST_MODE_FIT + parent: "infinity_scroll_stencil_dynamic_hor" + 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: -820.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: 60.0 z: 0.0 w: 1.0 @@ -12391,7 +12493,7 @@ nodes { type: TYPE_BOX blend_mode: BLEND_MODE_ALPHA texture: "kenney/button_blue" - id: "infinity_prefab_dynamic" + id: "infinity_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE pivot: PIVOT_CENTER @@ -12432,7 +12534,7 @@ nodes { w: 1.0 } size { - x: 380.0 + x: 200.0 y: 50.0 z: 0.0 w: 1.0 @@ -12445,7 +12547,125 @@ nodes { } type: TYPE_TEXT blend_mode: BLEND_MODE_ALPHA - text: "Dynamic element 1" + text: "Record 1" + font: "game" + id: "infinity_text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.3019608 + y: 0.4 + z: 0.8 + 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: "infinity_prefab" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} +nodes { + position { + x: 151.0 + y: -356.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: 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: "infinity_prefab_dynamic" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "infinity_page_content" + layer: "" + inherit_alpha: true + slice9 { + x: 20.0 + y: 10.0 + z: 20.0 + w: 20.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: 4.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: 200.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: "Dynamic 1" font: "game" id: "infinity_text_dynamic" xanchor: XANCHOR_NONE diff --git a/example/page/grid_page.lua b/example/page/grid_page.lua index 8b0dc06..1bac0a2 100644 --- a/example/page/grid_page.lua +++ b/example/page/grid_page.lua @@ -1,3 +1,5 @@ +local const = require("druid.const") + local M = {} @@ -6,12 +8,12 @@ local function simple_animate(node, pos) end -local function remove_node(self, button, is_shift) +local function remove_node(self, button, no_shift) gui.delete_node(button.node) self.druid:remove(button) local index = self.grid_static_grid:get_index_by_node(button.node) - self.grid_static_grid:remove(index, is_shift) + self.grid_static_grid:remove(index, no_shift and const.SHIFT.NO_SHIFT or const.SHIFT.RIGHT) for i = 1, #self.grid_node_buttons do if self.grid_node_buttons[i] == button then table.remove(self.grid_node_buttons, i) @@ -27,10 +29,10 @@ local function add_node(self, index) gui.set_enabled(cloned["grid_nodes_prefab"], true) local button = self.druid:new_button(cloned["grid_nodes_prefab"], function(_, params, button) - remove_node(self, button, true) + remove_node(self, button) end) button.on_long_click:subscribe(function() - remove_node(self, button) + remove_node(self, button, true) end) button:set_click_zone(self.grid_static_scroll.view_node) @@ -72,12 +74,12 @@ local function init_static_grid(self) end -local function remove_dynamic_node(self, button, is_shift_left) +local function remove_dynamic_node(self, button, shift_policy) gui.delete_node(button.node) self.druid:remove(button) local index = self.grid_dynamic_grid:get_index_by_node(button.node) - self.grid_dynamic_grid:remove(index, is_shift_left) + self.grid_dynamic_grid:remove(index, shift_policy) for i = 1, #self.dynamic_node_buttons do if self.dynamic_node_buttons[i] == button then table.remove(self.dynamic_node_buttons, i) @@ -97,7 +99,7 @@ local function add_node_dynamic(self, index, is_shift_left) remove_dynamic_node(self, button) end) button.on_long_click:subscribe(function() - remove_dynamic_node(self, button, true) + remove_dynamic_node(self, button, const.SHIFT.lEFT) end) button:set_click_zone(self.grid_dynamic_scroll.view_node) table.insert(self.dynamic_node_buttons, button) diff --git a/example/page/infinity_page.lua b/example/page/infinity_page.lua index 71bc250..bae7279 100644 --- a/example/page/infinity_page.lua +++ b/example/page/infinity_page.lua @@ -4,7 +4,7 @@ local function create_infinity_instance(self, record, index) local instance = gui.clone_tree(self.infinity_prefab) gui.set_enabled(instance["infinity_prefab"], true) - gui.set_text(instance["infinity_text"], "Infinity record " .. index) + gui.set_text(instance["infinity_text"], "Record " .. index) local button = self.druid:new_button(instance["infinity_prefab"], function() print("Infinity click on", index) @@ -27,13 +27,26 @@ local function create_infinity_instance_small(self, record, index) end - local function create_infinity_instance_dynamic(self, record, index) local instance = gui.clone_tree(self.infinity_prefab_dynamic) gui.set_enabled(instance["infinity_prefab_dynamic"], true) - gui.set_text(instance["infinity_text_dynamic"], "Dynamic record " .. index) + gui.set_text(instance["infinity_text_dynamic"], "Record " .. index) - gui.set_size(instance["infinity_prefab_dynamic"], vmath.vector3(300, 60 + index * 5, 0)) + gui.set_size(instance["infinity_prefab_dynamic"], vmath.vector3(200, 60 + index * 3, 0)) + local button = self.druid:new_button(instance["infinity_prefab_dynamic"], function() + print("Dynamic click on", index) + end) + + return instance["infinity_prefab_dynamic"], button +end + + +local function create_infinity_instance_dynamic_hor(self, record, index) + local instance = gui.clone_tree(self.infinity_prefab_dynamic) + gui.set_enabled(instance["infinity_prefab_dynamic"], true) + gui.set_text(instance["infinity_text_dynamic"], "Record " .. index) + + gui.set_size(instance["infinity_prefab_dynamic"], vmath.vector3(150 + 2 * index, 60, 0)) local button = self.druid:new_button(instance["infinity_prefab_dynamic"], function() print("Dynamic click on", index) end) @@ -53,9 +66,17 @@ local function setup_infinity_list(self) return create_infinity_instance(self, record, index) end) + self.infinity_list_hor = self.druid:new_infinity_list(data, self.infinity_scroll_hor, self.infinity_grid_hor, function(record, index) + -- function should return gui_node, [druid_component] + return create_infinity_instance(self, record, index) + end) + -- scroll to some index -- local pos = self.infinity_grid:get_pos(25) -- self.infinity_scroll:scroll_to(pos, true) + -- timer.delay(1, false, function() + -- self.infinity_list:scroll_to_index(1) + -- end) self.infinity_list_small = self.druid:new_infinity_list(data, self.infinity_scroll_3, self.infinity_grid_3, function(record, index) @@ -67,29 +88,44 @@ local function setup_infinity_list(self) -- function should return gui_node, [druid_component] return create_infinity_instance_dynamic(self, record, index) end) + + self.infinity_list_dynamic_hor = self.druid:new_infinity_list(data, self.infinity_scroll_dynamic_hor, self.infinity_grid_dynamic_hor, function(record, index) + -- function should return gui_node, [druid_component] + return create_infinity_instance_dynamic_hor(self, record, index) + end) end function M.setup_page(self) self.druid:new_scroll("infinity_page", "infinity_page_content") + self.infinity_prefab = gui.get_node("infinity_prefab") + self.infinity_prefab_small = gui.get_node("infinity_prefab_small") + self.infinity_prefab_dynamic = gui.get_node("infinity_prefab_dynamic") + gui.set_enabled(self.infinity_prefab, false) + gui.set_enabled(self.infinity_prefab_small, false) + gui.set_enabled(self.infinity_prefab_dynamic, false) + self.infinity_scroll = self.druid:new_scroll("infinity_scroll_stencil", "infinity_scroll_content") :set_horizontal_scroll(false) self.infinity_grid = self.druid:new_static_grid("infinity_scroll_content", "infinity_prefab", 1) - self.infinity_prefab = gui.get_node("infinity_prefab") - gui.set_enabled(self.infinity_prefab, false) + + self.infinity_scroll_hor = self.druid:new_scroll("infinity_scroll_stencil_hor", "infinity_scroll_content_hor") + :set_vertical_scroll(false) + self.infinity_grid_hor = self.druid:new_static_grid("infinity_scroll_content_hor", "infinity_prefab", 999) self.infinity_scroll_3 = self.druid:new_scroll("infinity_scroll_3_stencil", "infinity_scroll_3_content") :set_horizontal_scroll(false) self.infinity_grid_3 = self.druid:new_static_grid("infinity_scroll_3_content", "infinity_prefab_small", 3) - self.infinity_prefab_small = gui.get_node("infinity_prefab_small") - gui.set_enabled(self.infinity_prefab_small, false) self.infinity_scroll_dynamic = self.druid:new_scroll("infinity_scroll_stencil_dynamic", "infinity_scroll_content_dynamic") :set_horizontal_scroll(false) - self.infinity_grid_dynamic = self.druid:new_dynamic_grid("infinity_scroll_content_dynamic", "infinity_prefab", 1) - self.infinity_prefab_dynamic = gui.get_node("infinity_prefab_dynamic") - gui.set_enabled(self.infinity_prefab_dynamic, false) + self.infinity_grid_dynamic = self.druid:new_dynamic_grid("infinity_scroll_content_dynamic") + + self.infinity_scroll_dynamic_hor = self.druid:new_scroll("infinity_scroll_stencil_dynamic_hor", "infinity_scroll_content_dynamic_hor") + :set_vertical_scroll(false) + self.infinity_grid_dynamic_hor = self.druid:new_dynamic_grid("infinity_scroll_content_dynamic_hor") + setup_infinity_list(self) end