From d93823ae6ae5825a1a642c3b57c952e95136876c Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 3 Dec 2022 16:16:55 +0200 Subject: [PATCH] Solve #215 Better late init interest support --- druid/system/druid_instance.lua | 21 +- example/example.collection | 129 ++++++ example/example.gui_script | 1 + .../late_init_check.collection | 39 ++ .../late_init_check/late_init_check.gui | 374 ++++++++++++++++++ .../late_init_check.gui_script | 46 +++ 6 files changed, 605 insertions(+), 5 deletions(-) create mode 100644 example/examples/system/late_init_check/late_init_check.collection create mode 100644 example/examples/system/late_init_check/late_init_check.gui create mode 100644 example/examples/system/late_init_check/late_init_check.gui_script diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 33d44e7..384f44c 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -193,6 +193,18 @@ local function process_input(self, action_id, action, components) end +local function schedule_late_init(self) + if self._late_init_timer_id then + return + end + + self._late_init_timer_id = timer.delay(0, false, function() + self._late_init_timer_id = nil + self:late_init() + end) +end + + --- Druid class constructor -- @tparam DruidInstance self -- @tparam table context Druid context. Usually it is self of script @@ -215,10 +227,6 @@ function DruidInstance.initialize(self, context, style) for i = 1, #base_component.ALL_INTERESTS do self.components_interest[base_component.ALL_INTERESTS[i]] = {} end - - timer.delay(0, false, function() - self:late_init() - end) end @@ -244,6 +252,9 @@ function DruidInstance.new(self, component, ...) if instance.init then instance:init(...) end + if instance.on_late_init then + schedule_late_init(self) + end return instance end @@ -315,7 +326,7 @@ function DruidInstance.remove(self, component) end ---- Druid late update function call after init and before udpate step +--- Druid late update function call after init and before update step -- @tparam DruidInstance self function DruidInstance.late_init(self) local late_init_components = self.components_interest[base_component.ON_LATE_INIT] diff --git a/example/example.collection b/example/example.collection index 097a84f..1268d66 100644 --- a/example/example.collection +++ b/example/example.collection @@ -16,6 +16,8 @@ embedded_instances { " z: 0.0\n" " w: 1.0\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "" position { @@ -51,6 +53,8 @@ embedded_instances { " z: 0.0\n" " w: 1.0\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "" position { @@ -139,6 +143,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -202,6 +208,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -265,6 +273,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -328,6 +338,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -391,6 +403,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -454,6 +468,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -517,6 +533,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -580,6 +598,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -643,6 +663,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -706,6 +728,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -769,6 +793,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -832,6 +858,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -895,6 +923,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -958,6 +988,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1021,6 +1053,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1084,6 +1118,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1147,6 +1183,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1210,6 +1248,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1273,6 +1313,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1336,6 +1378,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1399,6 +1443,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1462,6 +1508,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1525,6 +1573,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1588,6 +1638,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1651,6 +1703,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1714,6 +1768,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1777,6 +1833,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1840,6 +1898,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1903,6 +1963,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -1966,6 +2028,8 @@ embedded_instances { " value: \"true\"\n" " type: PROPERTY_TYPE_BOOLEAN\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "embedded_components {\n" " id: \"collectionfactory\"\n" @@ -2003,3 +2067,68 @@ embedded_instances { z: 1.0 } } +embedded_instances { + id: "system_late_init_check" + 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_late_init_check\"\n" + " type: PROPERTY_TYPE_HASH\n" + " }\n" + " properties {\n" + " id: \"popup\"\n" + " value: \"true\"\n" + " type: PROPERTY_TYPE_BOOLEAN\n" + " }\n" + " property_decls {\n" + " }\n" + "}\n" + "embedded_components {\n" + " id: \"collectionfactory\"\n" + " type: \"collectionfactory\"\n" + " data: \"prototype: \\\"/example/examples/system/late_init_check/late_init_check.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 + } +} diff --git a/example/example.gui_script b/example/example.gui_script index e8f0709..08d76f4 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -177,6 +177,7 @@ local function init_lobby(self) self.lobby_grid:add(get_button(self, "Message input", "system_message_input", "/system/message_input/message_input.gui_script")) self.lobby_grid:add(get_button_disabled(self, "Input priority")) self.lobby_grid:add(get_button(self, "Inner templates", "system_inner_templates", "/system/inner_templates/inner_templates.gui_script")) + self.lobby_grid:add(get_button(self, "Late init check", "system_late_init_check", "/system/late_init_check/late_init_check.gui_script")) end diff --git a/example/examples/system/late_init_check/late_init_check.collection b/example/examples/system/late_init_check/late_init_check.collection new file mode 100644 index 0000000..9700af5 --- /dev/null +++ b/example/examples/system/late_init_check/late_init_check.collection @@ -0,0 +1,39 @@ +name: "late_init_check" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"late_init_check\"\n" + " component: \"/example/examples/system/late_init_check/late_init_check.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" + " property_decls {\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 + } +} diff --git a/example/examples/system/late_init_check/late_init_check.gui b/example/examples/system/late_init_check/late_init_check.gui new file mode 100644 index 0000000..9869261 --- /dev/null +++ b/example/examples/system/late_init_check/late_init_check.gui @@ -0,0 +1,374 @@ +script: "/example/examples/system/late_init_check/late_init_check.gui_script" +fonts { + name: "game" + font: "/example/assets/fonts/game.font" +} +textures { + name: "kenney" + texture: "/example/assets/images/kenney.atlas" +} +background_color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 +} +nodes { + position { + x: 300.0 + y: 415.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 600.0 + y: 830.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/empty" + id: "root" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL + custom_type: 0 + enabled: true + visible: true +} +nodes { + position { + x: 0.0 + y: 200.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 500.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Late init system check\n" + "\n" + "Should capture input only after create first input component" + font: "game" + id: "text_hint" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + parent: "root" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 + custom_type: 0 + enabled: true + visible: true +} +nodes { + position { + x: 0.0 + y: -46.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "prefab_button" + parent: "root" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/example/templates/button.gui" + template_node_child: false + custom_type: 0 + enabled: true +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 60.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/button_blue" + id: "prefab_button/button" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "prefab_button" + layer: "image" + inherit_alpha: true + slice9 { + x: 15.0 + y: 15.0 + z: 15.0 + w: 15.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + overridden_fields: 4 + template_node_child: true + size_mode: SIZE_MODE_MANUAL + custom_type: 0 + enabled: true + visible: true +} +nodes { + position { + x: 0.0 + y: 7.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 0.7 + y: 0.7 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Tap me!" + font: "game" + id: "prefab_button/text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 0.101960786 + y: 0.2 + z: 0.6 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "prefab_button/button" + layer: "text" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 0.0 + shadow_alpha: 0.78 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 + custom_type: 0 + enabled: true + visible: true +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 400.0 + y: 300.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/empty" + id: "grid" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_N + adjust_mode: ADJUST_MODE_FIT + parent: "root" + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL + custom_type: 0 + enabled: true + visible: true +} +layers { + name: "image" +} +layers { + name: "text" +} +material: "/builtins/materials/gui.material" +adjust_reference: ADJUST_REFERENCE_PARENT +max_nodes: 512 diff --git a/example/examples/system/late_init_check/late_init_check.gui_script b/example/examples/system/late_init_check/late_init_check.gui_script new file mode 100644 index 0000000..2820029 --- /dev/null +++ b/example/examples/system/late_init_check/late_init_check.gui_script @@ -0,0 +1,46 @@ +local druid = require("druid.druid") + + +local function create_button(self, index) + local cloned = gui.clone_tree(self.prefab) + local root = cloned["prefab_button/button"] + self.grid:add(root) + gui.set_enabled(root, true) + + local button = self.druid:new_button(root, function() + print("Created button with timer delay:", index) + end) + -- Override to check calls, don't do it in production ;) + button.on_late_init = function() + print("Late init button check", index) + end + + print("Button created, is input inited now?", self.druid.input_inited) +end + + +function init(self) + self.druid = druid.new(self) + + self.prefab = gui.get_node("prefab_button/button") + gui.set_enabled(self.prefab, false) + + self.grid = self.druid:new_static_grid("grid", self.prefab, 1) + + create_button(self, 0) + for index = 1, 4 do + timer.delay(index, false, function() + create_button(self, index) + end) + end +end + + +function final(self) + self.druid:final() +end + + +function on_input(self, action_id, action) + return self.druid:on_input(action_id, action) +end