LuaScriptInstance.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /// \file
  23. #pragma once
  24. #include "../LuaScript/LuaScriptEventListener.h"
  25. #include "../Scene/Component.h"
  26. struct lua_State;
  27. namespace Urho3D
  28. {
  29. class LuaFile;
  30. class LuaFunction;
  31. class LuaScript;
  32. class LuaScriptEventInvoker;
  33. /// Lua Script object methods.
  34. enum LuaScriptObjectMethod
  35. {
  36. LSOM_START = 0,
  37. LSOM_STOP,
  38. LSOM_DELAYEDSTART,
  39. LSOM_UPDATE,
  40. LSOM_POSTUPDATE,
  41. LSOM_FIXEDUPDATE,
  42. LSOM_FIXEDPOSTUPDATE,
  43. LSOM_LOAD,
  44. LSOM_SAVE,
  45. LSOM_READNETWORKUPDATE,
  46. LSOM_WRITENETWORKUPDATE,
  47. LSOM_APPLYATTRIBUTES,
  48. LSOM_TRANSFORMCHANGED,
  49. MAX_LUA_SCRIPT_OBJECT_METHODS
  50. };
  51. /// Lua script object component.
  52. class URHO3D_API LuaScriptInstance : public Component, public LuaScriptEventListener
  53. {
  54. URHO3D_OBJECT(LuaScriptInstance, Component);
  55. public:
  56. /// Construct.
  57. explicit LuaScriptInstance(Context* context);
  58. /// Destruct.
  59. ~LuaScriptInstance() override;
  60. /// Register object factory.
  61. /// @nobind
  62. static void RegisterObject(Context* context);
  63. /// Handle attribute write access.
  64. void OnSetAttribute(const AttributeInfo& attr, const Variant& src) override;
  65. /// Handle attribute read access.
  66. void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const override;
  67. /// Return attribute descriptions, or null if none defined.
  68. const Vector<AttributeInfo>* GetAttributes() const override { return &attributeInfos_; }
  69. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  70. void ApplyAttributes() override;
  71. /// Handle enabled/disabled state change.
  72. void OnSetEnabled() override;
  73. /// Add a scripted event handler by function.
  74. void AddEventHandler(const String& eventName, int functionIndex) override;
  75. /// Add a scripted event handler by function name.
  76. void AddEventHandler(const String& eventName, const String& functionName) override;
  77. /// Add a scripted event handler by function for a specific sender.
  78. void AddEventHandler(Object* sender, const String& eventName, int functionIndex) override;
  79. /// Add a scripted event handler by function name for a specific sender.
  80. void AddEventHandler(Object* sender, const String& eventName, const String& functionName) override;
  81. /// Remove a scripted event handler.
  82. void RemoveEventHandler(const String& eventName) override;
  83. /// Remove a scripted event handler for a specific sender.
  84. void RemoveEventHandler(Object* sender, const String& eventName) override;
  85. /// Remove all scripted event handlers for a specific sender.
  86. void RemoveEventHandlers(Object* sender) override;
  87. /// Remove all scripted event handlers.
  88. void RemoveAllEventHandlers() override;
  89. /// Remove all scripted event handlers, except those listed.
  90. void RemoveEventHandlersExcept(const Vector<String>& exceptionNames) override;
  91. /// Return whether has subscribed to an event.
  92. bool HasEventHandler(const String& eventName) const override;
  93. /// Return whether has subscribed to a specific sender's event.
  94. bool HasEventHandler(Object* sender, const String& eventName) const override;
  95. /// Create script object. Return true if successful.
  96. bool CreateObject(const String& scriptObjectType);
  97. /// Create script object. Return true if successful.
  98. bool CreateObject(LuaFile* scriptFile, const String& scriptObjectType);
  99. /// Set script file.
  100. void SetScriptFile(LuaFile* scriptFile);
  101. /// Set script object type.
  102. void SetScriptObjectType(const String& scriptObjectType);
  103. /// Set script file serialization attribute by calling a script function.
  104. void SetScriptDataAttr(const PODVector<unsigned char>& data);
  105. /// Set script network serialization attribute by calling a script function.
  106. void SetScriptNetworkDataAttr(const PODVector<unsigned char>& data);
  107. /// Return script file.
  108. LuaFile* GetScriptFile() const;
  109. /// Return script object type.
  110. const String& GetScriptObjectType() const { return scriptObjectType_; }
  111. /// Return Lua reference to script object.
  112. int GetScriptObjectRef() const { return scriptObjectRef_; }
  113. /// Get script file serialization attribute by calling a script function.
  114. PODVector<unsigned char> GetScriptDataAttr() const;
  115. /// Get script network serialization attribute by calling a script function.
  116. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  117. /// Return script object's funcition.
  118. LuaFunction* GetScriptObjectFunction(const String& functionName) const;
  119. /// Set script file attribute.
  120. void SetScriptFileAttr(const ResourceRef& value);
  121. /// Return script file attribute.
  122. ResourceRef GetScriptFileAttr() const;
  123. protected:
  124. /// Handle scene being assigned.
  125. void OnSceneSet(Scene* scene) override;
  126. /// Handle node transform being dirtied.
  127. void OnMarkedDirty(Node* node) override;
  128. private:
  129. /// Find script object attributes.
  130. void GetScriptAttributes();
  131. /// Find script object method refs.
  132. void FindScriptObjectMethodRefs();
  133. /// Subscribe to script method events.
  134. void SubscribeToScriptMethodEvents();
  135. /// Unsubscribe from script method events.
  136. void UnsubscribeFromScriptMethodEvents();
  137. /// Handle the logic update event.
  138. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  139. /// Handle the logic post update event.
  140. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  141. #if defined(URHO3D_PHYSICS) || defined(URHO3D_URHO2D)
  142. /// Handle the physics update event.
  143. void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
  144. /// Handle the physics post update event.
  145. void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
  146. #endif
  147. /// Release the script object.
  148. void ReleaseObject();
  149. /// Lua Script subsystem.
  150. LuaScript* luaScript_{};
  151. /// Lua state.
  152. lua_State* luaState_{};
  153. /// Event invoker.
  154. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  155. /// Script file.
  156. SharedPtr<LuaFile> scriptFile_;
  157. /// Script object type.
  158. String scriptObjectType_;
  159. /// Attributes, including script object variables.
  160. Vector<AttributeInfo> attributeInfos_;
  161. /// Lua reference to script object.
  162. int scriptObjectRef_{};
  163. /// Script object method.
  164. LuaFunction* scriptObjectMethods_[MAX_LUA_SCRIPT_OBJECT_METHODS]{};
  165. };
  166. }