CoreAPI.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // Copyright (c) 2008-2020 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 "../AngelScript/APITemplates.h"
  24. #include "../Core/ProcessUtils.h"
  25. #include "../Core/Spline.h"
  26. namespace Urho3D
  27. {
  28. void ArrayToVariantVector(CScriptArray* arr, VariantVector& dest)
  29. {
  30. unsigned size = arr->GetSize();
  31. dest.Resize(size);
  32. for (unsigned i = 0; i < size; ++i)
  33. dest[i] = *(static_cast<Variant*>(arr->At(i)));
  34. }
  35. void ArrayToStringVector(CScriptArray* arr, StringVector& dest)
  36. {
  37. unsigned size = arr->GetSize();
  38. dest.Resize(size);
  39. for (unsigned i = 0; i < size; ++i)
  40. dest[i] = *(static_cast<String*>(arr->At(i)));
  41. }
  42. static void RegisterVariant(asIScriptEngine* engine)
  43. {
  44. engine->RegisterGlobalFunction("VariantType GetVariantTypeFromName(const String&in)", asFUNCTIONPR(Variant::GetTypeFromName, (const String&), VariantType), asCALL_CDECL);
  45. engine->RegisterGlobalFunction("String GetVariantTypeName(VariantType)", asFUNCTIONPR(Variant::GetTypeName, (VariantType), String), asCALL_CDECL);
  46. }
  47. static String StringJoined(CScriptArray* arr, const String& glue)
  48. {
  49. Vector<String> subStrings = ArrayToVector<String>(arr);
  50. return String::Joined(subStrings, glue);
  51. }
  52. static void RegisterStringUtils(asIScriptEngine* engine)
  53. {
  54. engine->RegisterObjectMethod("String", "bool ToBool() const", asFUNCTIONPR(ToBool, (const String&), bool), asCALL_CDECL_OBJLAST);
  55. engine->RegisterObjectMethod("String", "float ToFloat() const", asFUNCTIONPR(ToFloat, (const String&), float), asCALL_CDECL_OBJLAST);
  56. engine->RegisterObjectMethod("String", "double ToDouble() const", asFUNCTIONPR(ToDouble, (const String&), double), asCALL_CDECL_OBJLAST);
  57. engine->RegisterObjectMethod("String", "int ToInt(int base = 10) const", asFUNCTIONPR(ToInt, (const String&, int), int), asCALL_CDECL_OBJFIRST);
  58. engine->RegisterObjectMethod("String", "uint ToUInt(int base = 10) const", asFUNCTIONPR(ToUInt, (const String&, int), unsigned), asCALL_CDECL_OBJFIRST);
  59. engine->RegisterObjectMethod("String", "int64 ToInt64(int base = 10) const", asFUNCTIONPR(ToInt64, (const String&, int), long long), asCALL_CDECL_OBJFIRST);
  60. engine->RegisterObjectMethod("String", "uint64 ToUInt64(int base = 10) const", asFUNCTIONPR(ToUInt64, (const String&, int), unsigned long long), asCALL_CDECL_OBJFIRST);
  61. engine->RegisterObjectMethod("String", "Color ToColor() const", asFUNCTIONPR(ToColor, (const String&), Color), asCALL_CDECL_OBJLAST);
  62. engine->RegisterObjectMethod("String", "IntRect ToIntRect() const", asFUNCTIONPR(ToIntRect, (const String&), IntRect), asCALL_CDECL_OBJLAST);
  63. engine->RegisterObjectMethod("String", "IntVector2 ToIntVector2() const", asFUNCTIONPR(ToIntVector2, (const String&), IntVector2), asCALL_CDECL_OBJLAST);
  64. engine->RegisterObjectMethod("String", "IntVector3 ToIntVector3() const", asFUNCTIONPR(ToIntVector3, (const String&), IntVector3), asCALL_CDECL_OBJLAST);
  65. engine->RegisterObjectMethod("String", "Quaternion ToQuaternion() const", asFUNCTIONPR(ToQuaternion, (const String&), Quaternion), asCALL_CDECL_OBJLAST);
  66. engine->RegisterObjectMethod("String", "Vector2 ToVector2() const", asFUNCTIONPR(ToVector2, (const String&), Vector2), asCALL_CDECL_OBJLAST);
  67. engine->RegisterObjectMethod("String", "Vector3 ToVector3() const", asFUNCTIONPR(ToVector3, (const String&), Vector3), asCALL_CDECL_OBJLAST);
  68. engine->RegisterObjectMethod("String", "Vector4 ToVector4(bool allowMissingCoords = false) const", asFUNCTIONPR(ToVector4, (const String&, bool), Vector4), asCALL_CDECL_OBJFIRST);
  69. engine->RegisterObjectMethod("String", "Variant ToVectorVariant() const", asFUNCTIONPR(ToVectorVariant, (const String&), Variant), asCALL_CDECL_OBJLAST);
  70. engine->RegisterObjectMethod("String", "Matrix3 ToMatrix3() const", asFUNCTIONPR(ToMatrix3, (const String&), Matrix3), asCALL_CDECL_OBJLAST);
  71. engine->RegisterObjectMethod("String", "Matrix3x4 ToMatrix3x4() const", asFUNCTIONPR(ToMatrix3x4, (const String&), Matrix3x4), asCALL_CDECL_OBJLAST);
  72. engine->RegisterObjectMethod("String", "Matrix4 ToMatrix4() const", asFUNCTIONPR(ToMatrix4, (const String&), Matrix4), asCALL_CDECL_OBJLAST);
  73. //engine->RegisterGlobalFunction("String ToStringHex(int)", asFUNCTION(ToStringHex), asCALL_CDECL);
  74. engine->RegisterGlobalFunction("String Join(String[]&, const String&in glue)", asFUNCTION(StringJoined), asCALL_CDECL);
  75. }
  76. static Object* CreateObject(const String& objectType)
  77. {
  78. if (Context* context = GetScriptContext())
  79. {
  80. if (SharedPtr<Object> object = context->CreateObject(objectType))
  81. {
  82. object->AddRef();
  83. return object;
  84. }
  85. }
  86. return nullptr;
  87. }
  88. static void SendEvent(const String& eventType, VariantMap& eventData)
  89. {
  90. Object* sender = GetScriptContextEventListenerObject();
  91. if (sender)
  92. sender->SendEvent(eventType, eventData);
  93. }
  94. static void SubscribeToEvent(const String& eventType, const String& handlerName)
  95. {
  96. ScriptEventListener* listener = GetScriptContextEventListener();
  97. if (listener)
  98. listener->AddEventHandler(eventType, handlerName);
  99. }
  100. static void SubscribeToSenderEvent(Object* sender, const String& eventType, const String& handlerName)
  101. {
  102. ScriptEventListener* listener = GetScriptContextEventListener();
  103. if (listener)
  104. listener->AddEventHandler(sender, eventType, handlerName);
  105. }
  106. static void UnsubscribeFromEvent(const String& eventType)
  107. {
  108. ScriptEventListener* listener = GetScriptContextEventListener();
  109. if (listener)
  110. listener->RemoveEventHandler(eventType);
  111. }
  112. static void UnsubscribeFromSenderEvent(Object* sender, const String& eventType)
  113. {
  114. ScriptEventListener* listener = GetScriptContextEventListener();
  115. if (listener)
  116. listener->RemoveEventHandler(sender, eventType);
  117. }
  118. static void UnsubscribeFromSenderEvents(Object* sender)
  119. {
  120. ScriptEventListener* listener = GetScriptContextEventListener();
  121. if (listener)
  122. listener->RemoveEventHandlers(sender);
  123. }
  124. static void UnsubscribeFromAllEvents()
  125. {
  126. ScriptEventListener* listener = GetScriptContextEventListener();
  127. if (listener)
  128. listener->RemoveEventHandlers();
  129. }
  130. static void UnsubscribeFromAllEventsExcept(CScriptArray* exceptions)
  131. {
  132. ScriptEventListener* listener = GetScriptContextEventListener();
  133. if (!listener || !exceptions)
  134. return;
  135. unsigned numExceptions = exceptions->GetSize();
  136. PODVector<StringHash> destExceptions(numExceptions);
  137. for (unsigned i = 0; i < numExceptions; ++i)
  138. destExceptions[i] = StringHash(*(static_cast<String*>(exceptions->At(i))));
  139. listener->RemoveEventHandlersExcept(destExceptions);
  140. }
  141. static bool HasSubscribedToEvent(const String& eventType)
  142. {
  143. ScriptEventListener* listener = GetScriptContextEventListener();
  144. return listener ? listener->HasEventHandler(StringHash(eventType)) : false;
  145. }
  146. static bool HasSubscribedToSenderEvent(Object* sender, const String& eventType)
  147. {
  148. ScriptEventListener* listener = GetScriptContextEventListener();
  149. return listener ? listener->HasEventHandler(sender, StringHash(eventType)) : false;
  150. }
  151. static void RegisterEventName(const String& eventName)
  152. {
  153. GetEventNameRegister().RegisterString(eventName.CString());
  154. }
  155. static Object* GetEventSender()
  156. {
  157. return GetScriptContext()->GetEventSender();
  158. }
  159. static const String& GetTypeName(StringHash type)
  160. {
  161. return GetScriptContext()->GetTypeName(type);
  162. }
  163. static void SetGlobalVar(const String& key, const Variant& value)
  164. {
  165. GetScriptContext()->SetGlobalVar(key, value);
  166. }
  167. static Variant GetGlobalVar(const String& key)
  168. {
  169. return GetScriptContext()->GetGlobalVar(key);
  170. }
  171. static VariantMap& GetGlobalVars()
  172. {
  173. return const_cast<VariantMap&>(GetScriptContext()->GetGlobalVars());
  174. }
  175. void RegisterObject(asIScriptEngine* engine)
  176. {
  177. engine->RegisterGlobalFunction("Object@ CreateObject(const String&in)", asFUNCTION(CreateObject), asCALL_CDECL);
  178. engine->RegisterGlobalFunction("void SendEvent(const String&in, VariantMap& eventData = VariantMap())", asFUNCTION(SendEvent), asCALL_CDECL);
  179. engine->RegisterGlobalFunction("void SubscribeToEvent(const String&in, const String&in)", asFUNCTION(SubscribeToEvent), asCALL_CDECL);
  180. engine->RegisterGlobalFunction("void SubscribeToEvent(Object@+, const String&in, const String&in)", asFUNCTION(SubscribeToSenderEvent), asCALL_CDECL);
  181. engine->RegisterGlobalFunction("void UnsubscribeFromEvent(const String&in)", asFUNCTION(UnsubscribeFromEvent), asCALL_CDECL);
  182. engine->RegisterGlobalFunction("void UnsubscribeFromEvent(Object@+, const String&in)", asFUNCTION(UnsubscribeFromSenderEvent), asCALL_CDECL);
  183. engine->RegisterGlobalFunction("void UnsubscribeFromEvents(Object@+)", asFUNCTION(UnsubscribeFromSenderEvents), asCALL_CDECL);
  184. engine->RegisterGlobalFunction("void UnsubscribeFromAllEvents()", asFUNCTION(UnsubscribeFromAllEvents), asCALL_CDECL);
  185. engine->RegisterGlobalFunction("void UnsubscribeFromAllEventsExcept(Array<String>@+)", asFUNCTION(UnsubscribeFromAllEventsExcept), asCALL_CDECL);
  186. engine->RegisterGlobalFunction("bool HasSubscribedToEvent(const String&in)", asFUNCTION(HasSubscribedToEvent), asCALL_CDECL);
  187. engine->RegisterGlobalFunction("bool HasSubscribedToEvent(Object@+, const String&in)", asFUNCTION(HasSubscribedToSenderEvent), asCALL_CDECL);
  188. engine->RegisterGlobalFunction("void RegisterEventName(const String&in)", asFUNCTION(RegisterEventName), asCALL_CDECL);
  189. engine->RegisterGlobalFunction("Object@+ GetEventSender()", asFUNCTION(GetEventSender), asCALL_CDECL);
  190. engine->RegisterGlobalFunction("const String& GetTypeName(StringHash)", asFUNCTION(GetTypeName), asCALL_CDECL);
  191. engine->RegisterGlobalFunction("void SetGlobalVar(const String&in, Variant&in)", asFUNCTION(SetGlobalVar), asCALL_CDECL);
  192. engine->RegisterGlobalFunction("Variant GetGlobalVar(const String&in)", asFUNCTION(GetGlobalVar), asCALL_CDECL);
  193. engine->RegisterGlobalFunction("VariantMap& get_globalVars()", asFUNCTION(GetGlobalVars), asCALL_CDECL);
  194. }
  195. void RegisterCoreAPI(asIScriptEngine* engine)
  196. {
  197. RegisterVariant(engine);
  198. RegisterStringUtils(engine);
  199. RegisterObject(engine);
  200. }
  201. }