input.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* input.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 String get_joy_name(int p_idx)=0;
  54. virtual void joy_connection_changed(int p_idx, bool p_connected, String p_name)=0;
  55. virtual Point2 get_mouse_pos() const=0;
  56. virtual Point2 get_mouse_speed() const=0;
  57. virtual int get_mouse_button_mask() const=0;
  58. virtual void warp_mouse_pos(const Vector2& p_to)=0;
  59. virtual Vector3 get_accelerometer()=0;
  60. virtual void action_press(const StringName& p_action)=0;
  61. virtual void action_release(const StringName& p_action)=0;
  62. void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
  63. Input();
  64. };
  65. VARIANT_ENUM_CAST(Input::MouseMode);
  66. class InputDefault : public Input {
  67. OBJ_TYPE( InputDefault, Input );
  68. _THREAD_SAFE_CLASS_
  69. int mouse_button_mask;
  70. Set<int> keys_pressed;
  71. Set<int> joy_buttons_pressed;
  72. Map<int,float> joy_axis;
  73. Map<StringName,int> custom_action_press;
  74. Map<int, String> joy_names;
  75. Vector3 accelerometer;
  76. Vector2 mouse_pos;
  77. MainLoop *main_loop;
  78. struct SpeedTrack {
  79. uint64_t last_tick;
  80. Vector2 speed;
  81. Vector2 accum;
  82. float accum_t;
  83. float min_ref_frame;
  84. float max_ref_frame;
  85. void update(const Vector2& p_delta_p);
  86. void reset();
  87. SpeedTrack();
  88. };
  89. SpeedTrack mouse_speed_track;
  90. public:
  91. virtual bool is_key_pressed(int p_scancode);
  92. virtual bool is_mouse_button_pressed(int p_button);
  93. virtual bool is_joy_button_pressed(int p_device, int p_button);
  94. virtual bool is_action_pressed(const StringName& p_action);
  95. virtual float get_joy_axis(int p_device,int p_axis);
  96. String get_joy_name(int p_idx);
  97. void joy_connection_changed(int p_idx, bool p_connected, String p_name);
  98. virtual Vector3 get_accelerometer();
  99. virtual Point2 get_mouse_pos() const;
  100. virtual Point2 get_mouse_speed() const;
  101. virtual int get_mouse_button_mask() const;
  102. virtual void warp_mouse_pos(const Vector2& p_to);
  103. void parse_input_event(const InputEvent& p_event);
  104. void set_accelerometer(const Vector3& p_accel);
  105. void set_joy_axis(int p_device,int p_axis,float p_value);
  106. void set_main_loop(MainLoop *main_loop);
  107. void set_mouse_pos(const Point2& p_posf);
  108. void action_press(const StringName& p_action);
  109. void action_release(const StringName& p_action);
  110. void iteration(float p_step);
  111. InputDefault();
  112. };
  113. #endif // INPUT_H