LuaScriptInstance.h 6.0 KB

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