LuaScriptInstance.h 7.1 KB

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