Optimize component and event creation, add cache for interests, optimize scroll

This commit is contained in:
Insality 2022-03-11 20:37:18 +02:00
parent cc35fe208c
commit c5c335d17c
5 changed files with 41 additions and 39 deletions

View File

@ -140,6 +140,7 @@ function Scroll.init(self, view_node, content_node)
self.druid = self:get_druid() self.druid = self:get_druid()
self.view_node = self:get_node(view_node) self.view_node = self:get_node(view_node)
self.view_border = helper.get_border(self.view_node)
self.content_node = self:get_node(content_node) self.content_node = self:get_node(content_node)
self.view_size = vmath.mul_per_elem(gui.get_size(self.view_node), gui.get_scale(self.view_node)) self.view_size = vmath.mul_per_elem(gui.get_size(self.view_node), gui.get_scale(self.view_node))
@ -419,15 +420,14 @@ function Scroll.is_node_in_view(self, node)
end end
local node_border = helper.get_border(node, node_offset_for_view) local node_border = helper.get_border(node, node_offset_for_view)
local view_border = helper.get_border(self.view_node)
-- Check is vertical outside (Left or Right): -- Check is vertical outside (Left or Right):
if node_border.z < view_border.x or node_border.x > view_border.z then if node_border.z < self.view_border.x or node_border.x > self.view_border.z then
return false return false
end end
-- Check is horizontal outside (Up or Down): -- Check is horizontal outside (Up or Down):
if node_border.w > view_border.y or node_border.y < view_border.w then if node_border.w > self.view_border.y or node_border.y < self.view_border.w then
return false return false
end end
@ -693,12 +693,10 @@ end
function Scroll._update_size(self) function Scroll._update_size(self)
local view_border = helper.get_border(self.view_node)
local content_border = helper.get_border(self.content_node) local content_border = helper.get_border(self.content_node)
local content_size = vmath.mul_per_elem(gui.get_size(self.content_node), gui.get_scale(self.content_node)) local content_size = vmath.mul_per_elem(gui.get_size(self.content_node), gui.get_scale(self.content_node))
self.available_pos = get_border_vector(view_border - content_border, self._offset) self.available_pos = get_border_vector(self.view_border - content_border, self._offset)
self.available_size = get_size_vector(self.available_pos) self.available_size = get_size_vector(self.available_pos)
self.drag.can_x = self.available_size.x > 0 and self._is_horizontal_scroll self.drag.can_x = self.available_size.x > 0 and self._is_horizontal_scroll
@ -727,7 +725,7 @@ function Scroll._update_size(self)
self.drag.can_y = content_size.y > self.view_size.y self.drag.can_y = content_size.y > self.view_size.y
end end
self.available_pos_extra = get_border_vector(view_border - content_border_extra, self._offset) self.available_pos_extra = get_border_vector(self.view_border - content_border_extra, self._offset)
self.available_size_extra = get_size_vector(self.available_pos_extra) self.available_size_extra = get_size_vector(self.available_pos_extra)
self:_set_scroll_position(self.position) self:_set_scroll_position(self.position)

View File

