LuaScriptInstance.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #pragma once
  23. #include "Component.h"
  24. struct lua_State;
  25. namespace Urho3D
  26. {
  27. class LuaScript;
  28. /// Lua Script object methods.
  29. enum LuaScriptObjectMethod
  30. {
  31. LSOM_START = 0,
  32. LSOM_STOP,
  33. // LSOM_DELAYEDSTART,
  34. LSOM_UPDATE,
  35. LSOM_POSTUPDATE,
  36. LSOM_FIXEDUPDATE,
  37. LSOM_FIXEDPOSTUPDATE,
  38. LSOM_LOAD,
  39. LSOM_SAVE,
  40. LSOM_READNETWORKUPDATE,
  41. LSOM_WRITENETWORKUPDATE,
  42. LSOM_APPLYATTRIBUTES,
  43. MAX_LUA_SCRIPT_OBJECT_METHODS
  44. };
  45. /// Lua script instance.
  46. class URHO3D_API LuaScriptInstance : public Component
  47. {
  48. OBJECT(LuaScriptInstance);
  49. public:
  50. /// Construct.
  51. LuaScriptInstance(Context* context);
  52. /// Destruct.
  53. ~LuaScriptInstance();
  54. /// Register object factory.
  55. static void RegisterObject(Context* context);
  56. ///// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  57. virtual void ApplyAttributes();
  58. /// Create script object.
  59. bool CreateObject(const String& scriptObjectType);
  60. /// Create script object.
  61. bool CreateObject(const String& scriptFileName, const String& scriptObjectType);
  62. /// Set script file name.
  63. void SetScriptFileName(const String& scriptFileName);
  64. /// Set script object type.
  65. void SetScriptObjectType(const String& scriptObjectType);
  66. /// Set script file serialization attribute by calling a script function.
  67. void SetScriptDataAttr(PODVector<unsigned char> data);
  68. /// Set script network serialization attribute by calling a script function.
  69. void SetScriptNetworkDataAttr(PODVector<unsigned char> data);
  70. /// Script subscribe to an event that can by send by any sender.
  71. void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
  72. /// Script unsubscribe from an event.
  73. void ScriptUnsubscribeFromEvent(const String& eventName);
  74. /// Script unsubscribe from all events.
  75. void ScriptUnsubscribeFromAllEvents();
  76. /// Script subscribe to a specific sender's event.
  77. void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
  78. /// Script unsubscribe from a specific sender's event.
  79. void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
  80. /// Script unsubscribe from a specific sender's all events.
  81. void ScriptUnsubscribeFromEvents(void* sender);
  82. /// Return script file name.
  83. const String& GetScriptFileName() const { return scriptFileName_; }
  84. /// Return script object type.
  85. const String& GetScriptObjectType() const { return scriptObjectType_; }
  86. /// Return script object ref.
  87. int GetScriptObjectRef() const { return scriptObjectRef_; }
  88. /// Get script file serialization attribute by calling a script function.
  89. PODVector<unsigned char> GetScriptDataAttr() const;
  90. /// Get script network serialization attribute by calling a script function.
  91. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  92. private:
  93. /// Find script object method refs.
  94. void FindScriptObjectMethodRefs();
  95. /// Handle the logic update event.
  96. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  97. /// Handle the logic post update event.
  98. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  99. /// Handle the physics update event.
  100. void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
  101. /// Handle the physics post update event.
  102. void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
  103. /// Handle event.
  104. void HandleEvent(StringHash eventType, VariantMap& eventData);
  105. /// Handle object event.
  106. void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
  107. /// Release the script object.
  108. void ReleaseObject();
  109. /// Call script object function.
  110. void CallScriptObjectFunction(int functionRef);
  111. /// Call script object function.
  112. void CallScriptObjectFunction(int functionRef, float timeStep);
  113. /// Call script object function.
  114. void CallScriptObjectFunction(int functionRef, Deserializer& deserializer);
  115. /// Call script object function.
  116. void CallScriptObjectFunction(int functionRef, Serializer& serializer) const;
  117. /// Call script object function.
  118. void CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData);
  119. // Lua Script.
  120. LuaScript* luaScript_;
  121. /// Lua state.
  122. lua_State* luaState_;
  123. /// Script file name.
  124. String scriptFileName_;
  125. /// Script object type.
  126. String scriptObjectType_;
  127. /// Script object ref.
  128. int scriptObjectRef_;
  129. /// Script object method refs.
  130. int scriptObjectMethodRefs_[MAX_LUA_SCRIPT_OBJECT_METHODS];
  131. /// Event type to function ref map.
  132. HashMap<StringHash, int> eventTypeToFunctionRefMap_;
  133. /// Object to event type to function ref map.
  134. HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
  135. };
  136. }