ScriptInstance.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_APPLYATTRIBUTES,
  44. MAX_SCRIPT_METHODS
  45. };
  46. /// Delay-executed method call.
  47. struct DelayedMethodCall
  48. {
  49. /// Period for repeating calls.
  50. float period_;
  51. /// Delay time remaining until execution.
  52. float delay_;
  53. /// Repeat flag.
  54. bool repeat_;
  55. /// Method declaration.
  56. String declaration_;
  57. /// Parameters.
  58. VariantVector parameters_;
  59. };
  60. /// %Script object component.
  61. class ScriptInstance : public Component, public ScriptEventListener
  62. {
  63. OBJECT(ScriptInstance);
  64. public:
  65. /// Construct.
  66. ScriptInstance(Context* context);
  67. /// Destruct.
  68. virtual ~ScriptInstance();
  69. /// Register object factory.
  70. static void RegisterObject(Context* context);
  71. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  72. virtual void ApplyAttributes();
  73. /// Add an event handler. Called by script exposed version of SubscribeToEvent().
  74. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  75. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  76. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  77. /// Create object of certain class from the script file. Return true if successful.
  78. bool CreateObject(ScriptFile* scriptFile, const String& className);
  79. /// Set script file only. Recreate object if necessary.
  80. void SetScriptFile(ScriptFile* scriptFile);
  81. /// Set class name only. Recreate object if necessary.
  82. void SetClassName(const String& className);
  83. /// Enable or disable scripted updates and event handlers.
  84. void SetActive(bool active);
  85. /// Set fixed updates per second. 0 (default) uses the physics frame rate.
  86. void SetFixedUpdateFps(int fps);
  87. /// Query for a method by declaration and execute if found.
  88. bool Execute(const String& declaration, const VariantVector& parameters = VariantVector());
  89. /// Execute a method.
  90. bool Execute(asIScriptFunction* method, const VariantVector& parameters = VariantVector());
  91. /// Add a delay-executed method call, optionally repeating.
  92. void DelayedExecute(float delay, bool repeat, const String& declaration, const VariantVector& parameters = VariantVector());
  93. /// Clear pending delay-executed method calls. If empty declaration given, clears all.
  94. void ClearDelayedExecute(const String& declaration = String());
  95. /// Return script file.
  96. ScriptFile* GetScriptFile() const { return scriptFile_; }
  97. /// Return script object.
  98. asIScriptObject* GetScriptObject() const { return scriptObject_; }
  99. /// Return class name.
  100. const String& GetClassName() const { return className_; }
  101. /// Return whether scripted updates and event handlers are enabled.
  102. bool IsActive() const { return active_; }
  103. /// Return fixed updates per second.
  104. int GetFixedUpdateFps() const { return fixedUpdateFps_; }
  105. /// Set script file attribute.
  106. void SetScriptFileAttr(ResourceRef value);
  107. /// Set delayed method calls attribute.
  108. void SetDelayedMethodCallsAttr(PODVector<unsigned char> value);
  109. /// Set fixed update time accumulator attribute.
  110. void SetFixedUpdateAccAttr(float value);
  111. /// Set script data attribute by calling a script function.
  112. void SetScriptDataAttr(PODVector<unsigned char> data);
  113. /// Return script file attribute.
  114. ResourceRef GetScriptFileAttr() const;
  115. /// Return delayed method calls attribute.
  116. PODVector<unsigned char> GetDelayedMethodCallsAttr() const;
  117. /// Return fixed update time accumulator attribute.
  118. float GetFixedUpdateAccAttr() const;
  119. /// Get script data attribute by calling a script function.
  120. PODVector<unsigned char> GetScriptDataAttr() const;
  121. private:
  122. /// (Re)create the script object and check for supported methods if successfully created.
  123. void CreateObject();
  124. /// Release the script object.
  125. void ReleaseObject();
  126. /// Clear supported methods.
  127. void ClearMethods();
  128. /// Check for supported methods.
  129. void GetSupportedMethods();
  130. /// Handle scene update event.
  131. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  132. /// Handle scene post-update event.
  133. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  134. /// Handle physics pre-step event.
  135. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  136. /// Handle physics post-step event.
  137. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  138. /// Handle an event in script.
  139. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  140. /// Handle script file reload start.
  141. void HandleScriptFileReload(StringHash eventType, VariantMap& eventData);
  142. /// Handle script file reload finished.
  143. void HandleScriptFileReloadFinished(StringHash eventType, VariantMap& eventData);
  144. /// Script subsystem.
  145. SharedPtr<Script> script_;
  146. /// Script file.
  147. WeakPtr<ScriptFile> scriptFile_;
  148. /// Script object.
  149. asIScriptObject* scriptObject_;
  150. /// Class name.
  151. String className_;
  152. /// Pointers to supported inbuilt methods.
  153. asIScriptFunction* methods_[MAX_SCRIPT_METHODS];
  154. /// Active flag.
  155. bool active_;
  156. /// Subscribed to scene update event flag.
  157. bool subscribed_;
  158. /// Fixed update FPS.
  159. int fixedUpdateFps_;
  160. /// Fixed update time interval.
  161. float fixedUpdateInterval_;
  162. /// Fixed update time accumulator.
  163. float fixedUpdateAcc_;
  164. /// Fixed post update time accumulator.
  165. float fixedPostUpdateAcc_;
  166. /// Delayed method calls.
  167. Vector<DelayedMethodCall> delayedMethodCalls_;
  168. };
  169. /// Return the Urho3D context of the active script context.
  170. Context* GetScriptContext();
  171. /// Return the ScriptInstance of the active script context.
  172. ScriptInstance* GetScriptContextInstance();
  173. /// Return the scene node of the active script context.
  174. Node* GetScriptContextNode();
  175. /// Return the scene of the active script context.
  176. Scene* GetScriptContextScene();
  177. /// Return the event listener of the active script context.
  178. ScriptEventListener* GetScriptContextEventListener();
  179. /// Return the event listener of the active script context as an Object pointer.
  180. Object* GetScriptContextEventListenerObject();
  181. }