ScriptInstance.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef SCRIPT_SCRIPTINSTANCE_H
  24. #define SCRIPT_SCRIPTINSTANCE_H
  25. #include "Component.h"
  26. #include "EventListener.h"
  27. #include "Quaternion.h"
  28. #include "ScriptEventListener.h"
  29. #include "SharedPtr.h"
  30. class Entity;
  31. class PhysicsWorld;
  32. class Scene;
  33. class ScriptEngine;
  34. class ScriptFile;
  35. class asIScriptFunction;
  36. class asIScriptObject;
  37. //! Inbuilt scripted component methods
  38. enum ScriptInstanceMethod
  39. {
  40. METHOD_START = 0,
  41. METHOD_STOP,
  42. METHOD_UPDATE,
  43. METHOD_POSTUPDATE,
  44. METHOD_UPDATEFIXED,
  45. METHOD_POSTUPDATEFIXED,
  46. METHOD_SAVE,
  47. METHOD_LOAD,
  48. METHOD_POSTLOAD,
  49. METHOD_SAVEXML,
  50. METHOD_LOADXML,
  51. METHOD_WRITENETUPDATE,
  52. METHOD_READNETUPDATE,
  53. METHOD_POSTNETUPDATE,
  54. METHOD_INTERPOLATE,
  55. METHOD_GETCOMPONENTREFS,
  56. MAX_SCRIPT_METHODS
  57. };
  58. //! A scripted component
  59. class ScriptInstance : public Component, public ScriptEventListener
  60. {
  61. DEFINE_TYPE(ScriptInstance);
  62. public:
  63. //! Construct with script engine pointer and name
  64. ScriptInstance(ScriptEngine* scriptEngine, const std::string& name = std::string());
  65. //! Destruct
  66. virtual ~ScriptInstance();
  67. //! Write component state to a stream
  68. virtual void save(Serializer& dest);
  69. //! Read component state from a stream
  70. virtual void load(Deserializer& source, ResourceCache* cache);
  71. //! Write component state to an XML element
  72. virtual void saveXML(XMLElement& dest);
  73. //! Read component state from an XML element
  74. virtual void loadXML(const XMLElement& source, ResourceCache* cache);
  75. //! Resolve component references after loading
  76. virtual void postLoad(ResourceCache* cache);
  77. //! Write a network update
  78. virtual bool writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info);
  79. //! Read a network update
  80. virtual void readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info);
  81. //! Resolve component references after a network update
  82. virtual void postNetUpdate(ResourceCache* cache);
  83. //! Perform client-side visual smoothing
  84. virtual void interpolate(bool snapToEnd);
  85. //! Return component references
  86. virtual void getComponentRefs(std::vector<ComponentRef>& dest);
  87. //! Add an event handler. Called by script exposed version of subscribeToEvent()
  88. virtual void addEventHandler(StringHash eventType, const std::string& handlerName);
  89. //! Set script file and class
  90. bool setScriptClass(ScriptFile* scriptFile, const std::string& className);
  91. //! Enable or disable scripted updates and event handlers
  92. void setEnabled(bool enable);
  93. //! Query for a method by declaration and execute if found
  94. bool execute(const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>());
  95. //! Execute a method
  96. bool execute(asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>());
  97. //! Return script engine
  98. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  99. //! Return script file
  100. ScriptFile* getScriptFile() const { return mScriptFile; }
  101. //! Return script object
  102. asIScriptObject* getScriptObject() const { return mScriptObject; }
  103. //! Return class name
  104. const std::string& getClassName() const { return mClassName; }
  105. //! Return whether object created and running
  106. bool isRunning() const { return mScriptObject != 0; }
  107. //! Return whether scripted updates and event handlers are enabled
  108. bool isEnabled() const { return mEnabled; }
  109. //! Create the script object. Check for supported methods and register self to the ScriptFile if successful
  110. bool createObject();
  111. //! Release the script object and unregister self from the ScriptFile
  112. void releaseObject();
  113. private:
  114. //! Clear supported methods
  115. void clearMethods();
  116. //! Check for supported methods
  117. void getSupportedMethods();
  118. //! Handle scene update event
  119. void handleSceneUpdate(StringHash eventType, VariantMap& eventData);
  120. //! Handle scene post-update event
  121. void handleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  122. //! Handle physics pre-step event
  123. void handlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  124. //! Handle physics post-step event
  125. void handlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  126. //! Handle an event with a handler in script
  127. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  128. //! Script engine
  129. SharedPtr<ScriptEngine> mScriptEngine;
  130. //! Script file
  131. WeakPtr<ScriptFile> mScriptFile;
  132. //! Script object
  133. asIScriptObject* mScriptObject;
  134. //! Class name
  135. std::string mClassName;
  136. //! Pointers to supported inbuilt methods
  137. asIScriptFunction* mMethods[MAX_SCRIPT_METHODS];
  138. //! Enabled flag
  139. bool mEnabled;
  140. };
  141. //! Return the ScriptInstance of the active context
  142. ScriptInstance* getScriptContextComponent();
  143. //! Return the entity of the active context
  144. Entity* getScriptContextEntity();
  145. #endif // SCRIPT_SCRIPTINSTANCE_H