GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ValueTypeMetadata.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 <map>
10 #include <memory>
11 
12 #include "GDCore/String.h"
13 namespace gd {
14 class SerializerElement;
15 } // namespace gd
16 
17 namespace gd {
18 
26 class GD_CORE_API ValueTypeMetadata {
27  public:
29  virtual ~ValueTypeMetadata(){};
30 
34  const gd::String &GetName() const { return name; }
35 
40  name = name_;
41  return *this;
42  }
43 
49  const gd::String &GetExtraInfo() const { return supplementaryInformation; }
50 
56  ValueTypeMetadata &SetExtraInfo(const gd::String &supplementaryInformation_) {
57  supplementaryInformation = supplementaryInformation_;
58  return *this;
59  }
60 
64  bool IsOptional() const { return optional; }
65 
69  ValueTypeMetadata &SetOptional(bool optional_ = true) {
70  optional = optional_;
71  return *this;
72  }
73 
77  const gd::String &GetDefaultValue() const { return defaultValue; }
78 
82  ValueTypeMetadata &SetDefaultValue(const gd::String &defaultValue_) {
83  defaultValue = defaultValue_;
84  return *this;
85  }
86 
90  bool IsDefined() const {
91  return !name.empty();
92  }
93 
98  bool IsObject() const {
100  }
101 
105  bool IsBehavior() const {
107  }
108 
113  bool IsNumber() const {
114  return gd::ValueTypeMetadata::IsTypeValue("number", name);
115  }
116 
120  bool IsString() const {
121  return gd::ValueTypeMetadata::IsTypeValue("string", name);
122  }
123 
127  bool IsBoolean() const {
128  return gd::ValueTypeMetadata::IsTypeValue("boolean", name);
129  }
130 
137  bool IsVariable() const {
139  }
140 
147  static bool IsVariable(const gd::String &type) {
148  return gd::ValueTypeMetadata::GetPrimitiveValueType(type) == "variable";
149  }
150 
155  bool IsVariableOnly() const {
156  return
157  // Any variable.
158  name == "variable" ||
159  // Old, "pre-scoped" variables:
160  name == "objectvar" || name == "globalvar" ||
161  name == "scenevar";
162  }
163 
171  }
172 
178  static bool IsTypeLegacyPreScopedVariable(const gd::String &type) {
179  return type == "scenevar" || type == "globalvar" || type == "objectvar";
180  }
181 
186  static bool IsTypeObject(const gd::String &parameterType) {
187  return parameterType == "object" || parameterType == "objectPtr" ||
188  parameterType == "objectList" ||
189  parameterType == "objectListOrEmptyIfJustDeclared" ||
190  parameterType == "objectListOrEmptyWithoutPicking";
191  }
192 
196  static bool IsTypeBehavior(const gd::String &parameterType) {
197  return parameterType == "behavior";
198  }
199 
203  bool IsResource() const {
204  return gd::ValueTypeMetadata::IsTypeValue("resource", name);
205  }
206 
215  static bool IsTypeExpression(const gd::String &type,
216  const gd::String &parameterType) {
217  if (type == "number") {
218  return parameterType == "number" || parameterType == "expression" ||
219  parameterType == "camera" || parameterType == "forceMultiplier" ||
220  parameterType == "numberWithChoices";
221  } else if (type == "string") {
222  // "key" and "mouse" are not mapped her, see GetPrimitiveValueType.
223  return parameterType == "string" || parameterType == "layer" ||
224  parameterType == "color" || parameterType == "file" ||
225  parameterType == "stringWithSelector" ||
226  parameterType == "sceneName" ||
227  parameterType == "layerEffectName" ||
228  parameterType == "layerEffectParameterName" ||
229  parameterType == "objectEffectName" ||
230  parameterType == "objectEffectParameterName" ||
231  parameterType == "objectPointName" ||
232  parameterType == "objectAnimationName" ||
233  parameterType == "objectSkinName" ||
234  parameterType == "functionParameterName" ||
235  parameterType == "externalLayoutName" ||
236  parameterType == "leaderboardId" ||
237  parameterType == "keyboardKey" ||
238  parameterType == "mouseButton" ||
239  parameterType == "identifier";
240  } else if (type == "boolean") {
241  return parameterType == "yesorno" || parameterType == "trueorfalse";
242  } else if (type == "variable") {
243  return
244  // Any variable.
245  parameterType == "variable" ||
246  parameterType == "variableOrProperty" ||
247  parameterType == "variableOrPropertyOrParameter" ||
248  // Old, "pre-scoped" variables:
249  parameterType == "objectvar" || parameterType == "globalvar" ||
250  parameterType == "scenevar";
251  } else if (type == "resource") {
252  return parameterType == "fontResource" ||
253  parameterType == "audioResource" ||
254  parameterType == "videoResource" ||
255  parameterType == "bitmapFontResource" ||
256  parameterType == "imageResource" ||
257  parameterType == "jsonResource" ||
258  parameterType == "tilemapResource" ||
259  parameterType == "tilesetResource" ||
260  parameterType == "model3DResource" ||
261  parameterType == "atlasResource" ||
262  parameterType == "spineResource" ||
263  // Deprecated, old parameter types:
264  parameterType == "soundfile" ||
265  parameterType == "musicfile" ||
266  parameterType == "password";
267  }
268  return false;
269  }
270 
275  static bool IsTypeValue(const gd::String &type,
276  const gd::String &parameterType) {
277  if (gd::ValueTypeMetadata::IsTypeExpression(type, parameterType)) {
278  return true;
279  }
280  // These 2 parameter types are not strings from the outside of a function as
281  // the generator add quote around a text, but from the events inside of the
282  // function the parameter is a string.
283  //
284  // See EventsCodeGenerator::GenerateParameterCodes
285  if (type == "string") {
286  return parameterType == "key" || parameterType == "mouse";
287  }
288  return false;
289  }
290 
298  static const gd::String &GetExpressionPrimitiveValueType(const gd::String &parameterType);
299 
307  static const gd::String &GetPrimitiveValueType(const gd::String &parameterType);
308  static const gd::String numberType;
309  static const gd::String stringType;
310  static const gd::String variableType;
311  static const gd::String booleanType;
312 
317  static const gd::String &ConvertPropertyTypeToValueType(const gd::String &propertyType);
318 
323  static const gd::String &GetResourceType(const gd::String &parameterType);
324 
328 
331  void SerializeTo(gd::SerializerElement &element) const;
332 
336  void UnserializeFrom(const gd::SerializerElement &element);
338 
339  private:
340  gd::String name;
341  gd::String supplementaryInformation;
342  bool optional;
343  gd::String defaultValue;
345 
346  static const gd::String numberValueType;
347  static const gd::String booleanValueType;
348  static const gd::String stringValueType;
349  static const gd::String colorValueType;
350  static const gd::String choiceValueType;
351  static const gd::String numberWithChoicesValueType;
352  static const gd::String behaviorValueType;
353  static const gd::String leaderboardIdValueType;
354  static const gd::String objectAnimationNameValueType;
355  static const gd::String objectSkinNameValueType;
356  static const gd::String keyboardKeyValueType;
357  static const gd::String layerValueType;
358  static const gd::String layerValueType2;
359 };
360 
361 } // namespace gd
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
Define a type for parameters of a function (action, condition or expression) or the returned value of...
Definition: ValueTypeMetadata.h:26
ValueTypeMetadata & SetName(const gd::String &name_)
Set the string representation of the type.
Definition: ValueTypeMetadata.h:39
static bool IsTypeBehavior(const gd::String &parameterType)
Return true if the type is "behavior".
Definition: ValueTypeMetadata.h:196
static bool IsTypeExpression(const gd::String &type, const gd::String &parameterType)
Return true if the type is an expression of the given type from the caller point of view.
Definition: ValueTypeMetadata.h:215
const gd::String & GetDefaultValue() const
Get the default value for the parameter.
Definition: ValueTypeMetadata.h:77
bool IsString() const
Return true if the type is a string.
Definition: ValueTypeMetadata.h:120
const gd::String & GetExtraInfo() const
Return an optional additional information, used for some parameters with special type (for example,...
Definition: ValueTypeMetadata.h:49
ValueTypeMetadata & SetDefaultValue(const gd::String &defaultValue_)
Set the default value, if the parameter is optional.
Definition: ValueTypeMetadata.h:82
bool IsResource() const
Return true if the type is a resource.
Definition: ValueTypeMetadata.h:203
const gd::String & GetName() const
Return the string representation of the type.
Definition: ValueTypeMetadata.h:34
static bool IsTypeLegacyPreScopedVariable(const gd::String &type)
Return true if the type is a variable but from a specific scope (scene, project or object)....
Definition: ValueTypeMetadata.h:178
bool IsDefined() const
Return true if the type is defined.
Definition: ValueTypeMetadata.h:90
ValueTypeMetadata & SetOptional(bool optional_=true)
Set if the parameter is optional.
Definition: ValueTypeMetadata.h:69
bool IsVariableOnly() const
Return true if the type of the parameter is a variable and not a property or a parameter.
Definition: ValueTypeMetadata.h:155
bool IsLegacyPreScopedVariable() const
Return true if the type is a variable but from a specific scope (scene, project or object)....
Definition: ValueTypeMetadata.h:169
bool IsOptional() const
Return true if the parameter is optional.
Definition: ValueTypeMetadata.h:64
static bool IsVariable(const gd::String &type)
Return true if the type of the parameter is a variable.
Definition: ValueTypeMetadata.h:147
bool IsBoolean() const
Return true if the type is a boolean.
Definition: ValueTypeMetadata.h:127
bool IsObject() const
Return true if the type is representing one object (or more, i.e: an object group).
Definition: ValueTypeMetadata.h:98
bool IsBehavior() const
Return true if the type is "behavior".
Definition: ValueTypeMetadata.h:105
static bool IsTypeObject(const gd::String &parameterType)
Return true if the type is representing one object (or more, i.e: an object group).
Definition: ValueTypeMetadata.h:186
bool IsVariable() const
Return true if the type of the parameter is a variable.
Definition: ValueTypeMetadata.h:137
static const gd::String & GetPrimitiveValueType(const gd::String &parameterType)
Return the primitive type from the parameter type. Declinations of "number" and "string" types (like ...
Definition: ValueTypeMetadata.cpp:56
ValueTypeMetadata & SetExtraInfo(const gd::String &supplementaryInformation_)
Set an optional additional information, used for some parameters with special type (for example,...
Definition: ValueTypeMetadata.h:56
bool IsNumber() const
Return true if the type is an expression of the given type.
Definition: ValueTypeMetadata.h:113
static bool IsTypeValue(const gd::String &type, const gd::String &parameterType)
Return true if the type is a value of the given primitive type from the function events point of view...
Definition: ValueTypeMetadata.h:275
Definition: CommonTools.h:24