InputEvent.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #ifndef INPUTEVENT_H
  2. #define INPUTEVENT_H
  3. #include <cstdint>
  4. #include <memory.h>
  5. #include "String.h"
  6. namespace godot {
  7. enum {
  8. BUTTON_LEFT=1,
  9. BUTTON_RIGHT=2,
  10. BUTTON_MIDDLE=3,
  11. BUTTON_WHEEL_UP=4,
  12. BUTTON_WHEEL_DOWN=5,
  13. BUTTON_WHEEL_LEFT=6,
  14. BUTTON_WHEEL_RIGHT=7,
  15. BUTTON_MASK_LEFT=(1<<(BUTTON_LEFT-1)),
  16. BUTTON_MASK_RIGHT=(1<<(BUTTON_RIGHT-1)),
  17. BUTTON_MASK_MIDDLE=(1<<(BUTTON_MIDDLE-1)),
  18. };
  19. enum {
  20. JOY_BUTTON_0 = 0,
  21. JOY_BUTTON_1 = 1,
  22. JOY_BUTTON_2 = 2,
  23. JOY_BUTTON_3 = 3,
  24. JOY_BUTTON_4 = 4,
  25. JOY_BUTTON_5 = 5,
  26. JOY_BUTTON_6 = 6,
  27. JOY_BUTTON_7 = 7,
  28. JOY_BUTTON_8 = 8,
  29. JOY_BUTTON_9 = 9,
  30. JOY_BUTTON_10 = 10,
  31. JOY_BUTTON_11 = 11,
  32. JOY_BUTTON_12 = 12,
  33. JOY_BUTTON_13 = 13,
  34. JOY_BUTTON_14 = 14,
  35. JOY_BUTTON_15 = 15,
  36. JOY_BUTTON_MAX = 16,
  37. JOY_L = JOY_BUTTON_4,
  38. JOY_R = JOY_BUTTON_5,
  39. JOY_L2 = JOY_BUTTON_6,
  40. JOY_R2 = JOY_BUTTON_7,
  41. JOY_L3 = JOY_BUTTON_8,
  42. JOY_R3 = JOY_BUTTON_9,
  43. JOY_SELECT = JOY_BUTTON_10,
  44. JOY_START = JOY_BUTTON_11,
  45. JOY_DPAD_UP = JOY_BUTTON_12,
  46. JOY_DPAD_DOWN = JOY_BUTTON_13,
  47. JOY_DPAD_LEFT = JOY_BUTTON_14,
  48. JOY_DPAD_RIGHT = JOY_BUTTON_15,
  49. // a little history about game controllers (who copied who)
  50. JOY_SNES_B = JOY_BUTTON_0,
  51. JOY_SNES_A = JOY_BUTTON_1,
  52. JOY_SNES_Y = JOY_BUTTON_2,
  53. JOY_SNES_X = JOY_BUTTON_3,
  54. JOY_SONY_CIRCLE=JOY_SNES_A,
  55. JOY_SONY_X=JOY_SNES_B,
  56. JOY_SONY_SQUARE=JOY_SNES_Y,
  57. JOY_SONY_TRIANGLE=JOY_SNES_X,
  58. JOY_SEGA_B=JOY_SNES_A,
  59. JOY_SEGA_A=JOY_SNES_B,
  60. JOY_SEGA_X=JOY_SNES_Y,
  61. JOY_SEGA_Y=JOY_SNES_X,
  62. JOY_XBOX_B=JOY_SEGA_B,
  63. JOY_XBOX_A=JOY_SEGA_A,
  64. JOY_XBOX_X=JOY_SEGA_X,
  65. JOY_XBOX_Y=JOY_SEGA_Y,
  66. JOY_DS_A = JOY_SNES_A,
  67. JOY_DS_B = JOY_SNES_B,
  68. JOY_DS_X = JOY_SNES_X,
  69. JOY_DS_Y = JOY_SNES_Y,
  70. JOY_WII_C = JOY_BUTTON_5,
  71. JOY_WII_Z = JOY_BUTTON_6,
  72. JOY_WII_MINUS = JOY_BUTTON_9,
  73. JOY_WII_PLUS = JOY_BUTTON_10,
  74. // end of history
  75. JOY_AXIS_0=0,
  76. JOY_AXIS_1=1,
  77. JOY_AXIS_2=2,
  78. JOY_AXIS_3=3,
  79. JOY_AXIS_4=4,
  80. JOY_AXIS_5=5,
  81. JOY_AXIS_6=6,
  82. JOY_AXIS_7=7,
  83. JOY_AXIS_MAX=8,
  84. JOY_ANALOG_0_X = JOY_AXIS_0,
  85. JOY_ANALOG_0_Y = JOY_AXIS_1,
  86. JOY_ANALOG_1_X = JOY_AXIS_2,
  87. JOY_ANALOG_1_Y = JOY_AXIS_3,
  88. JOY_ANALOG_2_X = JOY_AXIS_4,
  89. JOY_ANALOG_2_Y = JOY_AXIS_5,
  90. JOY_ANALOG_L2 = JOY_AXIS_6,
  91. JOY_ANALOG_R2 = JOY_AXIS_7,
  92. };
  93. /**
  94. * Input Modifier Status
  95. * for keyboard/mouse events.
  96. */
  97. struct InputModifierState {
  98. bool shift;
  99. bool alt;
  100. #ifdef APPLE_STYLE_KEYS
  101. union {
  102. bool command;
  103. bool meta; //< windows/mac key
  104. };
  105. bool control;
  106. #else
  107. union {
  108. bool command; //< windows/mac key
  109. bool control;
  110. };
  111. bool meta; //< windows/mac key
  112. #endif
  113. bool operator==(const InputModifierState& rvalue) const {
  114. return ( (shift==rvalue.shift) && (alt==rvalue.alt) && (control==rvalue.control) && (meta==rvalue.meta));
  115. }
  116. };
  117. struct InputEventKey {
  118. InputModifierState mod;
  119. bool pressed; /// otherwise release
  120. uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
  121. uint32_t unicode; ///unicode
  122. bool echo; /// true if this is an echo key
  123. };
  124. struct InputEventMouse {
  125. InputModifierState mod;
  126. int button_mask;
  127. float x,y;
  128. float global_x,global_y;
  129. int pointer_index;
  130. };
  131. struct InputEventMouseButton : public InputEventMouse {
  132. int button_index;
  133. bool pressed; //otherwise released
  134. bool doubleclick; //last even less than doubleclick time
  135. };
  136. struct InputEventMouseMotion : public InputEventMouse {
  137. float relative_x,relative_y;
  138. float speed_x,speed_y;
  139. };
  140. struct InputEventJoypadMotion {
  141. int axis; ///< Joypad axis
  142. float axis_value; ///< -1 to 1
  143. };
  144. struct InputEventJoypadButton {
  145. int button_index;
  146. bool pressed;
  147. float pressure; //0 to 1
  148. };
  149. struct InputEventScreenTouch {
  150. int index;
  151. float x,y;
  152. bool pressed;
  153. };
  154. struct InputEventScreenDrag {
  155. int index;
  156. float x,y;
  157. float relative_x,relative_y;
  158. float speed_x,speed_y;
  159. };
  160. struct InputEventAction {
  161. int action;
  162. bool pressed;
  163. };
  164. struct InputEvent {
  165. enum Type {
  166. NONE,
  167. KEY,
  168. MOUSE_MOTION,
  169. MOUSE_BUTTON,
  170. JOYPAD_MOTION,
  171. JOYPAD_BUTTON,
  172. SCREEN_TOUCH,
  173. SCREEN_DRAG,
  174. ACTION,
  175. TYPE_MAX
  176. };
  177. uint32_t ID;
  178. int type;
  179. int device;
  180. union {
  181. InputEventMouseMotion mouse_motion;
  182. InputEventMouseButton mouse_button;
  183. InputEventJoypadMotion joy_motion;
  184. InputEventJoypadButton joy_button;
  185. InputEventKey key;
  186. InputEventScreenTouch screen_touch;
  187. InputEventScreenDrag screen_drag;
  188. InputEventAction action;
  189. };
  190. bool is_pressed() const;
  191. bool is_action(const String& p_action) const;
  192. bool is_action_pressed(const String& p_action) const;
  193. bool is_action_released(const String& p_action) const;
  194. bool is_echo() const;
  195. void set_as_action(const String& p_action, bool p_pressed);
  196. InputEvent xform_by(const Transform2D& p_xform) const;
  197. bool operator==(const InputEvent &p_event) const;
  198. operator String() const;
  199. InputEvent() { memset(this,0,sizeof(InputEvent)); }
  200. };
  201. bool InputEvent::operator==(const InputEvent &p_event) const {
  202. if (type != p_event.type){
  203. return false;
  204. }
  205. switch(type) {
  206. /** Current clang-format style doesn't play well with the aligned return values of that switch. */
  207. /* clang-format off */
  208. case NONE:
  209. return true;
  210. case KEY:
  211. return key.unicode == p_event.key.unicode
  212. && key.scancode == p_event.key.scancode
  213. && key.echo == p_event.key.echo
  214. && key.pressed == p_event.key.pressed
  215. && key.mod == p_event.key.mod;
  216. case MOUSE_MOTION:
  217. return mouse_motion.x == p_event.mouse_motion.x
  218. && mouse_motion.y == p_event.mouse_motion.y
  219. && mouse_motion.relative_x == p_event.mouse_motion.relative_x
  220. && mouse_motion.relative_y == p_event.mouse_motion.relative_y
  221. && mouse_motion.button_mask == p_event.mouse_motion.button_mask
  222. && key.mod == p_event.key.mod;
  223. case MOUSE_BUTTON:
  224. return mouse_button.pressed == p_event.mouse_button.pressed
  225. && mouse_button.x == p_event.mouse_button.x
  226. && mouse_button.y == p_event.mouse_button.y
  227. && mouse_button.button_index == p_event.mouse_button.button_index
  228. && mouse_button.button_mask == p_event.mouse_button.button_mask
  229. && key.mod == p_event.key.mod;
  230. case JOYPAD_MOTION:
  231. return joy_motion.axis == p_event.joy_motion.axis
  232. && joy_motion.axis_value == p_event.joy_motion.axis_value;
  233. case JOYPAD_BUTTON:
  234. return joy_button.pressed == p_event.joy_button.pressed
  235. && joy_button.button_index == p_event.joy_button.button_index
  236. && joy_button.pressure == p_event.joy_button.pressure;
  237. case SCREEN_TOUCH:
  238. return screen_touch.pressed == p_event.screen_touch.pressed
  239. && screen_touch.index == p_event.screen_touch.index
  240. && screen_touch.x == p_event.screen_touch.x
  241. && screen_touch.y == p_event.screen_touch.y;
  242. case SCREEN_DRAG:
  243. return screen_drag.index == p_event.screen_drag.index
  244. && screen_drag.x == p_event.screen_drag.x
  245. && screen_drag.y == p_event.screen_drag.y;
  246. case ACTION:
  247. return action.action == p_event.action.action
  248. && action.pressed == p_event.action.pressed;
  249. /* clang-format on */
  250. default:
  251. ERR_PRINT("No logic to compare InputEvents of this type, this shouldn't happen.");
  252. }
  253. return false;
  254. }
  255. InputEvent::operator String() const {
  256. /*
  257. String str ="Device "+itos(device)+" ID "+itos(ID)+" ";
  258. switch(type) {
  259. case NONE: {
  260. return "Event: None";
  261. } break;
  262. case KEY: {
  263. str+= "Event: Key ";
  264. str=str+"Unicode: "+String::chr(key.unicode)+" Scan: "+itos( key.scancode )+" Echo: "+String(key.echo?"True":"False")+" Pressed"+String(key.pressed?"True":"False")+" Mod: ";
  265. if (key.mod.shift)
  266. str+="S";
  267. if (key.mod.control)
  268. str+="C";
  269. if (key.mod.alt)
  270. str+="A";
  271. if (key.mod.meta)
  272. str+="M";
  273. return str;
  274. } break;
  275. case MOUSE_MOTION: {
  276. str+= "Event: Motion ";
  277. str=str+" Pos: " +itos(mouse_motion.x)+","+itos(mouse_motion.y)+" Rel: "+itos(mouse_motion.relative_x)+","+itos(mouse_motion.relative_y)+" Mask: ";
  278. for (int i=0;i<8;i++) {
  279. if ((1<<i)&mouse_motion.button_mask)
  280. str+=itos(i+1);
  281. }
  282. str+=" Mod: ";
  283. if (key.mod.shift)
  284. str+="S";
  285. if (key.mod.control)
  286. str+="C";
  287. if (key.mod.alt)
  288. str+="A";
  289. if (key.mod.meta)
  290. str+="M";
  291. return str;
  292. } break;
  293. case MOUSE_BUTTON: {
  294. str+= "Event: Button ";
  295. str=str+"Pressed: "+itos(mouse_button.pressed)+" Pos: " +itos(mouse_button.x)+","+itos(mouse_button.y)+" Button: "+itos(mouse_button.button_index)+" Mask: ";
  296. for (int i=0;i<8;i++) {
  297. if ((1<<i)&mouse_button.button_mask)
  298. str+=itos(i+1);
  299. }
  300. str+=" Mod: ";
  301. if (key.mod.shift)
  302. str+="S";
  303. if (key.mod.control)
  304. str+="C";
  305. if (key.mod.alt)
  306. str+="A";
  307. if (key.mod.meta)
  308. str+="M";
  309. str+=String(" DoubleClick: ")+(mouse_button.doubleclick?"Yes":"No");
  310. return str;
  311. } break;
  312. case JOYPAD_MOTION: {
  313. str+= "Event: JoypadMotion ";
  314. str=str+"Axis: "+itos(joy_motion.axis)+" Value: " +rtos(joy_motion.axis_value);
  315. return str;
  316. } break;
  317. case JOYPAD_BUTTON: {
  318. str+= "Event: JoypadButton ";
  319. str=str+"Pressed: "+itos(joy_button.pressed)+" Index: " +itos(joy_button.button_index)+" pressure "+rtos(joy_button.pressure);
  320. return str;
  321. } break;
  322. case SCREEN_TOUCH: {
  323. str+= "Event: ScreenTouch ";
  324. str=str+"Pressed: "+itos(screen_touch.pressed)+" Index: " +itos(screen_touch.index)+" pos "+rtos(screen_touch.x)+","+rtos(screen_touch.y);
  325. return str;
  326. } break;
  327. case SCREEN_DRAG: {
  328. str+= "Event: ScreenDrag ";
  329. str=str+" Index: " +itos(screen_drag.index)+" pos "+rtos(screen_drag.x)+","+rtos(screen_drag.y);
  330. return str;
  331. } break;
  332. case ACTION: {
  333. str+= "Event: Action: "+InputMap::get_singleton()->get_action_from_id(action.action)+" Pressed: "+itos(action.pressed);
  334. return str;
  335. } break;
  336. }
  337. */
  338. return "";
  339. }
  340. void InputEvent::set_as_action(const String& p_action, bool p_pressed) {
  341. godot_input_event_set_as_action((godot_input_event *) this, (godot_string*) &p_action, p_pressed);
  342. }
  343. bool InputEvent::is_pressed() const {
  344. switch(type) {
  345. case KEY: return key.pressed;
  346. case MOUSE_BUTTON: return mouse_button.pressed;
  347. case JOYPAD_BUTTON: return joy_button.pressed;
  348. case SCREEN_TOUCH: return screen_touch.pressed;
  349. case JOYPAD_MOTION: return ::fabs(joy_motion.axis_value) > 0.5;
  350. case ACTION: return action.pressed;
  351. default: {}
  352. }
  353. return false;
  354. }
  355. bool InputEvent::is_echo() const {
  356. return (type==KEY && key.echo);
  357. }
  358. bool InputEvent::is_action(const String& p_action) const {
  359. return godot_input_event_is_action((godot_input_event *) this, (godot_string *) &p_action);
  360. }
  361. bool InputEvent::is_action_pressed(const String& p_action) const {
  362. return is_action(p_action) && is_pressed() && !is_echo();
  363. }
  364. bool InputEvent::is_action_released(const String& p_action) const {
  365. return is_action(p_action) && !is_pressed();
  366. }
  367. InputEvent InputEvent::xform_by(const Transform2D& p_xform) const {
  368. InputEvent ev=*this;
  369. switch(ev.type) {
  370. case InputEvent::MOUSE_BUTTON: {
  371. Vector2 g = p_xform.xform(Vector2(ev.mouse_button.global_x,ev.mouse_button.global_y));
  372. Vector2 l = p_xform.xform(Vector2(ev.mouse_button.x,ev.mouse_button.y));
  373. ev.mouse_button.x=l.x;
  374. ev.mouse_button.y=l.y;
  375. ev.mouse_button.global_x=g.x;
  376. ev.mouse_button.global_y=g.y;
  377. } break;
  378. case InputEvent::MOUSE_MOTION: {
  379. Vector2 g = p_xform.xform(Vector2(ev.mouse_motion.global_x,ev.mouse_motion.global_y));
  380. Vector2 l = p_xform.xform(Vector2(ev.mouse_motion.x,ev.mouse_motion.y));
  381. Vector2 r = p_xform.basis_xform(Vector2(ev.mouse_motion.relative_x,ev.mouse_motion.relative_y));
  382. Vector2 s = p_xform.basis_xform(Vector2(ev.mouse_motion.speed_x,ev.mouse_motion.speed_y));
  383. ev.mouse_motion.x=l.x;
  384. ev.mouse_motion.y=l.y;
  385. ev.mouse_motion.global_x=g.x;
  386. ev.mouse_motion.global_y=g.y;
  387. ev.mouse_motion.relative_x=r.x;
  388. ev.mouse_motion.relative_y=r.y;
  389. ev.mouse_motion.speed_x=s.x;
  390. ev.mouse_motion.speed_y=s.y;
  391. } break;
  392. case InputEvent::SCREEN_TOUCH: {
  393. Vector2 t = p_xform.xform(Vector2(ev.screen_touch.x,ev.screen_touch.y));
  394. ev.screen_touch.x=t.x;
  395. ev.screen_touch.y=t.y;
  396. } break;
  397. case InputEvent::SCREEN_DRAG: {
  398. Vector2 t = p_xform.xform(Vector2(ev.screen_drag.x,ev.screen_drag.y));
  399. Vector2 r = p_xform.basis_xform(Vector2(ev.screen_drag.relative_x,ev.screen_drag.relative_y));
  400. Vector2 s = p_xform.basis_xform(Vector2(ev.screen_drag.speed_x,ev.screen_drag.speed_y));
  401. ev.screen_drag.x=t.x;
  402. ev.screen_drag.y=t.y;
  403. ev.screen_drag.relative_x=r.x;
  404. ev.screen_drag.relative_y=r.y;
  405. ev.screen_drag.speed_x=s.x;
  406. ev.screen_drag.speed_y=s.y;
  407. } break;
  408. }
  409. return ev;
  410. }
  411. }
  412. #endif // INPUTEVENT_H