input.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************************/
  2. /* input.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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. #include "input.h"
  30. #include "input_map.h"
  31. #include "os/os.h"
  32. #include "globals.h"
  33. Input *Input::singleton=NULL;
  34. Input *Input::get_singleton() {
  35. return singleton;
  36. }
  37. void Input::set_mouse_mode(MouseMode p_mode) {
  38. ERR_FAIL_INDEX(p_mode,3);
  39. OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
  40. }
  41. Input::MouseMode Input::get_mouse_mode() const {
  42. return (MouseMode)OS::get_singleton()->get_mouse_mode();
  43. }
  44. void Input::_bind_methods() {
  45. ClassDB::bind_method(_MD("is_key_pressed","scancode"),&Input::is_key_pressed);
  46. ClassDB::bind_method(_MD("is_mouse_button_pressed","button"),&Input::is_mouse_button_pressed);
  47. ClassDB::bind_method(_MD("is_joy_button_pressed","device","button"),&Input::is_joy_button_pressed);
  48. ClassDB::bind_method(_MD("is_action_pressed","action"),&Input::is_action_pressed);
  49. ClassDB::bind_method(_MD("is_action_just_pressed","action"),&Input::is_action_just_pressed);
  50. ClassDB::bind_method(_MD("is_action_just_released","action"),&Input::is_action_just_released);
  51. ClassDB::bind_method(_MD("add_joy_mapping","mapping", "update_existing"),&Input::add_joy_mapping, DEFVAL(false));
  52. ClassDB::bind_method(_MD("remove_joy_mapping","guid"),&Input::remove_joy_mapping);
  53. ClassDB::bind_method(_MD("is_joy_known","device"),&Input::is_joy_known);
  54. ClassDB::bind_method(_MD("get_joy_axis","device","axis"),&Input::get_joy_axis);
  55. ClassDB::bind_method(_MD("get_joy_name","device"),&Input::get_joy_name);
  56. ClassDB::bind_method(_MD("get_joy_guid","device"),&Input::get_joy_guid);
  57. ClassDB::bind_method(_MD("get_connected_joysticks"),&Input::get_connected_joysticks);
  58. ClassDB::bind_method(_MD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength);
  59. ClassDB::bind_method(_MD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration);
  60. ClassDB::bind_method(_MD("get_joy_button_string", "button_index"), &Input::get_joy_button_string);
  61. ClassDB::bind_method(_MD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string);
  62. ClassDB::bind_method(_MD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string);
  63. ClassDB::bind_method(_MD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string);
  64. ClassDB::bind_method(_MD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0));
  65. ClassDB::bind_method(_MD("stop_joy_vibration", "device"), &Input::stop_joy_vibration);
  66. ClassDB::bind_method(_MD("get_accelerometer"),&Input::get_accelerometer);
  67. ClassDB::bind_method(_MD("get_magnetometer"),&Input::get_magnetometer);
  68. ClassDB::bind_method(_MD("get_gyroscope"),&Input::get_gyroscope);
  69. //ClassDB::bind_method(_MD("get_mouse_pos"),&Input::get_mouse_pos); - this is not the function you want
  70. ClassDB::bind_method(_MD("get_mouse_speed"),&Input::get_mouse_speed);
  71. ClassDB::bind_method(_MD("get_mouse_button_mask"),&Input::get_mouse_button_mask);
  72. ClassDB::bind_method(_MD("set_mouse_mode","mode"),&Input::set_mouse_mode);
  73. ClassDB::bind_method(_MD("get_mouse_mode"),&Input::get_mouse_mode);
  74. ClassDB::bind_method(_MD("warp_mouse_pos","to"),&Input::warp_mouse_pos);
  75. ClassDB::bind_method(_MD("action_press","action"),&Input::action_press);
  76. ClassDB::bind_method(_MD("action_release","action"),&Input::action_release);
  77. ClassDB::bind_method(_MD("set_custom_mouse_cursor","image:Texture","hotspot"),&Input::set_custom_mouse_cursor,DEFVAL(Vector2()));
  78. BIND_CONSTANT( MOUSE_MODE_VISIBLE );
  79. BIND_CONSTANT( MOUSE_MODE_HIDDEN );
  80. BIND_CONSTANT( MOUSE_MODE_CAPTURED );
  81. ADD_SIGNAL( MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "connected")) );
  82. }
  83. void Input::get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const {
  84. #ifdef TOOLS_ENABLED
  85. String pf=p_function;
  86. if (p_idx==0 && (pf=="is_action_pressed" || pf=="action_press" || pf=="action_release" || pf=="is_action_just_pressed" || pf=="is_action_just_released")) {
  87. List<PropertyInfo> pinfo;
  88. GlobalConfig::get_singleton()->get_property_list(&pinfo);
  89. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  90. const PropertyInfo &pi=E->get();
  91. if (!pi.name.begins_with("input/"))
  92. continue;
  93. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  94. r_options->push_back("\""+name+"\"");
  95. }
  96. }
  97. #endif
  98. }
  99. Input::Input() {
  100. singleton=this;
  101. }
  102. //////////////////////////////////////////////////////////