ScriptInstance.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /// Script object created.
  32. EVENT(E_SCRIPTOBJECTCREATED, ScriptObjectCreated)
  33. {
  34. }
  35. /// Inbuilt scripted component methods.
  36. enum ScriptInstanceMethod
  37. {
  38. METHOD_START = 0,
  39. METHOD_STOP,
  40. METHOD_DELAYEDSTART,
  41. METHOD_UPDATE,
  42. METHOD_POSTUPDATE,
  43. METHOD_FIXEDUPDATE,
  44. METHOD_FIXEDPOSTUPDATE,
  45. METHOD_LOAD,
  46. METHOD_SAVE,
  47. METHOD_READNETWORKUPDATE,
  48. METHOD_WRITENETWORKUPDATE,
  49. METHOD_APPLYATTRIBUTES,
  50. MAX_SCRIPT_METHODS
  51. };
  52. /// Delay-executed method call.
  53. struct DelayedMethodCall
  54. {
  55. /// Period for repeating calls.
  56. float period_;
  57. /// Delay time remaining until execution.
  58. float delay_;
  59. /// Repeat flag.
  60. bool repeat_;
  61. /// Method declaration.
  62. String declaration_;
  63. /// Parameters.
  64. VariantVector parameters_;
  65. };
  66. /// %Script object component.
  67. class ScriptInstance : public Component, public ScriptEventListener
  68. {
  69. OBJECT(ScriptInstance);
  70. public:
  71. /// Construct.
  72. ScriptInstance(Context* context);
  73. /// Destruct.
  74. virtual ~ScriptInstance();
  75. /// Register object factory.
  76. static void RegisterObject(Context* context);
  77. /// Return attribute descriptions, or null if none defined.
  78. virtual const Vector<AttributeInfo>* GetAttributes() const { return &attributeInfos_; }
  79. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  80. virtual void ApplyAttributes();
  81. /// Add an event handler. Called by script exposed version of SubscribeToEvent().
  82. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  83. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  84. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  85. /// Create object of certain class from the script file. Return true if successful.
  86. bool CreateObject(ScriptFile* scriptFile, const String& className);
  87. /// Set script file only. Recreate object if necessary.
  88. void SetScriptFile(ScriptFile* scriptFile);
  89. /// Set class name only. Recreate object if necessary.
  90. void SetClassName(const String& className);
  91. /// Enable or disable scripted updates and event handlers.
  92. void SetActive(bool active);
  93. /// Set fixed updates per second. 0 (default) uses the physics frame rate.
  94. void SetFixedUpdateFps(int fps);
  95. /// Query for a method by declaration and execute if found.
  96. bool Execute(const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  97. /// Execute a method.
  98. bool Execute(asIScriptFunction* method, const VariantVector& parameters = Variant::emptyVariantVector);
  99. /// Add a delay-executed method call, optionally repeating.
  100. void DelayedExecute(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. /// Return whether scripted updates and event handlers are enabled.
  110. bool IsActive() const { return active_; }
  111. /// Return fixed updates per second.
  112. int GetFixedUpdateFps() const { return fixedUpdateFps_; }
  113. /// Set script file attribute.
  114. void SetScriptFileAttr(ResourceRef value);
  115. /// Set delayed method calls attribute.
  116. void SetDelayedMethodCallsAttr(PODVector<unsigned char> value);
  117. /// Set fixed update time accumulator attribute.
  118. void SetFixedUpdateAccAttr(float value);
  119. /// Set script file serialization attribute by calling a script function.
  120. void SetScriptDataAttr(PODVector<unsigned char> data);
  121. /// Set script network serialization attribute by calling a script function.
  122. void SetScriptNetworkDataAttr(PODVector<unsigned char> data);
  123. /// Return script file attribute.
  124. ResourceRef GetScriptFileAttr() const;
  125. /// Return delayed method calls attribute.
  126. PODVector<unsigned char> GetDelayedMethodCallsAttr() const;
  127. /// Return fixed update time accumulator attribute.
  128. float GetFixedUpdateAccAttr() const;
  129. /// Get script file serialization attribute by calling a script function.
  130. PODVector<unsigned char> GetScriptDataAttr() const;
  131. /// Get script network serialization attribute by calling a script function.
  132. PODVector<unsigned char> GetScriptNetworkDataAttr() const;
  133. private:
  134. /// (Re)create the script object and check for supported methods if successfully created.
  135. void CreateObject();
  136. /// Release the script object.
  137. void ReleaseObject();
  138. /// Check for supported script methods.
  139. void GetScriptMethods();
  140. /// Check for script attributes.
  141. void GetScriptAttributes();
  142. /// Clear supported script methods.
  143. void ClearScriptMethods();
  144. /// Clear attributes to C++ side attributes only.
  145. void ClearScriptAttributes();
  146. /// Handle scene update event.
  147. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  148. /// Handle scene post-update event.
  149. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  150. /// Handle physics pre-step event.
  151. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  152. /// Handle physics post-step event.
  153. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  154. /// Handle an event in script.
  155. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  156. /// Handle script file reload start.
  157. void HandleScriptFileReload(StringHash eventType, VariantMap& eventData);
  158. /// Handle script file reload finished.
  159. void HandleScriptFileReloadFinished(StringHash eventType, VariantMap& eventData);
  160. /// Script subsystem.
  161. SharedPtr<Script> script_;
  162. /// Script file.
  163. WeakPtr<ScriptFile> scriptFile_;
  164. /// Script object.
  165. asIScriptObject* scriptObject_;
  166. /// Class name.
  167. String className_;
  168. /// Pointers to supported inbuilt methods.
  169. asIScriptFunction* methods_[MAX_SCRIPT_METHODS];
  170. /// Active flag.
  171. bool active_;
  172. /// Subscribed to scene update event flag.
  173. bool subscribed_;
  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. };
  187. /// Return the Urho3D context of the active script context.
  188. Context* GetScriptContext();
  189. /// Return the ScriptInstance of the active script context.
  190. ScriptInstance* GetScriptContextInstance();
  191. /// Return the scene node of the active script context.
  192. Node* GetScriptContextNode();
  193. /// Return the scene of the active script context.
  194. Scene* GetScriptContextScene();
  195. /// Return the event listener of the active script context.
  196. ScriptEventListener* GetScriptContextEventListener();
  197. /// Return the event listener of the active script context as an Object pointer.
  198. Object* GetScriptContextEventListenerObject();
  199. }