2
0

input.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* input.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef INPUT_H
  30. #define INPUT_H
  31. #include "object.h"
  32. #include "os/thread_safe.h"
  33. #include "os/main_loop.h"
  34. class Input : public Object {
  35. OBJ_TYPE( Input, Object );
  36. static Input *singleton;
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. enum MouseMode {
  41. MOUSE_MODE_VISIBLE,
  42. MOUSE_MODE_HIDDEN,
  43. MOUSE_MODE_CAPTURED
  44. };
  45. void set_mouse_mode(MouseMode p_mode);
  46. MouseMode get_mouse_mode() const;
  47. static Input *get_singleton();
  48. virtual bool is_key_pressed(int p_scancode)=0;
  49. virtual bool is_mouse_button_pressed(int p_button)=0;
  50. virtual bool is_joy_button_pressed(int p_device, int p_button)=0;
  51. virtual bool is_action_pressed(const StringName& p_action)=0;
  52. virtual float get_joy_axis(int p_device,int p_axis)=0;
  53. virtual Point2 get_mouse_pos() const=0;
  54. virtual Point2 get_mouse_speed() const=0;
  55. virtual Vector3 get_accelerometer()=0;
  56. virtual void action_press(const StringName& p_action)=0;
  57. virtual void action_release(const StringName& p_action)=0;
  58. Input();
  59. };
  60. VARIANT_ENUM_CAST(Input::MouseMode);
  61. class InputDefault : public Input {
  62. OBJ_TYPE( InputDefault, Input );
  63. _THREAD_SAFE_CLASS_
  64. int mouse_button_mask;
  65. Set<int> keys_pressed;
  66. Set<int> joy_buttons_pressed;
  67. Map<int,float> joy_axis;
  68. Map<StringName,int> custom_action_press;
  69. Vector3 accelerometer;
  70. Vector2 mouse_pos;
  71. MainLoop *main_loop;
  72. struct SpeedTrack {
  73. uint64_t last_tick;
  74. Vector2 speed;
  75. Vector2 accum;
  76. float accum_t;
  77. float min_ref_frame;
  78. float max_ref_frame;
  79. void update(const Vector2& p_delta_p);
  80. void reset();
  81. SpeedTrack();
  82. };
  83. SpeedTrack mouse_speed_track;
  84. public:
  85. virtual bool is_key_pressed(int p_scancode);
  86. virtual bool is_mouse_button_pressed(int p_button);
  87. virtual bool is_joy_button_pressed(int p_device, int p_button);
  88. virtual bool is_action_pressed(const StringName& p_action);
  89. virtual float get_joy_axis(int p_device,int p_axis);
  90. virtual Vector3 get_accelerometer();
  91. virtual Point2 get_mouse_pos() const;
  92. virtual Point2 get_mouse_speed() const;
  93. void parse_input_event(const InputEvent& p_event);
  94. void set_accelerometer(const Vector3& p_accel);
  95. void set_joy_axis(int p_device,int p_axis,float p_value);
  96. void set_main_loop(MainLoop *main_loop);
  97. void set_mouse_pos(const Point2& p_posf);
  98. void action_press(const StringName& p_action);
  99. void action_release(const StringName& p_action);
  100. void iteration(float p_step);
  101. InputDefault();
  102. };
  103. #endif // INPUT_H