From 49ee3164f6fe24b5ed653593085b123884a3eac1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 11 Jul 2026 03:16:43 +0100 Subject: [PATCH] feat(input): resolve chords and directional strings --- .../Runtime/EveInputGestures.cs | 82 +++++++++++++++++++ .../Runtime/EveInputGestures.cs.meta | 2 + 2 files changed, 84 insertions(+) create mode 100644 packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs create mode 100644 packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs.meta diff --git a/packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs b/packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs new file mode 100644 index 0000000..a318066 --- /dev/null +++ b/packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace GameCult.Eve.Surface +{ + public sealed class EveInputGesture + { + public string Kind { get; set; } = "direct"; + public IReadOnlyList Controls { get; set; } = Array.Empty(); + public int MaxStepIntervalMs { get; set; } = 650; + public string CompletionControl { get; set; } = ""; + } + + public sealed class EveInputBinding + { + public string BindingId { get; set; } = ""; + public string ActionId { get; set; } = ""; + public EveInputGesture Gesture { get; set; } = new EveInputGesture(); + public bool ActionBar { get; set; } + } + + public sealed class EveInputGestureResolver + { + private readonly IReadOnlyList _bindings; + private readonly HashSet _pressed = new HashSet(StringComparer.Ordinal); + private readonly Dictionary _sequences = new Dictionary(StringComparer.Ordinal); + + public EveInputGestureResolver(IEnumerable bindings) + { + _bindings = (bindings ?? Array.Empty()).Where(binding => binding != null).ToArray(); + } + + public IReadOnlyList Press(string control, long timestampMs) + { + _pressed.Add(control ?? ""); + var actions = new HashSet(StringComparer.Ordinal); + foreach (var binding in _bindings) + { + var gesture = binding.Gesture ?? new EveInputGesture(); + if (gesture.Kind == "direct" && gesture.Controls.FirstOrDefault() == control) + actions.Add(binding.ActionId); + else if (gesture.Kind == "chord" && gesture.Controls.Count > 0 && gesture.Controls.All(_pressed.Contains)) + actions.Add(binding.ActionId); + else if (gesture.Kind == "sequence" && Advance(binding, control ?? "", timestampMs)) + actions.Add(binding.ActionId); + } + return actions.ToArray(); + } + + public void Release(string control) => _pressed.Remove(control ?? ""); + + public void Reset() + { + _pressed.Clear(); + _sequences.Clear(); + } + + private bool Advance(EveInputBinding binding, string control, long timestampMs) + { + var controls = binding.Gesture.Controls; + if (controls.Count == 0) return false; + _sequences.TryGetValue(binding.BindingId, out var previous); + var index = previous.Index > 0 && timestampMs - previous.TimestampMs <= Math.Max(50, binding.Gesture.MaxStepIntervalMs) ? previous.Index : 0; + var next = controls[index] == control ? index + 1 : controls[0] == control ? 1 : 0; + if (next == controls.Count) + { + _sequences.Remove(binding.BindingId); + return string.IsNullOrWhiteSpace(binding.Gesture.CompletionControl); + } + _sequences[binding.BindingId] = new SequenceState(next, timestampMs); + return false; + } + + private readonly struct SequenceState + { + public SequenceState(int index, long timestampMs) { Index = index; TimestampMs = timestampMs; } + public int Index { get; } + public long TimestampMs { get; } + } + } +} diff --git a/packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs.meta b/packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs.meta new file mode 100644 index 0000000..3abaaab --- /dev/null +++ b/packages/org.gamecult.eve.surface/Runtime/EveInputGestures.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: e3c2941be243a754092ac2543d1e6bf7 \ No newline at end of file