mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
Update docs, add Editor script: Create Druid GUI Script, add simple using widgets example
This commit is contained in:
144
druid/editor_scripts/create_druid_gui_script.lua
Normal file
144
druid/editor_scripts/create_druid_gui_script.lua
Normal file
@@ -0,0 +1,144 @@
|
||||
local M = {}
|
||||
|
||||
local function to_camel_case(snake_str)
|
||||
local components = {}
|
||||
for component in snake_str:gmatch("[^_]+") do
|
||||
table.insert(components, component:sub(1, 1):upper() .. component:sub(2))
|
||||
end
|
||||
return table.concat(components, "")
|
||||
end
|
||||
|
||||
|
||||
function M.create_druid_gui_script(selection)
|
||||
local gui_filepath = editor.get(selection, "path")
|
||||
local filename = gui_filepath:match("([^/]+)%.gui$")
|
||||
print("Create Druid GUI Script for", gui_filepath)
|
||||
|
||||
local absolute_project_path = editor.external_file_attributes(".").path
|
||||
local widget_resource_path = gui_filepath:gsub("%.gui$", ".gui_script")
|
||||
local new_widget_absolute_path = absolute_project_path .. widget_resource_path
|
||||
|
||||
local widget_name = to_camel_case(filename)
|
||||
local widget_type = filename
|
||||
|
||||
-- Check if file already exists
|
||||
local f = io.open(new_widget_absolute_path, "r")
|
||||
if f then
|
||||
f:close()
|
||||
print("Widget file already exists at " .. new_widget_absolute_path)
|
||||
print("Creation aborted to prevent overwriting")
|
||||
return
|
||||
end
|
||||
|
||||
-- Get template path from preferences
|
||||
local template_path = editor.prefs.get("druid.gui_script_template_path")
|
||||
|
||||
-- Get template content using the path from preferences
|
||||
local template_content = editor.get(template_path, "text")
|
||||
if not template_content then
|
||||
print("Error: Could not load template from", template_path)
|
||||
print("Check the template path in [Druid] Settings")
|
||||
return
|
||||
end
|
||||
|
||||
-- Replace template variables
|
||||
template_content = template_content:gsub("{COMPONENT_NAME}", widget_name)
|
||||
template_content = template_content:gsub("{COMPONENT_TYPE}", widget_type)
|
||||
|
||||
-- Write file
|
||||
local file, err = io.open(new_widget_absolute_path, "w")
|
||||
if not file then
|
||||
print("Error creating widget file:", err)
|
||||
return
|
||||
end
|
||||
file:write(template_content)
|
||||
file:close()
|
||||
|
||||
print("Widget created at " .. widget_resource_path)
|
||||
|
||||
M.link_gui_script(selection, widget_resource_path)
|
||||
end
|
||||
|
||||
|
||||
---Links a GUI script to a GUI file by updating the script property
|
||||
---@param selection string The GUI resource to modify
|
||||
---@param widget_resource_path string The path to the GUI script to link
|
||||
function M.link_gui_script(selection, widget_resource_path)
|
||||
local defold_parser = require("druid.editor_scripts.defold_parser.defold_parser")
|
||||
local system = require("druid.editor_scripts.defold_parser.system.system")
|
||||
|
||||
local gui_filepath = editor.get(selection, "path")
|
||||
print("Linking GUI script to", gui_filepath)
|
||||
|
||||
-- Get the absolute path to the file
|
||||
local absolute_project_path = editor.external_file_attributes(".").path
|
||||
if not absolute_project_path:match("[\\/]$") then
|
||||
absolute_project_path = absolute_project_path .. "/"
|
||||
end
|
||||
local clean_gui_path = gui_filepath
|
||||
if clean_gui_path:sub(1, 1) == "/" then
|
||||
clean_gui_path = clean_gui_path:sub(2)
|
||||
end
|
||||
local gui_absolute_path = absolute_project_path .. clean_gui_path
|
||||
|
||||
-- Create a backup
|
||||
local backup_path = gui_absolute_path .. ".backup"
|
||||
print("Creating backup at:", backup_path)
|
||||
|
||||
-- Read and write backup
|
||||
local content, err_read = system.read_file(gui_absolute_path)
|
||||
if not content then
|
||||
print("Error reading original file for backup:", err_read)
|
||||
return
|
||||
end
|
||||
|
||||
local success, err_write = system.write_file(backup_path, content)
|
||||
if not success then
|
||||
print("Error creating backup file:", err_write)
|
||||
return
|
||||
end
|
||||
|
||||
-- Parse the GUI file
|
||||
print("Parsing GUI file...")
|
||||
local gui_data = defold_parser.load_from_file(gui_absolute_path)
|
||||
if not gui_data then
|
||||
print("Error: Failed to parse GUI file")
|
||||
return
|
||||
end
|
||||
|
||||
-- Update the script property
|
||||
print("Setting script property to:", widget_resource_path)
|
||||
gui_data.script = widget_resource_path
|
||||
|
||||
-- Write the updated GUI file
|
||||
print("Writing updated GUI file...")
|
||||
local save_success = defold_parser.save_to_file(gui_absolute_path, gui_data)
|
||||
|
||||
if not save_success then
|
||||
print("Error: Failed to save GUI file")
|
||||
print("Attempting to restore from backup...")
|
||||
|
||||
-- Restore from backup on failure
|
||||
local backup_content, backup_err_read = system.read_file(backup_path)
|
||||
if not backup_content then
|
||||
print("Error reading backup file:", backup_err_read)
|
||||
return
|
||||
end
|
||||
|
||||
local restore_success, restore_err_write = system.write_file(gui_absolute_path, backup_content)
|
||||
if not restore_success then
|
||||
print("Critical: Failed to restore from backup:", restore_err_write)
|
||||
return
|
||||
end
|
||||
|
||||
print("Restored successfully from backup")
|
||||
return
|
||||
end
|
||||
|
||||
-- Remove backup on success
|
||||
os.remove(backup_path)
|
||||
print("Successfully linked GUI script to:", gui_filepath)
|
||||
end
|
||||
|
||||
|
||||
return M
|
@@ -1,11 +1,12 @@
|
||||
local assign_layers = require("druid.editor_scripts.assign_layers")
|
||||
local create_druid_widget = require("druid.editor_scripts.create_druid_widget")
|
||||
local create_druid_gui_script = require("druid.editor_scripts.create_druid_gui_script")
|
||||
local druid_settings = require("druid.editor_scripts.druid_settings")
|
||||
|
||||
local M = {}
|
||||
|
||||
local DEFAULT_WIDGET_TEMPLATE_PATH = "/druid/templates/widget_full.lua.template"
|
||||
|
||||
local DEFAULT_GUI_SCRIPT_TEMPLATE_PATH = "/druid/templates/druid.gui_script.template"
|
||||
|
||||
---Define preferences schema
|
||||
function M.get_prefs_schema()
|
||||
@@ -13,6 +14,10 @@ function M.get_prefs_schema()
|
||||
["druid.widget_template_path"] = editor.prefs.schema.string({
|
||||
default = DEFAULT_WIDGET_TEMPLATE_PATH,
|
||||
scope = editor.prefs.SCOPE.PROJECT
|
||||
}),
|
||||
["druid.gui_script_template_path"] = editor.prefs.schema.string({
|
||||
default = DEFAULT_GUI_SCRIPT_TEMPLATE_PATH,
|
||||
scope = editor.prefs.SCOPE.PROJECT
|
||||
})
|
||||
}
|
||||
end
|
||||
@@ -47,6 +52,19 @@ function M.get_commands()
|
||||
end
|
||||
},
|
||||
|
||||
{
|
||||
label = "[Druid] Create Druid GUI Script",
|
||||
locations = { "Edit", "Assets" },
|
||||
query = { selection = {type = "resource", cardinality = "one"} },
|
||||
active = function(opts)
|
||||
local path = editor.get(opts.selection, "path")
|
||||
return path:match("%.gui$") ~= nil
|
||||
end,
|
||||
run = function(opts)
|
||||
return create_druid_gui_script.create_druid_gui_script(opts.selection)
|
||||
end
|
||||
},
|
||||
|
||||
{
|
||||
label = "[Druid] Settings",
|
||||
locations = { "Edit" },
|
||||
|
@@ -6,8 +6,6 @@ function M.open_settings()
|
||||
|
||||
local dialog_component = editor.ui.component(function(props)
|
||||
local template_path, set_template_path = editor.ui.use_state(editor.prefs.get("druid.widget_template_path"))
|
||||
|
||||
-- Check if the template path is valid
|
||||
local path_valid = editor.ui.use_memo(function(path)
|
||||
-- Use resource_exists to check if the resource exists
|
||||
local exists = false
|
||||
@@ -19,6 +17,16 @@ function M.open_settings()
|
||||
return exists
|
||||
end, template_path)
|
||||
|
||||
local gui_script_template_path, set_gui_script_template_path = editor.ui.use_state(editor.prefs.get("druid.gui_script_template_path"))
|
||||
local gui_script_template_path_valid = editor.ui.use_memo(function(path)
|
||||
local exists = false
|
||||
pcall(function()
|
||||
local content = editor.get(path, "text")
|
||||
exists = content ~= nil
|
||||
end)
|
||||
return exists
|
||||
end, gui_script_template_path)
|
||||
|
||||
return editor.ui.dialog({
|
||||
title = "Druid Settings",
|
||||
content = editor.ui.vertical({
|
||||
@@ -39,6 +47,20 @@ function M.open_settings()
|
||||
color = editor.ui.COLOR.WARNING
|
||||
}) or nil,
|
||||
|
||||
editor.ui.label({
|
||||
text = "GUI Script Template Path:"
|
||||
}),
|
||||
editor.ui.resource_field({
|
||||
value = gui_script_template_path,
|
||||
on_value_changed = set_gui_script_template_path,
|
||||
extensions = {"lua", "template"},
|
||||
padding = editor.ui.PADDING.SMALL
|
||||
}),
|
||||
not gui_script_template_path_valid and editor.ui.label({
|
||||
text = "Warning: Path not found!",
|
||||
color = editor.ui.COLOR.WARNING
|
||||
}) or nil,
|
||||
|
||||
-- Links section title
|
||||
editor.ui.label({
|
||||
text = "Documentation:",
|
||||
|
@@ -3,8 +3,7 @@ material: "/builtins/fonts/font-df.material"
|
||||
size: 40
|
||||
outline_alpha: 1.0
|
||||
outline_width: 2.0
|
||||
shadow_alpha: 1.0
|
||||
shadow_blur: 2
|
||||
shadow_blur: 0
|
||||
output_format: TYPE_DISTANCE_FIELD
|
||||
render_mode: MODE_MULTI_LAYER
|
||||
characters: "\302\241\302\253\302\273\302\277\303\200\303\202\303\206\303\207\303\210\303\211\303\212\303\213\303\216\303\217\303\224\303\231\303\233\303\234\303\237\303\240\303\241\303\242\303\243\303\244\303\246\303\247\303\250\303\251\303\252\303\253\303\255\303\256\303\257\303\261\303\262\303\263\303\264\303\265\303\266\303\271\303\273\303\274\303\277\305\222\305\223\305\270\320\201\320\220\320\221\320\222\320\223\320\224\320\225\320\226\320\227\320\230\320\231\320\232\320\233\320\234\320\235\320\236\320\237\320\240\320\241\320\242\320\243\320\244\320\245\320\246\320\247\320\250\320\251\320\252\320\253\320\254\320\255\320\256\320\257\320\260\320\261\320\262\320\263\320\264\320\265\320\266\320\267\320\270\320\271\320\272\320\273\320\274\320\275\320\276\320\277\321\200\321\201\321\202\321\203\321\204\321\205\321\206\321\207\321\210\321\211\321\212\321\213\321\214\321\215\321\216\321\217\321\221\342\200\224\343\200\201\343\200\202\343\200\214\343\200\215\357\274\201\357\274\214\357\274\237 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}"
|
||||
|
21
druid/templates/druid.gui_script.template
Normal file
21
druid/templates/druid.gui_script.template
Normal file
@@ -0,0 +1,21 @@
|
||||
local druid = require("druid.druid")
|
||||
|
||||
function init(self)
|
||||
self.druid = druid.new(self)
|
||||
end
|
||||
|
||||
function final(self)
|
||||
self.druid:final()
|
||||
end
|
||||
|
||||
function update(self, dt)
|
||||
self.druid:update(dt)
|
||||
end
|
||||
|
||||
function on_message(self, message_id, message, sender)
|
||||
self.druid:on_message(message_id, message, sender)
|
||||
end
|
||||
|
||||
function on_input(self, action_id, action)
|
||||
return self.druid:on_input(action_id, action)
|
||||
end
|
@@ -52,29 +52,8 @@ function M:init()
|
||||
self:set_hidden(not self._is_hidden)
|
||||
end):set_style(nil)
|
||||
|
||||
self.property_checkbox_prefab = self:get_node("property_checkbox/root")
|
||||
gui.set_enabled(self.property_checkbox_prefab, false)
|
||||
|
||||
self.property_slider_prefab = self:get_node("property_slider/root")
|
||||
gui.set_enabled(self.property_slider_prefab, false)
|
||||
|
||||
self.property_button_prefab = self:get_node("property_button/root")
|
||||
gui.set_enabled(self.property_button_prefab, false)
|
||||
|
||||
self.property_input_prefab = self:get_node("property_input/root")
|
||||
gui.set_enabled(self.property_input_prefab, false)
|
||||
|
||||
self.property_text_prefab = self:get_node("property_text/root")
|
||||
gui.set_enabled(self.property_text_prefab, false)
|
||||
|
||||
self.property_left_right_selector_prefab = self:get_node("property_left_right_selector/root")
|
||||
gui.set_enabled(self.property_left_right_selector_prefab, false)
|
||||
|
||||
self.property_vector3_prefab = self:get_node("property_vector3/root")
|
||||
gui.set_enabled(self.property_vector3_prefab, false)
|
||||
|
||||
-- We not using as a part of properties, since it handled in a way to be paginable
|
||||
self.paginator = self.druid:new_widget(property_left_right_selector, "property_left_right_selector", self.property_left_right_selector_prefab)
|
||||
self.paginator = self.druid:new_widget(property_left_right_selector, "property_left_right_selector", "root")
|
||||
self.paginator:set_text("Page")
|
||||
self.paginator:set_number_type(1, 1, true)
|
||||
self.paginator:set_value(self.current_page)
|
||||
@@ -85,6 +64,14 @@ function M:init()
|
||||
self.paginator.container:set_size(width)
|
||||
|
||||
gui.set_enabled(self.paginator.root, false)
|
||||
|
||||
gui.set_enabled(self:get_node("property_checkbox/root"), false)
|
||||
gui.set_enabled(self:get_node("property_slider/root"), false)
|
||||
gui.set_enabled(self:get_node("property_button/root"), false)
|
||||
gui.set_enabled(self:get_node("property_input/root"), false)
|
||||
gui.set_enabled(self:get_node("property_text/root"), false)
|
||||
gui.set_enabled(self:get_node("property_left_right_selector/root"), false)
|
||||
gui.set_enabled(self:get_node("property_vector3/root"), false)
|
||||
end
|
||||
|
||||
|
||||
@@ -179,53 +166,53 @@ end
|
||||
---@param on_create fun(checkbox: druid.widget.property_checkbox)|nil
|
||||
---@return druid.widget.properties_panel
|
||||
function M:add_checkbox(on_create)
|
||||
return self:add_inner_widget(property_checkbox, "property_checkbox", self.property_checkbox_prefab, on_create)
|
||||
return self:add_inner_widget(property_checkbox, "property_checkbox", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@param on_create fun(slider: druid.widget.property_slider)|nil
|
||||
---@return druid.widget.properties_panel
|
||||
function M:add_slider(on_create)
|
||||
return self:add_inner_widget(property_slider, "property_slider", self.property_slider_prefab, on_create)
|
||||
return self:add_inner_widget(property_slider, "property_slider", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@param on_create fun(button: druid.widget.property_button)|nil
|
||||
---@return druid.widget.properties_panel
|
||||
function M:add_button(on_create)
|
||||
return self:add_inner_widget(property_button, "property_button", self.property_button_prefab, on_create)
|
||||
return self:add_inner_widget(property_button, "property_button", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@param on_create fun(input: druid.widget.property_input)|nil
|
||||
---@return druid.widget.properties_panel
|
||||
function M:add_input(on_create)
|
||||
return self:add_inner_widget(property_input, "property_input", self.property_input_prefab, on_create)
|
||||
return self:add_inner_widget(property_input, "property_input", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@param on_create fun(text: druid.widget.property_text)|nil
|
||||
function M:add_text(on_create)
|
||||
return self:add_inner_widget(property_text, "property_text", self.property_text_prefab, on_create)
|
||||
return self:add_inner_widget(property_text, "property_text", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@param on_create fun(selector: druid.widget.property_left_right_selector)|nil
|
||||
function M:add_left_right_selector(on_create)
|
||||
return self:add_inner_widget(property_left_right_selector, "property_left_right_selector", self.property_left_right_selector_prefab, on_create)
|
||||
return self:add_inner_widget(property_left_right_selector, "property_left_right_selector", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@param on_create fun(vector3: druid.widget.property_vector3)|nil
|
||||
function M:add_vector3(on_create)
|
||||
return self:add_inner_widget(property_vector3, "property_vector3", self.property_vector3_prefab, on_create)
|
||||
return self:add_inner_widget(property_vector3, "property_vector3", "root", on_create)
|
||||
end
|
||||
|
||||
|
||||
---@generic T: druid.widget
|
||||
---@param widget_class T
|
||||
---@param template string|nil
|
||||
---@param nodes table<hash, node>|node|nil
|
||||
---@param nodes table<hash, node>|string|node|nil
|
||||
---@param on_create fun(widget: T)|nil
|
||||
---@return druid.widget.properties_panel
|
||||
function M:add_inner_widget(widget_class, template, nodes, on_create)
|
||||
|
Reference in New Issue
Block a user