mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 10:27:47 +02:00
Add get_pos and get_index methods
This commit is contained in:
parent
a92e4e6ae0
commit
32c3316a53
@ -19,6 +19,7 @@
|
|||||||
-- @tfield vector4 border The size of item content
|
-- @tfield vector4 border The size of item content
|
||||||
-- @tfield vector3 border_offer The border offset for correct anchor calculations
|
-- @tfield vector3 border_offer The border offset for correct anchor calculations
|
||||||
|
|
||||||
|
local const = require("druid.const")
|
||||||
local Event = require("druid.event")
|
local Event = require("druid.event")
|
||||||
local helper = require("druid.helper")
|
local helper = require("druid.helper")
|
||||||
local component = require("druid.component")
|
local component = require("druid.component")
|
||||||
@ -36,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)
|
||||||
@ -76,8 +78,27 @@ local function check_border(self, pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function update_pos(self, is_instant)
|
||||||
|
for i = 1, #self.nodes do
|
||||||
|
local node = self.nodes[i]
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
@ -89,22 +110,19 @@ 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
|
||||||
|
-- @treturn number The node index
|
||||||
|
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)
|
||||||
|
|
||||||
if is_instant then
|
local index = col + (row * self.in_row) + 1
|
||||||
gui.set_position(node, get_pos(self, i))
|
return math.floor(index)
|
||||||
else
|
|
||||||
self._set_position_function(node, get_pos(self, i))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.on_update_positions:trigger(self:get_context())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--- Set grid items offset, the distance between items
|
--- Set grid items offset, the distance between items
|
||||||
-- @function grid:set_offset
|
-- @function grid:set_offset
|
||||||
-- @tparam vector3 offset Offset
|
-- @tparam vector3 offset Offset
|
||||||
@ -132,7 +150,7 @@ function M.add(self, item, index)
|
|||||||
table.insert(self.nodes, index, item)
|
table.insert(self.nodes, index, item)
|
||||||
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)
|
check_border(self, pos)
|
||||||
update_pos(self)
|
update_pos(self)
|
||||||
|
|
||||||
|
@ -101,6 +101,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 = " "
|
||||||
|
Loading…
x
Reference in New Issue
Block a user