input.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************/
  2. /* input.cpp */
  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. #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. ObjectTypeDB::bind_method(_MD("is_key_pressed","scancode"),&Input::is_key_pressed);
  46. ObjectTypeDB::bind_method(_MD("is_mouse_button_pressed","button"),&Input::is_mouse_button_pressed);
  47. ObjectTypeDB::bind_method(_MD("is_joy_button_pressed","device","button"),&Input::is_joy_button_pressed);
  48. ObjectTypeDB::bind_method(_MD("is_action_pressed","action"),&Input::is_action_pressed);
  49. ObjectTypeDB::bind_method(_MD("get_joy_axis","device","axis"),&Input::get_joy_axis);
  50. ObjectTypeDB::bind_method(_MD("get_joy_name","device"),&Input::get_joy_name);
  51. ObjectTypeDB::bind_method(_MD("get_accelerometer"),&Input::get_accelerometer);
  52. //ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&Input::get_mouse_pos); - this is not the function you want
  53. ObjectTypeDB::bind_method(_MD("get_mouse_speed"),&Input::get_mouse_speed);
  54. ObjectTypeDB::bind_method(_MD("get_mouse_button_mask"),&Input::get_mouse_button_mask);
  55. ObjectTypeDB::bind_method(_MD("set_mouse_mode","mode"),&Input::set_mouse_mode);
  56. ObjectTypeDB::bind_method(_MD("get_mouse_mode"),&Input::get_mouse_mode);
  57. ObjectTypeDB::bind_method(_MD("warp_mouse_pos","to"),&Input::warp_mouse_pos);
  58. ObjectTypeDB::bind_method(_MD("action_press","action"),&Input::action_press);
  59. ObjectTypeDB::bind_method(_MD("action_release","action"),&Input::action_release);
  60. ObjectTypeDB::bind_method(_MD("set_custom_mouse_cursor","image:Texture","hotspot"),&Input::set_custom_mouse_cursor,DEFVAL(Vector2()));
  61. BIND_CONSTANT( MOUSE_MODE_VISIBLE );
  62. BIND_CONSTANT( MOUSE_MODE_HIDDEN );
  63. BIND_CONSTANT( MOUSE_MODE_CAPTURED );
  64. ADD_SIGNAL( MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "connected")) );
  65. }
  66. void Input::get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const {
  67. #ifdef TOOLS_ENABLED
  68. String pf=p_function;
  69. if (p_idx==0 && (pf=="is_action_pressed" || pf=="action_press" || pf=="action_release")) {
  70. List<PropertyInfo> pinfo;
  71. Globals::get_singleton()->get_property_list(&pinfo);
  72. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  73. const PropertyInfo &pi=E->get();
  74. if (!pi.name.begins_with("input/"))
  75. continue;
  76. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  77. r_options->push_back("\""+name+"\"");
  78. }
  79. }
  80. #endif
  81. }
  82. Input::Input() {
  83. singleton=this;
  84. }
  85. //////////////////////////////////////////////////////////