LuaScriptInstance.h 7.1 KB

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