diff --git a/docs_md/creating_gui_layout.md b/docs_md/creating_gui_layout.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/docs_md/creating_gui_layout.md @@ -0,0 +1 @@ + diff --git a/docs_md/druid_editor_scripts.md b/docs_md/druid_editor_scripts.md new file mode 100644 index 0000000..c1e9674 --- /dev/null +++ b/docs_md/druid_editor_scripts.md @@ -0,0 +1,5 @@ +# Druid Editor Scripts + +## Auto Layers setup + +## Create Druid Widget diff --git a/docs_md/widgets.md b/docs_md/widgets.md index e69de29..ab5f91c 100644 --- a/docs_md/widgets.md +++ b/docs_md/widgets.md @@ -0,0 +1,10 @@ +# Widgets + +What are widgets +What are difference from custom components +Using Druid Widgets +Creating your own widgets (it's a new default way to use druid!) +Best practices +Make reusable widgets +Binding Widget to Game Objects +Testing you widgets separately diff --git a/druid/druid.lua b/druid/druid.lua index 63684af..3a59db7 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -5,11 +5,33 @@ local druid_instance = require("druid.system.druid_instance") local default_style = require("druid.styles.default.style") ----Entry point for Druid UI Framework. Create a new Druid instance and adjust the settings. +--- Use empty function to save a bit of memory +local EMPTY_FUNCTION = function(_, message, context) end + +---@type druid.logger +local empty_logger = { + trace = EMPTY_FUNCTION, + debug = EMPTY_FUNCTION, + info = EMPTY_FUNCTION, + warn = EMPTY_FUNCTION, + error = EMPTY_FUNCTION, +} + +---@type druid.logger +local logger = { + trace = EMPTY_FUNCTION, + debug = EMPTY_FUNCTION, + info = EMPTY_FUNCTION, + warn = EMPTY_FUNCTION, + error = EMPTY_FUNCTION, +} + + +---Entry point for Druid UI Framework. +---Create a new Druid instance and adjust the Druid settings here. ---@class druid local M = {} - ---Create a new Druid instance for creating GUI components. ---@param context table The Druid context. Usually, this is the self of the gui_script. It is passed into all Druid callbacks. ---@param style table|nil The Druid style table to override style parameters for this Druid instance. @@ -26,6 +48,13 @@ function M.new(context, style) end +---Set the logger for the Druid instance. +---@param logger_instance druid.logger The logger +function M:set_logger(logger_instance) + self.logger = logger_instance or empty_logger +end + + ---Register a new external Druid component. ---Register component just makes the druid:new_{name} function. ---For example, if you register a component called "my_component", you can create it using druid:new_my_component(...). @@ -33,6 +62,7 @@ end ---The default way to create component is `druid_instance:new(component_class, ...)`. ---@param name string Module name ---@param module table Lua table with component +---@deprecated function M.register(name, module) druid_instance["new_" .. name] = function(self, ...) return druid_instance.new(self, module, ...) @@ -132,4 +162,29 @@ function M.get_widget(object_url) end +---Release a binded widget to the current game object. +---@param object_url string|userdata|url|nil Root object, if nil current object will be used +---@return boolean is_released True if the widget was released, false if it was not found +function M.release_widget(object_url) + object_url = object_url or msg.url() + if object_url then + object_url = msg.url(object_url --[[@as string]]) + end + + local socket_widgets = WRAPPED_WIDGETS[object_url.socket] + if not socket_widgets then + return false + end + + socket_widgets[object_url.path] = nil + + -- Remove the socket if it's empty + if next(socket_widgets) == nil then + WRAPPED_WIDGETS[object_url.socket] = nil + end + + return true +end + + return M diff --git a/druid/system/druid_annotations.lua b/druid/system/druid_annotations.lua index 5ec36e0..889ef7b 100644 --- a/druid/system/druid_annotations.lua +++ b/druid/system/druid_annotations.lua @@ -1,6 +1,13 @@ ---@class druid.widget: druid.component ---@field druid druid.instance Ready to use druid instance +---@class druid.logger +---@field trace fun(message: string, context: any) +---@field debug fun(message: string, context: any) +---@field info fun(message: string, context: any) +---@field warn fun(message: string, context: any) +---@field error fun(message: string, context: any) + ---@class GUITextMetrics ---@field width number ---@field height number diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 86e6ccb..d534e06 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -238,6 +238,7 @@ function M:new(component, ...) if instance.init then instance:init(...) end + if instance.on_late_init or (not self.input_inited and instance.on_input) then schedule_late_init(self) end