ScriptInstance.h 7.7 KB

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