Skip to content

Commit

Permalink
Solve #119 Add script for create custom component
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Mar 9, 2022
1 parent 7b8dfb1 commit b84b6c4
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 3 deletions.
2 changes: 1 addition & 1 deletion druid/styles/default/templates/druid_input.gui
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ nodes {
nodes {
position {
x: 0.0
y: 2.0
y: 3.0
z: 0.0
w: 1.0
}
Expand Down
29 changes: 29 additions & 0 deletions editor_scripts/component_template.lua
Original file line number Diff line number Diff line change
@@ -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 changes: 50 additions & 0 deletions editor_scripts/create_druid_component.py
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions editor_scripts/create_druid_component.sh
Original file line number Diff line number Diff line change
@@ -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 $@
30 changes: 30 additions & 0 deletions editor_scripts/gui_scheme.editor_script
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion editor_scripts/setup_layers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
1 change: 0 additions & 1 deletion example/example.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit b84b6c4

Please sign in to comment.