Copy grid from infinity scroll tests

This commit is contained in:
Insality 2020-09-20 23:03:59 +03:00
parent 1b9901edd9
commit 02e238348b

View File

@ -43,7 +43,10 @@ function M.init(self, parent, element, in_row)
self.anchor = vmath.vector3(0.5 + pivot.x, 0.5 - pivot.y, 0) self.anchor = vmath.vector3(0.5 + pivot.x, 0.5 - pivot.y, 0)
self.in_row = in_row or 1 self.in_row = in_row or 1
self.node_size = gui.get_size(self:get_node(element)) local node = self:get_node(element)
self.node_size = gui.get_size(node)
self.node_pivot = const.PIVOTS[gui.get_pivot(node)]
self.border = vmath.vector4(0) self.border = vmath.vector4(0)
self.border_offset = vmath.vector3(0) self.border_offset = vmath.vector3(0)
@ -56,19 +59,25 @@ function M.init(self, parent, element, in_row)
end end
local function check_border(self, pos) local function _update_border(self, pos, border)
local border = self.border
local size = self.node_size local size = self.node_size
local pivot = self.node_pivot
local W = pos.x - size.x/2 + self.border_offset.x local left = pos.x - size.x/2 - (size.x * pivot.x) + self.border_offset.x
local E = pos.x + size.x/2 + self.border_offset.x local right = pos.x + size.x/2 - (size.x * pivot.x) + self.border_offset.x
local N = pos.y + size.y/2 + self.border_offset.y local top = pos.y + size.y/2 - (size.y * pivot.y) + self.border_offset.y
local S = pos.y - size.y/2 + self.border_offset.y local bottom = pos.y - size.y/2 - (size.y * pivot.y)+ self.border_offset.y
border.x = math.min(border.x, W) border.x = math.min(border.x, left)
border.y = math.max(border.y, N) border.y = math.max(border.y, top)
border.z = math.max(border.z, E) border.z = math.max(border.z, right)
border.w = math.min(border.w, S) border.w = math.min(border.w, bottom)
end
local function update_border_offset(self, pos)
local border = self.border
_update_border(self, pos, border)
self.border_offset = vmath.vector3( self.border_offset = vmath.vector3(
(border.x + (border.z - border.x) * self.anchor.x), (border.x + (border.z - border.x) * self.anchor.x),
@ -79,9 +88,7 @@ end
local function update_pos(self, is_instant) local function update_pos(self, is_instant)
for i = 1, #self.nodes do for i, node in pairs(self.nodes) do
local node = self.nodes[i]
if is_instant then if is_instant then
gui.set_position(node, self:get_pos(i)) gui.set_position(node, self:get_pos(i))
else else
@ -118,8 +125,10 @@ function M.get_index(self, pos)
local col = (pos.x + self.border_offset.x) / (self.node_size.x + self.offset.x) local col = (pos.x + self.border_offset.x) / (self.node_size.x + self.offset.x)
local row = -(pos.y + self.border_offset.y) / (self.node_size.y + self.offset.y) local row = -(pos.y + self.border_offset.y) / (self.node_size.y + self.offset.y)
local index = col + (row * self.in_row) + 1 row = math.floor(row)
return math.floor(index)
local index = col + (row * self.in_row)
return math.ceil(index)
end end
@ -152,42 +161,91 @@ end
-- @tparam[opt] number index The item position. By default add as last item -- @tparam[opt] number index The item position. By default add as last item
function M.add(self, item, index) function M.add(self, item, index)
index = index or (#self.nodes + 1) index = index or (#self.nodes + 1)
if self.grid_mode == const.GRID_MODE.DYNAMIC then
table.insert(self.nodes, index, item) table.insert(self.nodes, index, item)
else
self.nodes[index] = item
end
gui.set_parent(item, self.parent) gui.set_parent(item, self.parent)
local pos = self:get_pos(index) local pos = self:get_pos(index)
check_border(self, pos) for i, _ in pairs(self.nodes) do
update_border_offset(self, self:get_pos(i))
end
-- Add new item instantly in new pos
gui.set_position(item, pos)
update_pos(self) update_pos(self)
self.on_add_item:trigger(self:get_context(), item, index) self.on_add_item:trigger(self:get_context(), item, index)
end end
function M:remove(index, delete_node)
assert(self.nodes[index], "No grid item at given index " .. index)
local parent_node = self.nodes[index]
if delete_node then
gui.delete_node(parent_node)
end
if self.grid_mode == const.GRID_MODE.DYNAMIC then
table.remove(self.nodes, index)
else
self.nodes[index] = nil
end
-- Recalculate borders
self.border = vmath.vector4(0)
update_border_offset(self, self:get_pos(1))
for i, _ in pairs(self.nodes) do
local pos = self:get_pos(i)
update_border_offset(self, pos)
end
update_pos(self)
end
--- Return grid content size --- Return grid content size
-- @function grid:get_size -- @function grid:get_size
-- @treturn vector3 The grid content size -- @treturn vector3 The grid content size
function M.get_size(self) function M.get_size(self, border)
border = border or self.border
return vmath.vector3( return vmath.vector3(
self.border.z - self.border.x, border.z - border.x,
self.border.y - self.border.w, border.y - border.w,
0) 0)
end end
function M:get_size_for_elements_count(count)
local border = vmath.vector4(0)
for i = 1, count do
local pos = self:get_pos(i)
_update_border(self, pos, border)
end
return M.get_size(self, border)
end
--- Return array of all node positions --- Return array of all node positions
-- @function grid:get_all_pos -- @function grid:get_all_pos
-- @treturn vector3[] All grid node positions -- @treturn vector3[] All grid node positions
function M.get_all_pos(self) function M.get_all_pos(self)
local result = {} local result = {}
for i = 1, #self.nodes do for i, node in pairs(self.nodes) do
table.insert(result, gui.get_position(self.nodes[i])) table.insert(result, gui.get_position(node))
end end
return result return result
end end
--- Chane set position function for grid nodes. It will call on --- Change set position function for grid nodes. It will call on
-- update poses on grid elements. Default: gui.set_position -- update poses on grid elements. Default: gui.set_position
-- @function grid:set_position_function -- @function grid:set_position_function
-- @tparam function callback Function on node set position -- @tparam function callback Function on node set position
@ -209,4 +267,11 @@ function M.clear(self)
end end
function M:set_grid_mode(grid_mode)
assert(grid_mode == const.GRID_MODE.STATIC or grid_mode == const.GRID_MODE.DYNAMIC)
self.grid_mode = grid_mode
end
return M return M