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 #pragma once
7 
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 #include "GDCore/Project/MemoryTrackedRegistry.h"
15 
16 namespace gd {
17 class Project;
18 class Object;
19 class SerializerElement;
20 class ObjectsContainer;
21 } // namespace gd
22 
23 namespace gd {
24 
31 class GD_CORE_API ObjectFolderOrObject {
32  public:
38  virtual ~ObjectFolderOrObject();
42  ObjectFolderOrObject(gd::String folderName_,
43  ObjectFolderOrObject* parent_ = nullptr);
48  ObjectFolderOrObject* parent_ = nullptr);
49 
53  gd::Object& GetObject() const { return *object; }
54 
58  bool IsFolder() const { return !folderName.empty(); }
62  const gd::String& GetFolderName() const { return folderName; }
63 
68  void SetFolderName(const gd::String& name);
69 
74  bool HasObjectNamed(const gd::String& name);
79  ObjectFolderOrObject& GetObjectNamed(const gd::String& name);
80 
85  std::size_t GetChildrenCount() const {
86  if (IsFolder()) return children.size();
87  return 0;
88  }
92  ObjectFolderOrObject& GetChildAt(std::size_t index);
96  const ObjectFolderOrObject& GetChildAt(std::size_t index) const;
105  ObjectFolderOrObject& GetObjectChild(const gd::String& name);
106 
112  if (parent == nullptr) {
113  return badObjectFolderOrObject;
114  }
115  return *parent;
116  };
117 
122  bool IsRootFolder() { return !object && !parent; }
123 
127  void MoveChild(std::size_t oldIndex, std::size_t newIndex);
132  void RemoveFolderChild(const ObjectFolderOrObject& childToRemove);
137  void RemoveRecursivelyObjectNamed(const gd::String& name);
141  void Clear();
142 
147  void InsertObject(gd::Object* insertedObject,
148  std::size_t position = (size_t)-1);
153  ObjectFolderOrObject& InsertNewFolder(const gd::String& newFolderName,
154  std::size_t position);
155 
160  ObjectFolderOrObject &GetOrCreateFolderChild(const gd::String &name);
161 
166  bool IsADescendantOf(const ObjectFolderOrObject& otherObjectFolderOrObject);
167 
172  std::size_t GetChildPosition(const ObjectFolderOrObject& child) const;
177  void MoveObjectFolderOrObjectToAnotherFolder(
178  gd::ObjectFolderOrObject& objectFolderOrObject,
179  gd::ObjectFolderOrObject& newParentFolder,
180  std::size_t newPosition);
181 
182  QuickCustomization::Visibility GetQuickCustomizationVisibility() const { return quickCustomizationVisibility; }
183  void SetQuickCustomizationVisibility(QuickCustomization::Visibility visibility) {
184  quickCustomizationVisibility = visibility;
185  }
186 
191 
194  void SerializeTo(SerializerElement& element) const;
195 
199  void UnserializeFrom(gd::Project& project,
200  const SerializerElement& element,
201  ObjectsContainer& objectsContainer);
203 
204  private:
205  static gd::ObjectFolderOrObject badObjectFolderOrObject;
206 
208  parent = nullptr; // nullptr if root folder, points to the parent folder otherwise.
209  QuickCustomization::Visibility quickCustomizationVisibility;
210 
211  // Representing an object:
212  gd::Object* object; // nullptr if folderName is set.
213 
214  // or representing a folder:
215  gd::String folderName; // Empty if object is set.
216  std::vector<std::unique_ptr<ObjectFolderOrObject>>
217  children; // Folder children.
218 
219  gd::MemoryTracked _memoryTracked{this, "ObjectFolderOrObject"};
220 };
221 
222 } // namespace gd
A non-copyable, non-movable member object that registers/unregisters its owner with MemoryTrackedRegi...
Definition: MemoryTrackedRegistry.h:238
Class representing a folder structure in order to organize objects in folders (to be used with an Obj...
Definition: ObjectFolderOrObject.h:31
bool IsRootFolder()
Returns true if the instance is a root folder (that's to say it has no parent).
Definition: ObjectFolderOrObject.h:122
const gd::String & GetFolderName() const
Returns the name of the folder.
Definition: ObjectFolderOrObject.h:62
gd::Object & GetObject() const
Returns the object behind the instance.
Definition: ObjectFolderOrObject.h:53
ObjectFolderOrObject & GetParent()
Returns the parent of the instance. If the instance has no parent (root folder), the null object is r...
Definition: ObjectFolderOrObject.h:111
bool IsFolder() const
Returns true if the instance represents a folder.
Definition: ObjectFolderOrObject.h:58
std::size_t GetChildrenCount() const
Returns the number of children. Returns 0 if the instance represents an object.
Definition: ObjectFolderOrObject.h:85
Represent an object of a platform.
Definition: Object.h:39
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