GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ObjectFolderOrObject.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-2023 Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #ifndef GDCORE_OBJECTFOLDEROROBJECT_H
7 #define GDCORE_OBJECTFOLDEROROBJECT_H
8 #include <memory>
9 #include <vector>
10 
11 #include "GDCore/Serialization/SerializerElement.h"
12 #include "GDCore/String.h"
13 #include "GDCore/Project/QuickCustomization.h"
14 
15 namespace gd {
16 class Project;
17 class Object;
18 class SerializerElement;
19 class ObjectsContainer;
20 } // namespace gd
21 
22 namespace gd {
23 
30 class GD_CORE_API ObjectFolderOrObject {
31  public:
37  virtual ~ObjectFolderOrObject();
41  ObjectFolderOrObject(gd::String folderName_,
42  ObjectFolderOrObject* parent_ = nullptr);
47  ObjectFolderOrObject* parent_ = nullptr);
48 
52  gd::Object& GetObject() const { return *object; }
53 
57  bool IsFolder() const { return !folderName.empty(); }
61  const gd::String& GetFolderName() const { return folderName; }
62 
67  void SetFolderName(const gd::String& name);
68 
73  bool HasObjectNamed(const gd::String& name);
78  ObjectFolderOrObject& GetObjectNamed(const gd::String& name);
79 
84  std::size_t GetChildrenCount() const {
85  if (IsFolder()) return children.size();
86  return 0;
87  }
91  ObjectFolderOrObject& GetChildAt(std::size_t index);
95  const ObjectFolderOrObject& GetChildAt(std::size_t index) const;
104  ObjectFolderOrObject& GetObjectChild(const gd::String& name);
105 
111  if (parent == nullptr) {
112  return badObjectFolderOrObject;
113  }
114  return *parent;
115  };
116 
121  bool IsRootFolder() { return !object && !parent; }
122 
126  void MoveChild(std::size_t oldIndex, std::size_t newIndex);
131  void RemoveFolderChild(const ObjectFolderOrObject& childToRemove);
136  void RemoveRecursivelyObjectNamed(const gd::String& name);
140  void Clear();
141 
146  void InsertObject(gd::Object* insertedObject,
147  std::size_t position = (size_t)-1);
152  ObjectFolderOrObject& InsertNewFolder(const gd::String& newFolderName,
153  std::size_t position);
158  bool IsADescendantOf(const ObjectFolderOrObject& otherObjectFolderOrObject);
159 
164  std::size_t GetChildPosition(const ObjectFolderOrObject& child) const;
169  void MoveObjectFolderOrObjectToAnotherFolder(
170  gd::ObjectFolderOrObject& objectFolderOrObject,
171  gd::ObjectFolderOrObject& newParentFolder,
172  std::size_t newPosition);
173 
174  QuickCustomization::Visibility GetQuickCustomizationVisibility() const { return quickCustomizationVisibility; }
175  void SetQuickCustomizationVisibility(QuickCustomization::Visibility visibility) {
176  quickCustomizationVisibility = visibility;
177  }
178 
183 
186  void SerializeTo(SerializerElement& element) const;
187 
191  void UnserializeFrom(gd::Project& project,
192  const SerializerElement& element,
193  ObjectsContainer& objectsContainer);
195 
196  private:
197  static gd::ObjectFolderOrObject badObjectFolderOrObject;
198 
200  parent = nullptr; // nullptr if root folder, points to the parent folder otherwise.
201  QuickCustomization::Visibility quickCustomizationVisibility;
202 
203  // Representing an object:
204  gd::Object* object; // nullptr if folderName is set.
205 
206  // or representing a folder:
207  gd::String folderName; // Empty if object is set.
208  std::vector<std::unique_ptr<ObjectFolderOrObject>>
209  children; // Folder children.
210 };
211 
212 } // namespace gd
213 
214 #endif // GDCORE_OBJECTFOLDEROROBJECT_H
Class representing a folder structure in order to organize objects in folders (to be used with an Obj...
Definition: ObjectFolderOrObject.h:30
bool IsRootFolder()
Returns true if the instance is a root folder (that's to say it has no parent).
Definition: ObjectFolderOrObject.h:121
const gd::String & GetFolderName() const
Returns the name of the folder.
Definition: ObjectFolderOrObject.h:61
gd::Object & GetObject() const
Returns the object behind the instance.
Definition: ObjectFolderOrObject.h:52
ObjectFolderOrObject & GetParent()
Returns the parent of the instance. If the instance has no parent (root folder), the null object is r...
Definition: ObjectFolderOrObject.h:110
bool IsFolder() const
Returns true if the instance represents a folder.
Definition: ObjectFolderOrObject.h:57
std::size_t GetChildrenCount() const
Returns the number of children. Returns 0 if the instance represents an object.
Definition: ObjectFolderOrObject.h:84
Represent an object of a platform.
Definition: Object.h:37
Base class representing a project (game), including all resources, scenes, objects,...
Definition: Project.h:50
Visibility
Definition: QuickCustomization.h:8
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24