LuaScriptInstance.h 5.9 KB

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