mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
Solve #119 Add script for create custom component
This commit is contained in:
parent
7b8dfb1ef0
commit
b84b6c461d
@ -71,7 +71,7 @@ nodes {
|
||||
nodes {
|
||||
position {
|
||||
x: 0.0
|
||||
y: 2.0
|
||||
y: 3.0
|
||||
z: 0.0
|
||||
w: 1.0
|
||||
}
|
||||
|
29
editor_scripts/component_template.lua
Normal file
29
editor_scripts/component_template.lua
Normal file
@ -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<hash, node>
|
||||
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}
|
50
editor_scripts/create_druid_component.py
Normal file
50
editor_scripts/create_druid_component.py
Normal file
@ -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()
|
14
editor_scripts/create_druid_component.sh
Normal file
14
editor_scripts/create_druid_component.sh
Normal file
@ -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 $@
|
@ -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
|
||||
|
@ -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 $@
|
||||
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 $@
|
||||
|
@ -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"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user