grid: add clear function, add grid anchoring and correct posing

update examples
This commit is contained in:
Insality 2019-04-07 15:16:18 +03:00
parent 263197eb8d
commit 636df146c9
2 changed files with 36 additions and 20 deletions

View File

@ -12,24 +12,33 @@ function M.init(instance, parent, element, in_row)
instance.nodes = {}
instance.offset = vmath.vector3(0)
instance.anchor = vmath.vector3(0)
instance.anchor = vmath.vector3(0.5, 0, 0)
instance.in_row = in_row or 1
instance.node_size = gui.get_size(helper.get_node(element))
instance.border = vmath.vector4(0)
instance.border_offset = vmath.vector3(0)
end
local function check_border(instance, pos)
local border = instance.border
local size = instance.node_size
local W = pos.x - size.x/2
local E = pos.x + size.x/2
local N = pos.y + size.y/2
local S = pos.y - size.y/2
instance.border.x = math.min(instance.border.x, W)
instance.border.y = math.max(instance.border.y, N)
instance.border.z = math.max(instance.border.z, E)
instance.border.w = math.min(instance.border.w, S)
local W = pos.x - size.x/2 + instance.border_offset.x
local E = pos.x + size.x/2 + instance.border_offset.x
local N = pos.y + size.y/2 + instance.border_offset.y
local S = pos.y - size.y/2 + instance.border_offset.y
border.x = math.min(border.x, W)
border.y = math.max(border.y, N)
border.z = math.max(border.z, E)
border.w = math.min(border.w, S)
instance.border_offset = vmath.vector3(
(border.x + (border.z - border.x) * instance.anchor.x),
(border.y + (border.w - border.y) * instance.anchor.y),
0
)
end
@ -38,10 +47,8 @@ local function get_pos(instance, index)
local row = math.ceil(index / instance.in_row) - 1
local col = (index - row * instance.in_row) - 1
temp_pos.x = col * (instance.node_size.x + instance.offset.x)
temp_pos.x = temp_pos.x + (0.5 - instance.anchor.x) * instance.node_size.x
temp_pos.y = -row * (instance.node_size.y + instance.offset.y)
temp_pos.y = temp_pos.y - (0.5 - instance.anchor.y) * instance.node_size.y
temp_pos.x = col * (instance.node_size.x + instance.offset.x) - instance.border_offset.x
temp_pos.y = -row * (instance.node_size.y + instance.offset.y) - instance.border_offset.y
return temp_pos
end
@ -74,12 +81,15 @@ function M.add(instance, item, index)
local pos = get_pos(instance, index)
check_border(instance, pos)
gui.set_position(item, pos)
update_pos(instance)
end
function M.get_size(instance)
return instance.border
return vmath.vector3(
instance.border.z - instance.border.x,
instance.border.w - instance.border.y,
0)
end
@ -93,4 +103,12 @@ function M.get_all_pos(instance)
end
function M.clear(instance)
for i = 1, #instance.nodes do
gui.delete_node(instance.nodes[i])
end
instance.nodes = {}
end
return M

View File

@ -5,9 +5,8 @@ local function init_left_page(self)
local prefab = gui.get_node("prefab")
self.left_grid = self.druid:new_grid("page_parent", prefab, 1)
self.left_grid:set_offset(vmath.vector3(0, 5, 0))
self.left_grid:set_anchor(vmath.vector3(0.5, 0, 0))
for i = 1, 30 do
for i = 1, 7 do
local nodes = gui.clone_tree(prefab)
gui.set_text(nodes["number"], i)
self.left_grid:add(nodes["prefab"])
@ -17,7 +16,7 @@ local function init_left_page(self)
local view_size = gui.get_size(gui.get_node("page_parent"))
self.left_scroll = self.druid:new_scroll("page_parent", "page_input",
vmath.vector4(0, view_size.y/2, 0, -size.w - view_size.y/2))
vmath.vector4(0, view_size.y/2, 0, -size.y - view_size.y/2))
self.left_scroll:set_points(self.left_grid:get_all_pos())
end
@ -32,7 +31,6 @@ local function init_right_page(self)
local prefab = gui.get_node("prefab")
self.right_grid = self.druid:new_grid("right_parent", prefab, 1)
self.right_grid:set_offset(vmath.vector3(0, 5, 0))
self.right_grid:set_anchor(vmath.vector3(0.5, 0, 0))
for i = 1, 20 do
local nodes = gui.clone_tree(prefab)
@ -47,7 +45,7 @@ local function init_right_page(self)
-- TODO: Should we calc scrolle size with parent size?
-- If yes, we will pass only content size to correct scrolling
self.right_scroll = self.druid:new_scroll("right_parent", "right_input",
vmath.vector4(0, 0, 0, -size.w - view_size.y))
vmath.vector4(0, 0, 0, -size.y - view_size.y))
self.right_scroll:set_points(self.right_grid:get_all_pos())