using SharpDX.DirectInput;
using System;
using System.Collections.Generic;
using System.Linq;
using Turbo.Plugins.Default;
namespace Turbo.Plugins.Jack.Keyboard
{
///
/// A centralized method to specify key event actions.
///
///
///
public class OptionTogglerPlugin : BasePlugin, IKeyEventHandler
{
private Dictionary>> KeyEvents;
///
/// Initializes a new instance of the class.
///
public OptionTogglerPlugin()
{
Enabled = true;
KeyEvents = new Dictionary>>();
}
///
/// Adds an action or multiple actions binded to an IKeyEvent.
///
/// The key event.
/// The actions.
public void AddAction(IKeyEvent keyEvent, params Action[] keyEvents)
{
var @event = GetActions(keyEvent, true);
@event.Value.AddRange(keyEvents);
}
///
/// Adds an action or multiple actions binded to a key.
///
/// The key.
/// The actions.
public void AddAction(Key key, params Action[] keyEvents)
{
var keyEvent = Hud.Input.CreateKeyEvent(true, key, false, false, false);
AddAction(keyEvent, keyEvents);
}
///
/// Called when the player pressed or released a key. This method is called during the data collection phase, which means no rendering is possible!
///
///
public void OnKeyEvent(IKeyEvent keyEvent)
{
var @event = GetActions(keyEvent);
if (!IsDefault(@event))
{
foreach (var action in @event.Value)
{
action.Invoke(Hud);
}
}
}
///
/// Gets the actions registered for this key event.
///
/// The key event.
/// if set to true [create if not found].
///
private KeyValuePair>> GetActions(IKeyEvent keyEvent, bool createIfNotFound = false)
{
var @event = KeyEvents.SingleOrDefault(x => x.Key.Matches(keyEvent));
if (IsDefault(@event) && createIfNotFound)
{
@event = new KeyValuePair>>(keyEvent, new List>());
KeyEvents.Add(@event.Key, @event.Value);
}
return @event;
}
///
/// Determines whether the specified object is default.
///
///
/// The object.
///
/// true if the specified object is default; otherwise, false.
///
private static bool IsDefault(T @object)
{
return default(T).Equals(@object);
}
}
}