From 760946765c8f79d1cbdf503e29a496b983f12663 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 10 Mar 2022 21:45:23 +0200 Subject: [PATCH] Update generate component script --- editor_scripts/component.lua_template | 7 ++++--- editor_scripts/create_druid_component.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/editor_scripts/component.lua_template b/editor_scripts/component.lua_template index eb70a9d..d50db11 100644 --- a/editor_scripts/component.lua_template +++ b/editor_scripts/component.lua_template @@ -8,18 +8,19 @@ local component = require("druid.component") ---@class {COMPONENT_TYPE}: druid.base_component{COMPONENT_ANNOTATIONS} +---@field druid druid_instance local {COMPONENT_NAME} = component.create("{COMPONENT_TYPE}") local SCHEME = { {SCHEME_LIST} } -{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(){COMPONENT_DEFINE} end @@ -27,5 +28,5 @@ end function {COMPONENT_NAME}:on_remove() end - +{COMPONENT_FUNCTIONS} return {COMPONENT_NAME} diff --git a/editor_scripts/create_druid_component.py b/editor_scripts/create_druid_component.py index 0b7c155..054bfa3 100644 --- a/editor_scripts/create_druid_component.py +++ b/editor_scripts/create_druid_component.py @@ -6,7 +6,7 @@ import sys import deftree current_filepath = os.path.abspath(os.path.dirname(__file__)) -TEMPLATE_FILE = open(current_filepath + "/component.lua_template", "r") +TEMPLATE_PATH = current_filepath + "/component.lua_template" component_annotations = "" component_functions = "" @@ -26,6 +26,10 @@ def process_component(node_name, component_name): global component_functions global component_define + if node_name == "root": + component_annotations += "\n---@field root node" + component_define += "\n\tself.root = self:get_node(SCHEME.ROOT)" + 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) @@ -112,7 +116,10 @@ def main(): if len(component_define) > 2: component_define = "\n" + component_define - filedata = TEMPLATE_FILE.read() + template_file = open(TEMPLATE_PATH, "r") + filedata = template_file.read() + template_file.close() + filedata = filedata.replace("{COMPONENT_NAME}", component_name) filedata = filedata.replace("{COMPONENT_TYPE}", component_type) filedata = filedata.replace("{COMPONENT_PATH}", component_require_path) @@ -124,7 +131,9 @@ def main(): output_file = open(output_full_path, "w") output_file.write(filedata) output_file.close() + print("Success: The file is created") print("File:", output_full_path) + main()