Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Input;
- using System.Diagnostics;
- namespace Hanafuda.Core.Input.Feeders
- {
- public class KeyboardFeeder
- : IInputFeeder
- {
- public void Update(IInputController controller)
- {
- KeyboardState keyState = Keyboard.GetState();
- if (keyState.GetPressedKeys().Length > 0 && keyState.IsKeyDown(Keys.Right))
- {
- var keys = keyState.GetPressedKeys();
- }
- foreach (var kvp in actions)
- {
- if (PressedKeySet(controller, keyState, kvp.Key))
- {
- // Check if only on up
- if (kvp.Key.Condition == ActivateState.Up && PressedKeySet(controller, lastState, kvp.Key))
- continue;
- controller.OnActionActivated(new ActionActivatedEventArgs(kvp.Value));
- }
- }
- lastState = keyState;
- }
- public void Bind (string action, KeySet keyset)
- {
- if (actions.ContainsKey(keyset))
- throw new ArgumentException("Cannot bind the same keyset multiple times.");
- actions.Add(keyset, action);
- }
- public bool Modal
- {
- get { return true; }
- }
- private Dictionary<KeySet, string> actions = new Dictionary<KeySet, string>();
- private KeyboardState lastState = Keyboard.GetState();
- private bool PressedKeySet(IInputController controller, KeyboardState state, KeySet set)
- {
- Keys[] pressedKeys = state.GetPressedKeys();
- int containsCount = 0;
- foreach (Keys key in pressedKeys)
- {
- if (set.ContainsBind(key))
- containsCount++;
- }
- return (containsCount == set.Binds.Length);
- }
- }
- public class KeySet
- {
- public KeySet(params Keys[] keys)
- : this(ActivateState.Up, keys)
- {
- }
- public KeySet(ActivateState condition, params Keys[] keys)
- {
- this.Condition = condition;
- this.Binds = keys;
- this.hashcode = GenerateHashcode(keys);
- }
- public Keys[] Binds { get; private set; }
- public ActivateState Condition { get; private set; }
- public bool ContainsBind (Keys key)
- {
- return this.Binds.Contains(key);
- }
- public override int GetHashCode()
- {
- return hashcode;
- }
- private int hashcode = 0;
- public static int GenerateHashcode(Keys[] keys)
- {
- Keys tmpkeys = Keys.None;
- foreach (Keys key in keys)
- tmpkeys |= key;
- return tmpkeys.GetHashCode();
- }
- }
- public enum ActivateState
- {
- Down,
- Up
- }
- }
Add Comment
Please, Sign In to add comment