ScriptInstance.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Copyright (c) 2008-2016 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 "../AngelScript/ScriptEventListener.h"
  24. #include "../Scene/Component.h"
  25. class asIScriptFunction;
  26. class asIScriptObject;
  27. namespace Urho3D
  28. {
  29. class Script;
  30. class ScriptFile;
  31. /// Inbuilt scripted component methods.
  32. enum ScriptInstanceMethod
  33. {
  34. METHOD_START = 0,
  35. METHOD_STOP,
  36. METHOD_DELAYEDSTART,
  37. METHOD_UPDATE,
  38. METHOD_POSTUPDATE,
  39. METHOD_FIXEDUPDATE,
  40. METHOD_FIXEDPOSTUPDATE,
  41. METHOD_LOAD,
  42. METHOD_SAVE,
  43. METHOD_READNETWORKUPDATE,
  44. METHOD_WRITENETWORKUPDATE,
  45. METHOD_APPLYATTRIBUTES,
  46. METHOD_TRANSFORMCHANGED,
  47. MAX_SCRIPT_METHODS
  48. };
  49. /// %Script object component.
  50. class URHO3D_API ScriptInstance : public Component, public ScriptEventListener
  51. {
  52. URHO3D_OBJECT(ScriptInstance, Component);
  53. public:
  54. /// Construct.
  55. ScriptInstance(Context* context);
  56. /// Destruct.
  57. virtual ~ScriptInstance();
  58. /// Register object factory.
  59. static void RegisterObject(Context* context);
  60. /// Handle attribute write access.
  61. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  62. /// Handle attribute read access.
  63. virtual void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const;
  64. /// Return attribute descriptions, or null if none defined.
  65. virtual const Vector<AttributeInfo>* GetAttributes() const { return &attributeInfos_; }
  66. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  67. virtual void ApplyAttributes();
  68. /// Handle enabled/disabled state change.
  69. virtual void OnSetEnabled();
  70. /// Add a scripted event handler.
  71. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  72. /// Add a scripted event handler for a specific sender.
  73. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  74. /// Remove a scripted event handler.
  75. virtual void RemoveEventHandler(StringHash eventType);
  76. /// Remove a scripted event handler for a specific sender.
  77. virtual void RemoveEventHandler(Object* sender, StringHash eventType);
  78. /// Remove all scripted event handlers for a specific sender.
  79. virtual void RemoveEventHandlers(Object* sender);
  80. /// Remove all scripted event handlers.
  81. virtual void RemoveEventHandlers();
  82. /// Remove all scripted event handlers, except those listed.
  83. virtual void RemoveEventHandlersExcept(const PODVector<StringHash>& exceptions);
  84. /// Return whether has subscribed to an event.
  85. virtual bool HasEventHandler(StringHash eventType) const;
  86. /// Return whether has subscribed to a specific sender's event.
  87. virtual bool HasEventHandler(Object* sender, StringHash eventType) const;
  88. /// Create object of certain class from the script file. Return true if successful.
  89. bool CreateObject(ScriptFile* scriptFile, const String& className);
  90. /// Set script file only. Recreate object if necessary.
  91. void SetScriptFile(ScriptFile* scriptFile);
  92. /// Set class name only. Recreate object if necessary.
  93. void SetClassName(const String& className);
  94. /// Query for a method by declaration and execute. Log an error if not found.
  95. bool Execute(const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  96. /// Execute a method.
  97. bool Execute(asIScriptFunction* method, const VariantVector& parameters = Variant::emptyVariantVector);
  98. /// Add a delay-executed method call, optionally repeating.
  99. void DelayedExecute
  100. (float delay, bool repeat, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  101. /// Clear pending delay-executed method calls. If empty declaration given, clears all.
  102. void ClearDelayedExecute(const String& declaration = String::EMPTY);
  103. /// Return script file.
  104. ScriptFile* GetScriptFile() const { return scriptFile_; }
  105. /// Return script object.
  106. asIScriptObject* GetScriptObject() const { return scriptObject_; }
  107. /// Return class name.
  108. const String& GetClassName() const { return className_; }
  109. /// Check if the object is derived from a class.
  110. bool IsA(const String& className) const;
  111. /// Check if has a method.
  112. bool HasMethod(const String& declaration) const;
  113. /// Set script file attribute.
  114. void SetScriptFileAttr(const ResourceRef& value);
  115. /// Set delayed method calls attribute.
  116. void SetDelayedCallsAttr(const PODVector<unsigned char>& value);
  117. /// Set script file serialization attribute by calling a script function.
  118. void SetScriptDataAttr(const PODVector<unsigned char>& data);
  119. /// Set script network serialization attribute by calling a script function.
  120. void SetScriptNetworkDataAttr(const PODVector<unsigned char>& data);
  121. /// Return script file attribute.
  122. ResourceRef GetScriptFileAttr() const;
  123. /// Return delayed method calls attribute.
  124. PODVector<unsigned char> GetDelayedCallsAttr() const;
  125. /// Get script file serialization attribute by calling a script function.
  126. PODVector<unsigned char> GetScriptDataAttr() const;
  127. /// Get script network serialization attribute by calling a script function.
  128. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  129. protected:
  130. /// Handle scene being assigned.
  131. virtual void OnSceneSet(Scene* scene);
  132. /// Handle node transform being dirtied.
  133. virtual void OnMarkedDirty(Node* node);
  134. private:
  135. /// (Re)create the script object and check for supported methods if successfully created.
  136. void CreateObject();
  137. /// Release the script object.
  138. void ReleaseObject();
  139. /// Check for supported script methods.
  140. void GetScriptMethods();
  141. /// Check for script attributes.
  142. void GetScriptAttributes();
  143. /// Store values of script attributes for hot reload.
  144. void StoreScriptAttributes();
  145. /// Restore values of script attributes after hot reload is complete.
  146. void RestoreScriptAttributes();
  147. /// Clear supported script methods.
  148. void ClearScriptMethods();
  149. /// Clear attributes to C++ side attributes only.
  150. void ClearScriptAttributes();
  151. /// Subscribe/unsubscribe from scene updates as necessary.
  152. void UpdateEventSubscription();
  153. /// Handle scene update event.
  154. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  155. /// Handle scene post-update event.
  156. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  157. #if defined(URHO3D_PHYSICS) || defined(URHO3D_URHO2D)
  158. /// Handle physics pre-step event.
  159. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  160. /// Handle physics post-step event.
  161. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  162. #endif
  163. /// Handle an event in script.
  164. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  165. /// Handle script file reload start.
  166. void HandleScriptFileReload(StringHash eventType, VariantMap& eventData);
  167. /// Handle script file reload finished.
  168. void HandleScriptFileReloadFinished(StringHash eventType, VariantMap& eventData);
  169. /// Script file.
  170. WeakPtr<ScriptFile> scriptFile_;
  171. /// Script object.
  172. asIScriptObject* scriptObject_;
  173. /// Class name.
  174. String className_;
  175. /// Pointers to supported inbuilt methods.
  176. asIScriptFunction* methods_[MAX_SCRIPT_METHODS];
  177. /// Delayed method calls.
  178. Vector<DelayedCall> delayedCalls_;
  179. /// Attributes, including script object variables.
  180. Vector<AttributeInfo> attributeInfos_;
  181. /// Storage for unapplied node and component ID attributes
  182. HashMap<AttributeInfo*, unsigned> idAttributes_;
  183. /// Storage for attributes while script object is being hot-reloaded.
  184. HashMap<String, Variant> storedAttributes_;
  185. /// Subscribed to scene update events flag.
  186. bool subscribed_;
  187. /// Subscribed to scene post and fixed update events flag.
  188. bool subscribedPostFixed_;
  189. };
  190. /// Return the Urho3D context of the active script context.
  191. URHO3D_API Context* GetScriptContext();
  192. /// Return the ScriptInstance of the active script context.
  193. URHO3D_API ScriptInstance* GetScriptContextInstance();
  194. /// Return the scene node of the active script context.
  195. URHO3D_API Node* GetScriptContextNode();
  196. /// Return the scene of the active script context.
  197. URHO3D_API Scene* GetScriptContextScene();
  198. /// Return the event listener of the active script context.
  199. URHO3D_API ScriptEventListener* GetScriptContextEventListener();
  200. /// Return the event listener of the active script context as an Object pointer.
  201. URHO3D_API Object* GetScriptContextEventListenerObject();
  202. }