LuaScriptInstance.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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(PODVector<unsigned char> data);
  78. /// Set script network serialization attribute by calling a script function.
  79. void SetScriptNetworkDataAttr(PODVector<unsigned char> data);
  80. /// Script subscribe to an event that can by send by any sender.
  81. void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
  82. /// Script unsubscribe from an event.
  83. void ScriptUnsubscribeFromEvent(const String& eventName);
  84. /// Script unsubscribe from all events.
  85. void ScriptUnsubscribeFromAllEvents();
  86. /// Script subscribe to a specific sender's event.
  87. void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
  88. /// Script unsubscribe from a specific sender's event.
  89. void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
  90. /// Script unsubscribe from a specific sender's all events.
  91. void ScriptUnsubscribeFromEvents(void* sender);
  92. /// Return script file.
  93. LuaFile* GetScriptFile() const;
  94. /// Return script object type.
  95. const String& GetScriptObjectType() const { return scriptObjectType_; }
  96. /// Return script object ref.
  97. int GetScriptObjectRef() const { return scriptObjectRef_; }
  98. /// Get script file serialization attribute by calling a script function.
  99. PODVector<unsigned char> GetScriptDataAttr() const;
  100. /// Get script network serialization attribute by calling a script function.
  101. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  102. /// Return script object's funcition.
  103. WeakPtr<LuaFunction> GetScriptObjectFunction(const String& functionName) const;
  104. /// Set script file attribute.
  105. void SetScriptFileAttr(ResourceRef value);
  106. /// Return script file attribute.
  107. ResourceRef GetScriptFileAttr() const;
  108. protected:
  109. /// Handle node transform being dirtied.
  110. virtual void OnMarkedDirty(Node* node);
  111. private:
  112. /// Find script object attributes.
  113. void GetScriptAttributes();
  114. /// Find script object method refs.
  115. void FindScriptObjectMethodRefs();
  116. /// Subscribe to script method events.
  117. void SubscribeToScriptMethodEvents();
  118. /// Unsubscribe from script method events.
  119. void UnsubscribeFromScriptMethodEvents();
  120. /// Handle the logic update event.
  121. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  122. /// Handle the logic post update event.
  123. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  124. /// Handle the physics update event.
  125. void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
  126. /// Handle the physics post update event.
  127. void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
  128. /// Handle event.
  129. void HandleEvent(StringHash eventType, VariantMap& eventData);
  130. /// Handle a specific sender's event.
  131. void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
  132. /// Release the script object.
  133. void ReleaseObject();
  134. // Lua Script subsystem.
  135. LuaScript* luaScript_;
  136. /// Lua state.
  137. lua_State* luaState_;
  138. /// Script file.
  139. SharedPtr<LuaFile> scriptFile_;
  140. /// Script object type.
  141. String scriptObjectType_;
  142. /// Attributes, including script object variables.
  143. Vector<AttributeInfo> attributeInfos_;
  144. /// Script object ref.
  145. int scriptObjectRef_;
  146. /// Script object method.
  147. WeakPtr<LuaFunction> scriptObjectMethods_[MAX_LUA_SCRIPT_OBJECT_METHODS];
  148. /// Event type to function map.
  149. HashMap<StringHash, WeakPtr<LuaFunction> > eventTypeToFunctionMap_;
  150. /// Object to event type to function map.
  151. HashMap<Object*, HashMap<StringHash, WeakPtr<LuaFunction> > > objectToEventTypeToFunctionMap_;
  152. };
  153. }