2
0

Scene.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "Mutex.h"
  25. #include "Node.h"
  26. #include "SceneResolver.h"
  27. #include "XMLElement.h"
  28. class File;
  29. class PackageFile;
  30. static const unsigned FIRST_REPLICATED_ID = 0x1;
  31. static const unsigned LAST_REPLICATED_ID = 0xffffff;
  32. static const unsigned FIRST_LOCAL_ID = 0x01000000;
  33. static const unsigned LAST_LOCAL_ID = 0xffffffff;
  34. /// Asynchronous loading progress of a scene.
  35. struct AsyncProgress
  36. {
  37. /// File for binary mode.
  38. SharedPtr<File> file_;
  39. /// XML file for XML mode.
  40. SharedPtr<XMLFile> xmlFile_;
  41. /// Current XML element for XML mode.
  42. XMLElement xmlElement_;
  43. /// Loaded root-level nodes.
  44. unsigned loadedNodes_;
  45. /// Total root-level nodes.
  46. unsigned totalNodes_;
  47. };
  48. /// Root scene node, represents the whole scene.
  49. class Scene : public Node
  50. {
  51. OBJECT(Scene);
  52. using Node::GetComponent;
  53. using Node::SaveXML;
  54. public:
  55. /// Construct.
  56. Scene(Context* context);
  57. /// Destruct.
  58. virtual ~Scene();
  59. /// Register object factory. Node must be registered first.
  60. static void RegisterObject(Context* context);
  61. /// Load from binary data. Return true if successful.
  62. virtual bool Load(Deserializer& source);
  63. /// Save to binary data. Return true if successful.
  64. virtual bool Save(Serializer& dest);
  65. /// Load from XML data. Return true if successful.
  66. virtual bool LoadXML(const XMLElement& source);
  67. /// Load from an XML file. Return true if successful.
  68. bool LoadXML(Deserializer& source);
  69. /// Save to an XML file. Return true if successful.
  70. bool SaveXML(Serializer& dest);
  71. /// Load from a binary file asynchronously. Return true if started successfully.
  72. bool LoadAsync(File* file);
  73. /// Load from an XML file asynchronously. Return true if started successfully.
  74. bool LoadAsyncXML(File* file);
  75. /// Stop asynchronous loading.
  76. void StopAsyncLoading();
  77. /// Instantiate scene content from binary data. Return root node if successful.
  78. Node* Instantiate(Deserializer& source, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
  79. /// Instantiate scene content from XML data. Return root node if successful.
  80. Node* InstantiateXML(const XMLElement& source, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
  81. /// Instantiate scene content from XML data. Return root node if successful.
  82. Node* InstantiateXML(Deserializer& source, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
  83. /// Clear scene completely of nodes and components.
  84. void Clear();
  85. /// %Set active flag. Only active scenes will be updated automatically.
  86. void SetActive(bool enable);
  87. /// %Set network client motion smoothing constant.
  88. void SetSmoothingConstant(float constant);
  89. /// %Set network client motion smoothing snap threshold.
  90. void SetSnapThreshold(float threshold);
  91. /// Add a required package file for networking. To be called on the server.
  92. void AddRequiredPackageFile(PackageFile* package);
  93. /// Clear required package files.
  94. void ClearRequiredPackageFiles();
  95. /// Reset specific owner reference from nodes on disconnect.
  96. void ResetOwner(Connection* owner);
  97. /// Register a node user variable hash reverse mapping (for editing.)
  98. void RegisterVar(const String& name);
  99. /// Unregister a node user variable hash reverse mapping.
  100. void UnregisterVar(const String& name);
  101. /// Clear all registered node user variable hash reverse mappings.
  102. void UnregisterAllVars();
  103. /// Return node from the whole scene by ID, or null if not found.
  104. Node* GetNode(unsigned id) const;
  105. /// Return component from the whole scene by ID, or null if not found.
  106. Component* GetComponent(unsigned id) const;
  107. /// Return active flag.
  108. bool IsActive() const { return active_; }
  109. /// Return asynchronous loading flag.
  110. bool IsAsyncLoading() const { return asyncLoading_; }
  111. /// Return asynchronous loading progress between 0.0 and 1.0, or 1.0 if not in progress.
  112. float GetAsyncProgress() const;
  113. /// Return source file name.
  114. const String& GetFileName() const { return fileName_; }
  115. /// Return source file checksum.
  116. unsigned GetChecksum() const { return checksum_; }
  117. /// Return motion smoothing constant.
  118. float GetSmoothingConstant() const { return smoothingConstant_; }
  119. /// Return motion smoothing snap threshold.
  120. float GetSnapThreshold() const { return snapThreshold_; }
  121. /// Return required package files.
  122. const Vector<SharedPtr<PackageFile> >& GetRequiredPackageFiles() const { return requiredPackageFiles_; }
  123. /// Return all replicated scene nodes.
  124. const HashMap<unsigned, Node*>& GetReplicatedNodes() const { return replicatedNodes_; }
  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. private:
  154. /// Handle the logic update event to update the scene, if active.
  155. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  156. /// Update asynchronous loading.
  157. void UpdateAsyncLoading();
  158. /// Finish asynchronous loading.
  159. void FinishAsyncLoading();
  160. /// Finish loading. Sets the scene filename and checksum.
  161. void FinishLoading(Deserializer* source);
  162. /// Replicated scene nodes by ID.
  163. HashMap<unsigned, Node*> replicatedNodes_;
  164. /// Local scene nodes by ID.
  165. HashMap<unsigned, Node*> localNodes_;
  166. /// Replicated components by ID.
  167. HashMap<unsigned, Component*> replicatedComponents_;
  168. /// Local components by ID.
  169. HashMap<unsigned, Component*> localComponents_;
  170. /// Asynchronous loading progress.
  171. AsyncProgress asyncProgress_;
  172. /// Node and component ID resolver for asynchronous loading.
  173. SceneResolver resolver_;
  174. /// Source file name.
  175. String fileName_;
  176. /// Required package files for networking.
  177. Vector<SharedPtr<PackageFile> > requiredPackageFiles_;
  178. /// Registered node user variable reverse mappings.
  179. HashMap<ShortStringHash, String> varNames_;
  180. /// Delayed dirty notification queue for components.
  181. PODVector<Component*> delayedDirtyComponents_;
  182. /// Mutex for the delayed dirty notification queue.
  183. Mutex sceneMutex_;
  184. /// Next free non-local node ID.
  185. unsigned replicatedNodeID_;
  186. /// Next free local node ID.
  187. unsigned localNodeID_;
  188. /// Next free non-local component ID.
  189. unsigned replicatedComponentID_;
  190. /// Next free local component ID.
  191. unsigned localComponentID_;
  192. /// Scene source file checksum.
  193. unsigned checksum_;
  194. /// Motion smoothing constant.
  195. float smoothingConstant_;
  196. /// Motion smoothing snap threshold.
  197. float snapThreshold_;
  198. /// Active flag.
  199. bool active_;
  200. /// Asynchronous loading flag.
  201. bool asyncLoading_;
  202. /// Threaded update flag.
  203. bool threadedUpdate_;
  204. };
  205. /// Register Scene library objects.
  206. void RegisterSceneLibrary(Context* context);