input_device.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "math_types.h"
  8. #include "memory_types.h"
  9. #include "string_id.h"
  10. namespace crown
  11. {
  12. /// Represents a generic input device.
  13. ///
  14. /// @ingroup Input
  15. struct InputDevice
  16. {
  17. bool _connected;
  18. u8 _num_buttons;
  19. u8 _num_axes;
  20. u8 _last_button;
  21. u8* _last_state; // num_buttons
  22. u8* _state; // num_buttons
  23. Vector3* _axis; // num_axes
  24. const char** _button_name; // num_buttons
  25. const char** _axis_name; // num_axes
  26. StringId32* _button_hash; // num_buttons
  27. StringId32* _axis_hash; // num_axes
  28. char* _name; // strlen32(name) + 1
  29. /// Returns the name of the input device.
  30. const char* name() const;
  31. /// Returns whether the input device is connected and functioning.
  32. bool connected() const;
  33. /// Returns the number of buttons of the input device.
  34. u8 num_buttons() const;
  35. /// Returns the number of axes of the input devices.
  36. u8 num_axes() const;
  37. /// Returns whether the button @a id is pressed in the current frame.
  38. bool pressed(u8 id) const;
  39. /// Returns whether the button @a id is released in the current frame.
  40. bool released(u8 id) const;
  41. /// Returns whether any button is pressed in the current frame.
  42. bool any_pressed() const;
  43. /// Returns whether any button is released in the current frame.
  44. bool any_released() const;
  45. /// Returns the value of the axis @a id.
  46. Vector3 axis(u8 id) const;
  47. /// Returns the name of the button @a id or NULL if no matching button is found.
  48. const char* button_name(u8 id);
  49. /// Returns the name of the axis @a id of NULL if no matching axis is found.
  50. const char* axis_name(u8 id);
  51. /// Returns the id of the button @a name or UINT8_MAX if no matching button is found.
  52. u8 button_id(StringId32 name);
  53. /// Returns the id of the axis @a name of UINT8_MAX if no matching axis is found.
  54. u8 axis_id(StringId32 name);
  55. void set_connected(bool connected);
  56. void set_button_state(u8 i, bool state);
  57. void set_axis(u8 i, const Vector3& value);
  58. void update();
  59. };
  60. /// Functions to manipulate InputDevice.
  61. ///
  62. /// @ingroup Input
  63. namespace input_device
  64. {
  65. /// Creates a new input device.
  66. InputDevice* create(Allocator& a, const char* name, u8 num_buttons, u8 num_axes, const char** button_names, const char** axis_names);
  67. /// Destroys the input device @a id.
  68. void destroy(Allocator& a, InputDevice& id);
  69. } // namespace input_device
  70. } // namespace crown