Guest User

Untitled

a guest
Oct 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Input;
  6. using System.Diagnostics;
  7.  
  8. namespace Hanafuda.Core.Input.Feeders
  9. {
  10. public class KeyboardFeeder
  11. : IInputFeeder
  12. {
  13. public void Update(IInputController controller)
  14. {
  15. KeyboardState keyState = Keyboard.GetState();
  16.  
  17. if (keyState.GetPressedKeys().Length > 0 && keyState.IsKeyDown(Keys.Right))
  18. {
  19. var keys = keyState.GetPressedKeys();
  20. }
  21.  
  22. foreach (var kvp in actions)
  23. {
  24. if (PressedKeySet(controller, keyState, kvp.Key))
  25. {
  26. // Check if only on up
  27. if (kvp.Key.Condition == ActivateState.Up && PressedKeySet(controller, lastState, kvp.Key))
  28. continue;
  29.  
  30. controller.OnActionActivated(new ActionActivatedEventArgs(kvp.Value));
  31. }
  32. }
  33.  
  34. lastState = keyState;
  35. }
  36.  
  37. public void Bind (string action, KeySet keyset)
  38. {
  39. if (actions.ContainsKey(keyset))
  40. throw new ArgumentException("Cannot bind the same keyset multiple times.");
  41.  
  42. actions.Add(keyset, action);
  43. }
  44.  
  45. public bool Modal
  46. {
  47. get { return true; }
  48. }
  49.  
  50. private Dictionary<KeySet, string> actions = new Dictionary<KeySet, string>();
  51. private KeyboardState lastState = Keyboard.GetState();
  52.  
  53. private bool PressedKeySet(IInputController controller, KeyboardState state, KeySet set)
  54. {
  55. Keys[] pressedKeys = state.GetPressedKeys();
  56.  
  57. int containsCount = 0;
  58. foreach (Keys key in pressedKeys)
  59. {
  60. if (set.ContainsBind(key))
  61. containsCount++;
  62. }
  63.  
  64. return (containsCount == set.Binds.Length);
  65. }
  66. }
  67.  
  68. public class KeySet
  69. {
  70. public KeySet(params Keys[] keys)
  71. : this(ActivateState.Up, keys)
  72. {
  73. }
  74.  
  75. public KeySet(ActivateState condition, params Keys[] keys)
  76. {
  77. this.Condition = condition;
  78. this.Binds = keys;
  79. this.hashcode = GenerateHashcode(keys);
  80. }
  81.  
  82. public Keys[] Binds { get; private set; }
  83.  
  84. public ActivateState Condition { get; private set; }
  85.  
  86. public bool ContainsBind (Keys key)
  87. {
  88. return this.Binds.Contains(key);
  89. }
  90.  
  91. public override int GetHashCode()
  92. {
  93. return hashcode;
  94. }
  95.  
  96. private int hashcode = 0;
  97.  
  98. public static int GenerateHashcode(Keys[] keys)
  99. {
  100. Keys tmpkeys = Keys.None;
  101. foreach (Keys key in keys)
  102. tmpkeys |= key;
  103.  
  104. return tmpkeys.GetHashCode();
  105. }
  106. }
  107.  
  108. public enum ActivateState
  109. {
  110. Down,
  111. Up
  112. }
  113. }
Add Comment
Please, Sign In to add comment