Solve #159 Add auto layouting for generate custom Druid component

This commit is contained in:
Insality 2022-03-10 19:50:16 +02:00
parent b84b6c461d
commit 505ff9a540
4 changed files with 108 additions and 13 deletions

View File

@ -1,24 +1,26 @@
--- For component interest functions --- For component interest functions
--- see https://github.com/Insality/druid/blob/develop/docs_md/02-creating_custom_components.md --- 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") 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 {COMPONENT_NAME} = component.create("{COMPONENT_TYPE}")
local SCHEME = { local SCHEME = {
{SCHEME_LIST} {SCHEME_LIST}
} }
{COMPONENT_FUNCTIONS}
--- Create this component via druid:new({COMPONENT_NAME}, template, nodes)
---@param template string ---@param template string
---@param nodes table<hash, node> ---@param nodes table<hash, node>
function {COMPONENT_NAME}:init(template, nodes) function {COMPONENT_NAME}:init(template, nodes)
self:set_template(template) self:set_template(template)
self:set_nodes(nodes) self:set_nodes(nodes)
self.root = self:get_node(SCHEME.ROOT) self.root = self:get_node(SCHEME.ROOT)
self.druid = self:get_druid() self.druid = self:get_druid(){COMPONENT_DEFINE}
end end

View File

@ -8,11 +8,77 @@ import deftree
current_filepath = os.path.abspath(os.path.dirname(__file__)) current_filepath = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_FILE = open(current_filepath + "/component_template.lua", "r") TEMPLATE_FILE = open(current_filepath + "/component_template.lua", "r")
component_annotations = ""
component_functions = ""
component_define = ""
def to_camel_case(snake_str): def to_camel_case(snake_str):
components = snake_str.split('_') components = snake_str.split('_')
return ''.join(x.title() for x in components[0:]) 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(): def main():
global component_annotations
global component_functions
global component_define
filename = sys.argv[1] filename = sys.argv[1]
print("Create Druid component from gui file", filename) print("Create Druid component from gui file", filename)
tree = deftree.parse(filename) tree = deftree.parse(filename)
@ -28,17 +94,27 @@ def main():
print("File:", output_full_path) print("File:", output_full_path)
return return
component_require_path = os.path.join(output_directory, output_filename).replace("/", ".").replace("..", "")
component_name = to_camel_case(output_filename) component_name = to_camel_case(output_filename)
component_type = output_filename component_type = output_filename
scheme_list = [] scheme_list = []
# Gather nodes from GUI scene # Gather nodes from GUI scene
for node in root.iter_elements("nodes"): for node in root.iter_elements("nodes"):
name = node.get_attribute("id").value node_name = node.get_attribute("id").value
scheme_list.append("\t" + name.upper() + " = \"" + name + "\"") 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 = TEMPLATE_FILE.read()
filedata = filedata.replace("{COMPONENT_NAME}", component_name) filedata = filedata.replace("{COMPONENT_NAME}", component_name)
filedata = filedata.replace("{COMPONENT_TYPE}", component_type) 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)) filedata = filedata.replace("{SCHEME_LIST}", ",\n".join(scheme_list))
output_file = open(output_full_path, "w") output_file = open(output_full_path, "w")

View File

@ -66,6 +66,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: false template_node_child: false
size_mode: SIZE_MODE_MANUAL size_mode: SIZE_MODE_MANUAL
custom_type: 0
} }
nodes { nodes {
position { position {
@ -106,6 +107,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template: "/example/examples/system/inner_templates/inner_panel.gui" template: "/example/examples/system/inner_templates/inner_panel.gui"
template_node_child: false template_node_child: false
custom_type: 0
} }
nodes { nodes {
position { position {
@ -161,6 +163,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_AUTO size_mode: SIZE_MODE_AUTO
custom_type: 0
} }
nodes { nodes {
position { position {
@ -216,6 +219,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_MANUAL size_mode: SIZE_MODE_MANUAL
custom_type: 0
} }
nodes { nodes {
position { position {
@ -256,6 +260,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template: "/example/examples/system/inner_templates/inner_button.gui" template: "/example/examples/system/inner_templates/inner_button.gui"
template_node_child: true template_node_child: true
custom_type: 0
} }
nodes { nodes {
position { position {
@ -311,6 +316,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_AUTO size_mode: SIZE_MODE_AUTO
custom_type: 0
} }
nodes { nodes {
position { position {
@ -366,6 +372,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_MANUAL size_mode: SIZE_MODE_MANUAL
custom_type: 0
} }
nodes { nodes {
position { position {
@ -429,6 +436,7 @@ nodes {
template_node_child: true template_node_child: true
text_leading: 1.0 text_leading: 1.0
text_tracking: 0.0 text_tracking: 0.0
custom_type: 0
} }
nodes { nodes {
position { position {
@ -469,6 +477,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template: "/example/examples/system/inner_templates/inner_button.gui" template: "/example/examples/system/inner_templates/inner_button.gui"
template_node_child: true template_node_child: true
custom_type: 0
} }
nodes { nodes {
position { position {
@ -524,6 +533,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_AUTO size_mode: SIZE_MODE_AUTO
custom_type: 0
} }
nodes { nodes {
position { position {
@ -579,6 +589,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_MANUAL size_mode: SIZE_MODE_MANUAL
custom_type: 0
} }
nodes { nodes {
position { position {
@ -642,6 +653,7 @@ nodes {
template_node_child: true template_node_child: true
text_leading: 1.0 text_leading: 1.0
text_tracking: 0.0 text_tracking: 0.0
custom_type: 0
} }
nodes { nodes {
position { position {
@ -682,6 +694,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template: "/example/examples/system/inner_templates/inner_button.gui" template: "/example/examples/system/inner_templates/inner_button.gui"
template_node_child: true template_node_child: true
custom_type: 0
} }
nodes { nodes {
position { position {
@ -737,6 +750,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_AUTO size_mode: SIZE_MODE_AUTO
custom_type: 0
} }
nodes { nodes {
position { position {
@ -792,6 +806,7 @@ nodes {
alpha: 1.0 alpha: 1.0
template_node_child: true template_node_child: true
size_mode: SIZE_MODE_MANUAL size_mode: SIZE_MODE_MANUAL
custom_type: 0
} }
nodes { nodes {
position { position {
@ -855,6 +870,7 @@ nodes {
template_node_child: true template_node_child: true
text_leading: 1.0 text_leading: 1.0
text_tracking: 0.0 text_tracking: 0.0
custom_type: 0
} }
nodes { nodes {
position { position {
@ -918,6 +934,7 @@ nodes {
template_node_child: false template_node_child: false
text_leading: 1.0 text_leading: 1.0
text_tracking: 0.0 text_tracking: 0.0
custom_type: 0
} }
nodes { nodes {
position { position {
@ -981,6 +998,7 @@ nodes {
template_node_child: false template_node_child: false
text_leading: 1.0 text_leading: 1.0
text_tracking: 0.0 text_tracking: 0.0
custom_type: 0
} }
layers { layers {
name: "image" name: "image"

View File

@ -2,7 +2,6 @@ local druid = require("druid.druid")
local InnerPanel = require("example.examples.system.inner_templates.inner_panel") local InnerPanel = require("example.examples.system.inner_templates.inner_panel")
function init(self) function init(self)
self.druid = druid.new(self) self.druid = druid.new(self)
local root = gui.get_node("inner_panel/root") local root = gui.get_node("inner_panel/root")