InputAPI.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "APITemplates.h"
  24. #include "Input.h"
  25. namespace Urho3D
  26. {
  27. void FakeAddRef(void* ptr);
  28. void FakeReleaseRef(void* ptr);
  29. static void RegisterInputConstants(asIScriptEngine* engine)
  30. {
  31. engine->RegisterGlobalProperty("const int MOUSEB_LEFT", (void*)&MOUSEB_LEFT);
  32. engine->RegisterGlobalProperty("const int MOUSEB_RIGHT", (void*)&MOUSEB_RIGHT);
  33. engine->RegisterGlobalProperty("const int MOUSEB_MIDDLE", (void*)&MOUSEB_MIDDLE);
  34. engine->RegisterGlobalProperty("const int QUAL_SHIFT", (void*)&QUAL_SHIFT);
  35. engine->RegisterGlobalProperty("const int QUAL_CTRL", (void*)&QUAL_CTRL);
  36. engine->RegisterGlobalProperty("const int QUAL_ALT", (void*)&QUAL_ALT);
  37. engine->RegisterGlobalProperty("const int QUAL_ANY", (void*)&QUAL_ANY);
  38. engine->RegisterGlobalProperty("const int KEY_BACKSPACE", (void*)&KEY_BACKSPACE);
  39. engine->RegisterGlobalProperty("const int KEY_TAB", (void*)&KEY_TAB);
  40. engine->RegisterGlobalProperty("const int KEY_RETURN", (void*)&KEY_RETURN);
  41. engine->RegisterGlobalProperty("const int KEY_RETURN2", (void*)&KEY_RETURN2);
  42. engine->RegisterGlobalProperty("const int KEY_KP_ENTER", (void*)&KEY_KP_ENTER);
  43. engine->RegisterGlobalProperty("const int KEY_PAUSE", (void*)&KEY_PAUSE);
  44. engine->RegisterGlobalProperty("const int KEY_CAPSLOCK", (void*)&KEY_CAPSLOCK);
  45. engine->RegisterGlobalProperty("const int KEY_ESC", (void*)&KEY_ESC);
  46. engine->RegisterGlobalProperty("const int KEY_SPACE", (void*)&KEY_SPACE);
  47. engine->RegisterGlobalProperty("const int KEY_PAGEUP", (void*)&KEY_PAGEUP);
  48. engine->RegisterGlobalProperty("const int KEY_PAGEDOWN", (void*)&KEY_PAGEDOWN);
  49. engine->RegisterGlobalProperty("const int KEY_END", (void*)&KEY_END);
  50. engine->RegisterGlobalProperty("const int KEY_HOME", (void*)&KEY_HOME);
  51. engine->RegisterGlobalProperty("const int KEY_LEFT", (void*)&KEY_LEFT);
  52. engine->RegisterGlobalProperty("const int KEY_UP", (void*)&KEY_UP);
  53. engine->RegisterGlobalProperty("const int KEY_RIGHT", (void*)&KEY_RIGHT);
  54. engine->RegisterGlobalProperty("const int KEY_DOWN", (void*)&KEY_DOWN);
  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 HAT_CENTER", (void*)&HAT_CENTER);
  108. engine->RegisterGlobalProperty("const int HAT_UP", (void*)&HAT_UP);
  109. engine->RegisterGlobalProperty("const int HAT_RIGHT", (void*)&HAT_RIGHT);
  110. engine->RegisterGlobalProperty("const int HAT_DOWN", (void*)&HAT_DOWN);
  111. engine->RegisterGlobalProperty("const int HAT_LEFT", (void*)&HAT_LEFT);
  112. }
  113. static Input* GetInput()
  114. {
  115. return GetScriptContext()->GetSubsystem<Input>();
  116. }
  117. static void RegisterInput(asIScriptEngine* engine)
  118. {
  119. engine->RegisterObjectType("TouchState", 0, asOBJ_REF);
  120. engine->RegisterObjectBehaviour("TouchState", asBEHAVE_ADDREF, "void f()", asFUNCTION(FakeAddRef), asCALL_CDECL_OBJLAST);
  121. engine->RegisterObjectBehaviour("TouchState", asBEHAVE_RELEASE, "void f()", asFUNCTION(FakeReleaseRef), asCALL_CDECL_OBJLAST);
  122. engine->RegisterObjectProperty("TouchState", "const int touchID", offsetof(TouchState, touchID_));
  123. engine->RegisterObjectProperty("TouchState", "const IntVector2 position", offsetof(TouchState, position_));
  124. engine->RegisterObjectProperty("TouchState", "const IntVector2 delta", offsetof(TouchState, delta_));
  125. engine->RegisterObjectProperty("TouchState", "const float pressure", offsetof(TouchState, pressure_));
  126. engine->RegisterObjectType("JoystickState", 0, asOBJ_REF);
  127. engine->RegisterObjectBehaviour("JoystickState", asBEHAVE_ADDREF, "void f()", asFUNCTION(FakeAddRef), asCALL_CDECL_OBJLAST);
  128. engine->RegisterObjectBehaviour("JoystickState", asBEHAVE_RELEASE, "void f()", asFUNCTION(FakeReleaseRef), asCALL_CDECL_OBJLAST);
  129. engine->RegisterObjectProperty("JoystickState", "const String name", offsetof(JoystickState, name_));
  130. engine->RegisterObjectMethod("JoystickState", "uint get_numButtons() const", asMETHOD(JoystickState, GetNumButtons), asCALL_THISCALL);
  131. engine->RegisterObjectMethod("JoystickState", "uint get_numAxes() const", asMETHOD(JoystickState, GetNumAxes), asCALL_THISCALL);
  132. engine->RegisterObjectMethod("JoystickState", "uint get_numHats() const", asMETHOD(JoystickState, GetNumHats), asCALL_THISCALL);
  133. engine->RegisterObjectMethod("JoystickState", "bool get_buttonDown(uint) const", asMETHOD(JoystickState, GetButtonDown), asCALL_THISCALL);
  134. engine->RegisterObjectMethod("JoystickState", "bool get_buttonPress(uint) const", asMETHOD(JoystickState, GetButtonPress), asCALL_THISCALL);
  135. engine->RegisterObjectMethod("JoystickState", "float get_axisPosition(uint) const", asMETHOD(JoystickState, GetAxisPosition), asCALL_THISCALL);
  136. engine->RegisterObjectMethod("JoystickState", "int get_hatPosition(uint) const", asMETHOD(JoystickState, GetHatPosition), asCALL_THISCALL);
  137. RegisterObject<Input>(engine, "Input");
  138. engine->RegisterObjectMethod("Input", "bool OpenJoystick(uint)", asMETHOD(Input, OpenJoystick), asCALL_THISCALL);
  139. engine->RegisterObjectMethod("Input", "void CloseJoystick(uint)", asMETHOD(Input, CloseJoystick), asCALL_THISCALL);
  140. engine->RegisterObjectMethod("Input", "bool DetectJoysticks()", asMETHOD(Input, DetectJoysticks), asCALL_THISCALL);
  141. engine->RegisterObjectMethod("Input", "void set_mouseVisible(bool)", asMETHOD(Input, SetMouseVisible), asCALL_THISCALL);
  142. engine->RegisterObjectMethod("Input", "bool get_mouseVisible() const", asMETHOD(Input, IsMouseVisible), asCALL_THISCALL);
  143. engine->RegisterObjectMethod("Input", "void set_toggleFullscreen(bool)", asMETHOD(Input, SetToggleFullscreen), asCALL_THISCALL);
  144. engine->RegisterObjectMethod("Input", "bool get_toggleFullscreen() const", asMETHOD(Input, GetToggleFullscreen), asCALL_THISCALL);
  145. engine->RegisterObjectMethod("Input", "bool get_keyDown(int) const", asMETHOD(Input, GetKeyDown), asCALL_THISCALL);
  146. engine->RegisterObjectMethod("Input", "bool get_keyPress(int) const", asMETHOD(Input, GetKeyPress), asCALL_THISCALL);
  147. engine->RegisterObjectMethod("Input", "bool get_mouseButtonDown(int) const", asMETHOD(Input, GetMouseButtonDown), asCALL_THISCALL);
  148. engine->RegisterObjectMethod("Input", "bool get_mouseButtonPress(int) const", asMETHOD(Input, GetMouseButtonPress), asCALL_THISCALL);
  149. engine->RegisterObjectMethod("Input", "bool get_qualifierDown(int) const", asMETHOD(Input, GetQualifierDown), asCALL_THISCALL);
  150. engine->RegisterObjectMethod("Input", "bool get_qualifierPress(int) const", asMETHOD(Input, GetQualifierPress), asCALL_THISCALL);
  151. engine->RegisterObjectMethod("Input", "int get_qualifiers() const", asMETHOD(Input, GetQualifiers), asCALL_THISCALL);
  152. engine->RegisterObjectMethod("Input", "IntVector2 get_mousePosition() const", asMETHOD(Input, GetMousePosition), asCALL_THISCALL);
  153. engine->RegisterObjectMethod("Input", "const IntVector2& get_mouseMove() const", asMETHOD(Input, GetMouseMove), asCALL_THISCALL);
  154. engine->RegisterObjectMethod("Input", "int get_mouseMoveX() const", asMETHOD(Input, GetMouseMoveX), asCALL_THISCALL);
  155. engine->RegisterObjectMethod("Input", "int get_mouseMoveY() const", asMETHOD(Input, GetMouseMoveY), asCALL_THISCALL);
  156. engine->RegisterObjectMethod("Input", "int get_mouseMoveWheel() const", asMETHOD(Input, GetMouseMoveWheel), asCALL_THISCALL);
  157. engine->RegisterObjectMethod("Input", "uint get_numTouches() const", asMETHOD(Input, GetNumTouches), asCALL_THISCALL);
  158. engine->RegisterObjectMethod("Input", "TouchState@+ get_touches(uint) const", asMETHOD(Input, GetTouch), asCALL_THISCALL);
  159. engine->RegisterObjectMethod("Input", "uint get_numJoysticks() const", asMETHOD(Input, GetNumJoysticks), asCALL_THISCALL);
  160. engine->RegisterObjectMethod("Input", "const String& get_joystickNames(uint) const", asMETHOD(Input, GetJoystickName), asCALL_THISCALL);
  161. engine->RegisterObjectMethod("Input", "JoystickState@+ get_joysticks(uint)", asMETHOD(Input, GetJoystick), asCALL_THISCALL);
  162. engine->RegisterObjectMethod("Input", "bool get_focus() const", asMETHOD(Input, HasFocus), asCALL_THISCALL);
  163. engine->RegisterObjectMethod("Input", "bool get_minimized() const", asMETHOD(Input, IsMinimized), asCALL_THISCALL);
  164. engine->RegisterGlobalFunction("Input@+ get_input()", asFUNCTION(GetInput), asCALL_CDECL);
  165. }
  166. void RegisterInputAPI(asIScriptEngine* engine)
  167. {
  168. RegisterInputConstants(engine);
  169. RegisterInput(engine);
  170. }
  171. }