diff --git a/editor_scripts/component_template.lua b/editor_scripts/component_template.lua index b67cf3b1..eb70a9d4 100644 --- a/editor_scripts/component_template.lua +++ b/editor_scripts/component_template.lua @@ -1,24 +1,26 @@ --- For component interest functions --- see https://github.com/Insality/druid/blob/develop/docs_md/02-creating_custom_components.md +--- Require this component in you gui file: +--- local {COMPONENT_NAME} = require("{COMPONENT_PATH}") +--- And create this component via: +--- self.{COMPONENT_TYPE} = self.druid:new({COMPONENT_NAME}, template, nodes) local component = require("druid.component") ----@class {COMPONENT_TYPE} : druid.base_component +---@class {COMPONENT_TYPE}: druid.base_component{COMPONENT_ANNOTATIONS} local {COMPONENT_NAME} = component.create("{COMPONENT_TYPE}") local SCHEME = { {SCHEME_LIST} } - - ---- Create this component via druid:new({COMPONENT_NAME}, template, nodes) +{COMPONENT_FUNCTIONS} ---@param template string ---@param nodes table function {COMPONENT_NAME}:init(template, nodes) - self:set_template(template) - self:set_nodes(nodes) - self.root = self:get_node(SCHEME.ROOT) - self.druid = self:get_druid() + self:set_template(template) + self:set_nodes(nodes) + self.root = self:get_node(SCHEME.ROOT) + self.druid = self:get_druid(){COMPONENT_DEFINE} end diff --git a/editor_scripts/create_druid_component.py b/editor_scripts/create_druid_component.py index 7398da9e..4a59fd80 100644 --- a/editor_scripts/create_druid_component.py +++ b/editor_scripts/create_druid_component.py @@ -8,11 +8,77 @@ current_filepath = os.path.abspath(os.path.dirname(__file__)) TEMPLATE_FILE = open(current_filepath + "/component_template.lua", "r") +component_annotations = "" +component_functions = "" +component_define = "" + def to_camel_case(snake_str): - components = snake_str.split('_') - return ''.join(x.title() for x in components[0:]) + components = snake_str.split('_') + return ''.join(x.title() for x in components[0:]) + + +def get_id(node_name): + return node_name.upper().replace("/", "_") + + +def process_component(node_name, component_name): + global component_annotations + global component_functions + global component_define + + if node_name.startswith("button"): + component_annotations += "\n---@field {0} druid.button".format(node_name) + component_functions += "\nfunction {1}:_on_{0}()\n\tprint(\"Click on {0}\")\nend\n\n".format(node_name, component_name) + component_define += "\n\tself.{0} = self.druid:new_button(SCHEME.{1}, self._on_{0})".format(node_name, get_id(node_name)) + + if node_name.startswith("text"): + component_annotations += "\n---@field {0} druid.text".format(node_name) + component_define += "\n\tself.{0} = self.druid:new_text(SCHEME.{1})".format(node_name, get_id(node_name)) + + if node_name.startswith("lang_text"): + component_annotations += "\n---@field {0} druid.text".format(node_name) + component_define += "\n\tself.{0} = self.druid:new_lang_text(SCHEME.{1}, \"lang_id\")".format(node_name, get_id(node_name)) + + if node_name.startswith("grid") or node_name.startswith("static_grid"): + component_annotations += "\n---@field {0} druid.static_grid".format(node_name) + component_define += "\n--TODO: Replace prefab_name with grid element prefab" + component_define += "\n\tself.{0} = self.druid:new_static_grid(SCHEME.{1}, \"prefab_name\", 1)".format(node_name, get_id(node_name)) + + if node_name.startswith("dynamic_grid"): + component_annotations += "\n---@field {0} druid.dynamic_grid".format(node_name) + component_define += "\n\tself.{0} = self.druid:new_dynamic_grid(SCHEME.{1})".format(node_name, get_id(node_name)) + + if node_name.startswith("scroll_view"): + field_name = node_name.replace("_view", "") + content_name = node_name.replace("_view", "_content") + component_annotations += "\n---@field {0} druid.scroll".format(field_name) + component_define += "\n\tself.{0} = self.druid:new_scroll(SCHEME.{1}, SCHEME.{2})".format(field_name, get_id(node_name), get_id(content_name)) + + if node_name.startswith("blocker"): + component_annotations += "\n---@field {0} druid.blocker".format(node_name) + component_define += "\n\tself.{0} = self.druid:new_blocker(SCHEME.{1})".format(node_name, get_id(node_name)) + + if node_name.startswith("slider"): + component_annotations += "\n---@field {0} druid.slider".format(node_name) + component_define += "\n--TODO: Replace slider end position. It should be only vertical or horizontal" + component_define += "\n\tself.{0} = self.druid:new_slider(SCHEME.{1}, vmath.vector3(100, 0, 0), self._on_{0}_change)".format(node_name, get_id(node_name)) + component_functions += "\nfunction {1}:_on_{0}_change(value)\n\tprint(\"Slider change:\", value)\nend\n\n".format(node_name, component_name) + + if node_name.startswith("progress"): + component_annotations += "\n---@field {0} druid.progress".format(node_name) + component_define += "\n\tself.{0} = self.druid:new_progress(SCHEME.{1}, \"x\")".format(node_name, get_id(node_name)) + + if node_name.startswith("timer"): + component_annotations += "\n---@field {0} druid.timer".format(node_name) + component_define += "\n\tself.{0} = self.druid:new_timer(SCHEME.{1}, 59, 0, self._on_{0}_end)".format(node_name, get_id(node_name)) + component_functions += "\nfunction {1}:_on_{0}_end()\n\tprint(\"Timer {0} trigger\")\nend\n\n".format(node_name, component_name) + def main(): + global component_annotations + global component_functions + global component_define + filename = sys.argv[1] print("Create Druid component from gui file", filename) tree = deftree.parse(filename) @@ -28,17 +94,27 @@ def main(): print("File:", output_full_path) return + component_require_path = os.path.join(output_directory, output_filename).replace("/", ".").replace("..", "") component_name = to_camel_case(output_filename) component_type = output_filename scheme_list = [] + # Gather nodes from GUI scene for node in root.iter_elements("nodes"): - name = node.get_attribute("id").value - scheme_list.append("\t" + name.upper() + " = \"" + name + "\"") + node_name = node.get_attribute("id").value + scheme_list.append("\t" + get_id(node_name) + " = \"" + node_name + "\"") + process_component(node_name, component_name) + + if len(component_define) > 2: + component_define = "\n" + component_define filedata = TEMPLATE_FILE.read() filedata = filedata.replace("{COMPONENT_NAME}", component_name) filedata = filedata.replace("{COMPONENT_TYPE}", component_type) + filedata = filedata.replace("{COMPONENT_PATH}", component_require_path) + filedata = filedata.replace("{COMPONENT_DEFINE}", component_define) + filedata = filedata.replace("{COMPONENT_FUNCTIONS}", component_functions) + filedata = filedata.replace("{COMPONENT_ANNOTATIONS}", component_annotations) filedata = filedata.replace("{SCHEME_LIST}", ",\n".join(scheme_list)) output_file = open(output_full_path, "w") diff --git a/example/examples/system/inner_templates/inner_templates.gui b/example/examples/system/inner_templates/inner_templates.gui index 9f3906bc..8bbd35eb 100644 --- a/example/examples/system/inner_templates/inner_templates.gui +++ b/example/examples/system/inner_templates/inner_templates.gui @@ -66,6 +66,7 @@ nodes { alpha: 1.0 template_node_child: false size_mode: SIZE_MODE_MANUAL + custom_type: 0 } nodes { position { @@ -106,6 +107,7 @@ nodes { alpha: 1.0 template: "/example/examples/system/inner_templates/inner_panel.gui" template_node_child: false + custom_type: 0 } nodes { position { @@ -161,6 +163,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_AUTO + custom_type: 0 } nodes { position { @@ -216,6 +219,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_MANUAL + custom_type: 0 } nodes { position { @@ -256,6 +260,7 @@ nodes { alpha: 1.0 template: "/example/examples/system/inner_templates/inner_button.gui" template_node_child: true + custom_type: 0 } nodes { position { @@ -311,6 +316,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_AUTO + custom_type: 0 } nodes { position { @@ -366,6 +372,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_MANUAL + custom_type: 0 } nodes { position { @@ -429,6 +436,7 @@ nodes { template_node_child: true text_leading: 1.0 text_tracking: 0.0 + custom_type: 0 } nodes { position { @@ -469,6 +477,7 @@ nodes { alpha: 1.0 template: "/example/examples/system/inner_templates/inner_button.gui" template_node_child: true + custom_type: 0 } nodes { position { @@ -524,6 +533,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_AUTO + custom_type: 0 } nodes { position { @@ -579,6 +589,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_MANUAL + custom_type: 0 } nodes { position { @@ -642,6 +653,7 @@ nodes { template_node_child: true text_leading: 1.0 text_tracking: 0.0 + custom_type: 0 } nodes { position { @@ -682,6 +694,7 @@ nodes { alpha: 1.0 template: "/example/examples/system/inner_templates/inner_button.gui" template_node_child: true + custom_type: 0 } nodes { position { @@ -737,6 +750,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_AUTO + custom_type: 0 } nodes { position { @@ -792,6 +806,7 @@ nodes { alpha: 1.0 template_node_child: true size_mode: SIZE_MODE_MANUAL + custom_type: 0 } nodes { position { @@ -855,6 +870,7 @@ nodes { template_node_child: true text_leading: 1.0 text_tracking: 0.0 + custom_type: 0 } nodes { position { @@ -918,6 +934,7 @@ nodes { template_node_child: false text_leading: 1.0 text_tracking: 0.0 + custom_type: 0 } nodes { position { @@ -981,6 +998,7 @@ nodes { template_node_child: false text_leading: 1.0 text_tracking: 0.0 + custom_type: 0 } layers { name: "image" diff --git a/example/examples/system/inner_templates/inner_templates.gui_script b/example/examples/system/inner_templates/inner_templates.gui_script index d932b6fa..b8c65327 100644 --- a/example/examples/system/inner_templates/inner_templates.gui_script +++ b/example/examples/system/inner_templates/inner_templates.gui_script @@ -2,7 +2,6 @@ local druid = require("druid.druid") local InnerPanel = require("example.examples.system.inner_templates.inner_panel") - function init(self) self.druid = druid.new(self) local root = gui.get_node("inner_panel/root")