InputComponent.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using OpenTK;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using OpenTK.Input;
  6. namespace fun.Client.Components
  7. {
  8. internal sealed class InputComponent : GameComponent
  9. {
  10. public MouseInput Mouse { get; private set; }
  11. public KeyboardInput Keyboard { get; private set; }
  12. //public GamePadInput Gamepad { get; private set; }
  13. public InputComponent(GameWindow game)
  14. : base(game)
  15. {
  16. Mouse = new MouseInput(game);
  17. Keyboard = new KeyboardInput();
  18. }
  19. public override void Initialize()
  20. {
  21. //OpenTK.Input.Mouse.SetPosition(Game.Width / 2, Game.Height / 2);
  22. }
  23. public override void Update(FrameEventArgs e)
  24. {
  25. if (Game.Focused)
  26. Mouse.Update();
  27. Keyboard.Update();
  28. if (Keyboard.GetKeyDown(Key.Escape))
  29. Game.Close();
  30. }
  31. }
  32. internal sealed class MouseInput
  33. {
  34. private GameWindow game;
  35. private List<MouseButton> lastMouseButtons;
  36. private List<MouseButton> currMouseButtons;
  37. private MouseState curr;
  38. private MouseState prev;
  39. public Vector3 Delta { get; private set; }
  40. public float MouseSensitivity { get; set; }
  41. public bool ShouldCenter { get; set; }
  42. public MouseInput(GameWindow game)
  43. {
  44. this.game = game;
  45. this.ShouldCenter = true;
  46. lastMouseButtons = new List<MouseButton>();
  47. currMouseButtons = new List<MouseButton>();
  48. MouseSensitivity = 0.1f;
  49. prev = Mouse.GetState();
  50. }
  51. public bool GetMouseButtonPressed(MouseButton mouseButton)
  52. {
  53. return currMouseButtons.Contains(mouseButton) && !lastMouseButtons.Contains(mouseButton);
  54. }
  55. public bool GetMouseButtonDown(MouseButton mouseButton)
  56. {
  57. return currMouseButtons.Contains(mouseButton);
  58. }
  59. public bool GetMouseButtonRelease(MouseButton mouseButton)
  60. {
  61. return !currMouseButtons.Contains(mouseButton) && lastMouseButtons.Contains(mouseButton);
  62. }
  63. //public void Update()
  64. //{
  65. // lastMouseButtons = currMouseButtons;
  66. // var centreScreen = new Vector2(game.Width / 2, game.Height / 2);
  67. // MouseState mouseState = Mouse.GetState();
  68. // currMouseButtons = new List<MouseButton>();
  69. // if (ShouldCenter)
  70. // Mouse.SetPosition(centreScreen.X, centreScreen.Y);
  71. // if (!currMouseButtons.Contains(MouseButton.Right) && mouseState.RightButton == ButtonState.Pressed)
  72. // currMouseButtons.Add(MouseButton.Right);
  73. // if (currMouseButtons.Contains(MouseButton.Right) && mouseState.RightButton == ButtonState.Released)
  74. // currMouseButtons.Remove(MouseButton.Right);
  75. // if (!currMouseButtons.Contains(MouseButton.Left) && mouseState.LeftButton == ButtonState.Pressed)
  76. // currMouseButtons.Add(MouseButton.Left);
  77. // if (currMouseButtons.Contains(MouseButton.Left) && mouseState.LeftButton == ButtonState.Released)
  78. // currMouseButtons.Remove(MouseButton.Left);
  79. // if (!currMouseButtons.Contains(MouseButton.Middle) && mouseState.MiddleButton == ButtonState.Pressed)
  80. // currMouseButtons.Add(MouseButton.Middle);
  81. // if (currMouseButtons.Contains(MouseButton.Middle) && mouseState.MiddleButton == ButtonState.Released)
  82. // currMouseButtons.Remove(MouseButton.Middle);
  83. // int deltaX = (int)(centreScreen.X - mouseState.X);
  84. // int deltaY = (int)(centreScreen.Y - mouseState.Y);
  85. // Delta = new Vector2(mouseState.X * MouseSensitivity, mouseState.Y * MouseSensitivity);//deltaX * MouseSensitivity, deltaY * MouseSensitivity);
  86. //}
  87. public void Update()
  88. {
  89. curr = Mouse.GetState();
  90. if (curr != prev)
  91. Delta = new Vector3(
  92. (prev.X - curr.X) * MouseSensitivity,
  93. (prev.Y - curr.Y) * MouseSensitivity,
  94. prev.Wheel - curr.Wheel);
  95. else
  96. Delta = Vector3.Zero;
  97. prev = curr;
  98. }
  99. }
  100. internal sealed class KeyboardInput
  101. {
  102. private Key[] lastKeys;
  103. private Key[] currKeys;
  104. public Key[] DownKeys { get { return currKeys; } }
  105. public Key[] ReleasedKeys { get { return lastKeys.Where(k => !currKeys.Contains(k)).ToArray(); } }
  106. public KeyboardInput()
  107. {
  108. currKeys = new Key[0];
  109. lastKeys = new Key[0];
  110. }
  111. public bool GetKeyPressed(Key key)
  112. {
  113. return !lastKeys.Contains(key) && currKeys.Contains(key);
  114. }
  115. public bool GetKeyDown(Key key)
  116. {
  117. return currKeys.Contains(key);
  118. }
  119. public bool GetKeyRelease(Key key)
  120. {
  121. return lastKeys.Contains(key) && !currKeys.Contains(key);
  122. }
  123. public void Update()
  124. {
  125. lastKeys = currKeys;
  126. var keyboardState = Keyboard.GetState();
  127. currKeys = Enum.GetValues(typeof(Key)).OfType<Key>()
  128. .Where(k => keyboardState.IsKeyDown(k)).ToArray(); ;
  129. }
  130. }
  131. // TODO: Gamepad implementireren
  132. }