Skip to content

Commit 730559a

Browse files
committed
Fix #333 Add settings for the Druid editor scripts
1 parent c2e0256 commit 730559a

File tree

3 files changed

+186
-28
lines changed

3 files changed

+186
-28
lines changed

druid/editor_scripts/druid.editor_script

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,32 @@ function M.get_prefs_schema()
2020
default = DEFAULT_GUI_SCRIPT_TEMPLATE_PATH,
2121
scope = editor.prefs.SCOPE.PROJECT
2222
}),
23+
["druid.command_assign_layers_enabled"] = editor.prefs.schema.boolean({
24+
default = true,
25+
scope = editor.prefs.SCOPE.PROJECT
26+
}),
27+
["druid.command_create_widget_enabled"] = editor.prefs.schema.boolean({
28+
default = true,
29+
scope = editor.prefs.SCOPE.PROJECT
30+
}),
31+
["druid.command_create_gui_script_enabled"] = editor.prefs.schema.boolean({
32+
default = true,
33+
scope = editor.prefs.SCOPE.PROJECT
34+
}),
35+
["druid.command_create_collection_enabled"] = editor.prefs.schema.boolean({
36+
default = true,
37+
scope = editor.prefs.SCOPE.PROJECT
38+
}),
2339
}
2440
end
2541

2642

2743
---Define the editor commands
2844
function M.get_commands()
29-
return {
30-
{
45+
local commands = {}
46+
47+
if editor.prefs.get("druid.command_assign_layers_enabled") then
48+
table.insert(commands, {
3149
label = "[Druid] Assign Layers",
3250
locations = { "Edit" },
3351
query = { selection = {type = "resource", cardinality = "one"} },
@@ -38,11 +56,13 @@ function M.get_commands()
3856
run = function(opts)
3957
return assign_layers.assign_layers(opts.selection)
4058
end
41-
},
59+
})
60+
end
4261

43-
{
62+
if editor.prefs.get("druid.command_create_widget_enabled") then
63+
table.insert(commands, {
4464
label = "[Druid] Create Druid Widget",
45-
locations = { "Edit", "Assets" },
65+
locations = { "Assets" },
4666
query = { selection = {type = "resource", cardinality = "one"} },
4767
active = function(opts)
4868
local path = editor.get(opts.selection, "path")
@@ -51,11 +71,13 @@ function M.get_commands()
5171
run = function(opts)
5272
return create_druid_widget.create_druid_widget(opts.selection)
5373
end
54-
},
74+
})
75+
end
5576

56-
{
77+
if editor.prefs.get("druid.command_create_gui_script_enabled") then
78+
table.insert(commands, {
5779
label = "[Druid] Create Druid GUI Script",
58-
locations = { "Edit", "Assets" },
80+
locations = { "Assets" },
5981
query = { selection = {type = "resource", cardinality = "one"} },
6082
active = function(opts)
6183
local path = editor.get(opts.selection, "path")
@@ -64,11 +86,13 @@ function M.get_commands()
6486
run = function(opts)
6587
return create_druid_gui_script.create_druid_gui_script(opts.selection)
6688
end
67-
},
89+
})
90+
end
6891

69-
{
92+
if editor.prefs.get("druid.command_create_collection_enabled") then
93+
table.insert(commands, {
7094
label = "[Druid] Create Druid Collection",
71-
locations = { "Edit", "Assets" },
95+
locations = { "Assets" },
7296
query = { selection = {type = "resource", cardinality = "one"} },
7397
active = function(opts)
7498
local path = editor.get(opts.selection, "path")
@@ -77,16 +101,18 @@ function M.get_commands()
77101
run = function(opts)
78102
return create_druid_collection.create_druid_collection(opts.selection)
79103
end
80-
},
104+
})
105+
end
81106

82-
{
83-
label = "[Druid] Settings",
84-
locations = { "Edit" },
85-
run = function()
86-
return druid_settings.open_settings()
87-
end
88-
}
89-
}
107+
table.insert(commands, {
108+
label = "[Druid] Settings",
109+
locations = { "Edit" },
110+
run = function()
111+
return druid_settings.open_settings()
112+
end
113+
})
114+
115+
return commands
90116
end
91117

