From f25a8acd9546a6e042ab34a7da3cc5685cc7802b Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 24 Sep 2020 09:32:39 +0300 Subject: [PATCH] Remove side from dynamic_grid, move it to node pivot --- docs_md/changelog.md | 1 + druid/base/dynamic_grid.lua | 13 ++++++++----- druid/const.lua | 5 +++++ example/page/grid_page.lua | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/docs_md/changelog.md b/docs_md/changelog.md index 536d82b..830b00f 100644 --- a/docs_md/changelog.md +++ b/docs_md/changelog.md @@ -106,6 +106,7 @@ Druid 0.5.0: - Element size got from _gui.get_size_ * _gui.get_scale_ - This grid can not have spaces between elements. You will get the error, if spawn element far away from other elements - The grid can spawn elements only in row or in collumn + - The grid node should have West or North pivot (vertical or horizontal element placement) - **#37** Add _on_layout_change_ support. Druid will keep and restore GUI component data between changing game layout. Override function _on_layout_change_ in your custom components to do stuff you need. - **#85** Move several components from `base` folder to `extended`. In future, to use them, you have to register them manually. This is need for decrease build size by excluding unused components - Add _scroll:set_vertical_scroll_ and _scroll:set_horizontal_scroll_ for disable scroll sides diff --git a/druid/base/dynamic_grid.lua b/druid/base/dynamic_grid.lua index 99d752e..06d3a10 100644 --- a/druid/base/dynamic_grid.lua +++ b/druid/base/dynamic_grid.lua @@ -37,16 +37,19 @@ local SIDE_VECTORS = { --- Component init function -- @function dynamic_grid:init -- @tparam node parent The gui node parent, where items will be placed --- @tparam enum.side side The grid side. By default - vertical function DynamicGrid:init(parent, side) - self.nodes = {} - self.side = side or const.SIDE.Y self.parent = self:get_node(parent) + local parent_pivot = gui.get_pivot(self.parent) + self.pivot = helper.get_pivot_offset(parent_pivot) + self.anchor = vmath.vector3(0.5 + self.pivot.x, 0.5 - self.pivot.y, 0) + + assert(parent_pivot == gui.PIVOT_W or parent_pivot == gui.PIVOT_N, const.ERRORS.GRID_DYNAMIC_ANCHOR) + self.side = (parent_pivot == gui.PIVOT_W and const.SIDE.X or const.SIDE.Y) + + self.nodes = {} self.offset = vmath.vector3(0) self.border = vmath.vector4(0) -- Current grid content size - self.pivot = helper.get_pivot_offset(gui.get_pivot(self.parent)) - self.anchor = vmath.vector3(0.5 + self.pivot.x, 0.5 - self.pivot.y, 0) self.on_add_item = Event() self.on_remove_item = Event() diff --git a/druid/const.lua b/druid/const.lua index cad9f8a..151b62b 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -102,6 +102,11 @@ M.SWIPE = { } +M.ERRORS = { + GRID_DYNAMIC_ANCHOR = "The pivot of dynamic grid node should be West or North" +} + + M.EMPTY_FUNCTION = function() end M.EMPTY_STRING = "" M.SPACE_STRING = " " diff --git a/example/page/grid_page.lua b/example/page/grid_page.lua index fd9268b..f7aafd7 100644 --- a/example/page/grid_page.lua +++ b/example/page/grid_page.lua @@ -79,7 +79,7 @@ end local function init_dynamic_grid(self) - self.dynamic_grid = self.druid:new_dynamic_grid("grid_dynamic_nodes", druid_const.SIDE.Y) + self.dynamic_grid = self.druid:new_dynamic_grid("grid_dynamic_nodes") self.prefab_dynamic = gui.get_node("grid_dynamic_prefab") gui.set_enabled(self.prefab_dynamic, false)