@ -12,6 +12,7 @@ local helper = require("druid.helper")
local BaseComponent = class("druid.component") local BaseComponent = class("druid.component")
local INTERESTS = {} -- Cache interests by component class in runtime
local IS_AUTO_TEMPLATE = not (sys.get_config("druid.no_auto_template") == "1") local IS_AUTO_TEMPLATE = not (sys.get_config("druid.no_auto_template") == "1")
@ -270,13 +271,7 @@ end
-- @tparam BaseComponent self @{BaseComponent} -- @tparam BaseComponent self @{BaseComponent}
-- @treturn BaseComponent|nil The druid component instance or nil -- @treturn BaseComponent|nil The druid component instance or nil
function BaseComponent.get_parent_component(self) function BaseComponent.get_parent_component(self)
local context = self:get_context() return self._meta.parent
if context.isInstanceOf and context:isInstanceOf(BaseComponent) then
return context
end
return nil
end end
@ -285,26 +280,27 @@ end
-- @tparam table druid_instance The parent druid instance -- @tparam table druid_instance The parent druid instance
-- @tparam table context Druid context. Usually it is self of script -- @tparam table context Druid context. Usually it is self of script
-- @tparam table style Druid style module -- @tparam table style Druid style module
-- @tparam table instance_class The component instance class
-- @treturn component BaseComponent itself -- @treturn component BaseComponent itself
-- @local -- @local
function BaseComponent.setup_component(self, druid_instance, context, style) function BaseComponent.setup_component(self, druid_instance, context, style, instance_class)
self._meta = { self._meta = {
template = "", template = "",
context = nil, context = context,
nodes = nil, nodes = nil,
style = nil, style = nil,
druid = druid_instance, druid = druid_instance,
input_enabled = true, input_enabled = true,
children = {} children = {},
parent = type(context) ~= "userdata" and context,
instance_class = instance_class
} }
self:__set_context(context)
self:set_style(style) self:set_style(style)
self:set_template("") self:set_template("")
local parent = self:get_parent_component() if self._meta.parent then
if parent then self._meta.parent:__add_children(self)
parent:__add_children(self)
end end
return self return self
@ -370,20 +366,16 @@ function BaseComponent.__tostring(self)
end end
--- Set current component context
-- @tparam BaseComponent self @{BaseComponent}
-- @tparam table context Druid context. Usually it is self of script
-- @local
function BaseComponent.__set_context(self, context)
self._meta.context = context
end
--- Get current component interests --- Get current component interests
-- @tparam BaseComponent self @{BaseComponent} -- @tparam BaseComponent self @{BaseComponent}
-- @treturn table List of component interests -- @treturn table List of component interests
-- @local -- @local
function BaseComponent.__get_interests(self) function BaseComponent.__get_interests(self)
local instance_class = self._meta.instance_class
if INTERESTS[instance_class] then
return INTERESTS[instance_class]
end
local interests = {} local interests = {}
for index = 1, #BaseComponent.ALL_INTERESTS do for index = 1, #BaseComponent.ALL_INTERESTS do
local interest = BaseComponent.ALL_INTERESTS[index] local interest = BaseComponent.ALL_INTERESTS[index]
@ -392,7 +384,8 @@ function BaseComponent.__get_interests(self)
end end
end end
return interests INTERESTS[instance_class] = interests
return INTERESTS[instance_class]
end end

View File

