mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 18:37:44 +02:00
Add infinity list component
This commit is contained in:
parent
04c39f1ce2
commit
f0a9d5818f
@ -37,6 +37,7 @@ function M.init(self, parent, element, in_row)
|
|||||||
self.nodes = {}
|
self.nodes = {}
|
||||||
|
|
||||||
self.offset = vmath.vector3(0)
|
self.offset = vmath.vector3(0)
|
||||||
|
self.grid_mode = const.GRID_MODE.DYNAMIC
|
||||||
|
|
||||||
local pivot = helper.get_pivot_offset(gui.get_pivot(self.parent))
|
local pivot = helper.get_pivot_offset(gui.get_pivot(self.parent))
|
||||||
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)
|
||||||
@ -50,22 +51,29 @@ function M.init(self, parent, element, in_row)
|
|||||||
self.on_remove_item = Event()
|
self.on_remove_item = Event()
|
||||||
self.on_clear = Event()
|
self.on_clear = Event()
|
||||||
self.on_update_positions = Event()
|
self.on_update_positions = Event()
|
||||||
|
|
||||||
|
self._set_position_function = gui.set_position
|
||||||
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 W = pos.x - size.x/2 + self.border_offset.x
|
local left = pos.x - size.x/2 + self.border_offset.x
|
||||||
local E = pos.x + size.x/2 + self.border_offset.x
|
local right = pos.x + size.x/2 + self.border_offset.x
|
||||||
local N = pos.y + size.y/2 + self.border_offset.y
|
local top = pos.y + size.y/2 + self.border_offset.y
|
||||||
local S = pos.y - size.y/2 + self.border_offset.y
|
local bottom = pos.y - size.y/2 + 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),
|
||||||
@ -75,8 +83,25 @@ local function check_border(self, pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function update_pos(self, is_instant)
|
||||||
|
for i, node in pairs(self.nodes) do
|
||||||
|
if is_instant then
|
||||||
|
gui.set_position(node, self:get_pos(i))
|
||||||
|
else
|
||||||
|
self._set_position_function(node, self:get_pos(i))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self.on_update_positions:trigger(self:get_context())
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local temp_pos = vmath.vector3(0)
|
local temp_pos = vmath.vector3(0)
|
||||||
local function get_pos(self, index)
|
--- Return pos for grid node index
|
||||||
|
-- @function grid:get_pos
|
||||||
|
-- @tparam number index The grid element index
|
||||||
|
-- @treturn vector3 Node position
|
||||||
|
function M.get_pos(self, index)
|
||||||
local row = math.ceil(index / self.in_row) - 1
|
local row = math.ceil(index / self.in_row) - 1
|
||||||
local col = (index - row * self.in_row) - 1
|
local col = (index - row * self.in_row) - 1
|
||||||
|
|
||||||
@ -88,13 +113,16 @@ local function get_pos(self, index)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function update_pos(self, is_instant)
|
--- Return index for grid pos
|
||||||
for i = 1, #self.nodes do
|
-- @function grid:get_index
|
||||||
local node = self.nodes[i]
|
-- @tparam vector3 pos The node position in the grid
|
||||||
gui.set_position(node, get_pos(self, i))
|
-- @treturn number The node index
|
||||||
end
|
function M.get_index(self, pos)
|
||||||
|
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)
|
||||||
|
|
||||||
self.on_update_positions:trigger(self:get_context())
|
local index = col + (row * self.in_row) + 1
|
||||||
|
return math.floor(index)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -127,41 +155,99 @@ 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)
|
||||||
table.insert(self.nodes, index, item)
|
|
||||||
|
if self.grid_mode == const.GRID_MODE.DYNAMIC then
|
||||||
|
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 = get_pos(self, 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
|
||||||
|
|
||||||
|
|
||||||
|
--- Change set position function for grid nodes. It will call on
|
||||||
|
-- update poses on grid elements. Default: gui.set_position
|
||||||
|
-- @function grid:set_position_function
|
||||||
|
-- @tparam function callback Function on node set position
|
||||||
|
function M.set_position_function(self, callback)
|
||||||
|
self._set_position_function = callback or gui.set_position
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Clear grid nodes array. GUI nodes will be not deleted!
|
--- Clear grid nodes array. GUI nodes will be not deleted!
|
||||||
-- If you want to delete GUI nodes, use grid.nodes array before grid:clear
|
-- If you want to delete GUI nodes, use grid.nodes array before grid:clear
|
||||||
-- @function grid:clear
|
-- @function grid:clear
|
||||||
@ -175,4 +261,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
|
||||||
|
117
druid/base/infinity_list.lua
Normal file
117
druid/base/infinity_list.lua
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
--- Manage data for huge dataset in scroll
|
||||||
|
--- It requires basic druid scroll and druid grid components
|
||||||
|
local const = require("druid.const")
|
||||||
|
local component = require("druid.component")
|
||||||
|
|
||||||
|
local M = component.create("infinity_list", { const.ON_UPDATE })
|
||||||
|
|
||||||
|
|
||||||
|
function M:init(data_list, scroll, grid, create_function)
|
||||||
|
self.view_size = gui.get_size(scroll.view_node)
|
||||||
|
self.prefab_size = grid.node_size
|
||||||
|
self.druid = self:get_druid()
|
||||||
|
self.scroll = scroll
|
||||||
|
self.grid = grid
|
||||||
|
self.grid:set_grid_mode(const.GRID_MODE.STATIC)
|
||||||
|
|
||||||
|
self.data = data_list
|
||||||
|
self.top_index = 1
|
||||||
|
|
||||||
|
self.create_function = create_function
|
||||||
|
|
||||||
|
self.nodes = {}
|
||||||
|
self.components = {}
|
||||||
|
|
||||||
|
self.elements_view_count = vmath.vector3(
|
||||||
|
math.ceil(self.view_size.x / self.prefab_size.x),
|
||||||
|
math.ceil(self.view_size.y / self.prefab_size.y),
|
||||||
|
0)
|
||||||
|
|
||||||
|
self:_refresh()
|
||||||
|
self.scroll.on_scroll:subscribe(function() self._check_elements(self) end)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:on_remove()
|
||||||
|
-- TODO: make this work
|
||||||
|
-- self.scroll.on_scroll:unsubscribe(self._check_elements)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:update(dt)
|
||||||
|
if self.scroll.animate then
|
||||||
|
self:_check_elements()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:set_data(data_list)
|
||||||
|
self.data = data_list
|
||||||
|
self:_refresh()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:_add_at(index)
|
||||||
|
if self.nodes[index] then
|
||||||
|
self:_remove_at(index)
|
||||||
|
end
|
||||||
|
|
||||||
|
local node, instance = self.create_function(self.data[index], index)
|
||||||
|
self.grid:add(node, index)
|
||||||
|
self.nodes[index] = node
|
||||||
|
self.components[index] = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:_remove_at(index)
|
||||||
|
self.grid:remove(index)
|
||||||
|
|
||||||
|
local node = self.nodes[index]
|
||||||
|
gui.delete_node(node)
|
||||||
|
self.nodes[index] = nil
|
||||||
|
|
||||||
|
if self.components[index] then
|
||||||
|
self.druid:remove(self.components[index])
|
||||||
|
self.components[index] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:_refresh()
|
||||||
|
for index, _ in pairs(self.nodes) do
|
||||||
|
self:_remove_at(index)
|
||||||
|
end
|
||||||
|
self:_check_elements()
|
||||||
|
self:_recalc_scroll_size()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:_check_elements()
|
||||||
|
local pos = gui.get_position(self.scroll.content_node)
|
||||||
|
pos.y = -pos.y
|
||||||
|
local top_index = self.grid:get_index(pos)
|
||||||
|
local last_index = top_index + (self.elements_view_count.x * self.elements_view_count.y) + 1
|
||||||
|
|
||||||
|
-- Clear outside elements
|
||||||
|
for index, _ in pairs(self.nodes) do
|
||||||
|
if index < top_index or index > last_index then
|
||||||
|
self:_remove_at(index)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Spawn current elements
|
||||||
|
for index = top_index, last_index do
|
||||||
|
if self.data[index] and not self.nodes[index] then
|
||||||
|
self:_add_at(index)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M:_recalc_scroll_size()
|
||||||
|
local element_size = self.grid:get_size_for_elements_count(#self.data)
|
||||||
|
self.scroll:set_size(element_size)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return M
|
@ -102,6 +102,12 @@ M.SWIPE = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
M.GRID_MODE = {
|
||||||
|
STATIC = "static",
|
||||||
|
DYNAMIC = "dynamic",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
M.EMPTY_FUNCTION = function() end
|
M.EMPTY_FUNCTION = function() end
|
||||||
M.EMPTY_STRING = ""
|
M.EMPTY_STRING = ""
|
||||||
M.SPACE_STRING = " "
|
M.SPACE_STRING = " "
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
-- @see druid.radio_group
|
-- @see druid.radio_group
|
||||||
-- @see druid.swipe
|
-- @see druid.swipe
|
||||||
-- @see druid.drag
|
-- @see druid.drag
|
||||||
|
-- @see druid.infinity_list
|
||||||
|
|
||||||
local const = require("druid.const")
|
local const = require("druid.const")
|
||||||
local druid_input = require("druid.helper.druid_input")
|
local druid_input = require("druid.helper.druid_input")
|
||||||
@ -40,6 +41,7 @@ local radio_group = require("druid.base.radio_group")
|
|||||||
local input = require("druid.base.input")
|
local input = require("druid.base.input")
|
||||||
local swipe = require("druid.base.swipe")
|
local swipe = require("druid.base.swipe")
|
||||||
local drag = require("druid.base.drag")
|
local drag = require("druid.base.drag")
|
||||||
|
local infinity_list = require("druid.base.infinity_list")
|
||||||
-- local infinity_scroll = require("druid.base.infinity_scroll")
|
-- local infinity_scroll = require("druid.base.infinity_scroll")
|
||||||
|
|
||||||
-- @classmod Druid
|
-- @classmod Druid
|
||||||
@ -477,10 +479,19 @@ end
|
|||||||
--- Create drag basic component
|
--- Create drag basic component
|
||||||
-- @function druid:new_drag
|
-- @function druid:new_drag
|
||||||
-- @tparam args ... drag init args
|
-- @tparam args ... drag init args
|
||||||
-- @treturn Componetn drag component
|
-- @treturn Component drag component
|
||||||
function Druid.new_drag(self, ...)
|
function Druid.new_drag(self, ...)
|
||||||
return Druid.create(self, drag, ...)
|
return Druid.create(self, drag, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Create infinity list basic component
|
||||||
|
-- @function druid:new_infinity_list
|
||||||
|
-- @tparam args ... drag init args
|
||||||
|
-- @treturn Component drag component
|
||||||
|
function Druid.new_infinity_list(self, ...)
|
||||||
|
return Druid.create(self, infinity_list, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
return Druid
|
return Druid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user