92118

druid/editor_scripts/druid_settings.lua

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local editor_scripts_internal = require("druid.editor_scripts.editor_scripts_internal")
2+
13
local M = {}
24

35

@@ -7,10 +9,8 @@ function M.open_settings()
79
local dialog_component = editor.ui.component(function(props)
810
local template_path, set_template_path = editor.ui.use_state(editor.prefs.get("druid.widget_template_path"))
911
local path_valid = editor.ui.use_memo(function(path)
10-
-- Use resource_exists to check if the resource exists
1112
local exists = false
1213
pcall(function()
13-
-- If we can get the text property, the resource exists
1414
local content = editor.get(path, "text")
1515
exists = content ~= nil
1616
end)
@@ -27,6 +27,11 @@ function M.open_settings()
2727
return exists
2828
end, gui_script_template_path)
2929

30+
local editor_script_set_layers_enabled, set_editor_script_set_layers_enabled = editor.ui.use_state(editor.prefs.get("druid.command_assign_layers_enabled"))
31+
local editor_script_create_widget_enabled, set_editor_script_create_widget_enabled = editor.ui.use_state(editor.prefs.get("druid.command_create_widget_enabled"))
32+
local editor_script_create_gui_script_enabled, set_editor_script_create_gui_script_enabled = editor.ui.use_state(editor.prefs.get("druid.command_create_gui_script_enabled"))
33+
local editor_script_create_collection_enabled, set_editor_script_create_collection_enabled = editor.ui.use_state(editor.prefs.get("druid.command_create_collection_enabled"))
34+
3035
return editor.ui.dialog({
3136
title = "Druid Settings",
3237
content = editor.ui.vertical({
@@ -61,7 +66,49 @@ function M.open_settings()
6166
color = editor.ui.COLOR.WARNING
6267
}) or nil,
6368

64-
-- Links section title
69+
editor.ui.label({
70+
text = "Editor Commands:",
71+
color = editor.ui.COLOR.TEXT
72+
}),
73+
74+
editor.ui.grid({
75+
columns = {{}, {grow = true}},
76+
children = {
77+
editor.bundle.grid_row(
78+
nil,
79+
editor.ui.check_box({
80+
value = editor_script_set_layers_enabled,
81+
on_value_changed = set_editor_script_set_layers_enabled,
82+
text = "Assign Layers"
83+
})
84+
),
85+
editor.bundle.grid_row(
86+
nil,
87+
editor.ui.check_box({
88+
value = editor_script_create_widget_enabled,
89+
on_value_changed = set_editor_script_create_widget_enabled,
90+
text = "Create Druid Widget"
91+
})
92+
),
93+
editor.bundle.grid_row(
94+
nil,
95+
editor.ui.check_box({
96+
value = editor_script_create_gui_script_enabled,
97+
on_value_changed = set_editor_script_create_gui_script_enabled,
98+
text = "Create Druid GUI Script"
99+
})
100+
),
101+
editor.bundle.grid_row(
102+
nil,
103+
editor.ui.check_box({
104+
value = editor_script_create_collection_enabled,
105+
on_value_changed = set_editor_script_create_collection_enabled,
106+
text = "Create Druid Collection"
107+
})
108+
)
109+
}
110+
}),
111+
65112
editor.ui.label({
66113
text = "Documentation:",
67114
color = editor.ui.COLOR.TEXT
@@ -124,17 +171,47 @@ function M.open_settings()
124171
editor.ui.dialog_button({
125172
text = "Save",
126173
default = true,
127-
result = { template_path = template_path }
174+
result = {
175+
template_path = template_path,
176+
gui_script_template_path = gui_script_template_path,
177+
editor_script_set_layers_enabled = editor_script_set_layers_enabled,
178+
editor_script_create_widget_enabled = editor_script_create_widget_enabled,
179+
editor_script_create_gui_script_enabled = editor_script_create_gui_script_enabled,
180+
editor_script_create_collection_enabled = editor_script_create_collection_enabled,
181+
}
128182
})
129183
}
130184
})
131185
end)
132186

133187
local result = editor.ui.show_dialog(dialog_component({}))
134-
if result and result.template_path then
135-
-- Update the preferences
136-
editor.prefs.set("druid.widget_template_path", result.template_path)
137-
print("Widget template path updated to:", result.template_path)
188+
if result then
189+
if result.template_path then
190+
editor.prefs.set("druid.widget_template_path", result.template_path)
191+
print("Widget template path updated to:", result.template_path)
192+
end
193+
if result.gui_script_template_path then
194+
editor.prefs.set("druid.gui_script_template_path", result.gui_script_template_path)
195+
print("GUI script template path updated to:", result.gui_script_template_path)
196+
end
197+
if result.editor_script_set_layers_enabled ~= nil then
198+
editor.prefs.set("druid.command_assign_layers_enabled", result.editor_script_set_layers_enabled)
199+
print("Assign layers enabled:", result.editor_script_set_layers_enabled)
200+
end
201+
if result.editor_script_create_widget_enabled ~= nil then
202+
editor.prefs.set("druid.command_create_widget_enabled", result.editor_script_create_widget_enabled)
203+
print("Create widget enabled:", result.editor_script_create_widget_enabled)
204+
end
205+
if result.editor_script_create_gui_script_enabled ~= nil then
206+
editor.prefs.set("druid.command_create_gui_script_enabled", result.editor_script_create_gui_script_enabled)
207+
print("Create GUI script enabled:", result.editor_script_create_gui_script_enabled)
208+
end
209+
if result.editor_script_create_collection_enabled ~= nil then
210+
editor.prefs.set("druid.command_create_collection_enabled", result.editor_script_create_collection_enabled)
211+
print("Create collection enabled:", result.editor_script_create_collection_enabled)
212+
end
213+
214+
editor_scripts_internal.call_editor_command("reload-extensions")
138215
end
139216

140217
return result
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
local M = {}
2+
3+
4+
---Read editor port from .internal/editor.port file
5+
---@return number|nil port Editor port number or nil if not found
6+
local function get_editor_port()
7+
local port_file_path = ".internal/editor.port"
8+
local port_file = io.open(port_file_path, "r")
9+
if not port_file then
10+
return nil
11+
end
12+
13+
local port_str = port_file:read("*a")
14+
port_file:close()
15+
16+
if not port_str then
17+
return nil
18+
end
19+
20+
-- Trim whitespace
21+
port_str = string.match(port_str, "%s*(.-)%s*$")
22+
local port = tonumber(port_str)
23+
return port
24+
end
25+
26+
27+
---Call editor HTTP API command
28+
---@param command string Command name (e.g., "fetch-libraries")
29+
---@return boolean success True if request was sent successfully
30+
function M.call_editor_command(command)
31+
if not command or command == "" then
32+
return false
33+
end
34+
35+
local port = get_editor_port()
36+
if not port then
37+
print("Asset Store: Could not read editor port from .internal/editor.port")
38+
return false
39+
end
40+
41+
local url = string.format("http://localhost:%d/command/%s", port, command)
42+
43+
-- Fire and forget - send POST request to editor API
44+
pcall(function()
45+
local response = http.request(url, {
46+
method = "POST",
47+
headers = { ["Accept"] = "application/json" }
48+
})
49+
end)
50+
51+
return true
52+
end
53+
54+
55+
return M

0 commit comments

Comments
 (0)