mirror of
https://github.com/Insality/druid
synced 2025-06-27 18:37:45 +02:00
Update grid and scroll documentation
This commit is contained in:
parent
0b1051b5b2
commit
246574c86b
@ -33,20 +33,20 @@ local SIDE_VECTORS = {
|
||||
BOT = vmath.vector3(0, 1, 0),
|
||||
}
|
||||
|
||||
|
||||
--- Component init function
|
||||
-- @function dynamic_grid:init
|
||||
-- @tparam node parent The gui node parent, where items will be placed
|
||||
function DynamicGrid:init(parent, side)
|
||||
self.parent = self:get_node(parent)
|
||||
local parent_pivot = gui.get_pivot(self.parent)
|
||||
|
||||
local parent_pivot = gui.get_pivot(self.parent)
|
||||
self.pivot = helper.get_pivot_offset(parent_pivot)
|
||||
|
||||
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.on_add_item = Event()
|
||||
@ -59,6 +59,11 @@ function DynamicGrid:init(parent, side)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:on_layout_change()
|
||||
self:_update(true)
|
||||
end
|
||||
|
||||
|
||||
--- Return pos for grid node index
|
||||
-- @function dynamic_grid:get_pos
|
||||
-- @tparam number index The grid element index
|
||||
@ -99,11 +104,6 @@ function DynamicGrid:get_pos(index, node, origin_index)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:on_layout_change()
|
||||
self:_update(true)
|
||||
end
|
||||
|
||||
|
||||
--- Set grid items offset, the distance between items
|
||||
-- @function dynamic_grid:set_offset
|
||||
-- @tparam vector3 offset Offset
|
||||
@ -211,23 +211,6 @@ function DynamicGrid:get_index_by_node(node)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:get_center_position()
|
||||
return vmath.vector3(
|
||||
(self.border.x + self.border.z)/2,
|
||||
(self.border.y + self.border.w)/2,
|
||||
0)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:get_zero_offset()
|
||||
-- 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.border.y + self.border.w)/2 + (self.border.y - self.border.w) * self.pivot.y),
|
||||
0)
|
||||
end
|
||||
|
||||
|
||||
--- Return array of all node positions
|
||||
-- @function dynamic_grid:get_all_pos
|
||||
-- @treturn vector3[] All grid node positions
|
||||
@ -263,14 +246,34 @@ function DynamicGrid:clear()
|
||||
end
|
||||
|
||||
|
||||
--- Return the grid nodes table
|
||||
-- @function dynamic_grid:get_nodes
|
||||
-- @treturn table<index, node> The grid nodes
|
||||
function DynamicGrid:get_nodes()
|
||||
return self.nodes
|
||||
function DynamicGrid:_add_node(node, index, origin_index)
|
||||
self.nodes[index] = {
|
||||
node = node,
|
||||
pos = self:get_pos(index, node, origin_index),
|
||||
size = self:_get_node_size(node),
|
||||
pivot = const.PIVOTS[gui.get_pivot(node)]
|
||||
}
|
||||
|
||||
-- Add new item instantly in new pos
|
||||
gui.set_parent(node, self.parent)
|
||||
gui.set_position(node, self.nodes[index].pos + self:_get_zero_offset())
|
||||
end
|
||||
|
||||
|
||||
--- Update grid inner state
|
||||
-- @function dynamic_grid:_update
|
||||
-- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback
|
||||
-- @local
|
||||
function DynamicGrid:_update(is_instant)
|
||||
self:_update_indexes()
|
||||
self:_update_borders()
|
||||
self:_update_pos(is_instant)
|
||||
end
|
||||
|
||||
|
||||
--- Update first and last indexes of grid nodes
|
||||
-- @function dynamic_grid:_update_indexes
|
||||
-- @local
|
||||
function DynamicGrid:_update_indexes()
|
||||
self.first_index = nil
|
||||
self.last_index = nil
|
||||
@ -284,6 +287,9 @@ function DynamicGrid:_update_indexes()
|
||||
end
|
||||
|
||||
|
||||
--- Update grid content borders, recalculate min and max values
|
||||
-- @function dynamic_grid:_update_borders
|
||||
-- @local
|
||||
function DynamicGrid:_update_borders()
|
||||
if not self.first_index then
|
||||
self.border = vmath.vector4(0)
|
||||
@ -310,8 +316,12 @@ function DynamicGrid:_update_borders()
|
||||
end
|
||||
|
||||
|
||||
--- Update grid nodes position
|
||||
-- @function dynamic_grid:_update_indexes
|
||||
-- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback
|
||||
-- @local
|
||||
function DynamicGrid:_update_pos(is_instant)
|
||||
local offset = self:get_zero_offset()
|
||||
local offset = self:_get_zero_offset()
|
||||
|
||||
for index, node in pairs(self.nodes) do
|
||||
if is_instant then
|
||||
@ -325,13 +335,6 @@ function DynamicGrid:_update_pos(is_instant)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:_update(is_instant)
|
||||
self:_update_indexes()
|
||||
self:_update_borders()
|
||||
self:_update_pos(is_instant)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:_get_next_node_pos(origin_node_index, new_node, place_side)
|
||||
local node = self.nodes[origin_node_index]
|
||||
|
||||
@ -356,17 +359,12 @@ function DynamicGrid:_get_node_size(node)
|
||||
end
|
||||
|
||||
|
||||
function DynamicGrid:_add_node(node, index, origin_index)
|
||||
self.nodes[index] = {
|
||||
node = node,
|
||||
pos = self:get_pos(index, node, origin_index),
|
||||
size = self:_get_node_size(node),
|
||||
pivot = const.PIVOTS[gui.get_pivot(node)]
|
||||
}
|
||||
|
||||
-- Add new item instantly in new pos
|
||||
gui.set_parent(node, self.parent)
|
||||
gui.set_position(node, self.nodes[index].pos + self:get_zero_offset())
|
||||
function DynamicGrid:_get_zero_offset()
|
||||
-- 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.border.y + self.border.w)/2 + (self.border.y - self.border.w) * self.pivot.y),
|
||||
0)
|
||||
end
|
||||
|
||||
|
||||
|
@ -327,6 +327,11 @@ end
|
||||
|
||||
|
||||
|
||||
--- Bind the grid component (Static or Dynamic) to recalc
|
||||
-- scroll size on grid changes
|
||||
-- @function scroll:bind_grid
|
||||
-- @tparam druid.static_grid|druid.dynamic_grid Druid grid component
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll:bind_grid(grid)
|
||||
if self._grid_on_change then
|
||||
self._grid_on_change:unsubscribe(self._grid_on_change_callback)
|
||||
@ -343,6 +348,7 @@ function Scroll:bind_grid(grid)
|
||||
self._grid_on_change_callback = self._grid_on_change:subscribe(function()
|
||||
self:set_size(grid:get_size())
|
||||
end)
|
||||
self:set_size(grid:get_size())
|
||||
|
||||
return self
|
||||
end
|
||||
@ -568,10 +574,6 @@ function Scroll:_update_size()
|
||||
|
||||
self.available_pos = get_border_vector(view_border - content_border)
|
||||
self.available_size = get_size_vector(self.available_pos)
|
||||
self.available_pos.x = self.available_pos.x
|
||||
self.available_pos.z = self.available_pos.z
|
||||
self.available_pos.y = self.available_pos.y
|
||||
self.available_pos.w = self.available_pos.w
|
||||
|
||||
self.drag.can_x = self.available_size.x > 0 and self._is_horizontal_scroll
|
||||
self.drag.can_y = self.available_size.y > 0 and self._is_vertical_scroll
|
||||
@ -601,10 +603,6 @@ function Scroll:_update_size()
|
||||
|
||||
self.available_pos_extra = get_border_vector(view_border - content_border_extra)
|
||||
self.available_size_extra = get_size_vector(self.available_pos_extra)
|
||||
self.available_pos_extra.x = self.available_pos_extra.x
|
||||
self.available_pos_extra.z = self.available_pos_extra.z
|
||||
self.available_pos_extra.y = self.available_pos_extra.y
|
||||
self.available_pos_extra.w = self.available_pos_extra.w
|
||||
end
|
||||
|
||||
|
||||
|
@ -155,7 +155,7 @@ function StaticGrid:add(item, index)
|
||||
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:_get_zero_offset())
|
||||
|
||||
self:_update_pos()
|
||||
|
||||
@ -243,7 +243,7 @@ function StaticGrid:clear()
|
||||
end
|
||||
|
||||
|
||||
function StaticGrid:get_zero_offset()
|
||||
function StaticGrid:_get_zero_offset()
|
||||
-- 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),
|
||||
@ -253,14 +253,10 @@ function StaticGrid:get_zero_offset()
|
||||
end
|
||||
|
||||
|
||||
--- Return the grid nodes table
|
||||
-- @function static_grid:get_nodes
|
||||
-- @treturn table<index, node> The grid nodes
|
||||
function StaticGrid:get_nodes()
|
||||
return self.nodes
|
||||
end
|
||||
|
||||
|
||||
--- Update grid inner state
|
||||
-- @function static_grid:_update
|
||||
-- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback
|
||||
-- @local
|
||||
function StaticGrid:_update(is_instant)
|
||||
self:_update_indexes()
|
||||
self:_update_borders()
|
||||
@ -268,6 +264,9 @@ function StaticGrid:_update(is_instant)
|
||||
end
|
||||
|
||||
|
||||
--- Update first and last indexes of grid nodes
|
||||
-- @function static_grid:_update_indexes
|
||||
-- @local
|
||||
function StaticGrid:_update_indexes()
|
||||
self.first_index = nil
|
||||
self.last_index = nil
|
||||
@ -281,6 +280,9 @@ function StaticGrid:_update_indexes()
|
||||
end
|
||||
|
||||
|
||||
--- Update grid content borders, recalculate min and max values
|
||||
-- @function static_grid:_update_borders
|
||||
-- @local
|
||||
function StaticGrid:_update_borders()
|
||||
if not self.first_index then
|
||||
self.border = vmath.vector4(0)
|
||||
@ -307,8 +309,12 @@ function StaticGrid:_update_borders()
|
||||
end
|
||||
|
||||
|
||||
--- Update grid nodes position
|
||||
-- @function static_grid:_update_indexes
|
||||
-- @tparam bool is_instant If true, node position update instantly, otherwise with set_position_function callback
|
||||
-- @local
|
||||
function StaticGrid:_update_pos(is_instant)
|
||||
local zero_offset = self:get_zero_offset()
|
||||
local zero_offset = self:_get_zero_offset()
|
||||
|
||||
for i, node in pairs(self.nodes) do
|
||||
local pos = self:get_pos(i)
|
||||
|
@ -1,5 +1,3 @@
|
||||
local druid_const = require("druid.const")
|
||||
|
||||
local M = {}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user