input.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <cstdint>
  3. #define PIKA_ADD_FLAG(NAME, SETNAME, VALUE) \
  4. bool NAME() const {return (flags & ((std::uint32_t)1<<VALUE)); } \
  5. void SETNAME(bool s) { \
  6. if (s) { flags = flags | ((std::uint32_t)1 << VALUE); } \
  7. else { flags = flags & ~((std::uint32_t)1 << VALUE); } \
  8. }
  9. namespace pika
  10. {
  11. struct Button
  12. {
  13. //internal use only
  14. float timer = 0;
  15. //internal use only
  16. std::uint32_t flags = 0;
  17. //true in the first frame the key is pressed
  18. PIKA_ADD_FLAG(pressed, setPressed, 0);
  19. //true while the key is held
  20. PIKA_ADD_FLAG(held, setHeld, 1);
  21. //true in the frame the key is released
  22. PIKA_ADD_FLAG(released, setReleased, 2);
  23. //true in the first frame is pressed then after a pause true every few milliseconds
  24. PIKA_ADD_FLAG(typed, setTyped, 3);
  25. //todo implement
  26. //true if the key is double pressed (true only for one frame, 3 presses would yield only one frame of this being true)
  27. PIKA_ADD_FLAG(doublePressed, setDoublePressed, 4);
  28. //last state of the button (last frame)
  29. PIKA_ADD_FLAG(lastState, setLastState, 5);
  30. enum
  31. {
  32. A = 0,
  33. B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
  34. NR0, NR1, NR2, NR3, NR4, NR5, NR6, NR7, NR8, NR9,
  35. Space, Enter, Escape,
  36. Up, Down, Left, Right,
  37. LeftCtrl, Tab, LeftAlt,
  38. BUTTONS_COUNT, //
  39. };
  40. };
  41. struct Input
  42. {
  43. //typed input doesn't work with mouse buttons
  44. Button lMouse = {};
  45. Button rMouse = {};
  46. //mouse position relevant to window
  47. int mouseX = 0;
  48. int mouseY = 0;
  49. Button buttons[Button::BUTTONS_COUNT] = {};
  50. float deltaTime = 0;
  51. };
  52. };
  53. #undef PIKA_ADD_FLAG