@ -13,7 +13,7 @@ local DruidEvent = class("druid.event")
-- @tparam DruidEvent self @{DruidEvent} -- @tparam DruidEvent self @{DruidEvent}
-- @tparam function initial_callback Subscribe the callback on new event, if callback exist -- @tparam function initial_callback Subscribe the callback on new event, if callback exist
function DruidEvent.initialize(self, initial_callback) function DruidEvent.initialize(self, initial_callback)
self._callbacks = {} self._callbacks = nil -- initialize later
if initial_callback then if initial_callback then
self:subscribe(initial_callback) self:subscribe(initial_callback)
@ -29,6 +29,7 @@ function DruidEvent.subscribe(self, callback, context)
assert(type(self) == "table", "You should subscribe to event with : syntax") assert(type(self) == "table", "You should subscribe to event with : syntax")
assert(type(callback) == "function", "Callback should be function") assert(type(callback) == "function", "Callback should be function")
self._callbacks = self._callbacks or {}
table.insert(self._callbacks, { table.insert(self._callbacks, {
callback = callback, callback = callback,
context = context context = context
@ -43,6 +44,10 @@ end
-- @tparam function callback Callback itself -- @tparam function callback Callback itself
-- @tparam table context Additional context as first param to callback call -- @tparam table context Additional context as first param to callback call
function DruidEvent.unsubscribe(self, callback, context) function DruidEvent.unsubscribe(self, callback, context)
if not self._callbacks then
return
end
for index, callback_info in ipairs(self._callbacks) do for index, callback_info in ipairs(self._callbacks) do
if callback_info.callback == callback and callback_info.context == context then if callback_info.callback == callback and callback_info.context == context then
table.remove(self._callbacks, index) table.remove(self._callbacks, index)
@ -56,6 +61,9 @@ end
-- @tparam DruidEvent self @{DruidEvent} -- @tparam DruidEvent self @{DruidEvent}
-- @treturn bool True if event have handlers -- @treturn bool True if event have handlers
function DruidEvent.is_exist(self) function DruidEvent.is_exist(self)
if not self._callbacks then
return false
end
return #self._callbacks > 0 return #self._callbacks > 0
end end
@ -63,7 +71,7 @@ end
--- Clear the all event handlers --- Clear the all event handlers
-- @tparam DruidEvent self @{DruidEvent} -- @tparam DruidEvent self @{DruidEvent}
function DruidEvent.clear(self) function DruidEvent.clear(self)
self._callbacks = {} self._callbacks = nil
end end
@ -71,6 +79,10 @@ end
-- @tparam DruidEvent self @{DruidEvent} -- @tparam DruidEvent self @{DruidEvent}
-- @tparam any ... All event params -- @tparam any ... All event params
function DruidEvent.trigger(self, ...) function DruidEvent.trigger(self, ...)
if not self._callbacks then
return false
end
for index, callback_info in ipairs(self._callbacks) do for index, callback_info in ipairs(self._callbacks) do
if callback_info.context then if callback_info.context then
callback_info.callback(callback_info.context, ...) callback_info.callback(callback_info.context, ...)

View File

@ -44,8 +44,8 @@ function DataList.init(self, scroll, grid, create_function)
self.druid = self:get_druid() self.druid = self:get_druid()
self.scroll = scroll self.scroll = scroll
self.grid = grid self.grid = grid
self.scroll:bind_grid(grid)
self.grid.style.IS_DYNAMIC_NODE_POSES = false self.grid.style.IS_DYNAMIC_NODE_POSES = false
self.scroll:bind_grid(grid)
-- Current visual elements indexes -- Current visual elements indexes
self.top_index = 1 self.top_index = 1

View File

@ -61,9 +61,10 @@ local data_list = require("druid.extended.data_list")
local DruidInstance = class("druid.druid_instance") local DruidInstance = class("druid.druid_instance")
local IS_NO_AUTO_INPUT = sys.get_config("druid.no_auto_input") == "1"
local function input_init(self) local function input_init(self)
if self._no_auto_input then if IS_NO_AUTO_INPUT then
return return
end end
@ -75,7 +76,7 @@ end
local function input_release(self) local function input_release(self)
if self._no_auto_input then if IS_NO_AUTO_INPUT then
return return
end end
@ -105,7 +106,7 @@ end
-- Create the component itself -- Create the component itself
local function create(self, instance_class) local function create(self, instance_class)
local instance = instance_class() local instance = instance_class()
instance:setup_component(self, self._context, self._style) instance:setup_component(self, self._context, self._style, instance_class)
table.insert(self.components_all, instance) table.insert(self.components_all, instance)
@ -206,7 +207,6 @@ function DruidInstance.initialize(self, context, style)
self._input_blacklist = nil self._input_blacklist = nil
self._input_whitelist = nil self._input_whitelist = nil
self._no_auto_input = (sys.get_config("druid.no_auto_input") == "1")
self.components_interest = {} self.components_interest = {}
self.components_all = {} self.components_all = {}
@ -239,7 +239,6 @@ function DruidInstance.new(self, component, ...)
instance:init(...) instance:init(...)
end end
self:log_message("Create component", { name = instance:get_name(), parent = instance:get_parent_name() })
return instance return instance
end end