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 
9 namespace gd {
10 class Project;
11 class ObjectsContainer;
12 class ObjectsContainersList;
13 class VariablesContainersList;
14 class PropertiesContainersList;
15 class NamedPropertyDescriptor;
16 } // namespace gd
17 
18 namespace gd {
19 
31  public:
33  const gd::ObjectsContainersList &objectsContainersList_,
34  const gd::VariablesContainersList &variablesContainersList_,
35  const gd::PropertiesContainersList &propertiesContainersList_)
36  : objectsContainersList(objectsContainersList_),
37  variablesContainersList(variablesContainersList_),
38  propertiesContainersList(propertiesContainersList_){};
39  virtual ~ProjectScopedContainers(){};
40 
42  MakeNewProjectScopedContainersForProjectAndLayout(const gd::Project &project,
43  const gd::Layout &layout) {
44  ProjectScopedContainers projectScopedContainers(
45  ObjectsContainersList::MakeNewObjectsContainersListForProjectAndLayout(
46  project, layout),
48  MakeNewVariablesContainersListForProjectAndLayout(project, layout),
49  PropertiesContainersList::MakeNewEmptyPropertiesContainersList());
50 
51  return projectScopedContainers;
52  }
53 
54  static ProjectScopedContainers MakeNewProjectScopedContainersFor(
55  const gd::ObjectsContainer &globalObjectsContainers,
56  const gd::ObjectsContainer &objectsContainers) {
57  ProjectScopedContainers projectScopedContainers(
58  ObjectsContainersList::MakeNewObjectsContainersListForContainers(
59  globalObjectsContainers, objectsContainers),
60  VariablesContainersList::MakeNewEmptyVariablesContainersList(),
61  PropertiesContainersList::MakeNewEmptyPropertiesContainersList());
62 
63  return projectScopedContainers;
64  }
65 
66  ProjectScopedContainers &AddPropertiesContainer(
67  const gd::PropertiesContainer &container) {
68  propertiesContainersList.Add(container);
69 
70  return *this;
71  }
72 
73  ProjectScopedContainers &AddParameters(
74  const std::vector<gd::ParameterMetadata> &parameters) {
75  parametersVectorsList.push_back(&parameters);
76 
77  return *this;
78  }
79 
80  template <class ReturnType>
81  ReturnType MatchIdentifierWithName(
82  const gd::String &name,
83  std::function<ReturnType()> objectCallback,
84  std::function<ReturnType()> variableCallback,
85  std::function<ReturnType()> propertyCallback,
86  std::function<ReturnType()> parameterCallback,
87  std::function<ReturnType()> notFoundCallback) const {
88  if (objectsContainersList.HasObjectOrGroupNamed(name))
89  return objectCallback();
90  else if (variablesContainersList.Has(name))
91  return variableCallback();
92  else if (ParameterMetadataTools::Has(parametersVectorsList, name))
93  return parameterCallback();
94  else if (propertiesContainersList.Has(name))
95  return propertyCallback();
96 
97  return notFoundCallback();
98  };
99 
100  void ForEachIdentifierMatchingSearch(
101  const gd::String &search,
102  std::function<void(const gd::String &name,
103  const ObjectConfiguration *objectConfiguration)>
104  objectCallback,
105  std::function<void(const gd::String &name, const gd::Variable &variable)>
106  variableCallback,
107  std::function<void(const gd::NamedPropertyDescriptor &property)>
108  propertyCallback,
109  std::function<void(const gd::ParameterMetadata &parameter)>
110  parameterCallback) const {
111  std::set<gd::String> namesAlreadySeen;
112 
113  objectsContainersList.ForEachNameMatchingSearch(
114  search,
115  [&](const gd::String &name,
116  const ObjectConfiguration *objectConfiguration) {
117  if (namesAlreadySeen.count(name) == 0) {
118  namesAlreadySeen.insert(name);
119  objectCallback(name, objectConfiguration);
120  }
121  });
122  variablesContainersList.ForEachVariableMatchingSearch(
123  search, [&](const gd::String &name, const gd::Variable &variable) {
124  if (namesAlreadySeen.count(name) == 0) {
125  namesAlreadySeen.insert(name);
126  variableCallback(name, variable);
127  }
128  });
129  gd::ParameterMetadataTools::ForEachParameterMatchingSearch(
130  parametersVectorsList,
131  search,
132  [&](const gd::ParameterMetadata &parameter) {
133  if (namesAlreadySeen.count(parameter.GetName()) == 0) {
134  namesAlreadySeen.insert(parameter.GetName());
135  parameterCallback(parameter);
136  }
137  });
138  propertiesContainersList.ForEachPropertyMatchingSearch(
139  search, [&](const gd::NamedPropertyDescriptor &property) {
140  if (namesAlreadySeen.count(property.GetName()) == 0) {
141  namesAlreadySeen.insert(property.GetName());
142  propertyCallback(property);
143  }
144  });
145  };
146 
147  const gd::ObjectsContainersList &GetObjectsContainersList() const {
148  return objectsContainersList;
149  };
150 
151  const gd::VariablesContainersList &GetVariablesContainersList() const {
152  return variablesContainersList;
153  };
154 
155  const gd::PropertiesContainersList &GetPropertiesContainersList() const {
156  return propertiesContainersList;
157  };
158 
159  const std::vector<const std::vector<gd::ParameterMetadata> *> &GetParametersVectorsList() const {
160  return parametersVectorsList;
161  };
162 
166 
167  private:
168  gd::ObjectsContainersList objectsContainersList;
169  gd::VariablesContainersList variablesContainersList;
170  gd::PropertiesContainersList propertiesContainersList;
171  std::vector<const std::vector<gd::ParameterMetadata> *> parametersVectorsList;
172 };
173 
174 } // namespace gd
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:39
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:36
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:301
bool HasObjectOrGroupNamed(const gd::String &name) const
Check if the specified object or group exists.
Definition: ObjectsContainersList.cpp:34
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:73
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:30
ProjectScopedContainers()
Definition: ProjectScopedContainers.h:165
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:31
Defines a variable which can be used by an object, a layout or a project.
Definition: Variable.h:29
A list of variables containers, useful for accessing variables in a scoped way.
Definition: VariablesContainersList.h:25
bool Has(const gd::String &name) const
Return true if the specified variable is in one of the containers.
Definition: VariablesContainersList.cpp:28
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:55
Definition: CommonTools.h:24