InputAPI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "APITemplates.h"
  25. #include "Controls.h"
  26. #include "Input.h"
  27. #include <angelscript.h>
  28. static void RegisterKeyCodes(asIScriptEngine* engine)
  29. {
  30. engine->RegisterGlobalProperty("const int MOUSEB_LEFT", (void*)&MOUSEB_LEFT);
  31. engine->RegisterGlobalProperty("const int MOUSEB_RIGHT", (void*)&MOUSEB_RIGHT);
  32. engine->RegisterGlobalProperty("const int MOUSEB_MIDDLE", (void*)&MOUSEB_MIDDLE);
  33. engine->RegisterGlobalProperty("const int QUAL_SHIFT", (void*)&QUAL_SHIFT);
  34. engine->RegisterGlobalProperty("const int QUAL_CTRL", (void*)&QUAL_CTRL);
  35. engine->RegisterGlobalProperty("const int KEY_BACK", (void*)&KEY_BACK);
  36. engine->RegisterGlobalProperty("const int KEY_TAB", (void*)&KEY_TAB);
  37. engine->RegisterGlobalProperty("const int KEY_CLEAR", (void*)&KEY_CLEAR);
  38. engine->RegisterGlobalProperty("const int KEY_RETURN", (void*)&KEY_RETURN);
  39. engine->RegisterGlobalProperty("const int KEY_SHIFT", (void*)&KEY_SHIFT);
  40. engine->RegisterGlobalProperty("const int KEY_CTRL", (void*)&KEY_CTRL);
  41. engine->RegisterGlobalProperty("const int KEY_MENU", (void*)&KEY_MENU);
  42. engine->RegisterGlobalProperty("const int KEY_PAUSE", (void*)&KEY_PAUSE);
  43. engine->RegisterGlobalProperty("const int KEY_CAPITAL", (void*)&KEY_CAPITAL);
  44. engine->RegisterGlobalProperty("const int KEY_ESC", (void*)&KEY_ESC);
  45. engine->RegisterGlobalProperty("const int KEY_CONVERT", (void*)&KEY_CONVERT);
  46. engine->RegisterGlobalProperty("const int KEY_NONCONVERT", (void*)&KEY_NONCONVERT);
  47. engine->RegisterGlobalProperty("const int KEY_ACCEPT", (void*)&KEY_ACCEPT);
  48. engine->RegisterGlobalProperty("const int KEY_MODECHANGE", (void*)&KEY_MODECHANGE);
  49. engine->RegisterGlobalProperty("const int KEY_SPACE", (void*)&KEY_SPACE);
  50. engine->RegisterGlobalProperty("const int KEY_PAGEUP", (void*)&KEY_PAGEUP);
  51. engine->RegisterGlobalProperty("const int KEY_PAGEDOWN", (void*)&KEY_PAGEDOWN);
  52. engine->RegisterGlobalProperty("const int KEY_END", (void*)&KEY_END);
  53. engine->RegisterGlobalProperty("const int KEY_HOME", (void*)&KEY_HOME);
  54. engine->RegisterGlobalProperty("const int KEY_LEFT", (void*)&KEY_LEFT);
  55. engine->RegisterGlobalProperty("const int KEY_UP", (void*)&KEY_UP);
  56. engine->RegisterGlobalProperty("const int KEY_RIGHT", (void*)&KEY_RIGHT);
  57. engine->RegisterGlobalProperty("const int KEY_DOWN", (void*)&KEY_DOWN);
  58. engine->RegisterGlobalProperty("const int KEY_SELECT", (void*)&KEY_SELECT);
  59. engine->RegisterGlobalProperty("const int KEY_PRINT", (void*)&KEY_PRINT);
  60. engine->RegisterGlobalProperty("const int KEY_EXEC", (void*)&KEY_EXEC);
  61. engine->RegisterGlobalProperty("const int KEY_SNAPSHOT", (void*)&KEY_SNAPSHOT);
  62. engine->RegisterGlobalProperty("const int KEY_INSERT", (void*)&KEY_INSERT);
  63. engine->RegisterGlobalProperty("const int KEY_DELETE", (void*)&KEY_DELETE);
  64. engine->RegisterGlobalProperty("const int KEY_HELP", (void*)&KEY_HELP);
  65. engine->RegisterGlobalProperty("const int KEY_LWIN", (void*)&KEY_LWIN);
  66. engine->RegisterGlobalProperty("const int KEY_RWIN", (void*)&KEY_RWIN);
  67. engine->RegisterGlobalProperty("const int KEY_APPS", (void*)&KEY_APPS);
  68. engine->RegisterGlobalProperty("const int KEY_SLEEP", (void*)&KEY_SLEEP);
  69. engine->RegisterGlobalProperty("const int KEY_NUMPAD0", (void*)&KEY_NUMPAD0);
  70. engine->RegisterGlobalProperty("const int KEY_NUMPAD1", (void*)&KEY_NUMPAD1);
  71. engine->RegisterGlobalProperty("const int KEY_NUMPAD2", (void*)&KEY_NUMPAD2);
  72. engine->RegisterGlobalProperty("const int KEY_NUMPAD3", (void*)&KEY_NUMPAD3);
  73. engine->RegisterGlobalProperty("const int KEY_NUMPAD4", (void*)&KEY_NUMPAD4);
  74. engine->RegisterGlobalProperty("const int KEY_NUMPAD5", (void*)&KEY_NUMPAD5);
  75. engine->RegisterGlobalProperty("const int KEY_NUMPAD6", (void*)&KEY_NUMPAD6);
  76. engine->RegisterGlobalProperty("const int KEY_NUMPAD7", (void*)&KEY_NUMPAD7);
  77. engine->RegisterGlobalProperty("const int KEY_NUMPAD8", (void*)&KEY_NUMPAD8);
  78. engine->RegisterGlobalProperty("const int KEY_NUMPAD9", (void*)&KEY_NUMPAD9);
  79. engine->RegisterGlobalProperty("const int KEY_MULTIPLY", (void*)&KEY_MULTIPLY);
  80. engine->RegisterGlobalProperty("const int KEY_ADD", (void*)&KEY_ADD);
  81. engine->RegisterGlobalProperty("const int KEY_SEPARATOR", (void*)&KEY_SEPARATOR);
  82. engine->RegisterGlobalProperty("const int KEY_SUBTRACT", (void*)&KEY_SUBTRACT);
  83. engine->RegisterGlobalProperty("const int KEY_DECIMAL", (void*)&KEY_DECIMAL);
  84. engine->RegisterGlobalProperty("const int KEY_DIVIDE", (void*)&KEY_DIVIDE);
  85. engine->RegisterGlobalProperty("const int KEY_F1", (void*)&KEY_F1);
  86. engine->RegisterGlobalProperty("const int KEY_F2", (void*)&KEY_F2);
  87. engine->RegisterGlobalProperty("const int KEY_F3", (void*)&KEY_F3);
  88. engine->RegisterGlobalProperty("const int KEY_F4", (void*)&KEY_F4);
  89. engine->RegisterGlobalProperty("const int KEY_F5", (void*)&KEY_F5);
  90. engine->RegisterGlobalProperty("const int KEY_F6", (void*)&KEY_F6);
  91. engine->RegisterGlobalProperty("const int KEY_F7", (void*)&KEY_F7);
  92. engine->RegisterGlobalProperty("const int KEY_F8", (void*)&KEY_F8);
  93. engine->RegisterGlobalProperty("const int KEY_F9", (void*)&KEY_F9);
  94. engine->RegisterGlobalProperty("const int KEY_F10", (void*)&KEY_F10);
  95. engine->RegisterGlobalProperty("const int KEY_F11", (void*)&KEY_F11);
  96. engine->RegisterGlobalProperty("const int KEY_F12", (void*)&KEY_F12);
  97. engine->RegisterGlobalProperty("const int KEY_F13", (void*)&KEY_F13);
  98. engine->RegisterGlobalProperty("const int KEY_F14", (void*)&KEY_F14);
  99. engine->RegisterGlobalProperty("const int KEY_F15", (void*)&KEY_F15);
  100. engine->RegisterGlobalProperty("const int KEY_F16", (void*)&KEY_F16);
  101. engine->RegisterGlobalProperty("const int KEY_F17", (void*)&KEY_F17);
  102. engine->RegisterGlobalProperty("const int KEY_F18", (void*)&KEY_F18);
  103. engine->RegisterGlobalProperty("const int KEY_F19", (void*)&KEY_F19);
  104. engine->RegisterGlobalProperty("const int KEY_F20", (void*)&KEY_F20);
  105. engine->RegisterGlobalProperty("const int KEY_F21", (void*)&KEY_F21);
  106. engine->RegisterGlobalProperty("const int KEY_F22", (void*)&KEY_F22);
  107. engine->RegisterGlobalProperty("const int KEY_F23", (void*)&KEY_F23);
  108. engine->RegisterGlobalProperty("const int KEY_F24", (void*)&KEY_F24);
  109. engine->RegisterGlobalProperty("const int KEY_NUMLOCK", (void*)&KEY_NUMLOCK);
  110. engine->RegisterGlobalProperty("const int KEY_SCROLL", (void*)&KEY_SCROLL);
  111. engine->RegisterGlobalProperty("const int KEY_LSHIFT", (void*)&KEY_LSHIFT);
  112. engine->RegisterGlobalProperty("const int KEY_RSHIFT", (void*)&KEY_RSHIFT);
  113. engine->RegisterGlobalProperty("const int KEY_LCTRL", (void*)&KEY_LCTRL);
  114. engine->RegisterGlobalProperty("const int KEY_RCTRL", (void*)&KEY_RCTRL);
  115. engine->RegisterGlobalProperty("const int KEY_LMENU", (void*)&KEY_LMENU);
  116. engine->RegisterGlobalProperty("const int KEY_RMENU", (void*)&KEY_RMENU);
  117. engine->RegisterGlobalProperty("const int KEY_BROWSER_BACK", (void*)&KEY_BROWSER_BACK);
  118. engine->RegisterGlobalProperty("const int KEY_BROWSER_FORWARD", (void*)&KEY_BROWSER_FORWARD);
  119. engine->RegisterGlobalProperty("const int KEY_BROWSER_REFRESH", (void*)&KEY_BROWSER_REFRESH);
  120. engine->RegisterGlobalProperty("const int KEY_BROWSER_STOP", (void*)&KEY_BROWSER_STOP);
  121. engine->RegisterGlobalProperty("const int KEY_BROWSER_SEARCH", (void*)&KEY_BROWSER_SEARCH);
  122. engine->RegisterGlobalProperty("const int KEY_BROWSER_FAVORITES", (void*)&KEY_BROWSER_FAVORITES);
  123. engine->RegisterGlobalProperty("const int KEY_BROWSER_HOME", (void*)&KEY_BROWSER_HOME);
  124. engine->RegisterGlobalProperty("const int KEY_VOLUME_MUTE", (void*)&KEY_VOLUME_MUTE);
  125. engine->RegisterGlobalProperty("const int KEY_VOLUME_DOWN", (void*)&KEY_VOLUME_DOWN);
  126. engine->RegisterGlobalProperty("const int KEY_VOLUME_UP", (void*)&KEY_VOLUME_UP);
  127. engine->RegisterGlobalProperty("const int KEY_MEDIA_NEXT_TRACK", (void*)&KEY_MEDIA_NEXT_TRACK);
  128. engine->RegisterGlobalProperty("const int KEY_MEDIA_PREV_TRACK", (void*)&KEY_MEDIA_PREV_TRACK);
  129. engine->RegisterGlobalProperty("const int KEY_MEDIA_STOP", (void*)&KEY_MEDIA_STOP);
  130. engine->RegisterGlobalProperty("const int KEY_MEDIA_PLAY_PAUSE", (void*)&KEY_MEDIA_PLAY_PAUSE);
  131. engine->RegisterGlobalProperty("const int KEY_LAUNCH_MAIL", (void*)&KEY_LAUNCH_MAIL);
  132. engine->RegisterGlobalProperty("const int KEY_LAUNCH_MEDIA_SELECT", (void*)&KEY_LAUNCH_MEDIA_SELECT);
  133. engine->RegisterGlobalProperty("const int KEY_LAUNCH_APP1", (void*)&KEY_LAUNCH_APP1);
  134. engine->RegisterGlobalProperty("const int KEY_LAUNCH_APP2", (void*)&KEY_LAUNCH_APP2);
  135. engine->RegisterGlobalProperty("const int KEY_OEM_1", (void*)&KEY_OEM_1);
  136. engine->RegisterGlobalProperty("const int KEY_OEM_PLUS", (void*)&KEY_OEM_PLUS);
  137. engine->RegisterGlobalProperty("const int KEY_OEM_COMMA", (void*)&KEY_OEM_COMMA);
  138. engine->RegisterGlobalProperty("const int KEY_OEM_MINUS", (void*)&KEY_OEM_MINUS);
  139. engine->RegisterGlobalProperty("const int KEY_OEM_PERIOD", (void*)&KEY_OEM_PERIOD);
  140. engine->RegisterGlobalProperty("const int KEY_OEM_2", (void*)&KEY_OEM_2);
  141. engine->RegisterGlobalProperty("const int KEY_OEM_3", (void*)&KEY_OEM_3);
  142. engine->RegisterGlobalProperty("const int KEY_OEM_4", (void*)&KEY_OEM_4);
  143. engine->RegisterGlobalProperty("const int KEY_OEM_5", (void*)&KEY_OEM_5);
  144. engine->RegisterGlobalProperty("const int KEY_OEM_6", (void*)&KEY_OEM_6);
  145. engine->RegisterGlobalProperty("const int KEY_OEM_7", (void*)&KEY_OEM_7);
  146. engine->RegisterGlobalProperty("const int KEY_OEM_8", (void*)&KEY_OEM_8);
  147. }
  148. static void ConstructControls(Controls* ptr)
  149. {
  150. new(ptr) Controls();
  151. }
  152. static void ConstructControlsCopy(const Controls& controls, Controls* ptr)
  153. {
  154. new(ptr) Controls(controls);
  155. }
  156. static void DestructControls(Controls* ptr)
  157. {
  158. ptr->~Controls();
  159. }
  160. static void RegisterControls(asIScriptEngine* engine)
  161. {
  162. engine->RegisterObjectType("Controls", sizeof(Controls), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  163. engine->RegisterObjectBehaviour("Controls", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructControls), asCALL_CDECL_OBJLAST);
  164. engine->RegisterObjectBehaviour("Controls", asBEHAVE_CONSTRUCT, "void f(const Controls& in)", asFUNCTION(ConstructControlsCopy), asCALL_CDECL_OBJLAST);
  165. engine->RegisterObjectBehaviour("Controls", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructControls), asCALL_CDECL_OBJLAST);
  166. engine->RegisterObjectMethod("Controls", "Controls &opAssign(const Controls& in)", asMETHOD(Controls, operator =), asCALL_THISCALL);
  167. engine->RegisterObjectMethod("Controls", "void Reset()", asMETHOD(Controls, Reset), asCALL_THISCALL);
  168. engine->RegisterObjectMethod("Controls", "void Set(uint, bool)", asMETHOD(Controls, Set), asCALL_THISCALL);
  169. engine->RegisterObjectMethod("Controls", "bool IsDown(uint)", asMETHOD(Controls, IsDown), asCALL_THISCALL);
  170. engine->RegisterObjectMethod("Controls", "bool IsPressed(uint, const Controls& in)", asMETHOD(Controls, IsPressed), asCALL_THISCALL);
  171. engine->RegisterObjectProperty("Controls", "uint buttons", offsetof(Controls, buttons_));
  172. engine->RegisterObjectProperty("Controls", "float yaw", offsetof(Controls, yaw_));
  173. engine->RegisterObjectProperty("Controls", "float pitch", offsetof(Controls, pitch_));
  174. engine->RegisterObjectProperty("Controls", "VariantMap extraData", offsetof(Controls, extraData_));
  175. }
  176. static Input* GetInput()
  177. {
  178. return GetScriptContext()->GetSubsystem<Input>();
  179. }
  180. static void RegisterInput(asIScriptEngine* engine)
  181. {
  182. RegisterObject<Input>(engine, "Input");
  183. engine->RegisterObjectMethod("Input", "void SetMousePosition(int, int)", asMETHODPR(Input, SetMousePosition, (int, int), void), asCALL_THISCALL);
  184. engine->RegisterObjectMethod("Input", "void SuppressNextChar()", asMETHOD(Input, SuppressNextChar), asCALL_THISCALL);
  185. engine->RegisterObjectMethod("Input", "void set_clipCursor(bool)", asMETHOD(Input, SetClipCursor), asCALL_THISCALL);
  186. engine->RegisterObjectMethod("Input", "bool get_clipCursor() const", asMETHOD(Input, GetClipCursor), asCALL_THISCALL);
  187. engine->RegisterObjectMethod("Input", "void set_toggleFullscreen(bool)", asMETHOD(Input, SetToggleFullscreen), asCALL_THISCALL);
  188. engine->RegisterObjectMethod("Input", "bool get_toggleFullscreen() const", asMETHOD(Input, GetToggleFullscreen), asCALL_THISCALL);
  189. engine->RegisterObjectMethod("Input", "void set_mousePosition(const IntVector2& in)", asMETHODPR(Input, SetMousePosition, (const IntVector2&), void), asCALL_THISCALL);
  190. engine->RegisterObjectMethod("Input", "IntVector2 get_mousePosition() const", asMETHOD(Input, GetMousePosition), asCALL_THISCALL);
  191. engine->RegisterObjectMethod("Input", "bool get_keyDown(int) const", asMETHOD(Input, GetKeyDown), asCALL_THISCALL);
  192. engine->RegisterObjectMethod("Input", "bool get_keyPress(int) const", asMETHOD(Input, GetKeyPress), asCALL_THISCALL);
  193. engine->RegisterObjectMethod("Input", "bool get_mouseButtonDown(int) const", asMETHOD(Input, GetMouseButtonDown), asCALL_THISCALL);
  194. engine->RegisterObjectMethod("Input", "bool get_mouseButtonPress(int) const", asMETHOD(Input, GetMouseButtonPress), asCALL_THISCALL);
  195. engine->RegisterObjectMethod("Input", "bool get_qualifierDown(int) const", asMETHOD(Input, GetQualifierDown), asCALL_THISCALL);
  196. engine->RegisterObjectMethod("Input", "bool get_qualifierPress(int) const", asMETHOD(Input, GetQualifierPress), asCALL_THISCALL);
  197. engine->RegisterObjectMethod("Input", "int get_qualifiers() const", asMETHOD(Input, GetQualifiers), asCALL_THISCALL);
  198. engine->RegisterObjectMethod("Input", "const IntVector2& get_mouseMove() const", asMETHOD(Input, GetMouseMove), asCALL_THISCALL);
  199. engine->RegisterObjectMethod("Input", "int get_mouseMoveX() const", asMETHOD(Input, GetMouseMoveX), asCALL_THISCALL);
  200. engine->RegisterObjectMethod("Input", "int get_mouseMoveY() const", asMETHOD(Input, GetMouseMoveY), asCALL_THISCALL);
  201. engine->RegisterObjectMethod("Input", "int get_mouseMoveWheel() const", asMETHOD(Input, GetMouseMoveWheel), asCALL_THISCALL);
  202. engine->RegisterObjectMethod("Input", "bool get_active() const", asMETHOD(Input, IsActive), asCALL_THISCALL);
  203. engine->RegisterObjectMethod("Input", "bool get_minimized() const", asMETHOD(Input, IsMinimized), asCALL_THISCALL);
  204. engine->RegisterGlobalFunction("Input@+ get_input()", asFUNCTION(GetInput), asCALL_CDECL);
  205. }
  206. void RegisterInputAPI(asIScriptEngine* engine)
  207. {
  208. RegisterKeyCodes(engine);
  209. RegisterControls(engine);
  210. RegisterInput(engine);
  211. }