Input.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "input/Input.h"
  2. #include "Quirks.h"
  3. #include "platform.h"
  4. #define INPUT WalrusRPG::Input
  5. using WalrusRPG::Input::Key;
  6. using WalrusRPG::Input::KeyState;
  7. struct InputMap
  8. {
  9. Key key;
  10. keycode_t key_code;
  11. };
  12. struct KeyBuffer
  13. {
  14. bool current;
  15. bool previous;
  16. };
  17. struct KeyBuffer key_states[Key::K_SIZE] = {{false, false}};
  18. // TODO: make these software-mappable
  19. #ifdef SFML
  20. using sf::Keyboard;
  21. InputMap key_map[] = {
  22. {Key::K_A, Keyboard::W},
  23. {Key::K_B, Keyboard::X},
  24. {Key::K_L, Keyboard::Q},
  25. {Key::K_R, Keyboard::S},
  26. {Key::K_UP, Keyboard::Up},
  27. {Key::K_DOWN, Keyboard::Down},
  28. {Key::K_LEFT, Keyboard::Left},
  29. {Key::K_RIGHT, Keyboard::Right},
  30. {Key::K_START, Keyboard::Return},
  31. {Key::K_SELECT, Keyboard::BackSpace},
  32. };
  33. #endif
  34. #ifdef NSPIRE
  35. static InputMap key_map[] = {
  36. {Key::K_A, KEY_NSPIRE_CTRL}, {Key::K_B, KEY_NSPIRE_SHIFT},
  37. {Key::K_L, KEY_NSPIRE_TAB}, {Key::K_R, KEY_NSPIRE_MENU},
  38. {Key::K_UP, KEY_NSPIRE_8}, {Key::K_DOWN, KEY_NSPIRE_5},
  39. {Key::K_LEFT, KEY_NSPIRE_4}, {Key::K_RIGHT, KEY_NSPIRE_6},
  40. {Key::K_START, KEY_NSPIRE_DOC}, {Key::K_SELECT, KEY_NSPIRE_ESC},
  41. };
  42. #endif
  43. KeyState INPUT::key_get_state(Key key)
  44. {
  45. // "Just" pressed/released before held/inactive to let these events through
  46. if (key_pressed(key))
  47. return KS_JUST_PRESSED;
  48. if (key_down(key))
  49. return KS_PRESSED;
  50. if (key_released(key))
  51. return KS_JUST_RELEASED;
  52. if (key_up(key))
  53. return KS_RELEASED;
  54. // Default case to mute compiler warnings, shouldn't even reach here in practice
  55. return KS_RELEASED;
  56. }
  57. void INPUT::key_poll()
  58. {
  59. for (unsigned i = 0; i < K_SIZE; i++)
  60. {
  61. key_states[i].previous = key_states[i].current;
  62. key_states[i].current = WalrusRPG::Quirks::get_key(key_map[i].key_code);
  63. }
  64. }
  65. bool INPUT::key_pressed(Key key)
  66. {
  67. return !key_states[key].previous && key_states[key].current;
  68. }
  69. bool INPUT::key_released(Key key)
  70. {
  71. return key_states[key].previous && !key_states[key].current;
  72. }
  73. bool INPUT::key_down(Key key)
  74. {
  75. return key_states[key].current;
  76. }
  77. bool INPUT::key_up(Key key)
  78. {
  79. return !key_states[key].current;
  80. }