146 lines
4.2 KiB
C#
146 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
namespace MrAG
|
|
{
|
|
public class KeyBindings
|
|
{
|
|
public class KeyBinding
|
|
{
|
|
public Keys Key;
|
|
public Delegate Press;
|
|
public Delegate Release;
|
|
public Delegate Hold;
|
|
public int HoldInterval = 0;
|
|
public double LastHold = 0;
|
|
|
|
public bool Pressed;
|
|
|
|
public KeyBinding(){}
|
|
|
|
public KeyBinding(Keys Key)
|
|
{
|
|
this.Key = Key;
|
|
}
|
|
|
|
public void Call_Press()
|
|
{
|
|
if (Press == null) return;
|
|
|
|
Press.DynamicInvoke();
|
|
}
|
|
|
|
public void Call_Release()
|
|
{
|
|
if (Release == null) return;
|
|
|
|
Release.DynamicInvoke();
|
|
}
|
|
|
|
public void Call_Hold()
|
|
{
|
|
if (Hold == null) return;
|
|
|
|
Hold.DynamicInvoke();
|
|
}
|
|
}
|
|
|
|
private static List<KeyBinding> Bindings = new List<KeyBinding>();
|
|
|
|
public static void Update(GameTime gameTime)
|
|
{
|
|
KeyboardState ks = Keyboard.GetState();
|
|
|
|
foreach (KeyBinding binding in Bindings)
|
|
{
|
|
if (ks.IsKeyDown(binding.Key))
|
|
{
|
|
if (!binding.Pressed)
|
|
{
|
|
binding.Call_Press();
|
|
binding.LastHold = gameTime.TotalGameTime.TotalMilliseconds;
|
|
binding.Pressed = true;
|
|
}else{
|
|
if (gameTime.TotalGameTime.TotalMilliseconds - binding.LastHold > binding.HoldInterval){
|
|
binding.LastHold = gameTime.TotalGameTime.TotalMilliseconds;
|
|
binding.Call_Hold();
|
|
}
|
|
}
|
|
}else{
|
|
if (binding.Pressed){
|
|
binding.Pressed = false;
|
|
binding.Call_Release();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Add(KeyBinding keyBinding)
|
|
{
|
|
Bindings.Add(keyBinding);
|
|
}
|
|
|
|
public static void Add(Keys KeyToPress, Action OnPress)
|
|
{
|
|
KeyBinding key = new MrAG.KeyBindings.KeyBinding(Keys.Left);
|
|
|
|
key.Key = KeyToPress;
|
|
key.Press = new Action(OnPress);
|
|
Add(key);
|
|
}
|
|
|
|
public static void Add(Keys KeyToPress, Action OnHold, int HoldInterval)
|
|
{
|
|
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
|
|
|
key.Key = KeyToPress;
|
|
key.Hold = OnHold;
|
|
key.HoldInterval = HoldInterval;
|
|
Add(key);
|
|
}
|
|
|
|
public static void Add(Keys KeyToPress, Action OnPress, Action OnHold, int HoldInterval)
|
|
{
|
|
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
|
|
|
key.Key = KeyToPress;
|
|
key.Press = new Action(OnPress);
|
|
key.Hold = OnHold;
|
|
key.HoldInterval = HoldInterval;
|
|
Add(key);
|
|
}
|
|
|
|
public static void Add(Keys KeyToPress, Action OnPress, Action OnRelease)
|
|
{
|
|
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
|
|
|
key.Key = KeyToPress;
|
|
key.Press = new Action(OnPress);
|
|
key.Release = new Action(OnRelease);
|
|
Add(key);
|
|
}
|
|
|
|
public static void Add(Keys KeyToPress, Action OnPress, Action OnHold, int HoldInterval, Action OnRelease)
|
|
{
|
|
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
|
|
|
key.Key = KeyToPress;
|
|
key.Press = new Action(OnPress);
|
|
key.Release = new Action(OnRelease);
|
|
key.Hold = OnHold;
|
|
key.HoldInterval = HoldInterval;
|
|
Add(key);
|
|
}
|
|
|
|
public static void Remove(Keys Key)
|
|
{
|
|
foreach (KeyBinding binding in Bindings)
|
|
{
|
|
if (binding.Key == Key)
|
|
Bindings.Remove(binding);
|
|
}
|
|
}
|
|
}
|
|
} |