1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SCENEPLUGIN_ENTITYCOLLECTION_H
17 #define SCENEPLUGIN_ENTITYCOLLECTION_H
18 
19 #include <scene_plugin/interface/intf_entity_collection.h>
20 
21 #include <base/containers/unique_ptr.h>
22 #include <base/containers/unordered_map.h>
23 
SCENE_BEGIN_NAMESPACE()24 SCENE_BEGIN_NAMESPACE()
25 
26 class EntityCollection : public IEntityCollection, private IEntityCollection::IListener {
27 public:
28     using Ptr = BASE_NS::unique_ptr<EntityCollection, Deleter>;
29 
30     EntityCollection(CORE_NS::IEcs& ecs, BASE_NS::string_view uri, BASE_NS::string_view contextUri);
31 
32     void AddListener(IEntityCollection::IListener& listener) override;
33     void RemoveListener(IEntityCollection::IListener& listener) override;
34 
35     //
36     // From IEntityCollection
37     //
38 
39     IEntityCollection::Ptr CreateNewEntityCollection(
40         BASE_NS::string_view uri, BASE_NS::string_view contextUri) override;
41 
42     CORE_NS::IEcs& GetEcs() const override;
43     BASE_NS::string GetUri() const override;
44     void SetUri(const BASE_NS::string& uri) override;
45 
46     BASE_NS::string GetContextUri() const override;
47 
48     BASE_NS::string GetSrc() const override;
49     void SetSrc(BASE_NS::string_view src) override;
50 
51     BASE_NS::string GetType() const override;
52     void SetType(BASE_NS::string_view type) override;
53 
54     size_t GetEntityCount() const override;
55     CORE_NS::EntityReference GetEntity(size_t collectionIndex) const override;
56     CORE_NS::EntityReference GetEntity(BASE_NS::string_view localContextId) const override;
57     CORE_NS::EntityReference GetEntityRecursive(BASE_NS::string_view localContextId) const override;
58     BASE_NS::array_view<const CORE_NS::EntityReference> GetEntities() const override;
59     void AddEntity(CORE_NS::EntityReference entitity) override;
60     void AddEntities(BASE_NS::array_view<const CORE_NS::EntityReference> entities) override;
61     bool RemoveEntity(CORE_NS::EntityReference entitity) override;
62     void RemoveEntities(BASE_NS::array_view<const CORE_NS::EntityReference> entities) override;
63     void RemoveEntityRecursive(CORE_NS::Entity entity) override;
64 
65     void SetId(BASE_NS::string_view id, CORE_NS::EntityReference entity) override;
66     BASE_NS::string_view GetId(CORE_NS::Entity entity) const override;
67     BASE_NS::string_view GetIdRecursive(CORE_NS::Entity entity) const override;
68 
69     void SetUniqueIdentifier(BASE_NS::string_view id, CORE_NS::EntityReference entity) override;
70     BASE_NS::string_view GetUniqueIdentifier(CORE_NS::Entity entity) const override;
71     BASE_NS::string_view GetUniqueIdentifierRecursive(CORE_NS::Entity entity) const override;
72 
73     size_t GetSubCollectionCount() const override;
74     IEntityCollection* GetSubCollection(size_t index) override;
75     const IEntityCollection* GetSubCollection(size_t index) const override;
76     int32_t GetSubCollectionIndex(BASE_NS::string_view uri) const override;
77     int32_t GetSubCollectionIndexByRoot(CORE_NS::Entity entity) const override;
78     IEntityCollection& AddSubCollection(
79         BASE_NS::string_view uri, BASE_NS::string_view contextUri, bool serializable) override;
80     IEntityCollection& AddSubCollectionClone(IEntityCollection& collection, BASE_NS::string_view uri) override;
81     void RemoveSubCollection(size_t index) override;
82 
83     size_t GetEntityCountRecursive(bool includeDestroyed, bool includeNonSerialized = true) const override;
84     void GetEntitiesRecursive(bool includeDestroyed, BASE_NS::vector<CORE_NS::EntityReference>& entitiesOut,
85         bool includeNonSerialized = true) const override;
86 
87     bool Contains(CORE_NS::Entity entity) const override;
88     bool IsExternal(CORE_NS::Entity entity) const override;
89     bool isSubCollectionRoot(CORE_NS::Entity entity) const override;
90     CORE_NS::EntityReference GetReference(CORE_NS::Entity entity) const override;
91 
92     void SetActive(bool active) override;
93     bool IsActive() const override;
94 
95     void MarkDestroyed(bool destroyed) override;
96     bool IsMarkedDestroyed() const override;
97 
98     void MarkModified(bool modified) override;
99     void MarkModified(bool modified, bool recursive) override;
100     bool IsMarkedModified() const override;
101 
102     void Clear() override;
103 
104     void CopyContents(IEntityCollection& srcCollection) override;
105 
106     BASE_NS::vector<CORE_NS::EntityReference> CopyContentsWithSerialization(IEntityCollection& srcCollection) override;
107     BASE_NS::vector<CORE_NS::EntityReference> CopyContentsWithSerialization(
108         IEntityCollection& srcCollection, BASE_NS::array_view<const CORE_NS::EntityReference> entities) override;
109 
110     bool MarkComponentSerialized(CORE_NS::Entity entity, BASE_NS::Uid component, bool serialize) override;
111     bool MarkAllPropertiesSerialized(CORE_NS::Entity entity, BASE_NS::Uid component) override;
112     bool MarkPropertySerialized(
113         CORE_NS::Entity entity, BASE_NS::Uid component, BASE_NS::string_view propertyPath, bool serialize) override;
114     bool IsPropertySerialized(
115         CORE_NS::Entity entity, BASE_NS::Uid component, BASE_NS::string_view propertyPath) override;
116     const PropertyList* GetSerializedProperties(CORE_NS::Entity entity, BASE_NS::Uid component) const override;
117 
118     void AddEntityToSubcollection(
119         BASE_NS::string_view collection, BASE_NS::string_view name, CORE_NS::Entity entity, bool makeUnique) override;
120 
121     bool IsSerialized() const override;
122     void SetSerialized(bool serialize) override;
123 
124 protected:
125     void Destroy() override;
126     ~EntityCollection() override;
127 
128 private:
129     // Prevent copying.
130     EntityCollection(const EntityCollection&) = delete;
131     EntityCollection& operator=(const EntityCollection&) = delete;
132 
133     // From IEntityCollection::IListener
134     void ModifiedChanged(IEntityCollection& entityCollection, bool modified) override;
135 
136     void DoGetEntitiesRecursive(
137         bool includeDestroyed, bool includeNonSerialized, BASE_NS::vector<CORE_NS::EntityReference>& entitiesOut) const;
138 
139     void ClonePrivate(EntityCollection& dst) const;
140     void DoCloneRecursive(EntityCollection& dst) const;
141 
142     // TODO: Create a better data structure for this information.
143     // Components to be serialized in an entity.
144     using ComponentMap = BASE_NS::unordered_map<BASE_NS::Uid, PropertyList>;
145     BASE_NS::unordered_map<CORE_NS::Entity, ComponentMap> serializationInfo_;
146 
147     CORE_NS::IEcs& ecs_;
148     BASE_NS::string uri_;
149     BASE_NS::string contextUri_;
150 
151     BASE_NS::string src_ {};
152     BASE_NS::string type_ {};
153 
154     BASE_NS::unordered_map<BASE_NS::string, CORE_NS::EntityReference> namedEntities_;
155     BASE_NS::unordered_map<BASE_NS::string, CORE_NS::EntityReference> entityIdentifiers_;
156     BASE_NS::vector<CORE_NS::EntityReference> entities_;
157 
158     BASE_NS::vector<EntityCollection::Ptr> collections_;
159 
160     bool isActive_ { true };
161     bool isMarkedDestroyed_ { false };
162     bool isMarkedModified_ { false };
163     bool isSerialized_ { true };
164 
165     BASE_NS::vector<IEntityCollection::IListener*> listeners_;
166 };
167 
168 SCENE_END_NAMESPACE()
169 
170 #endif // SCENEPLUGIN_ENTITYCOLLECTION_H
171