InputElement.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using fun.Communication;
  2. using fun.Core;
  3. using OpenTK;
  4. using OpenTK.Input;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Environment = fun.Core.Environment;
  8. namespace fun.Basics
  9. {
  10. public sealed class InputElement : Element, IInput
  11. {
  12. private Key[] lastKeys;
  13. private Key[] currKeys;
  14. public IEnumerable<Key> Keys { get; set; }
  15. public Vector2 MouseDelta { get; set; }
  16. public Vector3 Content { get; set; }
  17. public InputElement(Environment environment, Entity entity)
  18. : base(environment, entity)
  19. {
  20. currKeys = new Key[0];
  21. lastKeys = new Key[0];
  22. }
  23. public bool GetKeyPressed(Key key)
  24. {
  25. return !lastKeys.Contains(key) && currKeys.Contains(key);
  26. }
  27. public bool GetKeyDown(Key key)
  28. {
  29. return currKeys.Contains(key);
  30. }
  31. public bool GetKeyRelease(Key key)
  32. {
  33. return lastKeys.Contains(key) && !currKeys.Contains(key);
  34. }
  35. public override void Update(double time)
  36. {
  37. if (Keys == null)
  38. return;
  39. lastKeys = currKeys;
  40. currKeys = Keys.ToArray();
  41. }
  42. }
  43. }