input.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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"),&Input::action_press);
  59. ObjectTypeDB::bind_method(_MD("action_release"),&Input::action_release);
  60. BIND_CONSTANT( MOUSE_MODE_VISIBLE );
  61. BIND_CONSTANT( MOUSE_MODE_HIDDEN );
  62. BIND_CONSTANT( MOUSE_MODE_CAPTURED );
  63. ADD_SIGNAL( MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "connected")) );
  64. }
  65. void Input::get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const {
  66. #ifdef TOOLS_ENABLED
  67. String pf=p_function;
  68. if (p_idx==0 && (pf=="is_action_pressed" || pf=="action_press" || pf=="action_release")) {
  69. List<PropertyInfo> pinfo;
  70. Globals::get_singleton()->get_property_list(&pinfo);
  71. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  72. const PropertyInfo &pi=E->get();
  73. if (!pi.name.begins_with("input/"))
  74. continue;
  75. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  76. r_options->push_back("\""+name+"\"");
  77. }
  78. }
  79. #endif
  80. }
  81. Input::Input() {
  82. singleton=this;
  83. }
  84. //////////////////////////////////////////////////////////
  85. void InputDefault::SpeedTrack::update(const Vector2& p_delta_p) {
  86. uint64_t tick = OS::get_singleton()->get_ticks_usec();
  87. uint32_t tdiff = tick-last_tick;
  88. float delta_t = tdiff / 1000000.0;
  89. last_tick=tick;
  90. accum+=p_delta_p;
  91. accum_t+=delta_t;
  92. if (accum_t>max_ref_frame*10)
  93. accum_t=max_ref_frame*10;
  94. while( accum_t>=min_ref_frame ) {
  95. float slice_t = min_ref_frame / accum_t;
  96. Vector2 slice = accum*slice_t;
  97. accum=accum-slice;
  98. accum_t-=min_ref_frame;
  99. speed=(slice/min_ref_frame).linear_interpolate(speed,min_ref_frame/max_ref_frame);
  100. }
  101. }
  102. void InputDefault::SpeedTrack::reset() {
  103. last_tick = OS::get_singleton()->get_ticks_usec();
  104. speed=Vector2();
  105. accum_t=0;
  106. }
  107. InputDefault::SpeedTrack::SpeedTrack() {
  108. min_ref_frame=0.1;
  109. max_ref_frame=0.3;
  110. reset();
  111. }
  112. bool InputDefault::is_key_pressed(int p_scancode) {
  113. _THREAD_SAFE_METHOD_
  114. return keys_pressed.has(p_scancode);
  115. }
  116. bool InputDefault::is_mouse_button_pressed(int p_button) {
  117. _THREAD_SAFE_METHOD_
  118. return (mouse_button_mask&(1<<p_button))!=0;
  119. }
  120. static int _combine_device(int p_value,int p_device) {
  121. return p_value|(p_device<<20);
  122. }
  123. bool InputDefault::is_joy_button_pressed(int p_device, int p_button) {
  124. _THREAD_SAFE_METHOD_
  125. return joy_buttons_pressed.has(_combine_device(p_button,p_device));
  126. }
  127. bool InputDefault::is_action_pressed(const StringName& p_action) {
  128. if (custom_action_press.has(p_action))
  129. return true; //simpler
  130. const List<InputEvent> *alist = InputMap::get_singleton()->get_action_list(p_action);
  131. if (!alist)
  132. return NULL;
  133. for (const List<InputEvent>::Element *E=alist->front();E;E=E->next()) {
  134. int device=E->get().device;
  135. switch(E->get().type) {
  136. case InputEvent::KEY: {
  137. const InputEventKey &iek=E->get().key;
  138. if ((keys_pressed.has(iek.scancode)))
  139. return true;
  140. } break;
  141. case InputEvent::MOUSE_BUTTON: {
  142. const InputEventMouseButton &iemb=E->get().mouse_button;
  143. if(mouse_button_mask&(1<<iemb.button_index))
  144. return true;
  145. } break;
  146. case InputEvent::JOYSTICK_BUTTON: {
  147. const InputEventJoystickButton &iejb=E->get().joy_button;
  148. int c = _combine_device(iejb.button_index,device);
  149. if (joy_buttons_pressed.has(c))
  150. return true;
  151. } break;
  152. }
  153. }
  154. return false;
  155. }
  156. float InputDefault::get_joy_axis(int p_device,int p_axis) {
  157. _THREAD_SAFE_METHOD_
  158. int c = _combine_device(p_axis,p_device);
  159. if (joy_axis.has(c)) {
  160. return joy_axis[c];
  161. } else {
  162. return 0;
  163. }
  164. }
  165. String InputDefault::get_joy_name(int p_idx) {
  166. _THREAD_SAFE_METHOD_
  167. return joy_names[p_idx];
  168. };
  169. void InputDefault::joy_connection_changed(int p_idx, bool p_connected, String p_name) {
  170. _THREAD_SAFE_METHOD_
  171. joy_names[p_idx] = p_connected ? p_name : "";
  172. emit_signal("joy_connection_changed", p_idx, p_connected);
  173. };
  174. Vector3 InputDefault::get_accelerometer() {
  175. _THREAD_SAFE_METHOD_
  176. return accelerometer;
  177. }
  178. void InputDefault::parse_input_event(const InputEvent& p_event) {
  179. _THREAD_SAFE_METHOD_
  180. switch(p_event.type) {
  181. case InputEvent::KEY: {
  182. if (p_event.key.echo)
  183. break;
  184. if (p_event.key.scancode==0)
  185. break;
  186. // print_line(p_event);
  187. if (p_event.key.pressed)
  188. keys_pressed.insert(p_event.key.scancode);
  189. else
  190. keys_pressed.erase(p_event.key.scancode);
  191. } break;
  192. case InputEvent::MOUSE_BUTTON: {
  193. if (p_event.mouse_button.doubleclick)
  194. break;
  195. if (p_event.mouse_button.pressed)
  196. mouse_button_mask|=(1<<p_event.mouse_button.button_index);
  197. else
  198. mouse_button_mask&=~(1<<p_event.mouse_button.button_index);
  199. } break;
  200. case InputEvent::JOYSTICK_BUTTON: {
  201. int c = _combine_device(p_event.joy_button.button_index,p_event.device);
  202. if (p_event.joy_button.pressed)
  203. joy_buttons_pressed.insert(c);
  204. else
  205. joy_buttons_pressed.erase(c);
  206. } break;
  207. case InputEvent::JOYSTICK_MOTION: {
  208. set_joy_axis(p_event.device, p_event.joy_motion.axis, p_event.joy_motion.axis_value);
  209. } break;
  210. }
  211. if (main_loop)
  212. main_loop->input_event(p_event);
  213. }
  214. void InputDefault::set_joy_axis(int p_device,int p_axis,float p_value) {
  215. _THREAD_SAFE_METHOD_
  216. int c = _combine_device(p_axis,p_device);
  217. joy_axis[c]=p_value;
  218. }
  219. void InputDefault::set_accelerometer(const Vector3& p_accel) {
  220. _THREAD_SAFE_METHOD_
  221. accelerometer=p_accel;
  222. }
  223. void InputDefault::set_main_loop(MainLoop *p_main_loop) {
  224. main_loop=p_main_loop;
  225. }
  226. void InputDefault::set_mouse_pos(const Point2& p_posf) {
  227. mouse_speed_track.update(p_posf-mouse_pos);
  228. mouse_pos=p_posf;
  229. }
  230. Point2 InputDefault::get_mouse_pos() const {
  231. return mouse_pos;
  232. }
  233. Point2 InputDefault::get_mouse_speed() const {
  234. return mouse_speed_track.speed;
  235. }
  236. int InputDefault::get_mouse_button_mask() const {
  237. return OS::get_singleton()->get_mouse_button_state();
  238. }
  239. void InputDefault::warp_mouse_pos(const Vector2& p_to) {
  240. OS::get_singleton()->warp_mouse_pos(p_to);
  241. }
  242. void InputDefault::iteration(float p_step) {
  243. }
  244. void InputDefault::action_press(const StringName& p_action) {
  245. if (custom_action_press.has(p_action)) {
  246. custom_action_press[p_action]++;
  247. } else {
  248. custom_action_press[p_action]=1;
  249. }
  250. }
  251. void InputDefault::action_release(const StringName& p_action){
  252. ERR_FAIL_COND(!custom_action_press.has(p_action));
  253. custom_action_press[p_action]--;
  254. if (custom_action_press[p_action]==0) {
  255. custom_action_press.erase(p_action);
  256. }
  257. }
  258. InputDefault::InputDefault() {
  259. mouse_button_mask=0;
  260. main_loop=NULL;
  261. }