input.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* input.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. Input *Input::singleton = NULL;
  35. Input *Input::get_singleton() {
  36. return singleton;
  37. }
  38. void Input::set_mouse_mode(MouseMode p_mode) {
  39. ERR_FAIL_INDEX((int)p_mode, 4);
  40. OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
  41. }
  42. Input::MouseMode Input::get_mouse_mode() const {
  43. return (MouseMode)OS::get_singleton()->get_mouse_mode();
  44. }
  45. void Input::_bind_methods() {
  46. ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
  47. ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
  48. ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
  49. ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed);
  50. ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action"), &Input::is_action_just_pressed);
  51. ClassDB::bind_method(D_METHOD("is_action_just_released", "action"), &Input::is_action_just_released);
  52. ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &Input::get_action_strength);
  53. ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
  54. ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
  55. ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed);
  56. ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known);
  57. ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
  58. ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name);
  59. ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid);
  60. ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads);
  61. ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
  62. ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
  63. ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
  64. ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
  65. ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
  66. ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
  67. ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
  68. ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
  69. ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity);
  70. ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer);
  71. ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer);
  72. ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope);
  73. //ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want
  74. ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
  75. ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
  76. ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
  77. ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
  78. ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position);
  79. ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f));
  80. ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release);
  81. ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW));
  82. ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
  83. ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event);
  84. BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
  85. BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
  86. BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED);
  87. BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED);
  88. BIND_ENUM_CONSTANT(CURSOR_ARROW);
  89. BIND_ENUM_CONSTANT(CURSOR_IBEAM);
  90. BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
  91. BIND_ENUM_CONSTANT(CURSOR_CROSS);
  92. BIND_ENUM_CONSTANT(CURSOR_WAIT);
  93. BIND_ENUM_CONSTANT(CURSOR_BUSY);
  94. BIND_ENUM_CONSTANT(CURSOR_DRAG);
  95. BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
  96. BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
  97. BIND_ENUM_CONSTANT(CURSOR_VSIZE);
  98. BIND_ENUM_CONSTANT(CURSOR_HSIZE);
  99. BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
  100. BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
  101. BIND_ENUM_CONSTANT(CURSOR_MOVE);
  102. BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
  103. BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
  104. BIND_ENUM_CONSTANT(CURSOR_HELP);
  105. ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
  106. }
  107. void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  108. #ifdef TOOLS_ENABLED
  109. String pf = p_function;
  110. 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")) {
  111. List<PropertyInfo> pinfo;
  112. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  113. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  114. const PropertyInfo &pi = E->get();
  115. if (!pi.name.begins_with("input/"))
  116. continue;
  117. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  118. r_options->push_back("\"" + name + "\"");
  119. }
  120. }
  121. #endif
  122. }
  123. Input::Input() {
  124. singleton = this;
  125. }
  126. //////////////////////////////////////////////////////////