ScriptInstance.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "ScriptEventListener.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. /// Delay-executed method call.
  50. struct DelayedMethodCall
  51. {
  52. /// Period for repeating calls.
  53. float period_;
  54. /// Delay time remaining until execution.
  55. float delay_;
  56. /// Repeat flag.
  57. bool repeat_;
  58. /// Method declaration.
  59. String declaration_;
  60. /// Parameters.
  61. VariantVector parameters_;
  62. };
  63. /// %Script object component.
  64. class URHO3D_API ScriptInstance : public Component, public ScriptEventListener
  65. {
  66. OBJECT(ScriptInstance);
  67. public:
  68. /// Construct.
  69. ScriptInstance(Context* context);
  70. /// Destruct.
  71. virtual ~ScriptInstance();
  72. /// Register object factory.
  73. static void RegisterObject(Context* context);
  74. /// Handle attribute write access.
  75. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  76. /// Handle attribute read access.
  77. virtual void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const;
  78. /// Return attribute descriptions, or null if none defined.
  79. virtual const Vector<AttributeInfo>* GetAttributes() const { return &attributeInfos_; }
  80. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  81. virtual void ApplyAttributes();
  82. /// Handle enabled/disabled state change.
  83. virtual void OnSetEnabled();
  84. /// Add an event handler. Called by script exposed version of SubscribeToEvent().
  85. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  86. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  87. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  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. /// Set fixed updates per second. 0 (default) uses the physics frame rate.
  95. void SetFixedUpdateFps(int fps);
  96. /// Query for a method by declaration and execute if found.
  97. bool Execute(const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  98. /// Execute a method.
  99. bool Execute(asIScriptFunction* method, const VariantVector& parameters = Variant::emptyVariantVector);
  100. /// Add a delay-executed method call, optionally repeating.
  101. void DelayedExecute(float delay, bool repeat, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  102. /// Clear pending delay-executed method calls. If empty declaration given, clears all.
  103. void ClearDelayedExecute(const String& declaration = String::EMPTY);
  104. /// Return script file.
  105. ScriptFile* GetScriptFile() const { return scriptFile_; }
  106. /// Return script object.
  107. asIScriptObject* GetScriptObject() const { return scriptObject_; }
  108. /// Return class name.
  109. const String& GetClassName() const { return className_; }
  110. /// Return fixed updates per second.
  111. int GetFixedUpdateFps() const { return fixedUpdateFps_; }
  112. /// Set script file attribute.
  113. void SetScriptFileAttr(ResourceRef value);
  114. /// Set delayed method calls attribute.
  115. void SetDelayedMethodCallsAttr(PODVector<unsigned char> value);
  116. /// Set fixed update time accumulator attribute.
  117. void SetFixedUpdateAccAttr(float value);
  118. /// Set script file serialization attribute by calling a script function.
  119. void SetScriptDataAttr(PODVector<unsigned char> data);
  120. /// Set script network serialization attribute by calling a script function.
  121. void SetScriptNetworkDataAttr(PODVector<unsigned char> data);
  122. /// Return script file attribute.
  123. ResourceRef GetScriptFileAttr() const;
  124. /// Return delayed method calls attribute.
  125. PODVector<unsigned char> GetDelayedMethodCallsAttr() const;
  126. /// Return fixed update time accumulator attribute.
  127. float GetFixedUpdateAccAttr() const;
  128. /// Get script file serialization attribute by calling a script function.
  129. PODVector<unsigned char> GetScriptDataAttr() const;
  130. /// Get script network serialization attribute by calling a script function.
  131. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  132. protected:
  133. /// Handle node transform being dirtied.
  134. virtual void OnMarkedDirty(Node* node);
  135. private:
  136. /// (Re)create the script object and check for supported methods if successfully created.
  137. void CreateObject();
  138. /// Release the script object.
  139. void ReleaseObject();
  140. /// Check for supported script methods.
  141. void GetScriptMethods();
  142. /// Check for script attributes.
  143. void GetScriptAttributes();
  144. /// Clear supported script methods.
  145. void ClearScriptMethods();
  146. /// Clear attributes to C++ side attributes only.
  147. void ClearScriptAttributes();
  148. /// Subscribe/unsubscribe from scene updates as necessary.
  149. void UpdateEventSubscription();
  150. /// Handle scene update event.
  151. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  152. /// Handle scene post-update event.
  153. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  154. /// Handle physics pre-step event.
  155. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  156. /// Handle physics post-step event.
  157. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  158. /// Handle an event in script.
  159. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  160. /// Handle script file reload start.
  161. void HandleScriptFileReload(StringHash eventType, VariantMap& eventData);
  162. /// Handle script file reload finished.
  163. void HandleScriptFileReloadFinished(StringHash eventType, VariantMap& eventData);
  164. /// Script subsystem.
  165. SharedPtr<Script> script_;
  166. /// Script file.
  167. WeakPtr<ScriptFile> scriptFile_;
  168. /// Script object.
  169. asIScriptObject* scriptObject_;
  170. /// Class name.
  171. String className_;
  172. /// Pointers to supported inbuilt methods.
  173. asIScriptFunction* methods_[MAX_SCRIPT_METHODS];
  174. /// Fixed update FPS.
  175. int fixedUpdateFps_;
  176. /// Fixed update time interval.
  177. float fixedUpdateInterval_;
  178. /// Fixed update time accumulator.
  179. float fixedUpdateAcc_;
  180. /// Fixed post update time accumulator.
  181. float fixedPostUpdateAcc_;
  182. /// Delayed method calls.
  183. Vector<DelayedMethodCall> delayedMethodCalls_;
  184. /// Attributes, including script object variables.
  185. Vector<AttributeInfo> attributeInfos_;
  186. /// Storage for unapplied node and component ID attributes
  187. HashMap<AttributeInfo*, unsigned> idAttributes_;
  188. /// Subscribed to scene update events flag.
  189. bool subscribed_;
  190. /// Subscribed to scene post and fixed update events flag.
  191. bool subscribedPostFixed_;
  192. };
  193. /// Return the Urho3D context of the active script context.
  194. URHO3D_API Context* GetScriptContext();
  195. /// Return the ScriptInstance of the active script context.
  196. URHO3D_API ScriptInstance* GetScriptContextInstance();
  197. /// Return the scene node of the active script context.
  198. URHO3D_API Node* GetScriptContextNode();
  199. /// Return the scene of the active script context.
  200. URHO3D_API Scene* GetScriptContextScene();
  201. /// Return the event listener of the active script context.
  202. URHO3D_API ScriptEventListener* GetScriptContextEventListener();
  203. /// Return the event listener of the active script context as an Object pointer.
  204. URHO3D_API Object* GetScriptContextEventListenerObject();
  205. }