input.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* input.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "input.h"
  31. #include "core/input_map.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_settings.h"
  36. #endif
  37. Input *Input::singleton = NULL;
  38. Input *Input::get_singleton() {
  39. return singleton;
  40. }
  41. void Input::set_mouse_mode(MouseMode p_mode) {
  42. ERR_FAIL_INDEX((int)p_mode, 4);
  43. OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
  44. }
  45. Input::MouseMode Input::get_mouse_mode() const {
  46. return (MouseMode)OS::get_singleton()->get_mouse_mode();
  47. }
  48. void Input::_bind_methods() {
  49. ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
  50. ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
  51. ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
  52. ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed);
  53. ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action"), &Input::is_action_just_pressed);
  54. ClassDB::bind_method(D_METHOD("is_action_just_released", "action"), &Input::is_action_just_released);
  55. ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &Input::get_action_strength);
  56. ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
  57. ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
  58. ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
  59. ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
  60. ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
  61. ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
  62. ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
  63. ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
  64. ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
  65. ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
  66. ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
  67. ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
  68. ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
  69. ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
  70. ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
  71. ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
  72. ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500));
  73. ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity);
  74. ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer);
  75. ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer);
  76. ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope);
  77. //ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
  78. ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
  79. ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
  80. ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
  81. ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
  82. ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
  83. ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
  84. ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
  85. ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
  86. ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape);
  87. ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
  88. ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
  89. ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input);
  90. BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
  91. BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
  92. BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED);
  93. BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED);
  94. BIND_ENUM_CONSTANT(CURSOR_ARROW);
  95. BIND_ENUM_CONSTANT(CURSOR_IBEAM);
  96. BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
  97. BIND_ENUM_CONSTANT(CURSOR_CROSS);
  98. BIND_ENUM_CONSTANT(CURSOR_WAIT);
  99. BIND_ENUM_CONSTANT(CURSOR_BUSY);
  100. BIND_ENUM_CONSTANT(CURSOR_DRAG);
  101. BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
  102. BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
  103. BIND_ENUM_CONSTANT(CURSOR_VSIZE);
  104. BIND_ENUM_CONSTANT(CURSOR_HSIZE);
  105. BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
  106. BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
  107. BIND_ENUM_CONSTANT(CURSOR_MOVE);
  108. BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
  109. BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
  110. BIND_ENUM_CONSTANT(CURSOR_HELP);
  111. ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
  112. }
  113. void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  114. #ifdef TOOLS_ENABLED
  115. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
  116. String pf = p_function;
  117. if (p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" || pf == "is_action_just_pressed" || pf == "is_action_just_released" || pf == "get_action_strength")) {
  118. List<PropertyInfo> pinfo;
  119. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  120. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  121. const PropertyInfo &pi = E->get();
  122. if (!pi.name.begins_with("input/"))
  123. continue;
  124. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  125. r_options->push_back(quote_style + name + quote_style);
  126. }
  127. }
  128. #endif
  129. }
  130. Input::Input() {
  131. singleton = this;
  132. }
  133. //////////////////////////////////////////////////////////