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);
137 
142  void InsertObject(gd::Object* insertedObject,
143  std::size_t position = (size_t)-1);
148  ObjectFolderOrObject& InsertNewFolder(const gd::String& newFolderName,
149  std::size_t position);
154  bool IsADescendantOf(const ObjectFolderOrObject& otherObjectFolderOrObject);
155 
160  std::size_t GetChildPosition(const ObjectFolderOrObject& child) const;
165  void MoveObjectFolderOrObjectToAnotherFolder(
166  gd::ObjectFolderOrObject& objectFolderOrObject,
167  gd::ObjectFolderOrObject& newParentFolder,
168  std::size_t newPosition);
169 
170  QuickCustomization::Visibility GetQuickCustomizationVisibility() const { return quickCustomizationVisibility; }
171  void SetQuickCustomizationVisibility(QuickCustomization::Visibility visibility) {
172  quickCustomizationVisibility = visibility;
173  }
174 
179 
182  void SerializeTo(SerializerElement& element) const;
183 
187  void UnserializeFrom(gd::Project& project,
188  const SerializerElement& element,
189  ObjectsContainer& objectsContainer);
191 
192  private:
193  static gd::ObjectFolderOrObject badObjectFolderOrObject;
194 
196  parent = nullptr; // nullptr if root folder, points to the parent folder otherwise.
197  QuickCustomization::Visibility quickCustomizationVisibility;
198 
199  // Representing an object:
200  gd::Object* object; // nullptr if folderName is set.
201 
202  // or representing a folder:
203  gd::String folderName; // Empty if object is set.
204  std::vector<std::unique_ptr<ObjectFolderOrObject>>
205  children; // Folder children.
206 };
207 
208 } // namespace gd
209 
210 #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:51
Visibility
Definition: QuickCustomization.h:8
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24