GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
FunctionFolderOrFunction.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 
15 namespace gd {
16 class Project;
17 class EventsFunction;
18 class SerializerElement;
19 class EventsFunctionsContainer;
20 } // namespace gd
21 
22 namespace gd {
23 
30 class GD_CORE_API FunctionFolderOrFunction {
31  public:
37 
38  virtual ~FunctionFolderOrFunction();
39 
44  FunctionFolderOrFunction* parent_ = nullptr);
45 
50  FunctionFolderOrFunction* parent_ = nullptr);
51 
55  gd::EventsFunction& GetFunction() const { return *function; }
56 
60  bool IsFolder() const { return !folderName.empty(); }
61 
65  const gd::String& GetFolderName() const { return folderName; }
66 
71  void SetFolderName(const gd::String& name);
72 
77  bool HasFunctionNamed(const gd::String& name);
78 
83  FunctionFolderOrFunction& GetFunctionNamed(const gd::String& name);
84 
89  std::size_t GetChildrenCount() const {
90  if (IsFolder()) return children.size();
91  return 0;
92  }
93 
97  FunctionFolderOrFunction& GetChildAt(std::size_t index);
98 
102  const FunctionFolderOrFunction& GetChildAt(std::size_t index) const;
103 
109  FunctionFolderOrFunction& GetFunctionChild(const gd::String& name);
110 
115  FunctionFolderOrFunction& GetOrCreateChildFolder(const gd::String& name);
116 
122  if (parent == nullptr) {
123  return badFunctionFolderOrFunction;
124  }
125  return *parent;
126  };
127 
132  bool IsRootFolder() { return !function && !parent; }
133 
137  void MoveChild(std::size_t oldIndex, std::size_t newIndex);
138 
143  void RemoveFolderChild(const FunctionFolderOrFunction& childToRemove);
144 
149  void RemoveRecursivelyFunctionNamed(const gd::String& name);
150 
154  void Clear();
155 
160  void InsertFunction(gd::EventsFunction* insertedFunction,
161  std::size_t position = (size_t)-1);
162 
167  FunctionFolderOrFunction& InsertNewFolder(const gd::String& newFolderName,
168  std::size_t position);
169 
174  bool IsADescendantOf(const FunctionFolderOrFunction& otherFunctionFolderOrFunction);
175 
180  std::size_t GetChildPosition(const FunctionFolderOrFunction& child) const;
181 
186  void MoveFunctionFolderOrFunctionToAnotherFolder(
187  gd::FunctionFolderOrFunction& functionFolderOrFunction,
188  gd::FunctionFolderOrFunction& newParentFolder,
189  std::size_t newPosition);
190 
195 
198  void SerializeTo(SerializerElement& element) const;
199 
203  void UnserializeFrom(const SerializerElement& element,
204  EventsFunctionsContainer& functionsContainer);
206 
207  void UpdateGroupNameOfAllFunctions();
208 
209  private:
210  void DoUpdateGroupNameOfAllFunctions(const gd::String& groupPath);
211  const gd::String GetGroupPath();
212 
213  static gd::FunctionFolderOrFunction badFunctionFolderOrFunction;
214  static gd::String emptyGroupName;
215 
217  parent = nullptr; // nullptr if root folder, points to the parent folder otherwise.
218 
219  // Representing a function:
220  gd::EventsFunction* function; // nullptr if folderName is set.
221 
222  // or representing a folder:
223  gd::String folderName; // Empty if function is set.
224 
225  std::vector<std::unique_ptr<FunctionFolderOrFunction>>
226  children; // Folder children.
227 };
228 
229 } // namespace gd
Events that can be generated as a stand-alone function, and used as a condition, action or expression...
Definition: EventsFunction.h:38
Used as a base class for classes that will own events-backed functions.
Definition: EventsFunctionsContainer.h:27
Class representing a folder structure in order to organize functions in folders (to be used with a Ev...
Definition: FunctionFolderOrFunction.h:30
const gd::String & GetFolderName() const
Returns the name of the folder.
Definition: FunctionFolderOrFunction.h:65
FunctionFolderOrFunction & GetParent()
Returns the parent of the instance. If the instance has no parent (root folder), the null function is...
Definition: FunctionFolderOrFunction.h:121
gd::EventsFunction & GetFunction() const
Returns the function behind the instance.
Definition: FunctionFolderOrFunction.h:55
bool IsRootFolder()
Returns true if the instance is a root folder (that's to say it has no parent).
Definition: FunctionFolderOrFunction.h:132
bool IsFolder() const
Returns true if the instance represents a folder.
Definition: FunctionFolderOrFunction.h:60
std::size_t GetChildrenCount() const
Returns the number of children. Returns 0 if the instance represents a function.
Definition: FunctionFolderOrFunction.h:89
A generic container that can represent a value ( containing a string, double, bool or int),...
Definition: SerializerElement.h:37
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24