using AcDream.Core.Selection;
using AcDream.UI.Abstractions.Input;
namespace AcDream.App.UI.Layout;
///
/// Focused retained-toolbar adapter for retail global input actions. The input
/// dispatcher owns chords/scopes; this class only maps semantic actions to the
/// exact gmToolbarUI::ListenToGlobalMessage @ 0x004BE4E0 slot intent.
///
public sealed class ToolbarInputController
{
private readonly ToolbarController _toolbar;
private readonly SelectionState _selection;
public ToolbarInputController(ToolbarController toolbar, SelectionState selection)
{
_toolbar = toolbar ?? throw new ArgumentNullException(nameof(toolbar));
_selection = selection ?? throw new ArgumentNullException(nameof(selection));
}
/// Returns true when belongs to the toolbar.
public bool Handle(InputAction action)
{
if (TryMapShortcut(action, out int slot, out bool use))
{
_toolbar.UseShortcut(slot, use);
return true;
}
if (action == InputAction.CreateShortcut)
{
_toolbar.CreateShortcutToItem(_selection.SelectedObjectId ?? 0u);
return true;
}
return false;
}
internal static bool TryMapShortcut(InputAction action, out int slot, out bool use)
{
int value = (int)action;
if (value >= (int)InputAction.UseQuickSlot_1
&& value <= (int)InputAction.UseQuickSlot_9)
{
slot = value - (int)InputAction.UseQuickSlot_1;
use = true;
return true;
}
if (value >= (int)InputAction.SelectQuickSlot_1
&& value <= (int)InputAction.SelectQuickSlot_9)
{
slot = value - (int)InputAction.SelectQuickSlot_1;
use = false;
return true;
}
if (value >= (int)InputAction.UseQuickSlot_14
&& value <= (int)InputAction.UseQuickSlot_18)
{
slot = 13 + value - (int)InputAction.UseQuickSlot_14;
use = true;
return true;
}
slot = -1;
use = false;
return false;
}
}