Add scroll:bind_grid function

This commit is contained in:
Insality
2020-09-28 01:56:43 +03:00
parent c0bd3e599b
commit 0b1051b5b2
5 changed files with 67 additions and 28 deletions

View File

@@ -3,16 +3,20 @@ local druid_const = require("druid.const")
local M = {}
local function simple_animate(node, pos)
gui.animate(node, "position", pos, gui.EASING_OUTSINE, 0.2)
end
local function remove_node(self, button, is_shift)
gui.delete_node(button.node)
self.druid:remove(button)
local index = self.grid_nodes:get_index_by_node(button.node)
self.grid_nodes:remove(index, is_shift)
local index = self.grid_static_grid:get_index_by_node(button.node)
self.grid_static_grid:remove(index, is_shift)
for i = 1, #self.grid_node_buttons do
if self.grid_node_buttons[i] == button then
table.remove(self.grid_node_buttons, i)
self.grid_static_scroll:set_size(self.grid_nodes:get_size())
break
end
end
@@ -34,9 +38,7 @@ local function add_node(self, index)
table.insert(self.grid_node_buttons, button)
self.grid_nodes:add(cloned["grid_nodes_prefab"], index)
self.grid_static_scroll:set_size(self.grid_nodes:get_size())
self.grid_static_grid:add(cloned["grid_nodes_prefab"], index)
end
@@ -51,15 +53,11 @@ local function clear_nodes(self)
end
self.grid_node_buttons = {}
self.grid_nodes:clear()
self.grid_static_grid:clear()
end
local function init_static_grid(self)
self.grid_nodes = self.druid:new_static_grid("grid_nodes", "grid_nodes_prefab", 5)
self.grid_nodes:set_position_function(function(node, pos)
gui.animate(node, "position", pos, gui.EASING_OUTSINE, 0.2)
end)
self.grid_node_buttons = {}
gui.set_enabled(gui.get_node("grid_nodes_prefab"), false)
@@ -81,12 +79,11 @@ local function remove_dynamic_node(self, button)
gui.delete_node(button.node)
self.druid:remove(button)
local index = self.dynamic_grid:get_index_by_node(button.node)
self.dynamic_grid:remove(index)
local index = self.grid_dynamic_grid:get_index_by_node(button.node)
self.grid_dynamic_grid:remove(index)
for i = 1, #self.dynamic_node_buttons do
if self.dynamic_node_buttons[i] == button then
table.remove(self.dynamic_node_buttons, i)
self.grid_dynamic_scroll:set_size(self.dynamic_grid:get_size())
break
end
end
@@ -97,13 +94,12 @@ local function add_node_dynamic(self, index, is_shift_left)
local node = gui.clone(self.prefab_dynamic)
gui.set_enabled(node, true)
gui.set_size(node, vmath.vector3(250, math.random(60, 150), 0))
self.dynamic_grid:add(node, index, is_shift_left)
self.grid_dynamic_grid:add(node, index, is_shift_left)
local button = self.druid:new_button(node, function(_, params, button)
remove_dynamic_node(self, button)
end)
button:set_click_zone(self.grid_dynamic_scroll.view_node)
self.grid_dynamic_scroll:set_size(self.dynamic_grid:get_size())
table.insert(self.dynamic_node_buttons, button)
end
@@ -112,18 +108,13 @@ local function add_node_dynamic_hor(self, index)
local node = gui.clone(self.prefab_hor_dynamic)
gui.set_enabled(node, true)
gui.set_size(node, vmath.vector3(80 + math.random(0, 80), 80, 0))
self.dynamic_hor_grid:add(node, index)
self.grid_dynamic_hor_scroll:set_size(self.dynamic_hor_grid:get_size())
self.grid_dynamic_hor_grid:add(node, index)
end
local function init_dynamic_grid(self)
-- Vertical horizontal grid
self.dynamic_node_buttons = {}
self.dynamic_grid = self.druid:new_dynamic_grid("grid_dynamic_nodes")
self.dynamic_grid:set_position_function(function(node, pos)
gui.animate(node, gui.PROP_POSITION, pos, gui.EASING_OUTSINE, 0.2)
end)
self.prefab_dynamic = gui.get_node("grid_dynamic_prefab")
gui.set_enabled(self.prefab_dynamic, false)
@@ -132,19 +123,14 @@ local function init_dynamic_grid(self)
add_node_dynamic(self, i)
end
self.druid:new_button("button_add_start_dynamic/button", function()
local start_index = (self.dynamic_grid.first_index or 2) - 1
local start_index = (self.grid_dynamic_grid.first_index or 2) - 1
add_node_dynamic(self, start_index)
end)
self.druid:new_button("button_add_end_dynamic/button", function()
add_node_dynamic(self)
end)
-- Horizontal dynamic grid
self.dynamic_hor_grid = self.druid:new_dynamic_grid("grid_dynamic_hor_nodes")
self.dynamic_hor_grid:set_position_function(function(node, pos)
gui.animate(node, gui.PROP_POSITION, pos, gui.EASING_OUTSINE, 0.2)
end)
self.prefab_hor_dynamic = gui.get_node("grid_dynamic_hor_prefab")
gui.set_enabled(self.prefab_hor_dynamic, false)
@@ -164,12 +150,23 @@ end
function M.setup_page(self)
self.grid_page_scroll = self.druid:new_scroll("grid_page", "grid_page_content")
self.grid_static_grid = self.druid:new_static_grid("grid_nodes", "grid_nodes_prefab", 5)
:set_position_function(simple_animate)
self.grid_static_scroll = self.druid:new_scroll("grid_nodes_view", "grid_nodes")
:set_horizontal_scroll(false)
:bind_grid(self.grid_static_grid)
self.grid_dynamic_grid = self.druid:new_dynamic_grid("grid_dynamic_nodes")
:set_position_function(simple_animate)
self.grid_dynamic_scroll = self.druid:new_scroll("grid_dynamic_view", "grid_dynamic_nodes")
:set_horizontal_scroll(false)
:bind_grid(self.grid_dynamic_grid)
self.grid_dynamic_hor_grid = self.druid:new_dynamic_grid("grid_dynamic_hor_nodes")
:set_position_function(simple_animate)
self.grid_dynamic_hor_scroll = self.druid:new_scroll("grid_dynamic_hor_view", "grid_dynamic_hor_nodes")
:set_vertical_scroll(false)
:bind_grid(self.grid_dynamic_hor_grid)
init_static_grid(self)
init_dynamic_grid(self)