input_event.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* input_event.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_event.h"
  30. #include "input_map.h"
  31. #include "os/keyboard.h"
  32. /**
  33. *
  34. */
  35. bool InputEvent::operator==(const InputEvent &p_event) const {
  36. return true;
  37. }
  38. InputEvent::operator String() const {
  39. String str="Device "+itos(device)+" ID "+itos(ID)+" ";
  40. switch(type) {
  41. case NONE: {
  42. return "Event: None";
  43. } break;
  44. case KEY: {
  45. str+= "Event: Key ";
  46. str=str+"Unicode: "+String::chr(key.unicode)+" Scan: "+itos( key.scancode )+" Echo: "+String(key.echo?"True":"False")+" Pressed"+String(key.pressed?"True":"False")+" Mod: ";
  47. if (key.mod.shift)
  48. str+="S";
  49. if (key.mod.control)
  50. str+="C";
  51. if (key.mod.alt)
  52. str+="A";
  53. if (key.mod.meta)
  54. str+="M";
  55. return str;
  56. } break;
  57. case MOUSE_MOTION: {
  58. str+= "Event: Motion ";
  59. str=str+" Pos: " +itos(mouse_motion.x)+","+itos(mouse_motion.y)+" Rel: "+itos(mouse_motion.relative_x)+","+itos(mouse_motion.relative_y)+" Mask: ";
  60. for (int i=0;i<8;i++) {
  61. if ((1<<i)&mouse_motion.button_mask)
  62. str+=itos(i+1);
  63. }
  64. str+=" Mod: ";
  65. if (key.mod.shift)
  66. str+="S";
  67. if (key.mod.control)
  68. str+="C";
  69. if (key.mod.alt)
  70. str+="A";
  71. if (key.mod.meta)
  72. str+="M";
  73. return str;
  74. } break;
  75. case MOUSE_BUTTON: {
  76. str+= "Event: Button ";
  77. str=str+"Pressed: "+itos(mouse_button.pressed)+" Pos: " +itos(mouse_button.x)+","+itos(mouse_button.y)+" Button: "+itos(mouse_button.button_index)+" Mask: ";
  78. for (int i=0;i<8;i++) {
  79. if ((1<<i)&mouse_button.button_mask)
  80. str+=itos(i+1);
  81. }
  82. str+=" Mod: ";
  83. if (key.mod.shift)
  84. str+="S";
  85. if (key.mod.control)
  86. str+="C";
  87. if (key.mod.alt)
  88. str+="A";
  89. if (key.mod.meta)
  90. str+="M";
  91. str+=String(" DoubleClick: ")+(mouse_button.doubleclick?"Yes":"No");
  92. return str;
  93. } break;
  94. case JOYSTICK_MOTION: {
  95. str+= "Event: JoyMotion ";
  96. str=str+"Axis: "+itos(joy_motion.axis)+" Value: " +rtos(joy_motion.axis_value);
  97. return str;
  98. } break;
  99. case JOYSTICK_BUTTON: {
  100. str+= "Event: JoyButton ";
  101. str=str+"Pressed: "+itos(joy_button.pressed)+" Index: " +itos(joy_button.button_index)+" pressure "+rtos(joy_button.pressure);
  102. return str;
  103. } break;
  104. case SCREEN_TOUCH: {
  105. str+= "Event: ScreenTouch ";
  106. str=str+"Pressed: "+itos(screen_touch.pressed)+" Index: " +itos(screen_touch.index)+" pos "+rtos(screen_touch.x)+","+rtos(screen_touch.y);
  107. return str;
  108. } break;
  109. case SCREEN_DRAG: {
  110. str+= "Event: ScreenDrag ";
  111. str=str+" Index: " +itos(screen_drag.index)+" pos "+rtos(screen_drag.x)+","+rtos(screen_drag.y);
  112. return str;
  113. } break;
  114. case ACTION: {
  115. str+= "Event: Action: "+InputMap::get_singleton()->get_action_from_id(action.action)+" Pressed: "+itos(action.pressed);
  116. return str;
  117. } break;
  118. }
  119. return "";
  120. }
  121. void InputEvent::set_as_action(const String& p_action, bool p_pressed) {
  122. type=ACTION;
  123. action.action=InputMap::get_singleton()->get_action_id(p_action);
  124. action.pressed=p_pressed;
  125. }
  126. bool InputEvent::is_pressed() const {
  127. switch(type) {
  128. case KEY: return key.pressed;
  129. case MOUSE_BUTTON: return mouse_button.pressed;
  130. case JOYSTICK_BUTTON: return joy_button.pressed;
  131. case SCREEN_TOUCH: return screen_touch.pressed;
  132. case ACTION: return action.pressed;
  133. default: {}
  134. }
  135. return false;
  136. }
  137. bool InputEvent::is_echo() const {
  138. return (type==KEY && key.echo);
  139. }
  140. bool InputEvent::is_action(const String& p_action) const {
  141. return InputMap::get_singleton()->event_is_action(*this,p_action);
  142. }
  143. uint32_t InputEventKey::get_scancode_with_modifiers() const {
  144. uint32_t sc=scancode;
  145. if (mod.control)
  146. sc|=KEY_MASK_CTRL;
  147. if (mod.alt)
  148. sc|=KEY_MASK_ALT;
  149. if (mod.shift)
  150. sc|=KEY_MASK_SHIFT;
  151. if (mod.meta)
  152. sc|=KEY_MASK_META;
  153. return sc;
  154. }