input.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "globals.h"
  32. #include "input_map.h"
  33. #include "os/os.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(p_mode, 3);
  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. ObjectTypeDB::bind_method(_MD("is_key_pressed", "scancode"), &Input::is_key_pressed);
  47. ObjectTypeDB::bind_method(_MD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
  48. ObjectTypeDB::bind_method(_MD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
  49. ObjectTypeDB::bind_method(_MD("is_action_pressed", "action"), &Input::is_action_pressed);
  50. ObjectTypeDB::bind_method(_MD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false));
  51. ObjectTypeDB::bind_method(_MD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping);
  52. ObjectTypeDB::bind_method(_MD("is_joy_known", "device"), &Input::is_joy_known);
  53. ObjectTypeDB::bind_method(_MD("get_joy_axis", "device", "axis"), &Input::get_joy_axis);
  54. ObjectTypeDB::bind_method(_MD("get_joy_name", "device"), &Input::get_joy_name);
  55. ObjectTypeDB::bind_method(_MD("get_joy_guid", "device"), &Input::get_joy_guid);
  56. ObjectTypeDB::bind_method(_MD("get_connected_joysticks"), &Input::get_connected_joysticks);
  57. ObjectTypeDB::bind_method(_MD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
  58. ObjectTypeDB::bind_method(_MD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
  59. ObjectTypeDB::bind_method(_MD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
  60. ObjectTypeDB::bind_method(_MD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
  61. ObjectTypeDB::bind_method(_MD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
  62. ObjectTypeDB::bind_method(_MD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
  63. ObjectTypeDB::bind_method(_MD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
  64. ObjectTypeDB::bind_method(_MD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
  65. ObjectTypeDB::bind_method(_MD("get_gravity"), &Input::get_gravity);
  66. ObjectTypeDB::bind_method(_MD("get_accelerometer"), &Input::get_accelerometer);
  67. ObjectTypeDB::bind_method(_MD("get_magnetometer"), &Input::get_magnetometer);
  68. ObjectTypeDB::bind_method(_MD("get_gyroscope"), &Input::get_gyroscope);
  69. //ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&Input::get_mouse_pos); - this is not the function you want
  70. ObjectTypeDB::bind_method(_MD("get_mouse_speed"), &Input::get_mouse_speed);
  71. ObjectTypeDB::bind_method(_MD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
  72. ObjectTypeDB::bind_method(_MD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
  73. ObjectTypeDB::bind_method(_MD("get_mouse_mode"), &Input::get_mouse_mode);
  74. ObjectTypeDB::bind_method(_MD("warp_mouse_pos", "to"), &Input::warp_mouse_pos);
  75. ObjectTypeDB::bind_method(_MD("action_press", "action"), &Input::action_press);
  76. ObjectTypeDB::bind_method(_MD("action_release", "action"), &Input::action_release);
  77. ObjectTypeDB::bind_method(_MD("set_custom_mouse_cursor", "image:Texture", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2()));
  78. ObjectTypeDB::bind_method(_MD("parse_input_event", "event"), &Input::parse_input_event);
  79. BIND_CONSTANT(MOUSE_MODE_VISIBLE);
  80. BIND_CONSTANT(MOUSE_MODE_HIDDEN);
  81. BIND_CONSTANT(MOUSE_MODE_CAPTURED);
  82. // Those constants are used to change the mouse texture for given cursor shapes
  83. BIND_CONSTANT(CURSOR_ARROW);
  84. BIND_CONSTANT(CURSOR_IBEAM);
  85. BIND_CONSTANT(CURSOR_POINTING_HAND);
  86. BIND_CONSTANT(CURSOR_CROSS);
  87. BIND_CONSTANT(CURSOR_WAIT);
  88. BIND_CONSTANT(CURSOR_BUSY);
  89. BIND_CONSTANT(CURSOR_DRAG);
  90. BIND_CONSTANT(CURSOR_CAN_DROP);
  91. BIND_CONSTANT(CURSOR_FORBIDDEN);
  92. BIND_CONSTANT(CURSOR_VSIZE);
  93. BIND_CONSTANT(CURSOR_HSIZE);
  94. BIND_CONSTANT(CURSOR_BDIAGSIZE);
  95. BIND_CONSTANT(CURSOR_FDIAGSIZE);
  96. BIND_CONSTANT(CURSOR_MOVE);
  97. BIND_CONSTANT(CURSOR_VSPLIT);
  98. BIND_CONSTANT(CURSOR_HSPLIT);
  99. BIND_CONSTANT(CURSOR_HELP);
  100. ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "connected")));
  101. }
  102. void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  103. #ifdef TOOLS_ENABLED
  104. String pf = p_function;
  105. if (p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release")) {
  106. List<PropertyInfo> pinfo;
  107. Globals::get_singleton()->get_property_list(&pinfo);
  108. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  109. const PropertyInfo &pi = E->get();
  110. if (!pi.name.begins_with("input/"))
  111. continue;
  112. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  113. r_options->push_back("\"" + name + "\"");
  114. }
  115. }
  116. #endif
  117. }
  118. Input::Input() {
  119. singleton = this;
  120. }
  121. //////////////////////////////////////////////////////////