Input.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "Input.h"
  2. #include <libndls.h>
  3. #define INPUT WalrusRPG::Input
  4. using WalrusRPG::Input::Key;
  5. using WalrusRPG::Input::KeyState;
  6. struct InputMap
  7. {
  8. Key key;
  9. t_key key_code;
  10. };
  11. static KeyState key_states[Key::K_SIZE] = {KeyState::KS_RELEASED};
  12. static InputMap key_map[] = {
  13. {Key::K_A, KEY_NSPIRE_CTRL}, {Key::K_B, KEY_NSPIRE_SHIFT},
  14. {Key::K_L, KEY_NSPIRE_TAB}, {Key::K_R, KEY_NSPIRE_MENU},
  15. {Key::K_UP, KEY_NSPIRE_8}, {Key::K_DOWN, KEY_NSPIRE_5},
  16. {Key::K_LEFT, KEY_NSPIRE_4}, {Key::K_RIGHT, KEY_NSPIRE_6},
  17. {Key::K_START, KEY_NSPIRE_HOME}, {Key::K_SELECT, KEY_NSPIRE_ESC},
  18. };
  19. void INPUT::key_poll()
  20. {
  21. for (unsigned i = 0; i < K_SIZE; i++)
  22. {
  23. bool current_key_state = isKeyPressed(key_map[i].key_code);
  24. KeyState previous_key_state = key_states[i];
  25. KeyState resulting_key_state = KS_RELEASED;
  26. if (current_key_state)
  27. {
  28. if (previous_key_state == KS_RELEASED ||
  29. previous_key_state == KS_JUST_RELEASED)
  30. resulting_key_state = KS_JUST_PRESSED;
  31. else if (previous_key_state == KS_JUST_PRESSED ||
  32. previous_key_state == KS_PRESSED)
  33. resulting_key_state = KS_PRESSED;
  34. }
  35. else
  36. {
  37. if (previous_key_state == KS_PRESSED || previous_key_state == KS_JUST_PRESSED)
  38. resulting_key_state = KS_JUST_RELEASED;
  39. else if (previous_key_state == KS_JUST_RELEASED ||
  40. previous_key_state == KS_RELEASED)
  41. resulting_key_state = KS_RELEASED;
  42. }
  43. key_states[i] = resulting_key_state;
  44. }
  45. }
  46. bool INPUT::key_pressed(Key key)
  47. {
  48. return key_states[key] == KS_JUST_PRESSED;
  49. }
  50. bool INPUT::key_released(Key key)
  51. {
  52. return key_states[key] == KS_JUST_RELEASED;
  53. }
  54. bool INPUT::key_down(Key key)
  55. {
  56. return key_states[key] == KS_JUST_PRESSED || key_states[key] == KS_PRESSED;
  57. }
  58. bool INPUT::key_up(Key key)
  59. {
  60. return key_states[key] == KS_JUST_RELEASED || key_states[key] == KS_RELEASED;
  61. }