InputAPI.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 QUAL_ALT", (void*)&QUAL_ALT);
  36. engine->RegisterGlobalProperty("const int KEY_BACKSPACE", (void*)&KEY_BACKSPACE);
  37. engine->RegisterGlobalProperty("const int KEY_TAB", (void*)&KEY_TAB);
  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_ALT", (void*)&KEY_ALT);
  42. engine->RegisterGlobalProperty("const int KEY_PAUSE", (void*)&KEY_PAUSE);
  43. engine->RegisterGlobalProperty("const int KEY_CAPSLOCK", (void*)&KEY_CAPSLOCK);
  44. engine->RegisterGlobalProperty("const int KEY_ESC", (void*)&KEY_ESC);
  45. engine->RegisterGlobalProperty("const int KEY_SPACE", (void*)&KEY_SPACE);
  46. engine->RegisterGlobalProperty("const int KEY_PAGEUP", (void*)&KEY_PAGEUP);
  47. engine->RegisterGlobalProperty("const int KEY_PAGEDOWN", (void*)&KEY_PAGEDOWN);
  48. engine->RegisterGlobalProperty("const int KEY_END", (void*)&KEY_END);
  49. engine->RegisterGlobalProperty("const int KEY_HOME", (void*)&KEY_HOME);
  50. engine->RegisterGlobalProperty("const int KEY_LEFT", (void*)&KEY_LEFT);
  51. engine->RegisterGlobalProperty("const int KEY_UP", (void*)&KEY_UP);
  52. engine->RegisterGlobalProperty("const int KEY_RIGHT", (void*)&KEY_RIGHT);
  53. engine->RegisterGlobalProperty("const int KEY_DOWN", (void*)&KEY_DOWN);
  54. engine->RegisterGlobalProperty("const int KEY_PRINTSCREEN", (void*)&KEY_PRINTSCREEN);
  55. engine->RegisterGlobalProperty("const int KEY_INSERT", (void*)&KEY_INSERT);
  56. engine->RegisterGlobalProperty("const int KEY_DELETE", (void*)&KEY_DELETE);
  57. engine->RegisterGlobalProperty("const int KEY_LWIN", (void*)&KEY_LWIN);
  58. engine->RegisterGlobalProperty("const int KEY_RWIN", (void*)&KEY_RWIN);
  59. engine->RegisterGlobalProperty("const int KEY_APPS", (void*)&KEY_APPS);
  60. engine->RegisterGlobalProperty("const int KEY_NUMPAD0", (void*)&KEY_NUMPAD0);
  61. engine->RegisterGlobalProperty("const int KEY_NUMPAD1", (void*)&KEY_NUMPAD1);
  62. engine->RegisterGlobalProperty("const int KEY_NUMPAD2", (void*)&KEY_NUMPAD2);
  63. engine->RegisterGlobalProperty("const int KEY_NUMPAD3", (void*)&KEY_NUMPAD3);
  64. engine->RegisterGlobalProperty("const int KEY_NUMPAD4", (void*)&KEY_NUMPAD4);
  65. engine->RegisterGlobalProperty("const int KEY_NUMPAD5", (void*)&KEY_NUMPAD5);
  66. engine->RegisterGlobalProperty("const int KEY_NUMPAD6", (void*)&KEY_NUMPAD6);
  67. engine->RegisterGlobalProperty("const int KEY_NUMPAD7", (void*)&KEY_NUMPAD7);
  68. engine->RegisterGlobalProperty("const int KEY_NUMPAD8", (void*)&KEY_NUMPAD8);
  69. engine->RegisterGlobalProperty("const int KEY_NUMPAD9", (void*)&KEY_NUMPAD9);
  70. engine->RegisterGlobalProperty("const int KEY_MULTIPLY", (void*)&KEY_MULTIPLY);
  71. engine->RegisterGlobalProperty("const int KEY_ADD", (void*)&KEY_ADD);
  72. engine->RegisterGlobalProperty("const int KEY_SUBTRACT", (void*)&KEY_SUBTRACT);
  73. engine->RegisterGlobalProperty("const int KEY_DECIMAL", (void*)&KEY_DECIMAL);
  74. engine->RegisterGlobalProperty("const int KEY_DIVIDE", (void*)&KEY_DIVIDE);
  75. engine->RegisterGlobalProperty("const int KEY_F1", (void*)&KEY_F1);
  76. engine->RegisterGlobalProperty("const int KEY_F2", (void*)&KEY_F2);
  77. engine->RegisterGlobalProperty("const int KEY_F3", (void*)&KEY_F3);
  78. engine->RegisterGlobalProperty("const int KEY_F4", (void*)&KEY_F4);
  79. engine->RegisterGlobalProperty("const int KEY_F5", (void*)&KEY_F5);
  80. engine->RegisterGlobalProperty("const int KEY_F6", (void*)&KEY_F6);
  81. engine->RegisterGlobalProperty("const int KEY_F7", (void*)&KEY_F7);
  82. engine->RegisterGlobalProperty("const int KEY_F8", (void*)&KEY_F8);
  83. engine->RegisterGlobalProperty("const int KEY_F9", (void*)&KEY_F9);
  84. engine->RegisterGlobalProperty("const int KEY_F10", (void*)&KEY_F10);
  85. engine->RegisterGlobalProperty("const int KEY_F11", (void*)&KEY_F11);
  86. engine->RegisterGlobalProperty("const int KEY_F12", (void*)&KEY_F12);
  87. engine->RegisterGlobalProperty("const int KEY_F13", (void*)&KEY_F13);
  88. engine->RegisterGlobalProperty("const int KEY_F14", (void*)&KEY_F14);
  89. engine->RegisterGlobalProperty("const int KEY_F15", (void*)&KEY_F15);
  90. engine->RegisterGlobalProperty("const int KEY_F16", (void*)&KEY_F16);
  91. engine->RegisterGlobalProperty("const int KEY_F17", (void*)&KEY_F17);
  92. engine->RegisterGlobalProperty("const int KEY_F18", (void*)&KEY_F18);
  93. engine->RegisterGlobalProperty("const int KEY_F19", (void*)&KEY_F19);
  94. engine->RegisterGlobalProperty("const int KEY_F20", (void*)&KEY_F20);
  95. engine->RegisterGlobalProperty("const int KEY_F21", (void*)&KEY_F21);
  96. engine->RegisterGlobalProperty("const int KEY_F22", (void*)&KEY_F22);
  97. engine->RegisterGlobalProperty("const int KEY_F23", (void*)&KEY_F23);
  98. engine->RegisterGlobalProperty("const int KEY_F24", (void*)&KEY_F24);
  99. engine->RegisterGlobalProperty("const int KEY_NUMLOCK", (void*)&KEY_NUMLOCK);
  100. engine->RegisterGlobalProperty("const int KEY_SCROLLLOCK", (void*)&KEY_SCROLLLOCK);
  101. engine->RegisterGlobalProperty("const int KEY_LSHIFT", (void*)&KEY_LSHIFT);
  102. engine->RegisterGlobalProperty("const int KEY_RSHIFT", (void*)&KEY_RSHIFT);
  103. engine->RegisterGlobalProperty("const int KEY_LCTRL", (void*)&KEY_LCTRL);
  104. engine->RegisterGlobalProperty("const int KEY_RCTRL", (void*)&KEY_RCTRL);
  105. engine->RegisterGlobalProperty("const int KEY_LALT", (void*)&KEY_LALT);
  106. engine->RegisterGlobalProperty("const int KEY_RALT", (void*)&KEY_RALT);
  107. engine->RegisterGlobalProperty("const int KEY_OEM_1", (void*)&KEY_OEM_1);
  108. engine->RegisterGlobalProperty("const int KEY_OEM_PLUS", (void*)&KEY_OEM_PLUS);
  109. engine->RegisterGlobalProperty("const int KEY_OEM_COMMA", (void*)&KEY_OEM_COMMA);
  110. engine->RegisterGlobalProperty("const int KEY_OEM_MINUS", (void*)&KEY_OEM_MINUS);
  111. engine->RegisterGlobalProperty("const int KEY_OEM_PERIOD", (void*)&KEY_OEM_PERIOD);
  112. engine->RegisterGlobalProperty("const int KEY_OEM_2", (void*)&KEY_OEM_2);
  113. engine->RegisterGlobalProperty("const int KEY_OEM_3", (void*)&KEY_OEM_3);
  114. engine->RegisterGlobalProperty("const int KEY_OEM_4", (void*)&KEY_OEM_4);
  115. engine->RegisterGlobalProperty("const int KEY_OEM_5", (void*)&KEY_OEM_5);
  116. engine->RegisterGlobalProperty("const int KEY_OEM_6", (void*)&KEY_OEM_6);
  117. engine->RegisterGlobalProperty("const int KEY_OEM_7", (void*)&KEY_OEM_7);
  118. engine->RegisterGlobalProperty("const int KEY_OEM_8", (void*)&KEY_OEM_8);
  119. }
  120. static void ConstructControls(Controls* ptr)
  121. {
  122. new(ptr) Controls();
  123. }
  124. static void ConstructControlsCopy(const Controls& controls, Controls* ptr)
  125. {
  126. new(ptr) Controls(controls);
  127. }
  128. static void DestructControls(Controls* ptr)
  129. {
  130. ptr->~Controls();
  131. }
  132. static void RegisterControls(asIScriptEngine* engine)
  133. {
  134. engine->RegisterObjectType("Controls", sizeof(Controls), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  135. engine->RegisterObjectBehaviour("Controls", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructControls), asCALL_CDECL_OBJLAST);
  136. engine->RegisterObjectBehaviour("Controls", asBEHAVE_CONSTRUCT, "void f(const Controls& in)", asFUNCTION(ConstructControlsCopy), asCALL_CDECL_OBJLAST);
  137. engine->RegisterObjectBehaviour("Controls", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructControls), asCALL_CDECL_OBJLAST);
  138. engine->RegisterObjectMethod("Controls", "Controls &opAssign(const Controls& in)", asMETHOD(Controls, operator =), asCALL_THISCALL);
  139. engine->RegisterObjectMethod("Controls", "void Reset()", asMETHOD(Controls, Reset), asCALL_THISCALL);
  140. engine->RegisterObjectMethod("Controls", "void Set(uint, bool)", asMETHOD(Controls, Set), asCALL_THISCALL);
  141. engine->RegisterObjectMethod("Controls", "bool IsDown(uint)", asMETHOD(Controls, IsDown), asCALL_THISCALL);
  142. engine->RegisterObjectMethod("Controls", "bool IsPressed(uint, const Controls& in)", asMETHOD(Controls, IsPressed), asCALL_THISCALL);
  143. engine->RegisterObjectProperty("Controls", "uint buttons", offsetof(Controls, buttons_));
  144. engine->RegisterObjectProperty("Controls", "float yaw", offsetof(Controls, yaw_));
  145. engine->RegisterObjectProperty("Controls", "float pitch", offsetof(Controls, pitch_));
  146. engine->RegisterObjectProperty("Controls", "VariantMap extraData", offsetof(Controls, extraData_));
  147. }
  148. static Input* GetInput()
  149. {
  150. return GetScriptContext()->GetSubsystem<Input>();
  151. }
  152. static void RegisterInput(asIScriptEngine* engine)
  153. {
  154. RegisterObject<Input>(engine, "Input");
  155. engine->RegisterObjectMethod("Input", "void SetMousePosition(int, int)", asMETHODPR(Input, SetMousePosition, (int, int), void), asCALL_THISCALL);
  156. engine->RegisterObjectMethod("Input", "void SuppressNextChar()", asMETHOD(Input, SuppressNextChar), asCALL_THISCALL);
  157. engine->RegisterObjectMethod("Input", "void set_clipCursor(bool)", asMETHOD(Input, SetClipCursor), asCALL_THISCALL);
  158. engine->RegisterObjectMethod("Input", "bool get_clipCursor() const", asMETHOD(Input, GetClipCursor), asCALL_THISCALL);
  159. engine->RegisterObjectMethod("Input", "void set_toggleFullscreen(bool)", asMETHOD(Input, SetToggleFullscreen), asCALL_THISCALL);
  160. engine->RegisterObjectMethod("Input", "bool get_toggleFullscreen() const", asMETHOD(Input, GetToggleFullscreen), asCALL_THISCALL);
  161. engine->RegisterObjectMethod("Input", "void set_mousePosition(const IntVector2& in)", asMETHODPR(Input, SetMousePosition, (const IntVector2&), void), asCALL_THISCALL);
  162. engine->RegisterObjectMethod("Input", "IntVector2 get_mousePosition() const", asMETHOD(Input, GetMousePosition), asCALL_THISCALL);
  163. engine->RegisterObjectMethod("Input", "bool get_keyDown(int) const", asMETHOD(Input, GetKeyDown), asCALL_THISCALL);
  164. engine->RegisterObjectMethod("Input", "bool get_keyPress(int) const", asMETHOD(Input, GetKeyPress), asCALL_THISCALL);
  165. engine->RegisterObjectMethod("Input", "bool get_mouseButtonDown(int) const", asMETHOD(Input, GetMouseButtonDown), asCALL_THISCALL);
  166. engine->RegisterObjectMethod("Input", "bool get_mouseButtonPress(int) const", asMETHOD(Input, GetMouseButtonPress), asCALL_THISCALL);
  167. engine->RegisterObjectMethod("Input", "bool get_qualifierDown(int) const", asMETHOD(Input, GetQualifierDown), asCALL_THISCALL);
  168. engine->RegisterObjectMethod("Input", "bool get_qualifierPress(int) const", asMETHOD(Input, GetQualifierPress), asCALL_THISCALL);
  169. engine->RegisterObjectMethod("Input", "int get_qualifiers() const", asMETHOD(Input, GetQualifiers), asCALL_THISCALL);
  170. engine->RegisterObjectMethod("Input", "const IntVector2& get_mouseMove() const", asMETHOD(Input, GetMouseMove), asCALL_THISCALL);
  171. engine->RegisterObjectMethod("Input", "int get_mouseMoveX() const", asMETHOD(Input, GetMouseMoveX), asCALL_THISCALL);
  172. engine->RegisterObjectMethod("Input", "int get_mouseMoveY() const", asMETHOD(Input, GetMouseMoveY), asCALL_THISCALL);
  173. engine->RegisterObjectMethod("Input", "int get_mouseMoveWheel() const", asMETHOD(Input, GetMouseMoveWheel), asCALL_THISCALL);
  174. engine->RegisterObjectMethod("Input", "bool get_active() const", asMETHOD(Input, IsActive), asCALL_THISCALL);
  175. engine->RegisterObjectMethod("Input", "bool get_minimized() const", asMETHOD(Input, IsMinimized), asCALL_THISCALL);
  176. engine->RegisterGlobalFunction("Input@+ get_input()", asFUNCTION(GetInput), asCALL_CDECL);
  177. }
  178. void RegisterInputAPI(asIScriptEngine* engine)
  179. {
  180. RegisterKeyCodes(engine);
  181. RegisterControls(engine);
  182. RegisterInput(engine);
  183. }