mirror of
https://github.com/Insality/druid
synced 2025-09-27 10:02:19 +02:00
Add reinit data_list example, add debug mode for druid/components. Refactor scroll is_in_view. Use late remove while scroll update
This commit is contained in:
@@ -168,7 +168,6 @@ function Scroll.init(self, view_node, content_node)
|
||||
self._is_vertical_scroll = true
|
||||
self._grid_on_change = nil
|
||||
self._grid_on_change_callback = nil
|
||||
self._outside_offset_vector = vmath.vector3(0)
|
||||
|
||||
self:_update_size()
|
||||
end
|
||||
@@ -190,8 +189,6 @@ end
|
||||
|
||||
|
||||
function Scroll.update(self, dt)
|
||||
self:_update_params()
|
||||
|
||||
if self.drag.is_drag then
|
||||
self:_update_hand_scroll(dt)
|
||||
else
|
||||
@@ -402,8 +399,27 @@ end
|
||||
-- @tparam node node The node to check
|
||||
-- @treturn boolean True if node in visible scroll area
|
||||
function Scroll.is_node_in_view(self, node)
|
||||
local node_border = helper.get_border(node, gui.get_position(node))
|
||||
local view_border = helper.get_border(self.view_node, -(self.position - self._outside_offset_vector))
|
||||
local node_offset_for_view = gui.get_position(node)
|
||||
local parent = gui.get_parent(node)
|
||||
local is_parent_of_view = false
|
||||
while parent do
|
||||
if parent ~= self.view_node then
|
||||
local parent_pos = gui.get_position(parent)
|
||||
node_offset_for_view.x = node_offset_for_view.x + parent_pos.x
|
||||
node_offset_for_view.y = node_offset_for_view.y + parent_pos.y
|
||||
parent = gui.get_parent(parent)
|
||||
else
|
||||
is_parent_of_view = true
|
||||
parent = nil
|
||||
end
|
||||
end
|
||||
if not is_parent_of_view then
|
||||
error("The node to check is_node_in_view should be child if scroll view")
|
||||
return false
|
||||
end
|
||||
|
||||
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):
|
||||
if node_border.z < view_border.x or node_border.x > view_border.z then
|
||||
@@ -438,7 +454,10 @@ function Scroll.bind_grid(self, grid)
|
||||
|
||||
self._grid_on_change = grid.on_change_items
|
||||
self._grid_on_change_callback = self._grid_on_change:subscribe(function()
|
||||
self:set_size(grid:get_size(), grid:get_offset())
|
||||
local size = grid:get_size()
|
||||
local offset = grid:get_offset()
|
||||
self:set_size(size, offset)
|
||||
self:log_message("Change size from grid", { size = size, offset = offset })
|
||||
end)
|
||||
self:set_size(grid:get_size(), grid:get_offset())
|
||||
|
||||
@@ -547,7 +566,6 @@ function Scroll._set_scroll_position(self, position)
|
||||
if self.position.x ~= position.x or self.position.y ~= position.y then
|
||||
self.position.x = position.x
|
||||
self.position.y = position.y
|
||||
self:_update_params()
|
||||
gui.set_position(self.content_node, position)
|
||||
|
||||
self.on_scroll:trigger(self:get_context(), self.position)
|
||||
@@ -718,32 +736,6 @@ function Scroll._update_size(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._update_params(self)
|
||||
local t = self.target_position
|
||||
local b = self.available_pos
|
||||
|
||||
self._outside_offset_vector.x = 0
|
||||
self._outside_offset_vector.y = 0
|
||||
|
||||
-- Right border (minimum x)
|
||||
if t.x < b.x then
|
||||
self._outside_offset_vector.x = t.x - b.x
|
||||
end
|
||||
-- Left border (maximum x)
|
||||
if t.x > b.z then
|
||||
self._outside_offset_vector.x = t.x - b.z
|
||||
end
|
||||
-- Top border (minimum y)
|
||||
if t.y < b.y then
|
||||
self._outside_offset_vector.y = t.y - b.y
|
||||
end
|
||||
-- Bot border (maximum y)
|
||||
if t.y > b.w then
|
||||
self._outside_offset_vector.y = t.y - b.w
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Scroll._process_scroll_wheel(self, action_id, action)
|
||||
if not self._is_mouse_hover or self.style.WHEEL_SCROLL_SPEED == 0 then
|
||||
return false
|
||||
|
@@ -190,7 +190,16 @@ end
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @treturn string The component name
|
||||
function BaseComponent.get_name(self)
|
||||
return self._component.name
|
||||
return self._component.name .. self:get_uid()
|
||||
end
|
||||
|
||||
|
||||
--- Return parent component name
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @treturn string|nil The parent component name if exist or bil
|
||||
function BaseComponent.get_parent_name(self)
|
||||
local parent = self:get_parent_component()
|
||||
return parent and parent:get_name()
|
||||
end
|
||||
|
||||
|
||||
@@ -313,12 +322,33 @@ function BaseComponent.initialize(self, name, input_priority)
|
||||
name = name,
|
||||
input_priority = input_priority or const.PRIORITY_INPUT,
|
||||
default_input_priority = input_priority or const.PRIORITY_INPUT,
|
||||
is_debug = false,
|
||||
_is_input_priority_changed = true, -- Default true for sort once time after GUI init
|
||||
_uid = BaseComponent.get_uid()
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
--- Print log information if debug mode is enabled (protected)
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @tparam string message
|
||||
-- @tparam table context
|
||||
function BaseComponent.log_message(self, message, context)
|
||||
if not self._component.is_debug then
|
||||
return
|
||||
end
|
||||
print("[" .. self:get_name() .. "]:", message, helper.table_to_string(context))
|
||||
end
|
||||
|
||||
|
||||
--- Set debug logs for component enabled or disabled
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @tparam bool is_debug
|
||||
function BaseComponent.set_debug(self, is_debug)
|
||||
self._component.is_debug = is_debug
|
||||
end
|
||||
|
||||
|
||||
--- Return true, if input priority was changed
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @local
|
||||
|
@@ -61,6 +61,8 @@ function DataList.init(self, scroll, grid, create_function)
|
||||
self.scroll.on_scroll:subscribe(self._check_elements, self)
|
||||
|
||||
self.on_scroll_progress_change = Event()
|
||||
|
||||
self:set_data()
|
||||
end
|
||||
|
||||
|
||||
@@ -109,6 +111,8 @@ function DataList.add(self, data, index, shift_policy)
|
||||
self._data[index] = data
|
||||
self:_update_data_info()
|
||||
self:_check_elements()
|
||||
|
||||
self:log_message("Add element", { index = index })
|
||||
end
|
||||
|
||||
|
||||
@@ -120,6 +124,8 @@ end
|
||||
function DataList.remove(self, index, shift_policy)
|
||||
table.remove(self._data, index)
|
||||
self:_refresh()
|
||||
|
||||
self:log_message("Remove element", { index = index })
|
||||
end
|
||||
|
||||
|
||||
@@ -209,6 +215,8 @@ function DataList._add_at(self, index)
|
||||
node = node,
|
||||
component = instance
|
||||
}
|
||||
|
||||
self:log_message("Add element at", { index = index })
|
||||
end
|
||||
|
||||
|
||||
@@ -226,6 +234,8 @@ function DataList._remove_at(self, index)
|
||||
self.druid:remove(self._data_visual[index].component)
|
||||
end
|
||||
self._data_visual[index] = nil
|
||||
|
||||
self:log_message("Remove element at", { index = index })
|
||||
end
|
||||
|
||||
|
||||
@@ -270,6 +280,8 @@ function DataList._check_elements(self)
|
||||
progress = 0
|
||||
end
|
||||
|
||||
self:log_message("Check elements", { top_index = self.top_index, last_index = self.last_index, progress = progress })
|
||||
|
||||
if self.scroll_progress ~= progress then
|
||||
self.scroll_progress = progress
|
||||
self.on_scroll_progress_change:trigger(self:get_context(), progress)
|
||||
|
@@ -222,6 +222,27 @@ function M.is_web()
|
||||
end
|
||||
|
||||
|
||||
--- Transform table to oneline string
|
||||
-- @tparam table t
|
||||
-- @treturn string
|
||||
function M.table_to_string(t)
|
||||
if not t then
|
||||
return const.EMPTY_STRING
|
||||
end
|
||||
|
||||
local result = "{"
|
||||
|
||||
for key, value in pairs(t) do
|
||||
if #result > 1 then
|
||||
result = result .. ","
|
||||
end
|
||||
result = result .. key .. ": " .. value
|
||||
end
|
||||
|
||||
return result .. "}"
|
||||
end
|
||||
|
||||
|
||||
--- Distance from node position to his borders
|
||||
-- @function helper.get_border
|
||||
-- @tparam node node The gui node to check
|
||||
|
@@ -199,8 +199,9 @@ function DruidInstance.initialize(self, context, style)
|
||||
self._context = context
|
||||
self._style = style or settings.default_style
|
||||
self._deleted = false
|
||||
self._is_input_processing = false
|
||||
self._is_late_remove_enabled = false
|
||||
self._late_remove = {}
|
||||
self._is_debug = false
|
||||
self.url = msg.url()
|
||||
|
||||
self._input_blacklist = nil
|
||||
@@ -238,6 +239,7 @@ function DruidInstance.new(self, component, ...)
|
||||
instance:init(...)
|
||||
end
|
||||
|
||||
self:log_message("Create component", { name = instance:get_name(), parent = instance:get_parent_name() })
|
||||
return instance
|
||||
end
|
||||
|
||||
@@ -257,6 +259,8 @@ function DruidInstance.final(self)
|
||||
self._deleted = true
|
||||
|
||||
input_release(self)
|
||||
|
||||
self:log_message("Druid final")
|
||||
end
|
||||
|
||||
|
||||
@@ -265,7 +269,7 @@ end
|
||||
-- @tparam DruidInstance self
|
||||
-- @tparam Component component Component instance
|
||||
function DruidInstance.remove(self, component)
|
||||
if self._is_input_processing then
|
||||
if self._is_late_remove_enabled then
|
||||
table.insert(self._late_remove, component)
|
||||
return
|
||||
end
|
||||
@@ -301,6 +305,8 @@ function DruidInstance.remove(self, component)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self:log_message("Remove", { name = component:get_name(), parent = component:get_parent_name() })
|
||||
end
|
||||
|
||||
|
||||
@@ -319,10 +325,14 @@ function DruidInstance.update(self, dt)
|
||||
input_init(self)
|
||||
end
|
||||
|
||||
self._is_late_remove_enabled = true
|
||||
local components = self.components_interest[base_component.ON_UPDATE]
|
||||
for i = 1, #components do
|
||||
components[i]:update(dt)
|
||||
end
|
||||
self._is_late_remove_enabled = false
|
||||
|
||||
self:_clear_late_remove()
|
||||
end
|
||||
|
||||
|
||||
@@ -332,21 +342,15 @@ end
|
||||
-- @tparam table action Action from on_input
|
||||
-- @treturn bool The boolean value is input was consumed
|
||||
function DruidInstance.on_input(self, action_id, action)
|
||||
self._is_input_processing = true
|
||||
self._is_late_remove_enabled = true
|
||||
|
||||
local components = self.components_interest[base_component.ON_INPUT]
|
||||
check_sort_input_stack(self, components)
|
||||
local is_input_consumed = process_input(self, action_id, action, components)
|
||||
|
||||
self._is_input_processing = false
|
||||
|
||||
if #self._late_remove > 0 then
|
||||
for i = 1, #self._late_remove do
|
||||
self:remove(self._late_remove[i])
|
||||
end
|
||||
self._late_remove = {}
|
||||
end
|
||||
self._is_late_remove_enabled = false
|
||||
|
||||
self:_clear_late_remove()
|
||||
return is_input_consumed
|
||||
end
|
||||
|
||||
@@ -397,6 +401,8 @@ function DruidInstance.on_focus_lost(self)
|
||||
for i = 1, #components do
|
||||
components[i]:on_focus_lost()
|
||||
end
|
||||
|
||||
self:log_message("On focus lost")
|
||||
end
|
||||
|
||||
|
||||
@@ -409,11 +415,13 @@ function DruidInstance.on_focus_gained(self)
|
||||
for i = 1, #components do
|
||||
components[i]:on_focus_gained()
|
||||
end
|
||||
|
||||
self:log_message("On focus gained")
|
||||
end
|
||||
|
||||
|
||||
--- Druid on language change.
|
||||
-- This one called by global gruid.on_language_change, but can be
|
||||
-- This one called by global druid.on_language_change, but can be
|
||||
-- call manualy to update all translations
|
||||
-- @tparam DruidInstance self
|
||||
-- @local
|
||||
@@ -422,6 +430,8 @@ function DruidInstance.on_language_change(self)
|
||||
for i = 1, #components do
|
||||
components[i]:on_language_change()
|
||||
end
|
||||
|
||||
self:log_message("On language change")
|
||||
end
|
||||
|
||||
|
||||
@@ -450,7 +460,7 @@ end
|
||||
--- Set blacklist components for input processing.
|
||||
-- If blacklist is not empty and component contains in this list,
|
||||
-- component will be not processed on input step
|
||||
-- @tparam DruidInstance self
|
||||
-- @tparam DruidInstance self @{DruidInstance}
|
||||
-- @tparam[opt=nil] table|Component blacklist_components The array of component to blacklist
|
||||
function DruidInstance.set_blacklist(self, blacklist_components)
|
||||
if blacklist_components and blacklist_components.isInstanceOf then
|
||||
@@ -469,6 +479,40 @@ function DruidInstance.set_blacklist(self, blacklist_components)
|
||||
end
|
||||
|
||||
|
||||
--- Set debug mode for current Druid instance. It's enable debug log messages
|
||||
-- @tparam DruidInstance self @{DruidInstance}
|
||||
-- @tparam bool is_debug
|
||||
-- @treturn self @{DruidInstance}
|
||||
function DruidInstance.set_debug(self, is_debug)
|
||||
self._is_debug = is_debug
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Log message, if is_debug mode is enabled
|
||||
-- @tparam DruidInstance self @{DruidInstance}
|
||||
-- @tparam string message
|
||||
-- @tparam[opt] table context
|
||||
function DruidInstance.log_message(self, message, context)
|
||||
if not self._is_debug then
|
||||
return
|
||||
end
|
||||
print("[Druid]:", message, helper.table_to_string(context))
|
||||
end
|
||||
|
||||
|
||||
--- Remove all components on late remove step
|
||||
-- @tparam DruidInstance self @{DruidInstance}
|
||||
-- @local
|
||||
function DruidInstance._clear_late_remove(self)
|
||||
if #self._late_remove > 0 then
|
||||
for i = 1, #self._late_remove do
|
||||
self:remove(self._late_remove[i])
|
||||
end
|
||||
self._late_remove = {}
|
||||
end
|
||||
end
|
||||
|
||||
--- Create button basic component
|
||||
-- @tparam DruidInstance self
|
||||
-- @tparam node node Gui node
|
||||
|
Reference in New Issue
Block a user