ScriptInstance.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Component.h"
  25. #include "ScriptEventListener.h"
  26. class asIScriptFunction;
  27. class asIScriptObject;
  28. namespace Urho3D
  29. {
  30. class Script;
  31. class ScriptFile;
  32. /// Inbuilt scripted component methods.
  33. enum ScriptInstanceMethod
  34. {
  35. METHOD_START = 0,
  36. METHOD_STOP,
  37. METHOD_DELAYEDSTART,
  38. METHOD_UPDATE,
  39. METHOD_POSTUPDATE,
  40. METHOD_FIXEDUPDATE,
  41. METHOD_FIXEDPOSTUPDATE,
  42. METHOD_LOAD,
  43. METHOD_SAVE,
  44. METHOD_APPLYATTRIBUTES,
  45. MAX_SCRIPT_METHODS
  46. };
  47. /// Delay-executed method call.
  48. struct DelayedMethodCall
  49. {
  50. /// Period for repeating calls.
  51. float period_;
  52. /// Delay time remaining until execution.
  53. float delay_;
  54. /// Repeat flag.
  55. bool repeat_;
  56. /// Method declaration.
  57. String declaration_;
  58. /// Parameters.
  59. VariantVector parameters_;
  60. };
  61. /// %Script object component.
  62. class ScriptInstance : public Component, public ScriptEventListener
  63. {
  64. OBJECT(ScriptInstance);
  65. public:
  66. /// Construct.
  67. ScriptInstance(Context* context);
  68. /// Destruct.
  69. virtual ~ScriptInstance();
  70. /// Register object factory.
  71. static void RegisterObject(Context* context);
  72. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  73. virtual void ApplyAttributes();
  74. /// Add an event handler. Called by script exposed version of SubscribeToEvent().
  75. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  76. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  77. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  78. /// Create object of certain class from the script file. Return true if successful.
  79. bool CreateObject(ScriptFile* scriptFile, const String& className);
  80. /// Set script file only. Recreate object if necessary.
  81. void SetScriptFile(ScriptFile* scriptFile);
  82. /// Set class name only. Recreate object if necessary.
  83. void SetClassName(const String& className);
  84. /// Enable or disable scripted updates and event handlers.
  85. void SetActive(bool active);
  86. /// Set fixed updates per second. 0 (default) uses the physics frame rate.
  87. void SetFixedUpdateFps(int fps);
  88. /// Query for a method by declaration and execute if found.
  89. bool Execute(const String& declaration, const VariantVector& parameters = VariantVector());
  90. /// Execute a method.
  91. bool Execute(asIScriptFunction* method, const VariantVector& parameters = VariantVector());
  92. /// Add a delay-executed method call, optionally repeating.
  93. void DelayedExecute(float delay, bool repeat, const String& declaration, const VariantVector& parameters = VariantVector());
  94. /// Clear pending delay-executed method calls. If empty declaration given, clears all.
  95. void ClearDelayedExecute(const String& declaration = String());
  96. /// Return script file.
  97. ScriptFile* GetScriptFile() const { return scriptFile_; }
  98. /// Return script object.
  99. asIScriptObject* GetScriptObject() const { return scriptObject_; }
  100. /// Return class name.
  101. const String& GetClassName() const { return className_; }
  102. /// Return whether scripted updates and event handlers are enabled.
  103. bool IsActive() const { return active_; }
  104. /// Return fixed updates per second.
  105. int GetFixedUpdateFps() const { return fixedUpdateFps_; }
  106. /// Set script file attribute.
  107. void SetScriptFileAttr(ResourceRef value);
  108. /// Set delayed method calls attribute.
  109. void SetDelayedMethodCallsAttr(PODVector<unsigned char> value);
  110. /// Set fixed update time accumulator attribute.
  111. void SetFixedUpdateAccAttr(float value);
  112. /// Set script data attribute by calling a script function.
  113. void SetScriptDataAttr(PODVector<unsigned char> data);
  114. /// Return script file attribute.
  115. ResourceRef GetScriptFileAttr() const;
  116. /// Return delayed method calls attribute.
  117. PODVector<unsigned char> GetDelayedMethodCallsAttr() const;
  118. /// Return fixed update time accumulator attribute.
  119. float GetFixedUpdateAccAttr() const;
  120. /// Get script data attribute by calling a script function.
  121. PODVector<unsigned char> GetScriptDataAttr() const;
  122. private:
  123. /// (Re)create the script object and check for supported methods if successfully created.
  124. void CreateObject();
  125. /// Release the script object.
  126. void ReleaseObject();
  127. /// Clear supported methods.
  128. void ClearMethods();
  129. /// Check for supported methods.
  130. void GetSupportedMethods();
  131. /// Handle scene update event.
  132. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  133. /// Handle scene post-update event.
  134. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  135. /// Handle physics pre-step event.
  136. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  137. /// Handle physics post-step event.
  138. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  139. /// Handle an event in script.
  140. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  141. /// Handle script file reload start.
  142. void HandleScriptFileReload(StringHash eventType, VariantMap& eventData);
  143. /// Handle script file reload finished.
  144. void HandleScriptFileReloadFinished(StringHash eventType, VariantMap& eventData);
  145. /// Script subsystem.
  146. SharedPtr<Script> script_;
  147. /// Script file.
  148. WeakPtr<ScriptFile> scriptFile_;
  149. /// Script object.
  150. asIScriptObject* scriptObject_;
  151. /// Class name.
  152. String className_;
  153. /// Pointers to supported inbuilt methods.
  154. asIScriptFunction* methods_[MAX_SCRIPT_METHODS];
  155. /// Active flag.
  156. bool active_;
  157. /// DelayedStart called flag.
  158. bool delayedStart_;
  159. /// Fixed update FPS.
  160. int fixedUpdateFps_;
  161. /// Fixed update time interval.
  162. float fixedUpdateInterval_;
  163. /// Fixed update time accumulator.
  164. float fixedUpdateAcc_;
  165. /// Fixed post update time accumulator.
  166. float fixedPostUpdateAcc_;
  167. /// Delayed method calls.
  168. Vector<DelayedMethodCall> delayedMethodCalls_;
  169. };
  170. /// Return the Urho3D context of the active script context.
  171. Context* GetScriptContext();
  172. /// Return the ScriptInstance of the active script context.
  173. ScriptInstance* GetScriptContextInstance();
  174. /// Return the scene node of the active script context.
  175. Node* GetScriptContextNode();
  176. /// Return the scene of the active script context.
  177. Scene* GetScriptContextScene();
  178. /// Return the event listener of the active script context.
  179. ScriptEventListener* GetScriptContextEventListener();
  180. /// Return the event listener of the active script context as an Object pointer.
  181. Object* GetScriptContextEventListenerObject();
  182. }