Add Bind layout and hotkey node bind

This commit is contained in:
Insality
2025-04-21 19:53:44 +03:00
parent c33dbd5942
commit 4e71fee9ba
5 changed files with 61 additions and 5 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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

View File

@@ -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