GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ProjectScopedContainers.h
1 #pragma once
2 #include <set>
3 
4 #include "GDCore/Extensions/Metadata/ParameterMetadataTools.h"
5 #include "ObjectsContainersList.h"
6 #include "PropertiesContainersList.h"
7 #include "VariablesContainersList.h"
8 #include "VariablesContainer.h"
9 
10 namespace gd {
11 class Project;
12 class ObjectsContainer;
13 class NamedPropertyDescriptor;
14 class ParameterMetadataContainer;
15 class BaseEvent;
16 class EventsFunctionsExtension;
17 class EventsFunction;
18 class EventsBasedBehavior;
19 class EventsBasedObject;
20 } // namespace gd
21 
22 namespace gd {
23 
35  public:
37  const gd::ObjectsContainersList &objectsContainersList_,
38  const gd::VariablesContainersList &variablesContainersList_,
39  const gd::VariablesContainer *legacyGlobalVariables_,
40  const gd::VariablesContainer *legacySceneVariables_,
41  const gd::PropertiesContainersList &propertiesContainersList_)
42  : objectsContainersList(objectsContainersList_),
43  variablesContainersList(variablesContainersList_),
44  legacyGlobalVariables(legacyGlobalVariables_),
45  legacySceneVariables(legacySceneVariables_),
46  propertiesContainersList(propertiesContainersList_){};
47  virtual ~ProjectScopedContainers(){};
48 
50  MakeNewProjectScopedContainersForProjectAndLayout(const gd::Project &project,
51  const gd::Layout &layout);
52 
54  MakeNewProjectScopedContainersForProject(const gd::Project &project);
55 
60  const gd::ObjectsContainer &globalObjectsContainers,
61  const gd::ObjectsContainer &objectsContainers);
62 
64  MakeNewProjectScopedContainersForEventsFunctionsExtension(
65  const gd::Project &project,
66  const gd::EventsFunctionsExtension &eventsFunctionsExtension);
67 
69  MakeNewProjectScopedContainersForFreeEventsFunction(
70  const gd::Project &project,
71  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
72  const gd::EventsFunction &eventsFunction,
73  gd::ObjectsContainer &parameterObjectsContainer,
74  gd::VariablesContainer &parameterVariablesContainer);
75 
77  MakeNewProjectScopedContainersForBehaviorEventsFunction(
78  const gd::Project &project,
79  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
80  const gd::EventsBasedBehavior &eventsBasedBehavior,
81  const gd::EventsFunction &eventsFunction,
82  gd::ObjectsContainer &parameterObjectsContainer,
83  gd::VariablesContainer &parameterVariablesContainer,
84  gd::VariablesContainer &propertyVariablesContainer);
85 
87  MakeNewProjectScopedContainersForObjectEventsFunction(
88  const gd::Project &project,
89  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
90  const gd::EventsBasedObject &eventsBasedObject,
91  const gd::EventsFunction &eventsFunction,
92  gd::ObjectsContainer &parameterObjectsContainer,
93  gd::VariablesContainer &parameterVariablesContainer,
94  gd::VariablesContainer &propertyVariablesContainer);
95 
97  MakeNewProjectScopedContainersForEventsBasedObject(
98  const gd::Project &project,
99  const gd::EventsFunctionsExtension &eventsFunctionsExtension,
100  const gd::EventsBasedObject &eventsBasedObject,
101  gd::ObjectsContainer &outputObjectsContainer);
102 
104  MakeNewProjectScopedContainersWithLocalVariables(
105  const ProjectScopedContainers &projectScopedContainers,
106  const gd::BaseEvent &event);
107 
108  ProjectScopedContainers &AddPropertiesContainer(
109  const gd::PropertiesContainer &container) {
110  propertiesContainersList.Add(container);
111 
112  return *this;
113  }
114 
115  ProjectScopedContainers &AddParameters(
116  const ParameterMetadataContainer &parameters) {
117  parametersVectorsList.push_back(&parameters);
118 
119  return *this;
120  }
121 
122  template <class ReturnType>
123  ReturnType MatchIdentifierWithName(
124  const gd::String &name,
125  std::function<ReturnType()> objectCallback,
126  std::function<ReturnType()> variableCallback,
127  std::function<ReturnType()> propertyCallback,
128  std::function<ReturnType()> parameterCallback,
129  std::function<ReturnType()> notFoundCallback) const {
130  if (objectsContainersList.HasObjectOrGroupNamed(name))
131  return objectCallback();
132  else if (variablesContainersList.Has(name)) {
133  const auto &variablesContainer =
135  const auto sourceType = variablesContainer.GetSourceType();
136  if (sourceType == gd::VariablesContainer::SourceType::Properties) {
137  return propertyCallback();
138  } else if (sourceType == gd::VariablesContainer::SourceType::Parameters) {
139  return parameterCallback();
140  }
141  return variableCallback();
142  } else if (ParameterMetadataTools::Has(parametersVectorsList, name))
143  return parameterCallback();
144  else if (propertiesContainersList.Has(name))
145  return propertyCallback();
146 
147  return notFoundCallback();
148  };
149 
150  void ForEachIdentifierMatchingSearch(
151  const gd::String &search,
152  std::function<void(const gd::String &name,
153  const ObjectConfiguration *objectConfiguration)>
154  objectCallback,
155  std::function<void(const gd::String &name, const gd::Variable &variable)>
156  variableCallback,
157  std::function<void(const gd::NamedPropertyDescriptor &property)>
158  propertyCallback,
159  std::function<void(const gd::ParameterMetadata &parameter)>
160  parameterCallback) const {
161  std::set<gd::String> namesAlreadySeen;
162 
163  objectsContainersList.ForEachNameMatchingSearch(
164  search,
165  [&](const gd::String &name,
166  const ObjectConfiguration *objectConfiguration) {
167  if (namesAlreadySeen.count(name) == 0) {
168  namesAlreadySeen.insert(name);
169  objectCallback(name, objectConfiguration);
170  }
171  });
172  variablesContainersList.ForEachVariableMatchingSearch(
173  search, [&](const gd::String &name, const gd::Variable &variable) {
174  if (namesAlreadySeen.count(name) == 0) {
175  namesAlreadySeen.insert(name);
176  variableCallback(name, variable);
177  }
178  });
179  gd::ParameterMetadataTools::ForEachParameterMatchingSearch(
180  parametersVectorsList,
181  search,
182  [&](const gd::ParameterMetadata &parameter) {
183  if (namesAlreadySeen.count(parameter.GetName()) == 0) {
184  namesAlreadySeen.insert(parameter.GetName());
185  parameterCallback(parameter);
186  }
187  });
188  propertiesContainersList.ForEachPropertyMatchingSearch(
189  search, [&](const gd::NamedPropertyDescriptor &property) {
190  if (namesAlreadySeen.count(property.GetName()) == 0) {
191  namesAlreadySeen.insert(property.GetName());
192  propertyCallback(property);
193  }
194  });
195  };
196 
197  const gd::ObjectsContainersList &GetObjectsContainersList() const {
198  return objectsContainersList;
199  };
200 
201  const gd::VariablesContainersList &GetVariablesContainersList() const {
202  return variablesContainersList;
203  };
204 
210  return variablesContainersList;
211  };
212 
219  return legacyGlobalVariables;
220  };
221 
228  return legacySceneVariables;
229  };
230 
231  const gd::PropertiesContainersList &GetPropertiesContainersList() const {
232  return propertiesContainersList;
233  };
234 
235  const std::vector<const ParameterMetadataContainer *> &GetParametersVectorsList() const {
236  return parametersVectorsList;
237  };
238 
242  : legacyGlobalVariables(nullptr), legacySceneVariables(nullptr){};
243 
244 private:
245  gd::ObjectsContainersList objectsContainersList;
246  gd::VariablesContainersList variablesContainersList;
247  const gd::VariablesContainer *legacyGlobalVariables;
248  const gd::VariablesContainer *legacySceneVariables;
249  gd::PropertiesContainersList propertiesContainersList;
250  std::vector<const ParameterMetadataContainer *> parametersVectorsList;
251 };
252 
253 } // namespace gd
Base class defining an event.
Definition: Event.h:43
Represents a behavior that is implemented with events.
Definition: EventsBasedBehavior.h:31
Represents an object that is implemented with events.
Definition: EventsBasedObject.h:30
Events that can be generated as a stand-alone function, and used as a condition, action or expression...
Definition: EventsFunction.h:38
Hold a list of Events Functions (gd::EventsFunction) and Events Based Behaviors.
Definition: EventsFunctionsExtension.h:39
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:40
Used to describe a property shown in a property grid.
Definition: NamedPropertyDescriptor.h:21
const gd::String & GetName() const
Get the name of the property.
Definition: NamedPropertyDescriptor.h:47
Base class used to represent an object configuration. For example, this can be the animations in a sp...
Definition: ObjectConfiguration.h:38
Used as a base class for classes that will own objects (see gd::Object).
Definition: ObjectsContainer.h:37
A list of objects containers, useful for accessing objects in a scoped way, along with methods to acc...
Definition: ObjectsContainersList.h:29
void ForEachNameMatchingSearch(const gd::String &search, std::function< void(const gd::String &name, const gd::ObjectConfiguration *objectConfiguration)> fn) const
Call the callback for each object or group name matching the search passed in parameter.
Definition: ObjectsContainersList.cpp:332
bool HasObjectOrGroupNamed(const gd::String &name) const
Check if the specified object or group exists.
Definition: ObjectsContainersList.cpp:56
Used as a base class for classes that will own events-backed functions.
Definition: ParameterMetadataContainer.h:26
Describe a parameter of an instruction (action, condition) or of an expression: type,...
Definition: ParameterMetadata.h:27
const gd::String & GetName() const
Return the name of the parameter.
Definition: ParameterMetadata.h:79
Base class representing a project (game), including all resources, scenes, objects,...
Definition: Project.h:50
Holds references to variables, objects, properties and other containers.
Definition: ProjectScopedContainers.h:34
static ProjectScopedContainers MakeNewProjectScopedContainersFor(const gd::ObjectsContainer &globalObjectsContainers, const gd::ObjectsContainer &objectsContainers)
Definition: ProjectScopedContainers.cpp:43
gd::VariablesContainersList & GetVariablesContainersList()
Allow modification of the variables containers list. This is used by code generation which does push ...
Definition: ProjectScopedContainers.h:209
const gd::VariablesContainer * GetLegacyGlobalVariables() const
Return the global variables of the current scene or the current extension. It allows legacy "globalva...
Definition: ProjectScopedContainers.h:218
ProjectScopedContainers()
Definition: ProjectScopedContainers.h:241
const gd::VariablesContainer * GetLegacySceneVariables() const
Return the scene variables of the current scene or the current extension. It allows legacy "scenevar"...
Definition: ProjectScopedContainers.h:227
A container of properties, used for custom behaviors, custom objects, extensions.....
Definition: PropertiesContainer.h:17
A list of property containers, useful for accessing properties in a scoped way.
Definition: PropertiesContainersList.h:26
bool Has(const gd::String &name) const
Return true if the specified property is in one of the containers.
Definition: PropertiesContainersList.cpp:27
void Add(const gd::PropertiesContainer &propertiesContainer)
Add a new container of properties in the list. Add containers in order from the most global one to th...
Definition: PropertiesContainersList.h:38
void ForEachPropertyMatchingSearch(const gd::String &search, std::function< void(const gd::NamedPropertyDescriptor &property)> fn) const
Call the callback for each property having a name matching the specified search.
Definition: PropertiesContainersList.cpp:58
String represents an UTF8 encoded string.
Definition: String.h:33
Defines a variable which can be used by an object, a layout or a project.
Definition: Variable.h:29
Class defining a container for gd::Variable.
Definition: VariablesContainer.h:28
A list of variables containers, useful for accessing variables in a scoped way.
Definition: VariablesContainersList.h:29
const VariablesContainer & GetVariablesContainerFromVariableOrPropertyOrParameterName(const gd::String &variableName) const
Definition: VariablesContainersList.cpp:150
bool Has(const gd::String &name) const
Return true if the specified variable is in one of the containers.
Definition: VariablesContainersList.cpp:131
void ForEachVariableMatchingSearch(const gd::String &search, std::function< void(const gd::String &name, const gd::Variable &variable)> fn) const
Call the callback for each variable having a name matching the specified search.
Definition: VariablesContainersList.cpp:217
Definition: CommonTools.h:24