From 3ac29861719e42f10ad559890e747e34fe70be64 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 27 Jan 2022 22:29:13 +0200 Subject: [PATCH] #162 Add static grid last row alignment --- druid/base/static_grid.lua | 29 ++++++++++++++++++- druid/styles/default/style.lua | 1 + .../grid/static_grid/static_grid.gui_script | 4 +++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/druid/base/static_grid.lua b/druid/base/static_grid.lua index 7716718..43863de 100644 --- a/druid/base/static_grid.lua +++ b/druid/base/static_grid.lua @@ -70,9 +70,11 @@ end -- or create your own style -- @table style -- @tfield[opt=false] bool IS_DYNAMIC_NODE_POSES If true, always center grid content as grid pivot sets +-- @tfield[opt=false] bool IS_ALIGN_LAST_ROW If true, always align last row of the grid as grid pivot sets function StaticGrid.on_style_change(self, style) self.style = {} self.style.IS_DYNAMIC_NODE_POSES = style.IS_DYNAMIC_NODE_POSES or false + self.style.IS_ALIGN_LAST_ROW = style.IS_ALIGN_LAST_ROW or false end @@ -122,7 +124,9 @@ 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 + self._zero_offset.x + local zero_offset_x = self:_get_zero_offset_x(row) + + _temp_pos.x = col * self.node_size.x + zero_offset_x _temp_pos.y = -row * self.node_size.y + self._zero_offset.y _temp_pos.z = 0 @@ -437,4 +441,27 @@ function StaticGrid:_get_zero_offset() end +--- Return offset x for last row in grid. Used to align this row accorting to grid's anchor +-- @function static:_grid:_get_zero_offset_x +-- @treturn number The offset x value +-- @local +function StaticGrid:_get_zero_offset_x(row_index) + if not self.style.IS_DYNAMIC_NODE_POSES or not self.style.IS_ALIGN_LAST_ROW then + return self._zero_offset.x + end + + local offset_x = self._zero_offset.x + local last_row = math.ceil(self.last_index / self.in_row) - 1 + + if last_row > 0 and last_row == row_index then + local elements_in_row = (self.last_index - (last_row * self.in_row)) - 1 + local offset = elements_in_row * self.node_size.x * self.anchor.x + offset_x = self.node_size.x * self.node_pivot.x - self.node_size.x * self.pivot.x - offset + end + + return offset_x +end + + + return StaticGrid diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 147aad4..791cd51 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -61,6 +61,7 @@ M["drag"] = { M["static_grid"] = { IS_DYNAMIC_NODE_POSES = false, -- Always align by content size with node anchor + IS_ALIGN_LAST_ROW = true, -- Align the last row of grid } diff --git a/example/examples/grid/static_grid/static_grid.gui_script b/example/examples/grid/static_grid/static_grid.gui_script index f162460..2579bda 100644 --- a/example/examples/grid/static_grid/static_grid.gui_script +++ b/example/examples/grid/static_grid/static_grid.gui_script @@ -39,6 +39,10 @@ function init(self) grid_e, } + for i = 1, #grids do + grids[i].style.IS_DYNAMIC_NODE_POSES = false + end + local animate_grides = function() for _, grid in ipairs(grids) do