using System;
using System.Collections.Generic;
using Glass;
namespace Clave.Glass
{
///
/// One source's actions and the chords bound to each, kept registered as shortcuts with a keyboard manager. A game
/// asks for an action instead of asking about chords.
///
public class KeyBindings
{
private struct Binding
{
// In the order a player sees them, which a ChordSet doesn't keep.
public Chord[] Chords;
public ChordSet Set;
public bool Disabled;
}
private readonly KeyAction[] actions;
private readonly Dictionary bound = new Dictionary();
/// The name the source's bindings are saved under.
/// If two actions share an id or a default chord.
public KeyBindings(string id, KeyboardManager keys, KeyPriority priority, params KeyAction[] actions)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentException("A source needs an id to be saved under.", nameof(id));
}
if (actions == null)
{
throw new ArgumentNullException(nameof(actions));
}
Id = id;
Keys = keys ?? throw new ArgumentNullException(nameof(keys));
Priority = priority;
this.actions = (KeyAction[])actions.Clone();
var ids = new HashSet();
foreach (KeyAction action in this.actions)
{
if (action == null)
{
throw new ArgumentNullException(nameof(actions));
}
if (!ids.Add(action.Id))
{
throw new ArgumentException($"Two actions have the id {action.Id}.", nameof(actions));
}
bound.Add(action, new Binding { Chords = new Chord[0], Set = ChordSet.Empty });
}
try
{
foreach (KeyAction action in this.actions)
{
Bind(action, action.Defaults);
}
}
catch
{
// So a source that fails to build leaves no shortcuts behind.
foreach (KeyAction action in this.actions)
{
Bind(action, new Chord[0]);
}
throw;
}
}
public KeyBindings(string id, KeyboardManager keys, params KeyAction[] actions)
: this(id, keys, KeyPriority.Game, actions)
{
}
public string Id { get; }
public KeyboardManager Keys { get; }
///
/// Gets the priority every chord here is a shortcut at.
///
public KeyPriority Priority { get; }
public IReadOnlyList Actions
{
get { return actions; }
}
///
/// Raised after an action's chords change, with the action.
///
public event Action Rebound;
private void RequireAction(KeyAction action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
if (!bound.ContainsKey(action))
{
throw new ArgumentException($"{action.Id} isn't one of this source's actions.", nameof(action));
}
}
public IReadOnlyList ChordsOf(KeyAction action)
{
RequireAction(action);
return bound[action].Chords;
}
///
/// Returns the keys of the action's first binding, named as the keyboard manager's labels name them, or an
/// empty string for an action bound to nothing. A menu or a hint shows one way to do something, not all.
///
public string LabelOf(KeyAction action)
{
IReadOnlyList chords = ChordsOf(action);
if (chords.Count == 0)
{
return "";
}
return Keys.KeyLabels.LabelOf(chords[0]);
}
///
/// Returns the action is bound to, or null.
///
public KeyAction ActionOf(Chord chord)
{
foreach (KeyAction action in actions)
{
if (bound[action].Set.Contains(chord))
{
return action;
}
}
return null;
}
private static bool HasFormula(IReadOnlyList chords, KeyFormula formula)
{
foreach (Chord chord in chords)
{
if (chord.Formula.Equals(formula))
{
return true;
}
}
return false;
}
///
/// Gets whether is bound to chords that match exactly as its defaults do, in any
/// order.
///
public bool IsDefault(KeyAction action)
{
IReadOnlyList chords = ChordsOf(action);
if (chords.Count != action.Defaults.Count)
{
return false;
}
foreach (Chord chord in chords)
{
if (!HasFormula(action.Defaults, chord.Formula))
{
return false;
}
}
return true;
}
private void Register(IReadOnlyList chords, KeyAction action, bool disabled)
{
var added = new List();
try
{
foreach (Chord chord in chords)
{
Keys.AddShortcut(chord, Priority, action.Name, action.Context);
added.Add(chord);
if (disabled)
{
Keys.SetShortcutEnabled(chord, false);
}
}
}
catch
{
foreach (Chord chord in added)
{
Keys.RemoveShortcut(chord);
}
throw;
}
}
///
/// Binds to , in that order, in place of what it had.
///
///
/// If a chord is already a shortcut, such as one bound to another action. The action keeps what it had.
///
public void Bind(KeyAction action, IReadOnlyList chords)
{
RequireAction(action);
if (chords == null)
{
throw new ArgumentNullException(nameof(chords));
}
var copy = new Chord[chords.Count];
for (int i = 0; i < copy.Length; ++i)
{
copy[i] = chords[i] ?? throw new ArgumentNullException(nameof(chords));
}
Binding previous = bound[action];
foreach (Chord chord in previous.Chords)
{
Keys.RemoveShortcut(chord);
}
try
{
Register(copy, action, previous.Disabled);
}
catch
{
Register(previous.Chords, action, previous.Disabled);
throw;
}
bound[action] = new Binding { Chords = copy, Set = ChordSet.Of(copy), Disabled = previous.Disabled };
Rebound?.Invoke(action);
}
public bool IsEnabled(KeyAction action)
{
RequireAction(action);
return !bound[action].Disabled;
}
///
/// Switches off or back on. While it is off its chords never fire and hide nothing,
/// and a rebind keeps it off.
///
public void SetEnabled(KeyAction action, bool enabled)
{
RequireAction(action);
Binding binding = bound[action];
binding.Disabled = !enabled;
bound[action] = binding;
foreach (Chord chord in binding.Chords)
{
Keys.SetShortcutEnabled(chord, enabled);
}
}
///
/// Returns whether any chord of fires for .
///
public bool ShouldFire(KeyAction action, in KeyboardSnapshot keyboard)
{
RequireAction(action);
return Keys.ShouldFireAny(bound[action].Set, keyboard);
}
}
}