2
0

input.cpp 8.4 KB

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