input.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*************************************************************************/
  2. /* input.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. Input *Input::singleton=NULL;
  33. Input *Input::get_singleton() {
  34. return singleton;
  35. }
  36. void Input::set_mouse_mode(MouseMode p_mode) {
  37. ERR_FAIL_INDEX(p_mode,3);
  38. OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
  39. }
  40. Input::MouseMode Input::get_mouse_mode() const {
  41. return (MouseMode)OS::get_singleton()->get_mouse_mode();
  42. }
  43. void Input::_bind_methods() {
  44. ObjectTypeDB::bind_method(_MD("is_key_pressed","scancode"),&Input::is_key_pressed);
  45. ObjectTypeDB::bind_method(_MD("is_mouse_button_pressed","button"),&Input::is_mouse_button_pressed);
  46. ObjectTypeDB::bind_method(_MD("is_joy_button_pressed","device","button"),&Input::is_joy_button_pressed);
  47. ObjectTypeDB::bind_method(_MD("is_action_pressed","action"),&Input::is_action_pressed);
  48. ObjectTypeDB::bind_method(_MD("get_joy_axis","device","axis"),&Input::get_joy_axis);
  49. ObjectTypeDB::bind_method(_MD("get_accelerometer"),&Input::get_accelerometer);
  50. ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&Input::get_mouse_pos);
  51. ObjectTypeDB::bind_method(_MD("get_mouse_speed"),&Input::get_mouse_speed);
  52. ObjectTypeDB::bind_method(_MD("set_mouse_mode","mode"),&Input::set_mouse_mode);
  53. ObjectTypeDB::bind_method(_MD("get_mouse_mode"),&Input::get_mouse_mode);
  54. BIND_CONSTANT( MOUSE_MODE_VISIBLE );
  55. BIND_CONSTANT( MOUSE_MODE_HIDDEN );
  56. BIND_CONSTANT( MOUSE_MODE_CAPTURED );
  57. }
  58. Input::Input() {
  59. singleton=this;
  60. }
  61. //////////////////////////////////////////////////////////
  62. void InputDefault::SpeedTrack::update(const Vector2& p_delta_p) {
  63. uint64_t tick = OS::get_singleton()->get_ticks_usec();
  64. uint32_t tdiff = tick-last_tick;
  65. float delta_t = tdiff / 1000000.0;
  66. last_tick=tick;
  67. accum+=p_delta_p;
  68. accum_t+=delta_t;
  69. if (accum_t>max_ref_frame*10)
  70. accum_t=max_ref_frame*10;
  71. while( accum_t>=min_ref_frame ) {
  72. float slice_t = min_ref_frame / accum_t;
  73. Vector2 slice = accum*slice_t;
  74. accum=accum-slice;
  75. accum_t-=min_ref_frame;
  76. speed=(slice/min_ref_frame).linear_interpolate(speed,min_ref_frame/max_ref_frame);
  77. }
  78. }
  79. void InputDefault::SpeedTrack::reset() {
  80. last_tick = OS::get_singleton()->get_ticks_usec();
  81. speed=Vector2();
  82. accum_t=0;
  83. }
  84. InputDefault::SpeedTrack::SpeedTrack() {
  85. min_ref_frame=0.1;
  86. max_ref_frame=0.3;
  87. reset();
  88. }
  89. bool InputDefault::is_key_pressed(int p_scancode) {
  90. _THREAD_SAFE_METHOD_
  91. return keys_pressed.has(p_scancode);
  92. }
  93. bool InputDefault::is_mouse_button_pressed(int p_button) {
  94. _THREAD_SAFE_METHOD_
  95. return (mouse_button_mask&(1<<p_button))!=0;
  96. }
  97. static int _combine_device(int p_value,int p_device) {
  98. return p_value|(p_device<<20);
  99. }
  100. bool InputDefault::is_joy_button_pressed(int p_device, int p_button) {
  101. _THREAD_SAFE_METHOD_
  102. return joy_buttons_pressed.has(_combine_device(p_button,p_device));
  103. }
  104. bool InputDefault::is_action_pressed(const StringName& p_action) {
  105. if (custom_action_press.has(p_action))
  106. return true; //simpler
  107. const List<InputEvent> *alist = InputMap::get_singleton()->get_action_list(p_action);
  108. if (!alist)
  109. return NULL;
  110. for (const List<InputEvent>::Element *E=alist->front();E;E=E->next()) {
  111. int device=E->get().device;
  112. switch(E->get().type) {
  113. case InputEvent::KEY: {
  114. const InputEventKey &iek=E->get().key;
  115. if ((keys_pressed.has(iek.scancode)))
  116. return true;
  117. } break;
  118. case InputEvent::MOUSE_BUTTON: {
  119. const InputEventMouseButton &iemb=E->get().mouse_button;
  120. if(mouse_button_mask&(1<<iemb.button_index))
  121. return true;
  122. } break;
  123. case InputEvent::JOYSTICK_BUTTON: {
  124. const InputEventJoystickButton &iejb=E->get().joy_button;
  125. int c = _combine_device(iejb.button_index,device);
  126. if (joy_buttons_pressed.has(c))
  127. return true;
  128. } break;
  129. }
  130. }
  131. return false;
  132. }
  133. float InputDefault::get_joy_axis(int p_device,int p_axis) {
  134. _THREAD_SAFE_METHOD_
  135. int c = _combine_device(p_axis,p_device);
  136. if (joy_axis.has(c)) {
  137. return joy_axis[c];
  138. } else {
  139. return 0;
  140. }
  141. }
  142. Vector3 InputDefault::get_accelerometer() {
  143. _THREAD_SAFE_METHOD_
  144. return accelerometer;
  145. }
  146. void InputDefault::parse_input_event(const InputEvent& p_event) {
  147. _THREAD_SAFE_METHOD_
  148. switch(p_event.type) {
  149. case InputEvent::KEY: {
  150. if (p_event.key.echo)
  151. break;
  152. if (p_event.key.scancode==0)
  153. break;
  154. if (p_event.key.pressed)
  155. keys_pressed.insert(p_event.key.scancode);
  156. else
  157. keys_pressed.erase(p_event.key.scancode);
  158. } break;
  159. case InputEvent::MOUSE_BUTTON: {
  160. if (p_event.mouse_button.doubleclick)
  161. break;
  162. if (p_event.mouse_button.pressed)
  163. mouse_button_mask|=(1<<p_event.mouse_button.button_index);
  164. else
  165. mouse_button_mask&=~(1<<p_event.mouse_button.button_index);
  166. } break;
  167. case InputEvent::JOYSTICK_BUTTON: {
  168. int c = _combine_device(p_event.joy_button.button_index,p_event.device);
  169. if (p_event.joy_button.pressed)
  170. joy_buttons_pressed.insert(c);
  171. else
  172. joy_buttons_pressed.erase(c);
  173. } break;
  174. case InputEvent::JOYSTICK_MOTION: {
  175. set_joy_axis(p_event.device, p_event.joy_motion.axis, p_event.joy_motion.axis_value);
  176. } break;
  177. }
  178. if (main_loop)
  179. main_loop->input_event(p_event);
  180. }
  181. void InputDefault::set_joy_axis(int p_device,int p_axis,float p_value) {
  182. _THREAD_SAFE_METHOD_
  183. int c = _combine_device(p_axis,p_device);
  184. joy_axis[c]=p_value;
  185. }
  186. void InputDefault::set_accelerometer(const Vector3& p_accel) {
  187. _THREAD_SAFE_METHOD_
  188. accelerometer=p_accel;
  189. }
  190. void InputDefault::set_main_loop(MainLoop *p_main_loop) {
  191. main_loop=p_main_loop;
  192. }
  193. void InputDefault::set_mouse_pos(const Point2& p_posf) {
  194. mouse_speed_track.update(p_posf-mouse_pos);
  195. mouse_pos=p_posf;
  196. }
  197. Point2 InputDefault::get_mouse_pos() const {
  198. return mouse_pos;
  199. }
  200. Point2 InputDefault::get_mouse_speed() const {
  201. return mouse_speed_track.speed;
  202. }
  203. void InputDefault::iteration(float p_step) {
  204. }
  205. void InputDefault::action_press(const StringName& p_action) {
  206. if (custom_action_press.has(p_action)) {
  207. custom_action_press[p_action]++;
  208. } else {
  209. custom_action_press[p_action]=1;
  210. }
  211. }
  212. void InputDefault::action_release(const StringName& p_action){
  213. ERR_FAIL_COND(!custom_action_press.has(p_action));
  214. custom_action_press[p_action]--;
  215. if (custom_action_press[p_action]==0) {
  216. custom_action_press.erase(p_action);
  217. }
  218. }
  219. InputDefault::InputDefault() {
  220. mouse_button_mask=0;
  221. main_loop=NULL;
  222. }