input_device.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "input_device.h"
  6. #include "error.h"
  7. #include <string.h> // memcpy
  8. namespace crown
  9. {
  10. const char* InputDevice::name() const
  11. {
  12. return _name;
  13. }
  14. uint8_t InputDevice::num_buttons() const
  15. {
  16. return _num_buttons;
  17. }
  18. uint8_t InputDevice::num_axes() const
  19. {
  20. return _num_axes;
  21. }
  22. bool InputDevice::pressed(uint8_t i) const
  23. {
  24. CE_ASSERT(i < _num_buttons, "Index out of bounds");
  25. return (~_last_state[i] & _current_state[i]) != 0;
  26. }
  27. bool InputDevice::released(uint8_t i) const
  28. {
  29. CE_ASSERT(i < _num_buttons, "Index out of bounds");
  30. return (_last_state[i] & ~_current_state[i]) != 0;
  31. }
  32. bool InputDevice::any_pressed() const
  33. {
  34. return pressed(_last_button);
  35. }
  36. bool InputDevice::any_released() const
  37. {
  38. return released(_last_button);
  39. }
  40. Vector3 InputDevice::axis(uint8_t i) const
  41. {
  42. CE_ASSERT(i < _num_axes, "Index out of bounds");
  43. return _axis[i];
  44. }
  45. void InputDevice::set_button_state(uint8_t i, bool state)
  46. {
  47. CE_ASSERT(i < _num_buttons, "Index out of bounds");
  48. _last_button = i;
  49. _current_state[i] = state;
  50. }
  51. void InputDevice::set_axis(uint8_t i, const Vector3& value)
  52. {
  53. CE_ASSERT(i < _num_axes, "Index out of bounds");
  54. _axis[i] = value;
  55. }
  56. void InputDevice::update()
  57. {
  58. memcpy(_last_state, _current_state, sizeof(uint8_t)*_num_buttons);
  59. }
  60. } // namespace crown