LuaScriptInstance.h 7.0 KB

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