#66 Add set_whitelist and set_blacklist functions

This commit is contained in:
Insality 2021-10-17 17:50:49 +03:00
parent a9de3771e3
commit a014fca1a1
8 changed files with 2136 additions and 6 deletions

View File

@ -212,4 +212,5 @@ Have a good day.
- If true - Static Grid will by always align to content anchor. - If true - Static Grid will by always align to content anchor.
- If false (currently behaviour) - all poses for static grid is predefined and not depends on element's count (see example: static grid and static grid with dynamic poses) - If false (currently behaviour) - all poses for static grid is predefined and not depends on element's count (see example: static grid and static grid with dynamic poses)
- **#132** Add example with grid add/remove with animations - **#132** Add example with grid add/remove with animations
- **#112** Allow remap default Druid input bindings. - **#112** Allow remap default Druid input bindings.
- **#66** Add `druid:set_whitelist()` and `druid.set_blacklist()` functions. It's affects only on input process step, you can allow/forbid interact with list of specific components

View File

@ -351,6 +351,26 @@ function BaseComponent.__remove_children(self, children)
end end
--- Return all children components, recursive
-- @tparam BaseComponent self
-- @treturn table Array of childrens if the Druid component instance
function BaseComponent.get_childrens(self)
local childrens = {}
for i = 1, #self._meta.children do
local children = self._meta.children[i]
table.insert(childrens, children)
local recursive_childrens = children:get_childrens()
for j = 1, #recursive_childrens do
table.insert(childrens, recursive_childrens[j])
end
end
return childrens
end
--- Create new component. It will inheritance from basic --- Create new component. It will inheritance from basic
-- druid component. -- druid component.
-- @tparam string name BaseComponent name -- @tparam string name BaseComponent name

View File

@ -132,8 +132,8 @@ local function check_sort_input_stack(self, components)
local component = components[i] local component = components[i]
if component:_is_input_priority_changed() then if component:_is_input_priority_changed() then
is_need_sort_input_stack = true is_need_sort_input_stack = true
component:_reset_input_priority_changed()
end end
component:_reset_input_priority_changed()
end end
if is_need_sort_input_stack then if is_need_sort_input_stack then
@ -142,7 +142,35 @@ local function check_sort_input_stack(self, components)
end end
local function process_input(action_id, action, components, is_input_consumed)
--- Check whitelists and blacklists for input components
local function can_use_input_component(self, component)
local can_by_whitelist = true
local can_by_blacklist = true
if self._input_whitelist and #self._input_whitelist > 0 then
if helper.contains(self._input_whitelist, component) then
can_by_whitelist = true
else
can_by_whitelist = false
end
end
if self._input_blacklist and #self._input_blacklist > 0 then
if helper.contains(self._input_blacklist, component) then
can_by_blacklist = false
else
can_by_blacklist = true
end
end
return can_by_blacklist and can_by_whitelist
end
local function process_input(self, action_id, action, components)
local is_input_consumed = false
if #components == 0 then if #components == 0 then
return is_input_consumed return is_input_consumed
end end
@ -150,7 +178,7 @@ local function process_input(action_id, action, components, is_input_consumed)
for i = #components, 1, -1 do for i = #components, 1, -1 do
local component = components[i] local component = components[i]
local meta = component._meta local meta = component._meta
if meta.input_enabled then if meta.input_enabled and can_use_input_component(self, component) then
if not is_input_consumed then if not is_input_consumed then
is_input_consumed = component:on_input(action_id, action) is_input_consumed = component:on_input(action_id, action)
else else
@ -177,6 +205,9 @@ function DruidInstance.initialize(self, context, style)
self._late_remove = {} self._late_remove = {}
self.url = msg.url() self.url = msg.url()
self._input_blacklist = nil
self._input_whitelist = nil
self.components = {} self.components = {}
for i = 1, #base_component.ALL_INTERESTS do for i = 1, #base_component.ALL_INTERESTS do
self.components[base_component.ALL_INTERESTS[i]] = {} self.components[base_component.ALL_INTERESTS[i]] = {}
@ -294,13 +325,13 @@ end
-- @tparam DruidInstance self -- @tparam DruidInstance self
-- @tparam hash action_id Action_id from on_input -- @tparam hash action_id Action_id from on_input
-- @tparam table action Action from on_input -- @tparam table action Action from on_input
-- @treturn bool The boolean value is input was consumed
function DruidInstance.on_input(self, action_id, action) function DruidInstance.on_input(self, action_id, action)
self._is_input_processing = true self._is_input_processing = true
local is_input_consumed = false
local components = self.components[base_component.ON_INPUT] local components = self.components[base_component.ON_INPUT]
check_sort_input_stack(self, components) check_sort_input_stack(self, components)
is_input_consumed = process_input(action_id, action, components, is_input_consumed) local is_input_consumed = process_input(self, action_id, action, components)
self._is_input_processing = false self._is_input_processing = false
@ -376,6 +407,7 @@ end
--- Druid on language change. --- Druid on language change.
-- This one called by global gruid.on_language_change, but can be -- This one called by global gruid.on_language_change, but can be
-- call manualy to update all translations -- call manualy to update all translations
-- @tparam DruidInstance self
-- @function druid.on_language_change -- @function druid.on_language_change
function DruidInstance.on_language_change(self) function DruidInstance.on_language_change(self)
local components = self.components[base_component.ON_LANGUAGE_CHANGE] local components = self.components[base_component.ON_LANGUAGE_CHANGE]
@ -385,6 +417,52 @@ function DruidInstance.on_language_change(self)
end end
--- Set whitelist components for input processing.
-- If whitelist is not empty and component not contains in this list,
-- component will be not processed on input step
-- @tparam DruidInstance self
-- @tparam[opt=nil] table|Component whitelist_components The array of component to whitelist
-- @function druid.set_whitelist
function DruidInstance.set_whitelist(self, whitelist_components)
if whitelist_components and whitelist_components.isInstanceOf then
whitelist_components = { whitelist_components }
end
for i = 1, #whitelist_components do
local component = whitelist_components[i]
local childrens = component:get_childrens()
for j = 1, #childrens do
table.insert(whitelist_components, childrens[j])
end
end
self._input_whitelist = whitelist_components
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[opt=nil] table|Component blacklist_components The array of component to blacklist
-- @function druid.set_blacklist
function DruidInstance.set_blacklist(self, blacklist_components)
if blacklist_components and blacklist_components.isInstanceOf then
blacklist_components = { blacklist_components }
end
for i = 1, #blacklist_components do
local component = blacklist_components[i]
local childrens = component:get_childrens()
for j = 1, #childrens do
table.insert(blacklist_components, childrens[j])
end
end
self._input_blacklist = blacklist_components
end
--- Create button basic component --- Create button basic component
-- @tparam DruidInstance self -- @tparam DruidInstance self
-- @tparam node node Gui node -- @tparam node node Gui node

