ScriptInstance.h 7.5 KB

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