Scene.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "HashMap.h"
  25. #include "HashSet.h"
  26. #include "Mutex.h"
  27. #include "Node.h"
  28. #include "SceneResolver.h"
  29. #include "XMLElement.h"
  30. class File;
  31. class PackageFile;
  32. static const unsigned FIRST_REPLICATED_ID = 0x1;
  33. static const unsigned LAST_REPLICATED_ID = 0xffffff;
  34. static const unsigned FIRST_LOCAL_ID = 0x01000000;
  35. static const unsigned LAST_LOCAL_ID = 0xffffffff;
  36. /// Asynchronous loading progress of a scene.
  37. struct AsyncProgress
  38. {
  39. /// File for binary mode.
  40. SharedPtr<File> file_;
  41. /// XML file for XML mode.
  42. SharedPtr<XMLFile> xmlFile_;
  43. /// Current XML element for XML mode.
  44. XMLElement xmlElement_;
  45. /// Loaded root-level nodes.
  46. unsigned loadedNodes_;
  47. /// Total root-level nodes.
  48. unsigned totalNodes_;
  49. };
  50. /// Root scene node, represents the whole scene.
  51. class Scene : public Node
  52. {
  53. OBJECT(Scene);
  54. using Node::GetComponent;
  55. using Node::SaveXML;
  56. public:
  57. /// Construct.
  58. Scene(Context* context);
  59. /// Destruct.
  60. virtual ~Scene();
  61. /// Register object factory. Node must be registered first.
  62. static void RegisterObject(Context* context);
  63. /// Load from binary data. Return true if successful.
  64. virtual bool Load(Deserializer& source);
  65. /// Save to binary data. Return true if successful.
  66. virtual bool Save(Serializer& dest);
  67. /// Load from XML data. Return true if successful.
  68. virtual bool LoadXML(const XMLElement& source);
  69. /// Add a replication state that is tracking this scene.
  70. virtual void AddReplicationState(NodeReplicationState* state);
  71. /// Load from an XML file. Return true if successful.
  72. bool LoadXML(Deserializer& source);
  73. /// Save to an XML file. Return true if successful.
  74. bool SaveXML(Serializer& dest);
  75. /// Load from a binary file asynchronously. Return true if started successfully.
  76. bool LoadAsync(File* file);
  77. /// Load from an XML file asynchronously. Return true if started successfully.
  78. bool LoadAsyncXML(File* file);
  79. /// Stop asynchronous loading.
  80. void StopAsyncLoading();
  81. /// Instantiate scene content from binary data. Return root node if successful.
  82. Node* Instantiate(Deserializer& source, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
  83. /// Instantiate scene content from XML data. Return root node if successful.
  84. Node* InstantiateXML(const XMLElement& source, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
  85. /// Instantiate scene content from XML data. Return root node if successful.
  86. Node* InstantiateXML(Deserializer& source, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
  87. /// Clear scene completely of nodes and components.
  88. void Clear();
  89. /// %Set active flag. Only active scenes will be updated automatically.
  90. void SetActive(bool enable);
  91. /// %Set network client motion smoothing constant.
  92. void SetSmoothingConstant(float constant);
  93. /// %Set network client motion smoothing snap threshold.
  94. void SetSnapThreshold(float threshold);
  95. /// Add a required package file for networking. To be called on the server.
  96. void AddRequiredPackageFile(PackageFile* package);
  97. /// Clear required package files.
  98. void ClearRequiredPackageFiles();
  99. /// Register a node user variable hash reverse mapping (for editing.)
  100. void RegisterVar(const String& name);
  101. /// Unregister a node user variable hash reverse mapping.
  102. void UnregisterVar(const String& name);
  103. /// Clear all registered node user variable hash reverse mappings.
  104. void UnregisterAllVars();
  105. /// Return node from the whole scene by ID, or null if not found.
  106. Node* GetNode(unsigned id) const;
  107. /// Return component from the whole scene by ID, or null if not found.
  108. Component* GetComponent(unsigned id) const;
  109. /// Return active flag.
  110. bool IsActive() const { return active_; }
  111. /// Return asynchronous loading flag.
  112. bool IsAsyncLoading() const { return asyncLoading_; }
  113. /// Return asynchronous loading progress between 0.0 and 1.0, or 1.0 if not in progress.
  114. float GetAsyncProgress() const;
  115. /// Return source file name.
  116. const String& GetFileName() const { return fileName_; }
  117. /// Return source file checksum.
  118. unsigned GetChecksum() const { return checksum_; }
  119. /// Return motion smoothing constant.
  120. float GetSmoothingConstant() const { return smoothingConstant_; }
  121. /// Return motion smoothing snap threshold.
  122. float GetSnapThreshold() const { return snapThreshold_; }
  123. /// Return required package files.
  124. const Vector<SharedPtr<PackageFile> >& GetRequiredPackageFiles() const { return requiredPackageFiles_; }
  125. /// Return a node user variable name, or empty if not registered.
  126. const String& GetVarName(ShortStringHash hash) const;
  127. /// Update scene. Called by HandleUpdate.
  128. void Update(float timeStep);
  129. /// Begin a threaded update. During threaded update components can choose to delay dirty processing.
  130. void BeginThreadedUpdate();
  131. /// End a threaded update. Notify components that marked themselves for delayed dirty processing.
  132. void EndThreadedUpdate();
  133. /// Add a component to the delayed dirty notify queue. Is thread-safe.
  134. void DelayedMarkedDirty(Component* component);
  135. /// Return threaded update flag.
  136. bool IsThreadedUpdate() const { return threadedUpdate_; }
  137. /// Get free node ID, either non-local or local.
  138. unsigned GetFreeNodeID(CreateMode mode);
  139. /// Get free component ID, either non-local or local.
  140. unsigned GetFreeComponentID(CreateMode mode);
  141. /// Node added. Assign scene pointer and add to ID map.
  142. void NodeAdded(Node* node);
  143. /// Node removed. Remove from ID map.
  144. void NodeRemoved(Node* node);
  145. /// Component added. Add to ID map.
  146. void ComponentAdded(Component* component);
  147. /// Component removed. Remove from ID map.
  148. void ComponentRemoved(Component* component);
  149. /// %Set node user variable reverse mappings.
  150. void SetVarNamesAttr(String value);
  151. /// Return node user variable reverse mappings.
  152. String GetVarNamesAttr() const;
  153. /// Prepare network update by comparing attributes and marking replication states dirty as necessary.
  154. void PrepareNetworkUpdate();
  155. /// Clean up all references to a network connection that is about to be removed.
  156. void CleanupConnection(Connection* connection);
  157. /// Mark a node for attribute check on the next network update.
  158. void MarkNetworkUpdate(Node* node);
  159. /// Mark a comoponent for attribute check on the next network update.
  160. void MarkNetworkUpdate(Component* component);
  161. /// Mark a node dirty in scene replication states. The node does not need to have own replication state yet.
  162. void MarkReplicationDirty(Node* node);
  163. private:
  164. /// Handle the logic update event to update the scene, if active.
  165. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  166. /// Update asynchronous loading.
  167. void UpdateAsyncLoading();
  168. /// Finish asynchronous loading.
  169. void FinishAsyncLoading();
  170. /// Finish loading. Sets the scene filename and checksum.
  171. void FinishLoading(Deserializer* source);
  172. /// Replicated scene nodes by ID.
  173. HashMap<unsigned, Node*> replicatedNodes_;
  174. /// Local scene nodes by ID.
  175. HashMap<unsigned, Node*> localNodes_;
  176. /// Replicated components by ID.
  177. HashMap<unsigned, Component*> replicatedComponents_;
  178. /// Local components by ID.
  179. HashMap<unsigned, Component*> localComponents_;
  180. /// Asynchronous loading progress.
  181. AsyncProgress asyncProgress_;
  182. /// Node and component ID resolver for asynchronous loading.
  183. SceneResolver resolver_;
  184. /// Source file name.
  185. String fileName_;
  186. /// Required package files for networking.
  187. Vector<SharedPtr<PackageFile> > requiredPackageFiles_;
  188. /// Registered node user variable reverse mappings.
  189. HashMap<ShortStringHash, String> varNames_;
  190. /// Nodes to check for attribute changes on the next network update.
  191. HashSet<unsigned> networkUpdateNodes_;
  192. /// Components to check for attribute changes on the next network update.
  193. HashSet<unsigned> networkUpdateComponents_;
  194. /// Delayed dirty notification queue for components.
  195. PODVector<Component*> delayedDirtyComponents_;
  196. /// Mutex for the delayed dirty notification queue.
  197. Mutex sceneMutex_;
  198. /// Next free non-local node ID.
  199. unsigned replicatedNodeID_;
  200. /// Next free non-local component ID.
  201. unsigned replicatedComponentID_;
  202. /// Next free local node ID.
  203. unsigned localNodeID_;
  204. /// Next free local component ID.
  205. unsigned localComponentID_;
  206. /// Scene source file checksum.
  207. unsigned checksum_;
  208. /// Motion smoothing constant.
  209. float smoothingConstant_;
  210. /// Motion smoothing snap threshold.
  211. float snapThreshold_;
  212. /// Active flag.
  213. bool active_;
  214. /// Asynchronous loading flag.
  215. bool asyncLoading_;
  216. /// Threaded update flag.
  217. bool threadedUpdate_;
  218. };
  219. /// Register Scene library objects.
  220. void RegisterSceneLibrary(Context* context);