GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
InitialInstancesContainer.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-2016 Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 
7 #pragma once
8 
9 #include <list>
10 #include "GDCore/Project/InitialInstance.h"
11 #include "GDCore/Project/MemoryTrackedRegistry.h"
12 #include "GDCore/String.h"
13 namespace gd {
14 class InitialInstanceFunctor;
15 }
16 namespace gd {
17 class Project;
18 }
19 namespace gd {
20 class SerializerElement;
21 }
22 
23 namespace gd {
24 
39 class GD_CORE_API InitialInstancesContainer {
40  public:
43  : initialInstances(other.initialInstances) {};
44  InitialInstancesContainer& operator=(const InitialInstancesContainer& other) {
45  if (this != &other) {
46  initialInstances = other.initialInstances;
47  }
48  return *this;
49  }
50  virtual ~InitialInstancesContainer();
51 
63  return new InitialInstancesContainer(*this);
64  };
65 
81  void Create(const InitialInstancesContainer &source);
82 
87 
91  std::size_t GetInstancesCount() const;
92 
97  void IterateOverInstances(InitialInstanceFunctor &func);
98 
103  void IterateOverInstances(
104  const std::function< bool(gd::InitialInstance &) >& func);
105 
114  void IterateOverInstancesWithZOrdering(InitialInstanceFunctor &func,
115  const gd::String &layer);
116 
121  InitialInstance &InsertInitialInstance(const InitialInstance &instance);
122 
127  InitialInstance &InsertNewInitialInstance();
128 
132  void RemoveInstance(const gd::InitialInstance &instance);
133 
137  void RemoveAllInstancesOnLayer(const gd::String &layerName);
138 
142  void MoveInstancesToLayer(const gd::String &fromLayer,
143  const gd::String &toLayer);
144 
148  void RemoveInitialInstancesOfObject(const gd::String &objectName);
149 
153  void RenameInstancesOfObject(const gd::String &oldName,
154  const gd::String &newName);
155 
159  std::size_t GetLayerInstancesCount(const gd::String &layerName) const;
160 
165  bool SomeInstancesAreOnLayer(const gd::String &layerName) const;
166 
170  bool HasInstancesOfObject(const gd::String &objectName) const;
171 
175  bool
176  IsInstancesCountOfObjectGreaterThan(const gd::String &objectName,
177  const std::size_t minInstanceCount) const;
178 
182  void Clear();
183 
185 
190 
194  virtual void SerializeTo(SerializerElement &element) const;
195 
199  virtual void UnserializeFrom(gd::Project &project,
200  const SerializerElement &element);
202 
203 private:
204  void RemoveInstanceIf(
205  std::function<bool(const gd::InitialInstance &)> predicate);
206 
207  std::list<gd::InitialInstance> initialInstances;
208 
209  static gd::InitialInstance badPosition;
210  gd::MemoryTracked _memoryTracked{this, "InitialInstancesContainer"};
211 };
212 
220 class GD_CORE_API InitialInstanceFunctor {
221  public:
223  virtual ~InitialInstanceFunctor();
224 
225  virtual void operator()(InitialInstance &instance) = 0;
226 };
227 
235  public:
237  : highestZOrder(0),
238  lowestZOrder(0),
239  instancesCount(0),
240  firstCall(true),
241  layerRestricted(false){};
242  virtual ~HighestZOrderFinder(){};
243 
244  virtual void operator()(gd::InitialInstance &instance);
245 
249  void RestrictSearchToLayer(const gd::String &layerName_) {
250  layerName = layerName_;
251  layerRestricted = true;
252  };
253 
258  int GetHighestZOrder() const { return highestZOrder; }
259 
264  int GetLowestZOrder() const { return lowestZOrder; }
265 
270  size_t GetInstancesCount() const { return instancesCount; }
271 
272  void Reset() {
273  highestZOrder = 0;
274  lowestZOrder = 0;
275  instancesCount = 0;
276  firstCall = true;
277  layerRestricted = false;
278  layerName.clear();
279  }
280 
281  private:
282  int highestZOrder;
283  int lowestZOrder;
284  size_t instancesCount;
285  bool firstCall;
286 
287  bool layerRestricted;
289  gd::String layerName;
290 };
291 
292 } // namespace gd
Tool class picking returning the highest Z order of instances on a layer.
Definition: InitialInstancesContainer.h:234
void RestrictSearchToLayer(const gd::String &layerName_)
Restrict to instances on the specified layer.
Definition: InitialInstancesContainer.h:249
int GetHighestZOrder() const
After calling the instances container iterate method with this functor, this method will return the h...
Definition: InitialInstancesContainer.h:258
int GetLowestZOrder() const
After calling the instances container iterate method with this functor, this method will return the l...
Definition: InitialInstancesContainer.h:264
size_t GetInstancesCount() const
After calling the instances container iterate method with this functor, this method will return the n...
Definition: InitialInstancesContainer.h:270
Tool class to be used with gd::InitialInstancesContainer::IterateOverInstances.
Definition: InitialInstancesContainer.h:220
Represents an instance of an object to be created on a layout start up.
Definition: InitialInstance.h:29
Defines a container of gd::InitialInstances.
Definition: InitialInstancesContainer.h:39
InitialInstancesContainer * Clone() const
Return a pointer to a copy of the container. A such method is needed as the IDE may want to store cop...
Definition: InitialInstancesContainer.h:62
A non-copyable, non-movable member object that registers/unregisters its owner with MemoryTrackedRegi...
Definition: MemoryTrackedRegistry.h:238
Base class representing a project (game), including all resources, scenes, objects,...
Definition: Project.h:51
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