GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
EventsFunctionsContainer.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #pragma once
7 
8 #include <vector>
9 #include "GDCore/Project/EventsFunction.h"
10 #include "GDCore/Project/FunctionFolderOrFunction.h"
11 #include "GDCore/String.h"
12 #include "GDCore/Tools/SerializableWithNameList.h"
13 namespace gd {
14 class SerializerElement;
15 }
16 
17 namespace gd {
18 
26 class GD_CORE_API EventsFunctionsContainer
27  : private SerializableWithNameList<gd::EventsFunction> {
28 public:
29  enum FunctionOwner {
30  Extension,
31  Object,
32  Behavior};
33 
34  EventsFunctionsContainer(FunctionOwner source_) : owner(source_) {
35  // The properties folders are not copied.
36  // It's not an issue because the UI uses the serialization for duplication.
37  rootFolder = gd::make_unique<gd::FunctionFolderOrFunction>("__ROOT");
38  }
39 
41  : owner(other.owner) {
42  Init(other);
43  }
44 
45  EventsFunctionsContainer &operator=(const EventsFunctionsContainer &other) {
46  if (this != &other) {
47  owner = other.owner;
48  Init(other);
49  }
50 
51  return *this;
52  }
53 
60  FunctionOwner GetOwner() const {
61  return owner;
62  }
63 
67 
70  bool HasEventsFunctionNamed(const gd::String& name) const {
71  return Has(name);
72  }
73 
81  return Get(name);
82  }
83 
90  const gd::EventsFunction& GetEventsFunction(const gd::String& name) const {
91  return Get(name);
92  }
93 
100  gd::EventsFunction& GetEventsFunction(std::size_t index) {
101  return Get(index);
102  }
103 
110  const gd::EventsFunction& GetEventsFunction(std::size_t index) const {
111  return Get(index);
112  }
113 
117  std::size_t GetEventsFunctionsCount() const { return GetCount(); }
118 
119  gd::EventsFunction &InsertNewEventsFunction(const gd::String &name,
120  std::size_t position) {
121  auto &newlyCreatedFunction = InsertNew(name, position);
122  rootFolder->InsertFunction(&newlyCreatedFunction);
123  return newlyCreatedFunction;
124  }
125 
127  InsertEventsFunction(const gd::EventsFunction &eventFunction,
128  std::size_t position) {
129  auto &newlyCreatedFunction = Insert(eventFunction, position);
130  rootFolder->InsertFunction(&newlyCreatedFunction);
131  return newlyCreatedFunction;
132  }
133 
134  void RemoveEventsFunction(const gd::String& name) {
135  rootFolder->RemoveRecursivelyFunctionNamed(name);
136  return Remove(name);
137  }
138  void ClearEventsFunctions() { return Clear(); }
139  void MoveEventsFunction(std::size_t oldIndex, std::size_t newIndex) {
140  return Move(oldIndex, newIndex);
141  };
142  std::size_t GetEventsFunctionPosition(const gd::EventsFunction& eventsFunction) {
143  return GetPosition(eventsFunction);
144  };
145 
149  const std::vector<std::unique_ptr<gd::EventsFunction>>& GetInternalVector()
150  const {
151  return elements;
152  };
153 
157  std::vector<std::unique_ptr<gd::EventsFunction>>& GetInternalVector() {
158  return elements;
159  };
161 
165 
171  gd::EventsFunction &InsertNewEventsFunctionInFolder(
172  const gd::String &name,
173  gd::FunctionFolderOrFunction &functionFolderOrFunction,
174  std::size_t position);
175 
181  std::vector<const FunctionFolderOrFunction *>
182  GetAllFunctionFolderOrFunction() const;
183 
184  gd::FunctionFolderOrFunction &GetRootFolder() { return *rootFolder; }
185 
186  void AddMissingFunctionsInRootFolder();
187 
191  void SerializeFoldersTo(SerializerElement &element) const;
192 
196  void UnserializeFoldersFrom(const SerializerElement &element);
198 
202 
206  return SerializeElementsTo("eventsFunction", element);
207  };
208 
213  const SerializerElement& element) {
214  return UnserializeElementsFrom("eventsFunction", project, element);
215  };
217  protected:
222  void Init(const gd::EventsFunctionsContainer& other) {
223  // The properties folders are not copied.
224  // It's not an issue because the UI uses the serialization for duplication.
225  rootFolder = gd::make_unique<gd::FunctionFolderOrFunction>("__ROOT");
227  };
228 
229 private:
230  FunctionOwner owner;
231  std::unique_ptr<gd::FunctionFolderOrFunction> rootFolder;
232 };
233 
234 } // namespace gd
Base class used to represents a behavior that can be applied to an object. It stores the content (i....
Definition: Behavior.h:24
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
FunctionOwner GetOwner() const
Get the source of the function container.
Definition: EventsFunctionsContainer.h:60
std::size_t GetEventsFunctionsCount() const
Return the number of functions.
Definition: EventsFunctionsContainer.h:117
gd::EventsFunction & GetEventsFunction(const gd::String &name)
Get the function with the specified name.
Definition: EventsFunctionsContainer.h:80
void SerializeEventsFunctionsTo(SerializerElement &element) const
Serialize events functions.
Definition: EventsFunctionsContainer.h:205
std::vector< std::unique_ptr< gd::EventsFunction > > & GetInternalVector()
Provide a raw access to the vector containing the functions.
Definition: EventsFunctionsContainer.h:157
gd::EventsFunction & GetEventsFunction(std::size_t index)
Get the function at the specified index in the list.
Definition: EventsFunctionsContainer.h:100
const std::vector< std::unique_ptr< gd::EventsFunction > > & GetInternalVector() const
Provide a raw access to the vector containing the functions.
Definition: EventsFunctionsContainer.h:149
const gd::EventsFunction & GetEventsFunction(std::size_t index) const
Get the function at the specified index in the list.
Definition: EventsFunctionsContainer.h:110
bool HasEventsFunctionNamed(const gd::String &name) const
Check if the function with the specified name exists.
Definition: EventsFunctionsContainer.h:70
void UnserializeEventsFunctionsFrom(gd::Project &project, const SerializerElement &element)
Unserialize the events functions.
Definition: EventsFunctionsContainer.h:212
const gd::EventsFunction & GetEventsFunction(const gd::String &name) const
Get the function with the specified name.
Definition: EventsFunctionsContainer.h:90
void Init(const gd::EventsFunctionsContainer &other)
Definition: EventsFunctionsContainer.h:222
Class representing a folder structure in order to organize functions in folders (to be used with a Ev...
Definition: FunctionFolderOrFunction.h:30
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
A class template that store a list of elements that can be accessed by their names and serialized.
Definition: SerializableWithNameList.h:33
void Init(const SerializableWithNameList< T > &other)
Definition: SerializableWithNameList.inl:190
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