ScriptInstance.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_POSTLOAD,
  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. std::string declaration_;
  51. /// Parameters
  52. VariantVector parameters_;
  53. };
  54. /// Scripted 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. /// Handle attribute write access
  66. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  67. /// Handle attribute read access
  68. virtual Variant OnGetAttribute(const AttributeInfo& attr);
  69. /// Perform post-load after the whole scene has been loaded
  70. virtual void PostLoad();
  71. /// Add an event handler. Called by script exposed version of SubscribeToEvent()
  72. virtual void AddEventHandler(StringHash eventType, const std::string& handlerName);
  73. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent()
  74. virtual void AddEventHandler(Object* sender, StringHash eventType, const std::string& handlerName);
  75. /// Create object of certain class from the script file. Return true if successful
  76. bool CreateObject(ScriptFile* scriptFile, const std::string& className);
  77. /// Set script file only. Recreate object if necessary
  78. void SetScriptFile(ScriptFile* scriptFile);
  79. /// Set class name only. Recreate object if necessary
  80. void SetClassName(const std::string& className);
  81. /// Enable or disable scripted updates and event handlers
  82. void SetActive(bool active);
  83. /// Set fixed updates per second. 0 (default) uses the physics frame rate
  84. void SetFixedUpdateFps(int fps);
  85. /// Query for a method by declaration and execute if found
  86. bool Execute(const std::string& declaration, const VariantVector& parameters = VariantVector());
  87. /// Execute a method
  88. bool Execute(asIScriptFunction* method, const VariantVector& parameters = VariantVector());
  89. /// Add a delay-executed method call
  90. void DelayedExecute(float delay, const std::string& declaration, const VariantVector& parameters = VariantVector());
  91. /// Clear pending delay-executed method calls
  92. void ClearDelayedExecute();
  93. /// Return script file
  94. ScriptFile* GetScriptFile() const { return scriptFile_; }
  95. /// Return script object
  96. asIScriptObject* GetScriptObject() const { return scriptObject_; }
  97. /// Return class name
  98. const std::string& GetClassName() const { return className_; }
  99. /// Return whether scripted updates and event handlers are enabled
  100. bool IsActive() const { return active_; }
  101. /// Return fixed updates per second
  102. int GetFixedUpdateFps() const { return fixedUpdateFps_; }
  103. private:
  104. /// (Re)create the script object and check for supported methods if successfully created
  105. void CreateObject();
  106. /// Release the script object
  107. void ReleaseObject();
  108. /// Clear supported methods
  109. void ClearMethods();
  110. /// Check for supported methods
  111. void GetSupportedMethods();
  112. /// Get script object's serialization data by calling a script function
  113. std::vector<unsigned char> GetScriptData() const;
  114. /// Set script object's serialization data by calling a script function
  115. void SetScriptData(std::vector<unsigned char> data);
  116. /// Handle scene update event
  117. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  118. /// Handle scene post-update event
  119. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  120. /// Handle physics pre-step event
  121. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  122. /// Handle physics post-step event
  123. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  124. /// Handle an event in script
  125. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  126. /// Handle script file reload start
  127. void HandleScriptFileReload(StringHash eventType, VariantMap& eventData);
  128. /// Handle script file reload finished
  129. void HandleScriptFileReloadFinished(StringHash eventType, VariantMap& eventData);
  130. /// Script subsystem
  131. SharedPtr<Script> script_;
  132. /// Script file
  133. WeakPtr<ScriptFile> scriptFile_;
  134. /// Script object
  135. asIScriptObject* scriptObject_;
  136. /// Class name
  137. std::string className_;
  138. /// Pointers to supported inbuilt methods
  139. asIScriptFunction* methods_[MAX_SCRIPT_METHODS];
  140. /// Active flag
  141. bool active_;
  142. /// Fixed update FPS
  143. int fixedUpdateFps_;
  144. /// Fixed update time interval
  145. float fixedUpdateInterval_;
  146. /// Fixed update time accumulator
  147. float fixedUpdateAcc_;
  148. /// Fixed post update time accumulator
  149. float fixedPostUpdateAcc_;
  150. /// Delayed method calls
  151. std::vector<DelayedMethodCall> delayedMethodCalls_;
  152. };
  153. /// Return the Urho3D context of the active script context
  154. Context* GetScriptContext();
  155. /// Return the ScriptInstance of the active script context
  156. ScriptInstance* GetScriptContextInstance();
  157. /// Return the scene node of the active script context
  158. Node* GetScriptContextNode();
  159. /// Return the scene of the active script context
  160. Scene* GetScriptContextScene();
  161. /// Return the event listener of the active script context
  162. ScriptEventListener* GetScriptContextEventListener();
  163. /// Return the event listener of the active script context as an Object pointer
  164. Object* GetScriptContextEventListenerObject();