Scene.h 8.6 KB

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