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_INTF_ENTITYCOLLECTION_H
17 #define SCENEPLUGIN_INTF_ENTITYCOLLECTION_H
18 
19 #include <scene_plugin/namespace.h>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/string.h>
23 #include <base/containers/string_view.h>
24 #include <core/ecs/entity_reference.h>
25 #include <core/ecs/intf_ecs.h>
26 
SCENE_BEGIN_NAMESPACE()27 SCENE_BEGIN_NAMESPACE()
28 
29 class IEntityCollection {
30 public:
31     struct Deleter {
32         constexpr Deleter() noexcept = default;
33         void operator()(IEntityCollection* ptr) const
34         {
35             ptr->Destroy();
36         }
37     };
38     using Ptr = BASE_NS::unique_ptr<IEntityCollection, Deleter>;
39 
40     class IListener {
41     public:
42         virtual void ModifiedChanged(IEntityCollection& entityCollection, bool modified) = 0;
43     };
44     virtual void AddListener(IListener& listener) = 0;
45     virtual void RemoveListener(IListener& listener) = 0;
46 
47     // A list of property paths in a component to be serialized.
48     using PropertyList = BASE_NS::vector<BASE_NS::string>;
49 
50     // Creates a new Empty Entity Collection with the same ECS as source
51     virtual IEntityCollection::Ptr CreateNewEntityCollection(
52         BASE_NS::string_view uri, BASE_NS::string_view contextUri) = 0;
53 
54     virtual CORE_NS::IEcs& GetEcs() const = 0;
55     virtual BASE_NS::string GetUri() const = 0;
56     virtual void SetUri(const BASE_NS::string& uri) = 0;
57     virtual BASE_NS::string GetContextUri() const = 0;
58 
59     virtual BASE_NS::string GetSrc() const = 0;
60     virtual void SetSrc(BASE_NS::string_view src) = 0;
61 
62     virtual BASE_NS::string GetType() const = 0;
63     virtual void SetType(BASE_NS::string_view type) = 0;
64 
65     // Entities.
66     virtual size_t GetEntityCount() const = 0;
67     virtual CORE_NS::EntityReference GetEntity(size_t collectionIndex) const = 0;
68     virtual CORE_NS::EntityReference GetEntity(BASE_NS::string_view localContextId) const = 0;
69     virtual CORE_NS::EntityReference GetEntityRecursive(BASE_NS::string_view localContextId) const = 0;
70     virtual BASE_NS::array_view<const CORE_NS::EntityReference> GetEntities() const = 0;
71     virtual void AddEntity(CORE_NS::EntityReference entitity) = 0;
72     virtual void AddEntities(BASE_NS::array_view<const CORE_NS::EntityReference> entities) = 0;
73     virtual bool RemoveEntity(CORE_NS::EntityReference entitity) = 0;
74     virtual void RemoveEntities(BASE_NS::array_view<const CORE_NS::EntityReference> entities) = 0;
75     virtual void RemoveEntityRecursive(CORE_NS::Entity entity) = 0;
76     virtual void SetId(BASE_NS::string_view id, CORE_NS::EntityReference entity) = 0;
77     virtual BASE_NS::string_view GetId(CORE_NS::Entity entity) const = 0;
78     virtual BASE_NS::string_view GetIdRecursive(CORE_NS::Entity entity) const = 0;
79 
80     virtual void SetUniqueIdentifier(BASE_NS::string_view id, CORE_NS::EntityReference entity) = 0;
81     virtual BASE_NS::string_view GetUniqueIdentifier(CORE_NS::Entity entity) const = 0;
82     virtual BASE_NS::string_view GetUniqueIdentifierRecursive(CORE_NS::Entity entity) const = 0;
83 
84     // Sub-collections.
85     virtual size_t GetSubCollectionCount() const = 0;
86     virtual IEntityCollection* GetSubCollection(size_t index) = 0;
87     virtual const IEntityCollection* GetSubCollection(size_t index) const = 0;
88     virtual int32_t GetSubCollectionIndex(BASE_NS::string_view uri) const = 0;
89     virtual int32_t GetSubCollectionIndexByRoot(CORE_NS::Entity entity) const = 0;
90     virtual IEntityCollection& AddSubCollection(
91         BASE_NS::string_view uri, BASE_NS::string_view contextUri, bool serializable = true) = 0;
92     virtual IEntityCollection& AddSubCollectionClone(IEntityCollection& collection, BASE_NS::string_view uri) = 0;
93     virtual void RemoveSubCollection(size_t index) = 0;
94 
95     // All entities recursively.
96     virtual size_t GetEntityCountRecursive(bool includeDestroyed, bool includeNonSerialized = true) const = 0;
97     virtual void GetEntitiesRecursive(bool includeDestroyed, BASE_NS::vector<CORE_NS::EntityReference>& entitiesOut,
98         bool includeNonSerialized = true) const = 0;
99 
100     virtual bool Contains(CORE_NS::Entity entity) const = 0;
101     virtual bool IsExternal(CORE_NS::Entity entity) const = 0;
102     virtual bool isSubCollectionRoot(CORE_NS::Entity entity) const = 0;
103     virtual CORE_NS::EntityReference GetReference(CORE_NS::Entity entity) const = 0;
104 
105     virtual void SetActive(bool active) = 0;
106     virtual bool IsActive() const = 0;
107 
108     virtual void MarkDestroyed(bool destroyed) = 0;
109     virtual bool IsMarkedDestroyed() const = 0;
110 
111     virtual void MarkModified(bool modified) = 0;
112     virtual void MarkModified(bool modified, bool recursive) = 0;
113     virtual bool IsMarkedModified() const = 0;
114 
115     virtual void Clear() = 0;
116 
117     virtual void CopyContents(IEntityCollection& srcCollection) = 0;
118 
119     virtual BASE_NS::vector<CORE_NS::EntityReference> CopyContentsWithSerialization(
120         IEntityCollection& srcCollection) = 0;
121     virtual BASE_NS::vector<CORE_NS::EntityReference> CopyContentsWithSerialization(
122         IEntityCollection& srcCollection, BASE_NS::array_view<const CORE_NS::EntityReference> entities) = 0;
123 
124     virtual void AddEntityToSubcollection(
125         BASE_NS::string_view collection, BASE_NS::string_view name, CORE_NS::Entity entity, bool makeUnique = true) = 0;
126 
127     // Serialization state.
128     virtual bool MarkComponentSerialized(CORE_NS::Entity entity, BASE_NS::Uid component, bool serialize) = 0;
129     virtual bool MarkAllPropertiesSerialized(CORE_NS::Entity entity, BASE_NS::Uid component) = 0;
130     virtual bool MarkPropertySerialized(
131         CORE_NS::Entity entity, BASE_NS::Uid component, BASE_NS::string_view propertyPath, bool serialize) = 0;
132     virtual bool IsPropertySerialized(
133         CORE_NS::Entity entity, BASE_NS::Uid component, BASE_NS::string_view propertyPath) = 0;
134     virtual const PropertyList* GetSerializedProperties(CORE_NS::Entity entity, BASE_NS::Uid component) const = 0;
135 
136     virtual bool IsSerialized() const = 0;
137     virtual void SetSerialized(bool serialize) = 0;
138 
139 protected:
140     IEntityCollection() = default;
141     virtual ~IEntityCollection() = default;
142     virtual void Destroy() = 0;
143 };
144 
145 SCENE_END_NAMESPACE()
146 
147 #endif // SCENEPLUGIN_INTF_ENTITYCOLLECTION_H
148