Scene.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "Node.h"
  25. #include "XMLElement.h"
  26. class File;
  27. class PackageFile;
  28. /// First replicated node/component ID
  29. static const unsigned FIRST_NONLOCAL_ID = 0x1;
  30. /// Last replicated node/component ID
  31. static const unsigned LAST_NONLOCAL_ID = 0xffffff;
  32. /// First local node/component ID
  33. static const unsigned FIRST_LOCAL_ID = 0x01000000;
  34. /// Last local node/component ID
  35. static const unsigned LAST_LOCAL_ID = 0xffffffff;
  36. /// Scene's networking mode
  37. enum NetworkMode
  38. {
  39. NM_NONETWORK,
  40. NM_SERVER,
  41. NM_CLIENT
  42. };
  43. /// Scene's asynchronous loading progress
  44. struct AsyncProgress
  45. {
  46. /// File for binary mode
  47. SharedPtr<File> file_;
  48. /// XML file for XML mode
  49. SharedPtr<XMLFile> xmlFile_;
  50. /// Current XML element for XML mode
  51. XMLElement xmlElement_;
  52. /// Loaded root-level nodes
  53. unsigned loadedNodes_;
  54. /// Total root-level nodes
  55. unsigned totalNodes_;
  56. };
  57. /// Root scene node, represents the whole scene
  58. class Scene : public Node
  59. {
  60. OBJECT(Scene);
  61. using Node::SaveXML;
  62. public:
  63. /// Construct
  64. Scene(Context* context);
  65. /// Destruct
  66. virtual ~Scene();
  67. /// Register object factory. Node must be registered first
  68. static void RegisterObject(Context* context);
  69. /// Load from binary data. Return true if successful
  70. virtual bool Load(Deserializer& source);
  71. /// Save to binary data. Return true if successful
  72. virtual bool Save(Serializer& dest);
  73. /// Load from XML data. Return true if successful
  74. virtual bool LoadXML(const XMLElement& source);
  75. /// Update scene
  76. void Update(float timeStep);
  77. /// Load from an XML file. Return true if successful
  78. bool LoadXML(Deserializer& source);
  79. /// Save to an XML file. Return true if successful
  80. bool SaveXML(Serializer& dest);
  81. /// Load from a binary file asynchronously. Return true if started successfully
  82. bool LoadAsync(File* file);
  83. /// Load from an XML file asynchronously. Return true if started successfully
  84. bool LoadAsyncXML(File* file);
  85. /// Stop asynchronous loading
  86. void StopAsyncLoading();
  87. /// Set networking mode
  88. void SetNetworkMode(NetworkMode mode);
  89. /// Set active flag. Only active scenes will be updated automatically
  90. void SetActive(bool enable);
  91. /// Clear scene completely of nodes and components
  92. void Clear();
  93. /// Clear scene of all non-local child nodes. Note: if they have local children, they will be removed as well
  94. void ClearNonLocal();
  95. /// Add a required package file for multiplayer. To be called on the server
  96. void AddRequiredPackageFile(PackageFile* file);
  97. /// Clear required package files
  98. void ClearRequiredPackageFiles();
  99. /// Reset specific owner reference from nodes on disconnect
  100. void ResetOwner(Connection* owner);
  101. /// Return node from the whole scene by ID, or null if not found
  102. Node* GetNodeByID(unsigned id) const;
  103. /// Return component from the whole scene by ID, or null if not found
  104. Component* GetComponentByID(unsigned id) const;
  105. /// Return networking mode
  106. NetworkMode GetNetworkMode() const { return networkMode_; }
  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 std::string& GetFileName() const { return fileName_; }
  115. /// Return source file checksum
  116. unsigned GetChecksum() const { return checksum_; }
  117. /// Return required package files
  118. const std::vector<SharedPtr<PackageFile> >& GetRequiredPackageFiles() const { return requiredPackageFiles_; }
  119. /// Return all nodes
  120. const std::map<unsigned, Node*>& GetAllNodes() const { return allNodes_; }
  121. /// Return all components
  122. const std::map<unsigned, Component*>& GetAllComponents() const { return allComponents_; }
  123. /// Get free node ID, either non-local or local
  124. unsigned GetFreeunsigned(bool local);
  125. /// Get free component ID, either non-local or local
  126. unsigned GetFreeComponentID(bool local);
  127. /// Node added. Assign scene pointer and add to ID map
  128. void NodeAdded(Node* node);
  129. /// Node removed. Remove from ID map
  130. void NodeRemoved(Node* node);
  131. /// Component added. Add to ID map
  132. void ComponentAdded(Component* component);
  133. /// Component removed. Remove from ID map
  134. void ComponentRemoved(Component* component);
  135. private:
  136. /// Handle the logic update event to update the scene, if active
  137. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  138. /// Update asynchronous loading
  139. void UpdateAsyncLoading();
  140. /// Finish asynchronous loading
  141. void FinishAsyncLoading();
  142. /// Finish loading
  143. void FinishLoading(Deserializer* source);
  144. /// Map of scene nodes by ID
  145. std::map<unsigned, Node*> allNodes_;
  146. /// Map of components by ID
  147. std::map<unsigned, Component*> allComponents_;
  148. /// Networking mode
  149. NetworkMode networkMode_;
  150. /// Asynchronous loading progress
  151. AsyncProgress asyncProgress_;
  152. /// Source file name
  153. std::string fileName_;
  154. /// Required package files for multiplayer
  155. std::vector<SharedPtr<PackageFile> > requiredPackageFiles_;
  156. /// Next free non-local node ID
  157. unsigned nonLocalNodeID_;
  158. /// Next free local node ID
  159. unsigned localNodeID_;
  160. /// Next free non-local component ID
  161. unsigned nonLocalComponentID_;
  162. /// Next free local component ID
  163. unsigned localComponentID_;
  164. /// Scene source file checksum
  165. unsigned checksum_;
  166. /// Active flag
  167. bool active_;
  168. /// Asynchronous loading flag
  169. bool asyncLoading_;
  170. };
  171. /// Register Scene library objects
  172. void RegisterSceneLibrary(Context* context);