LuaScriptInstance.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. static void RegisterObject(Context* context);
  62. /// Handle attribute write access.
  63. void OnSetAttribute(const AttributeInfo& attr, const Variant& src) override;
  64. /// Handle attribute read access.
  65. void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const override;
  66. /// Return attribute descriptions, or null if none defined.
  67. const Vector<AttributeInfo>* GetAttributes() const override { return &attributeInfos_; }
  68. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  69. void ApplyAttributes() override;
  70. /// Handle enabled/disabled state change.
  71. void OnSetEnabled() override;
  72. /// Add a scripted event handler by function.
  73. void AddEventHandler(const String& eventName, int functionIndex) override;
  74. /// Add a scripted event handler by function name.
  75. void AddEventHandler(const String& eventName, const String& functionName) override;
  76. /// Add a scripted event handler by function for a specific sender.
  77. void AddEventHandler(Object* sender, const String& eventName, int functionIndex) override;
  78. /// Add a scripted event handler by function name for a specific sender.
  79. void AddEventHandler(Object* sender, const String& eventName, const String& functionName) override;
  80. /// Remove a scripted event handler.
  81. void RemoveEventHandler(const String& eventName) override;
  82. /// Remove a scripted event handler for a specific sender.
  83. void RemoveEventHandler(Object* sender, const String& eventName) override;
  84. /// Remove all scripted event handlers for a specific sender.
  85. void RemoveEventHandlers(Object* sender) override;
  86. /// Remove all scripted event handlers.
  87. void RemoveAllEventHandlers() override;
  88. /// Remove all scripted event handlers, except those listed.
  89. void RemoveEventHandlersExcept(const Vector<String>& exceptionNames) override;
  90. /// Return whether has subscribed to an event.
  91. bool HasEventHandler(const String& eventName) const override;
  92. /// Return whether has subscribed to a specific sender's event.
  93. bool HasEventHandler(Object* sender, const String& eventName) const override;
  94. /// Create script object. Return true if successful.
  95. bool CreateObject(const String& scriptObjectType);
  96. /// Create script object. Return true if successful.
  97. bool CreateObject(LuaFile* scriptFile, const String& scriptObjectType);
  98. /// Set script file.
  99. void SetScriptFile(LuaFile* scriptFile);
  100. /// Set script object type.
  101. void SetScriptObjectType(const String& scriptObjectType);
  102. /// Set script file serialization attribute by calling a script function.
  103. void SetScriptDataAttr(const PODVector<unsigned char>& data);
  104. /// Set script network serialization attribute by calling a script function.
  105. void SetScriptNetworkDataAttr(const PODVector<unsigned char>& data);
  106. /// Return script file.
  107. LuaFile* GetScriptFile() const;
  108. /// Return script object type.
  109. const String& GetScriptObjectType() const { return scriptObjectType_; }
  110. /// Return Lua reference to script object.
  111. int GetScriptObjectRef() const { return scriptObjectRef_; }
  112. /// Get script file serialization attribute by calling a script function.
  113. PODVector<unsigned char> GetScriptDataAttr() const;
  114. /// Get script network serialization attribute by calling a script function.
  115. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  116. /// Return script object's funcition.
  117. LuaFunction* GetScriptObjectFunction(const String& functionName) const;
  118. /// Set script file attribute.
  119. void SetScriptFileAttr(const ResourceRef& value);
  120. /// Return script file attribute.
  121. ResourceRef GetScriptFileAttr() const;
  122. protected:
  123. /// Handle scene being assigned.
  124. void OnSceneSet(Scene* scene) override;
  125. /// Handle node transform being dirtied.
  126. void OnMarkedDirty(Node* node) override;
  127. private:
  128. /// Find script object attributes.
  129. void GetScriptAttributes();
  130. /// Find script object method refs.
  131. void FindScriptObjectMethodRefs();
  132. /// Subscribe to script method events.
  133. void SubscribeToScriptMethodEvents();
  134. /// Unsubscribe from script method events.
  135. void UnsubscribeFromScriptMethodEvents();
  136. /// Handle the logic update event.
  137. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  138. /// Handle the logic post update event.
  139. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  140. #if defined(URHO3D_PHYSICS) || defined(URHO3D_URHO2D)
  141. /// Handle the physics update event.
  142. void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
  143. /// Handle the physics post update event.
  144. void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
  145. #endif
  146. /// Release the script object.
  147. void ReleaseObject();
  148. /// Lua Script subsystem.
  149. LuaScript* luaScript_{};
  150. /// Lua state.
  151. lua_State* luaState_{};
  152. /// Event invoker.
  153. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  154. /// Script file.
  155. SharedPtr<LuaFile> scriptFile_;
  156. /// Script object type.
  157. String scriptObjectType_;
  158. /// Attributes, including script object variables.
  159. Vector<AttributeInfo> attributeInfos_;
  160. /// Lua reference to script object.
  161. int scriptObjectRef_{};
  162. /// Script object method.
  163. LuaFunction* scriptObjectMethods_[MAX_LUA_SCRIPT_OBJECT_METHODS]{};
  164. };
  165. }