fix: support multiple bind_default_trigger calls
Previously trigger_action_name + trigger_widget_id were module-local scalars, so the second bind_default_trigger call overwrote the first (and reused the action name 'panel_toggle' so lib-core.input's binding was also clobbered). Switched to a triggers array; each binding gets a unique input action 'panel_toggle_<widget_id>' so multiple widgets can be toggled by their own keys independently.
This commit is contained in:
@@ -5,7 +5,7 @@ handles input dispatch (mouse click + wheel, edge-detected), renders a
|
|||||||
titled panel overlay, and supports a context-menu layer. Designed as the
|
titled panel overlay, and supports a context-menu layer. Designed as the
|
||||||
glue layer between game modules and the engine render/input surfaces.
|
glue layer between game modules and the engine render/input surfaces.
|
||||||
|
|
||||||
**Version:** 0.1.0
|
**Version:** 0.1.1
|
||||||
**Lib-ID:** lib-core.panel
|
**Lib-ID:** lib-core.panel
|
||||||
**Requires:** engine.render.*, engine.input.*, lib-core.input (lazy, for default-trigger)
|
**Requires:** engine.render.*, engine.input.*, lib-core.input (lazy, for default-trigger)
|
||||||
**Tags:** panel, overlay, ui, input, context-menu
|
**Tags:** panel, overlay, ui, input, context-menu
|
||||||
|
|||||||
24
init.lua
24
init.lua
@@ -43,8 +43,10 @@ local widgets = {} -- widget_id (string) → widget_def table
|
|||||||
local active = nil -- active widget_id or nil
|
local active = nil -- active widget_id or nil
|
||||||
local theme = {} -- merged DEFAULT_THEME + overrides
|
local theme = {} -- merged DEFAULT_THEME + overrides
|
||||||
local ctx_menu = nil -- context-menu state table or nil
|
local ctx_menu = nil -- context-menu state table or nil
|
||||||
local trigger_action_name = nil -- input action name for default trigger
|
-- Default-trigger bindings: array of {action_name, widget_id}. Multiple
|
||||||
local trigger_widget_id = nil -- which widget_id to toggle on trigger
|
-- bind_default_trigger calls append independent entries (each widget gets
|
||||||
|
-- its own input-action so they don't clobber each other in lib-core.input).
|
||||||
|
local triggers = {}
|
||||||
local last_mouse_left = false -- for edge-detection (was down last frame)
|
local last_mouse_left = false -- for edge-detection (was down last frame)
|
||||||
local last_mouse_right = false -- for edge-detection (was down last frame)
|
local last_mouse_right = false -- for edge-detection (was down last frame)
|
||||||
|
|
||||||
@@ -313,6 +315,8 @@ end
|
|||||||
--- Binds a keyboard key as the default toggle trigger for a widget.
|
--- Binds a keyboard key as the default toggle trigger for a widget.
|
||||||
--- key defaults to "tab" when nil. Lazy-requires lib-core.input to avoid
|
--- key defaults to "tab" when nil. Lazy-requires lib-core.input to avoid
|
||||||
--- module-load-time circular dependency.
|
--- module-load-time circular dependency.
|
||||||
|
--- Multiple calls register independent triggers (each widget gets its own
|
||||||
|
--- input-action named "panel_toggle_<widget_id>" so bindings don't clobber).
|
||||||
--- Loud-error if widget_id is not registered ("register before bind").
|
--- Loud-error if widget_id is not registered ("register before bind").
|
||||||
function M.bind_default_trigger(key, widget_id)
|
function M.bind_default_trigger(key, widget_id)
|
||||||
if key == nil then key = "tab" end
|
if key == nil then key = "tab" end
|
||||||
@@ -321,9 +325,9 @@ function M.bind_default_trigger(key, widget_id)
|
|||||||
.. "' (register before bind)")
|
.. "' (register before bind)")
|
||||||
end
|
end
|
||||||
local input = require("lib-core.input") -- lazy require: avoid load-time cycle
|
local input = require("lib-core.input") -- lazy require: avoid load-time cycle
|
||||||
input.bind("panel_toggle", {key})
|
local action_name = "panel_toggle_" .. widget_id
|
||||||
trigger_action_name = "panel_toggle"
|
input.bind(action_name, {key})
|
||||||
trigger_widget_id = widget_id
|
table.insert(triggers, { action_name = action_name, widget_id = widget_id })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- -----------------------------------------------------------------------
|
-- -----------------------------------------------------------------------
|
||||||
@@ -384,11 +388,13 @@ end
|
|||||||
--- - Mouse edge-detection for click events (dispatches on press, not hold)
|
--- - Mouse edge-detection for click events (dispatches on press, not hold)
|
||||||
--- - Mouse-wheel event dispatch
|
--- - Mouse-wheel event dispatch
|
||||||
function M.update(dt)
|
function M.update(dt)
|
||||||
-- Check default trigger (lazy requires lib-core.input internally if bound)
|
-- Check all default triggers (lazy requires lib-core.input if any are bound)
|
||||||
if trigger_action_name then
|
if #triggers > 0 then
|
||||||
local input = require("lib-core.input")
|
local input = require("lib-core.input")
|
||||||
if input.was_action_pressed(trigger_action_name) then
|
for _, t in ipairs(triggers) do
|
||||||
M.toggle(trigger_widget_id)
|
if input.was_action_pressed(t.action_name) then
|
||||||
|
M.toggle(t.widget_id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
{"id":"lib-core.panel","version":"0.1.0","api_min":"0.1","deps":[]}
|
{"id":"lib-core.panel","version":"0.1.1","api_min":"0.1","deps":[]}
|
||||||
|
|||||||
Reference in New Issue
Block a user