View File

@ -1058,3 +1058,66 @@ embedded_instances {
z: 1.0 z: 1.0
} }
} }
embedded_instances {
id: "system_whitelist_blacklist"
data: "components {\n"
" id: \"screen_factory\"\n"
" component: \"/monarch/screen_factory.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" properties {\n"
" id: \"screen_id\"\n"
" value: \"system_whitelist_blacklist\"\n"
" type: PROPERTY_TYPE_HASH\n"
" }\n"
" properties {\n"
" id: \"popup\"\n"
" value: \"true\"\n"
" type: PROPERTY_TYPE_BOOLEAN\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collectionfactory\"\n"
" type: \"collectionfactory\"\n"
" data: \"prototype: \\\"/example/examples/system/whitelist_blacklist/whitelist_blacklist.collection\\\"\\n"
"load_dynamically: false\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

View File

@ -130,6 +130,7 @@ local function init_lobby(self)
self.lobby_grid:add(get_title(self, "System")) self.lobby_grid:add(get_title(self, "System"))
self.lobby_grid:add(get_button_disabled(self, "Styles")) self.lobby_grid:add(get_button_disabled(self, "Styles"))
self.lobby_grid:add(get_button(self, "Whitelist / Blacklist", "system_whitelist_blacklist"))
self.lobby_grid:add(get_button_disabled(self, "Custom components")) self.lobby_grid:add(get_button_disabled(self, "Custom components"))
self.lobby_grid:add(get_button_disabled(self, "Component interests")) self.lobby_grid:add(get_button_disabled(self, "Component interests"))
self.lobby_grid:add(get_button_disabled(self, "Nested Druids")) self.lobby_grid:add(get_button_disabled(self, "Nested Druids"))

View File

@ -0,0 +1,37 @@
name: "whitelist_blacklist"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"whitelist_blacklist\"\n"
" component: \"/example/examples/system/whitelist_blacklist/whitelist_blacklist.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,66 @@
local druid = require("druid.druid")
local function click_callback(self)
print("Just tap callback")
end
function init(self)
self.druid_whitelist = druid.new(self)
-- Whitelist
self.button_wl_left = self.druid_whitelist:new_button("button_whitelist_1/button", click_callback)
self.button_wl_middle = self.druid_whitelist:new_button("button_whitelist_2/button", click_callback)
self.button_wl_right = self.druid_whitelist:new_button("button_whitelist_3/button", click_callback)
self.druid_whitelist:set_whitelist(self.button_wl_left)
self.druid_blacklist = druid.new(self)
-- Blacklist
self.button_bl_left = self.druid_blacklist:new_button("button_blacklist_1/button", click_callback)
self.button_bl_middle = self.druid_blacklist:new_button("button_blacklist_2/button", click_callback)
self.button_bl_right = self.druid_blacklist:new_button("button_blacklist_3/button", click_callback)
self.druid_blacklist:set_blacklist(self.button_bl_left)
self.druid_wb = druid.new(self)
-- Blacklist and Whitelist
self.button_wb_left = self.druid_wb:new_button("button_wb_1/button", click_callback)
self.button_wb_middle = self.druid_wb:new_button("button_wb_2/button", click_callback)
self.button_wb_right = self.druid_wb:new_button("button_wb_3/button", click_callback)
self.druid_wb:set_whitelist(self.button_wb_left)
self.druid_wb:set_blacklist(self.button_wb_left)
end
function final(self)
self.druid_whitelist:final()
self.druid_blacklist:final()
self.druid_wb:final()
end
function update(self, dt)
self.druid_whitelist:update(dt)
self.druid_blacklist:update(dt)
self.druid_wb:update(dt)
end
function on_message(self, message_id, message, sender)
self.druid_whitelist:on_message(message_id, message, sender)
self.druid_blacklist:on_message(message_id, message, sender)
self.druid_wb:on_message(message_id, message, sender)
end
function on_input(self, action_id, action)
local result_1 = self.druid_whitelist:on_input(action_id, action)
local result_2 = self.druid_blacklist:on_input(action_id, action)
local result_3 = self.druid_wb:on_input(action_id, action)
return result_1 or result_2 or result_3
end