Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Oct 14, 2024
1 parent 3c9f539 commit d11aeb6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
5 changes: 3 additions & 2 deletions druid/annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1580,10 +1580,11 @@ local druid_instance = {}
function druid_instance.final(self) end

--- Create new component.
---@generic T
---@param self druid_instance
---@param component druid.base_component Component module
---@param component T Component module
---@param ... any Other component params to pass it to component:init function
---@return druid.base_component Component instance
---@return T Component instance
function druid_instance.new(self, component, ...) end

--- Create @{BackHandler} component
Expand Down
11 changes: 11 additions & 0 deletions druid/base/scroll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@ function Scroll.set_view_size(self, size)
end


--- Refresh scroll view size
-- @tparam Scroll self @{Scroll}
function Scroll.update_view_size(self)
self.view_size = helper.get_scaled_size(self.view_node)
self.view_border = helper.get_border(self.view_node)
self:_update_size()

return self
end


--- Enable or disable scroll inert.
-- If disabled, scroll through points (if exist)
-- If no points, just simple drag without inertion
Expand Down
56 changes: 44 additions & 12 deletions druid/extended/data_list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ function DataList.init(self, scroll, grid, create_function)
self.scroll_progress = 0

self._create_function = create_function
self._is_use_cache = false
self._cache = {}
self._data = {}
self._data_visual = {}

Expand All @@ -80,6 +82,14 @@ function DataList.on_remove(self)
end


--- Set refresh function for DataList component
-- @tparam DataList self @{DataList}
function DataList.set_use_cache(self, is_use_cache)
self._is_use_cache = is_use_cache
return self
end


--- Set new data set for DataList component
-- @tparam DataList self @{DataList}
-- @tparam table data The new data array
Expand Down Expand Up @@ -199,7 +209,7 @@ function DataList.scroll_to_index(self, index)
end


--- Add element at passed index
--- Add element at passed index using cache or create new
-- @tparam DataList self @{DataList}
-- @tparam number index
-- @local
Expand All @@ -208,19 +218,32 @@ function DataList._add_at(self, index)
self:_remove_at(index)
end

local node, instance = self._create_function(self:get_context(), self._data[index], index, self)
local data = self._data[index]
local node, instance

-- Use cache if available and is_use_cache is set
if #self._cache > 0 and self._is_use_cache then
local cached = table.remove(self._cache)
node = cached.node
instance = cached.component
gui.set_enabled(node, true)
else
-- Create a new element if no cache or refresh function is not set
node, instance = self._create_function(self:get_context(), data, index, self)
end

self._data_visual[index] = {
data = self._data[index],
data = data,
node = node,
component = instance,
}
self.grid:add(node, index, const.SHIFT.NO_SHIFT)

self.on_element_add:trigger(self:get_context(), index, node, instance)
self.on_element_add:trigger(self:get_context(), index, node, instance, data)
end


--- Remove element from passed index
--- Remove element from passed index and add it to cache if applicable
-- @tparam DataList self @{DataList}
-- @tparam number index
-- @local
Expand All @@ -230,18 +253,27 @@ function DataList._remove_at(self, index)
local visual_data = self._data_visual[index]
local node = visual_data.node
local instance = visual_data.component

gui.delete_node(node)
if instance then
-- We should remove instance from druid that spawned component
instance._meta.druid:remove(instance)
local data = visual_data.data

self.on_element_remove:trigger(self:get_context(), index, node, instance, data)

if self._is_use_cache then
-- Disable the node and add it to the cache instead of deleting it
gui.set_enabled(node, false)
table.insert(self._cache, visual_data) -- Cache the removed element
else
-- If no refresh function, delete the node and component as usual
gui.delete_node(node)
if instance then
instance._meta.druid:remove(instance)
end
end
self._data_visual[index] = nil

self.on_element_remove:trigger(self:get_context(), index)
self._data_visual[index] = nil
end



--- Refresh all elements in DataList
-- @tparam DataList self @{DataList}
-- @local
Expand Down
2 changes: 1 addition & 1 deletion example/examples/general/overview/overview.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
local function setup_slider(self)
local slider = self.druid:new_slider("slider_pin", vmath.vector3(95, 0, 0), function(_, value)
gui.set_text(gui.get_node("text_progress_slider"), math.ceil(value * 100) .. "%")
end)
end) --[[@as druid.slider]]

slider:set(0.2)
end
Expand Down
7 changes: 4 additions & 3 deletions example/examples/general/scroll/scroll.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ function init(self)
self.druid:new_scroll("children_scroll_3", "children_scroll_content_3")

-- Content with less size than view
self.druid:new_scroll("scroll_smaller_view", "scroll_smaller_content")
:set_extra_stretch_size(0)
:set_inert(false)
local small_scroll = self.druid:new_scroll("scroll_smaller_view", "scroll_smaller_content") --[[@as druid.scroll]]
small_scroll.style.SMALL_CONTENT_SCROLL = true
small_scroll:set_extra_stretch_size(0) -- it also update scroll size due the change of SMALL_CONTENT_SCROLL
small_scroll:set_inert(false)

-- Scroll with points of interests
self.druid:new_scroll("scroll_with_points", "scroll_with_points_content")
Expand Down

0 comments on commit d11aeb6

Please sign in to comment.