input.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Controller
  42. {
  43. enum Buttons
  44. {
  45. A = 0,
  46. B,
  47. X,
  48. Y,
  49. LBumper,
  50. RBumper,
  51. Back,
  52. Start,
  53. Guide,
  54. LThumb,
  55. Rthumb,
  56. Up,
  57. Right,
  58. Down,
  59. Left,
  60. ButtonsCount
  61. };
  62. Button buttons[Buttons::ButtonsCount] = {};
  63. float LT = 0.f;
  64. float RT = 0.f;
  65. struct
  66. {
  67. constexpr static float JOYSTICK_SENSITIVITY = 0.7f;
  68. float x = 0.f, y = 0.f;
  69. bool left() { return x < -JOYSTICK_SENSITIVITY; }
  70. bool right() { return x > JOYSTICK_SENSITIVITY; }
  71. bool up() { return y > JOYSTICK_SENSITIVITY; }
  72. bool down() { return y < -JOYSTICK_SENSITIVITY; }
  73. }LStick, RStick;
  74. bool connected = 0;
  75. void resetAllButtons()
  76. {
  77. *this = {};
  78. }
  79. };
  80. struct Input
  81. {
  82. //typed input doesn't work with mouse buttons
  83. Button lMouse = {};
  84. Button rMouse = {};
  85. //mouse position relative to window
  86. int mouseX = 0;
  87. int mouseY = 0;
  88. Button buttons[Button::BUTTONS_COUNT] = {};
  89. char typedInput[20] = {};
  90. float deltaTime = 0;
  91. //focus is here because it makes sense for the replay
  92. bool hasFocus = 0;
  93. bool lastFrameHasFocus = 0;
  94. constexpr static int MAX_CONTROLLERS_COUNT = 4; //don't change
  95. Controller controllers[MAX_CONTROLLERS_COUNT] = {};
  96. //a logic or between all the controllers
  97. Controller anyController = {};
  98. };
  99. void processAButton(pika::Button &b, int action);
  100. };
  101. #undef PIKA_ADD_FLAG