Input.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_DOC}, {Key::K_SELECT, KEY_NSPIRE_ESC},
  18. };
  19. KeyState INPUT::key_get_state(Key key)
  20. {
  21. return key_states[key];
  22. }
  23. void INPUT::key_poll()
  24. {
  25. for (unsigned i = 0; i < K_SIZE; i++)
  26. {
  27. bool current_key_state = isKeyPressed(key_map[i].key_code);
  28. KeyState previous_key_state = key_states[i];
  29. KeyState resulting_key_state = KS_RELEASED;
  30. if (current_key_state)
  31. {
  32. if (previous_key_state == KS_RELEASED ||
  33. previous_key_state == KS_JUST_RELEASED)
  34. resulting_key_state = KS_JUST_PRESSED;
  35. else if (previous_key_state == KS_JUST_PRESSED ||
  36. previous_key_state == KS_PRESSED)
  37. resulting_key_state = KS_PRESSED;
  38. }
  39. else
  40. {
  41. if (previous_key_state == KS_PRESSED || previous_key_state == KS_JUST_PRESSED)
  42. resulting_key_state = KS_JUST_RELEASED;
  43. else if (previous_key_state == KS_JUST_RELEASED ||
  44. previous_key_state == KS_RELEASED)
  45. resulting_key_state = KS_RELEASED;
  46. }
  47. key_states[i] = resulting_key_state;
  48. }
  49. }
  50. bool INPUT::key_pressed(Key key)
  51. {
  52. return key_states[key] == KS_JUST_PRESSED;
  53. }
  54. bool INPUT::key_released(Key key)
  55. {
  56. return key_states[key] == KS_JUST_RELEASED;
  57. }
  58. bool INPUT::key_down(Key key)
  59. {
  60. return key_states[key] == KS_JUST_PRESSED || key_states[key] == KS_PRESSED;
  61. }
  62. bool INPUT::key_up(Key key)
  63. {
  64. return key_states[key] == KS_JUST_RELEASED || key_states[key] == KS_RELEASED;
  65. }