From 4e71fee9ba37f32b8d3047819d7f4aa3c4e82039 Mon Sep 17 00:00:00 2001 From: Insality Date: Mon, 21 Apr 2025 19:53:44 +0300 Subject: [PATCH] Add Bind layout and hotkey node bind --- druid/base/scroll.lua | 27 +++++++++++++++++++++++++++ druid/base/static_grid.lua | 7 +++++++ druid/extended/hotkey.lua | 17 ++++++++++++++++- druid/extended/layout.lua | 12 ++++++++++-- wiki/changelog.md | 3 +-- 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 8f623fe..d8c8070 100755 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -445,6 +445,33 @@ function M:bind_grid(grid) end +---Bind the layout component to recalculate +-- scroll size on layout changes +---@param layout druid.layout|nil Druid layout component +---@return druid.scroll self Current scroll instance +function M:bind_layout(layout) + if self._layout_on_change then + self._layout_on_change:unsubscribe(self._layout_on_change_callback) + + self._layout_on_change = nil + self._layout_on_change_callback = nil + end + + if not layout then + return self + end + + self._layout_on_change = layout.on_size_changed + self._layout_on_change_callback = function(size) + self:set_size(size) + end + self._layout_on_change:subscribe(self._layout_on_change_callback) + self:set_size(layout:get_size()) + + return self +end + + ---Strict drag scroll area. Useful for -- restrict events outside stencil node ---@param node node|string Gui node diff --git a/druid/base/static_grid.lua b/druid/base/static_grid.lua index 553b849..968956b 100644 --- a/druid/base/static_grid.lua +++ b/druid/base/static_grid.lua @@ -243,6 +243,13 @@ function M:remove(index, shift_policy, is_instant) end +---Return items count in grid +---@return number count The items count in grid +function M:get_items_count() + return #self.nodes +end + + ---Return grid content size ---@return vector3 size The grid content size function M:get_size() diff --git a/druid/extended/hotkey.lua b/druid/extended/hotkey.lua index 0671e1c..58070cd 100644 --- a/druid/extended/hotkey.lua +++ b/druid/extended/hotkey.lua @@ -23,6 +23,7 @@ local component = require("druid.component") ---@field style druid.hotkey.style The style of the hotkey component ---@field private _hotkeys table The list of hotkeys ---@field private _modificators table The list of modificators +---@field private _node node|nil The node to bind the hotkey to local M = component.create("hotkey") @@ -35,7 +36,7 @@ function M:init(keys, callback, callback_argument) self._hotkeys = {} self._modificators = {} - + self._node = nil self.on_hotkey_pressed = event.create() self.on_hotkey_released = event.create(callback) @@ -134,6 +135,10 @@ function M:on_input(action_id, action) return false end + if self._node and not gui.is_enabled(self._node, true) then + return false + end + if self._modificators[action_id] ~= nil and action.pressed then self._modificators[action_id] = true end @@ -192,4 +197,14 @@ function M:set_repeat(is_enabled_repeated) end +---If node is provided, the hotkey can be disabled, if the node is disabled +---@param node node|nil The node to bind the hotkey to. Nil to unbind the node +---@return druid.hotkey self Current instance +function M:bind_node(node) + self._node = node + + return self +end + + return M diff --git a/druid/extended/layout.lua b/druid/extended/layout.lua index 34ecdb8..5a8c40b 100644 --- a/druid/extended/layout.lua +++ b/druid/extended/layout.lua @@ -56,9 +56,11 @@ function M:init(node_or_node_id, layout_type) self.size = gui.get_size(self.node) self.padding = gui.get_slice9(self.node) - -- Grab default margins from slice9 z/w values + -- Margin X is a Slice9 R Value + -- Margin Y is a Slice9 B Value self.margin = { x = self.padding.z, y = self.padding.w } - -- Use symmetrical padding from x/z + -- Padding X is a Slice9 L Value + -- Padding Y is a Slice9 T Value self.padding.z = self.padding.x self.padding.w = self.padding.y @@ -86,6 +88,12 @@ function M:get_entities() end +---@return number count The count of entities in layout +function M:get_entities_count() + return #self.entities +end + + ---@param node node The node to set the index of ---@param index number The index to set the node to ---@return druid.layout self for chaining diff --git a/wiki/changelog.md b/wiki/changelog.md index b61102f..6abc3d4 100644 --- a/wiki/changelog.md +++ b/wiki/changelog.md @@ -589,13 +589,12 @@ By the way, the PR number of this release is #300. Sounds veeery huge and long j - **Widgets** are here! This is the evolution of custom components, but with no boilerplate code and far more convenient usage. All Druid examples have been migrated to use widgets. Widgets are now a default way to create a new custom components (basically any of your GUI element on the screen). -- **Experimental features** like shader pipeline in GUI and widget usage in GO scripts. Curious Defolders can find examples of these features and try them out. - - **No more calling `druid.register()`!** All Druid components are now available by default with `self.druid:new_*` functions, making getting started simpler than ever. - **Druid UI Kit** brings fonts, atlas, and ready-to-use GUI templates right out of the box - a long-requested feature that lets you use Druid UI elements instantly in your projects. I think now it's a possible to create a external dependencies with a set of GUI templates and Druid's widgets to make a ready to use UI kit for projects! The flow to init widgets always now from two steps: - Add GUI template to your GUI scene - Call `self.widget = self.druid:new_widget(widget_file, "template_id")` to init widget + - Call `self.widget = self.druid:new_widget(widget_file, "template_id", "tempalate_id/root")` to clone root node from template and init widget from it - **Completely reworked documentation** with full code annotations. Start with the [Quick API Reference](/api/quick_api_reference.md) to get familiar with **Druid**. Any documentation are generated from the code annotations, so in case to update documentation, you need to update annotations in the code.