diff --git a/druid/styles/default/templates/druid_input.gui b/druid/styles/default/templates/druid_input.gui index 19417dd..71dbf6b 100644 --- a/druid/styles/default/templates/druid_input.gui +++ b/druid/styles/default/templates/druid_input.gui @@ -71,7 +71,7 @@ nodes { nodes { position { x: 0.0 - y: 2.0 + y: 3.0 z: 0.0 w: 1.0 } diff --git a/editor_scripts/component_template.lua b/editor_scripts/component_template.lua new file mode 100644 index 0000000..b67cf3b --- /dev/null +++ b/editor_scripts/component_template.lua @@ -0,0 +1,29 @@ +--- For component interest functions +--- see https://github.com/Insality/druid/blob/develop/docs_md/02-creating_custom_components.md + +local component = require("druid.component") + +---@class {COMPONENT_TYPE} : druid.base_component +local {COMPONENT_NAME} = component.create("{COMPONENT_TYPE}") + +local SCHEME = { +{SCHEME_LIST} +} + + +--- Create this component via druid:new({COMPONENT_NAME}, template, nodes) +---@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() +end + + +function {COMPONENT_NAME}:on_remove() +end + + +return {COMPONENT_NAME} diff --git a/editor_scripts/create_druid_component.py b/editor_scripts/create_druid_component.py new file mode 100644 index 0000000..7398da9 --- /dev/null +++ b/editor_scripts/create_druid_component.py @@ -0,0 +1,50 @@ +# @license MIT, Insality 2021 +# @source https://github.com/Insality/druid + +import os +import sys +import deftree + +current_filepath = os.path.abspath(os.path.dirname(__file__)) +TEMPLATE_FILE = open(current_filepath + "/component_template.lua", "r") + +def to_camel_case(snake_str): + components = snake_str.split('_') + return ''.join(x.title() for x in components[0:]) + +def main(): + filename = sys.argv[1] + print("Create Druid component from gui file", filename) + tree = deftree.parse(filename) + root = tree.get_root() + + output_directory = os.path.dirname(filename) + output_filename = os.path.splitext(os.path.basename(filename))[0] + + output_full_path = os.path.join(output_directory, output_filename + ".lua") + is_already_exists = os.path.exists(output_full_path) + if is_already_exists: + print("Error: The file is already exists") + print("File:", output_full_path) + return + + 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 + "\"") + + filedata = TEMPLATE_FILE.read() + filedata = filedata.replace("{COMPONENT_NAME}", component_name) + filedata = filedata.replace("{COMPONENT_TYPE}", component_type) + filedata = filedata.replace("{SCHEME_LIST}", ",\n".join(scheme_list)) + + 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() diff --git a/editor_scripts/create_druid_component.sh b/editor_scripts/create_druid_component.sh new file mode 100644 index 0000000..a5e1162 --- /dev/null +++ b/editor_scripts/create_druid_component.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# @license MIT, Insality 2022 +# @source https://github.com/Insality/druid + +echo "Run bash for $1" +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +is_defree_installed=$(pip3 list --disable-pip-version-check | grep -E "deftree") +if [ -z "$is_defree_installed" ]; then + echo "The python deftree is not installed. Installing..." + pip3 install deftree +fi + +python3 $DIR/create_druid_component.py $@ diff --git a/editor_scripts/gui_scheme.editor_script b/editor_scripts/gui_scheme.editor_script index fbcc025..3619d60 100644 --- a/editor_scripts/gui_scheme.editor_script +++ b/editor_scripts/gui_scheme.editor_script @@ -68,6 +68,36 @@ function M.get_commands() } } end + }, + + { + label = "Create Druid Component", + + locations = {"Edit"}, + + query = { + selection = {type = "resource", cardinality = "one"} + }, + + active = function(opts) + local path = editor.get(opts.selection, "path") + return ends_with(path, ".gui") + end, + + run = function(opts) + local file = opts.selection + print("Run script for", editor.get(file, "path")) + return { + { + action = "shell", + command = { + "bash", + "./editor_scripts/create_druid_component.sh", + "." .. editor.get(file, "path") + } + } + } + end } } end diff --git a/editor_scripts/setup_layers.sh b/editor_scripts/setup_layers.sh index 36c76c1..8863eef 100755 --- a/editor_scripts/setup_layers.sh +++ b/editor_scripts/setup_layers.sh @@ -5,4 +5,10 @@ echo "Run bash for $1" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -/usr/local/bin/python3.7 $DIR/setup_layers.py $@ \ No newline at end of file +is_defree_installed=$(pip3 list --disable-pip-version-check | grep -E "deftree") +if [ -z "$is_defree_installed" ]; then + echo "The python deftree is not installed. Installing..." + pip3 install deftree +fi + +python3 $DIR/setup_layers.py $@ diff --git a/example/example.gui_script b/example/example.gui_script index 55827cc..1967e43 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -163,7 +163,6 @@ local function init_lobby(self) self.lobby_grid:add(get_title(self, "System")) self.lobby_grid:add(get_button_disabled(self, "Styles")) self.lobby_grid:add(get_button(self, "Whitelist / Blacklist", "system_whitelist_blacklist", "/system/whitelist_blacklist/whitelist_blacklist.gui_script")) - 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, "Nested Druids")) self.lobby_grid:add(get_button(self, "Message input", "system_message_input", "/system/message_input/message_input.gui_script"))