GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
InstructionMetadata.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 "AbstractFunctionMetadata.h"
10 
11 #include <algorithm>
12 #include <functional>
13 #include <map>
14 #include <memory>
15 
16 #include "GDCore/Events/Instruction.h"
17 #include "GDCore/Project/ParameterMetadataContainer.h"
18 #include "GDCore/String.h"
19 #include "ParameterMetadata.h"
20 #include "ParameterOptions.h"
21 
22 namespace gd {
23 class Project;
24 class Layout;
25 class EventsCodeGenerator;
26 class EventsCodeGenerationContext;
27 class SerializerElement;
28 } // namespace gd
29 
30 namespace gd {
31 
40  public:
44  InstructionMetadata(const gd::String &extensionNamespace,
45  const gd::String &name,
46  const gd::String &fullname,
47  const gd::String &description,
48  const gd::String &sentence,
49  const gd::String &group,
50  const gd::String &icon,
51  const gd::String &smallIcon);
52 
58 
59  virtual ~InstructionMetadata(){};
60 
61  const gd::String &GetFullName() const { return fullname; }
62  const gd::String &GetDescription() const { return description; }
63  const gd::String &GetSentence() const { return sentence; }
64  const gd::String &GetGroup() const { return group; }
65  ParameterMetadata &GetParameter(size_t i) { return parameters.GetParameter(i); }
66  const ParameterMetadata &GetParameter(size_t i) const {
67  return parameters.GetParameter(i);
68  }
69  size_t GetParametersCount() const { return parameters.GetParametersCount(); }
70  const ParameterMetadataContainer &GetParameters() const {
71  return parameters;
72  }
73  const gd::String &GetIconFilename() const { return iconFilename; }
74  const gd::String &GetSmallIconFilename() const { return smallIconFilename; }
75  bool CanHaveSubInstructions() const { return canHaveSubInstructions; }
76 
81  const gd::String &GetHelpPath() const { return helpPath; }
82 
88  helpPath = path;
89  return *this;
90  }
91 
96  bool IsPrivate() const { return isPrivate; }
97 
103  isPrivate = true;
104  return *this;
105  }
106 
111  return relevantContext == "Any" || relevantContext == "Layout";
112  }
113 
118  return relevantContext == "Any" || relevantContext == "Function";
119  }
120 
125  return relevantContext == "Any" || relevantContext == "Function" ||
126  relevantContext == "AsynchronousFunction";
127  }
128 
133  return relevantContext == "Any" || relevantContext == "Object";
134  }
135 
140  relevantContext = "Layout";
141  return *this;
142  }
143 
148  relevantContext = "Function";
149  return *this;
150  }
151 
156  relevantContext = "AsynchronousFunction";
157  return *this;
158  }
159 
164  relevantContext = "Object";
165  return *this;
166  }
167 
173  bool IsAsync() const {
174  return !codeExtraInformation.asyncFunctionCallName.empty();
175  }
176 
181  bool IsOptionallyAsync() const {
182  return IsAsync() && !codeExtraInformation.functionCallName.empty();
183  }
184 
189  canHaveSubInstructions = true;
190  return *this;
191  }
192 
199  hidden = true;
200  return *this;
201  }
202 
207  group = str;
208  return *this;
209  }
210 
214  bool IsHidden() const { return hidden; }
215 
233  InstructionMetadata &AddParameter(
234  const gd::String &type,
235  const gd::String &label,
236  const gd::String &supplementaryInformation = "",
237  bool parameterIsOptional = false) override;
238 
250  InstructionMetadata &AddCodeOnlyParameter(
251  const gd::String &type, const gd::String &supplementaryInformation) override;
252 
259  InstructionMetadata &SetDefaultValue(const gd::String &defaultValue_) override {
260  if (parameters.GetParametersCount() > 0) {
261  parameters.GetInternalVector().back()->SetDefaultValue(defaultValue_);
262  }
263  return *this;
264  };
265 
273  const gd::String &longDescription) override {
274  if (parameters.GetParametersCount() > 0) {
275  parameters.GetInternalVector().back()->SetLongDescription(longDescription);
276  }
277  return *this;
278  }
279 
288  if (parameters.GetParametersCount() > 0) {
289  parameters.GetInternalVector().back()->SetExtraInfo(extraInfo);
290  }
291  return *this;
292  }
293 
301  InstructionMetadata &UseStandardOperatorParameters(
302  const gd::String &type, const ParameterOptions &options);
303 
311  InstructionMetadata &UseStandardRelationalOperatorParameters(
312  const gd::String &type, const ParameterOptions &options);
313 
319  isObjectInstruction = true;
320  return *this;
321  }
322 
328  isBehaviorInstruction = true;
329  return *this;
330  }
331 
335  bool IsObjectInstruction() const { return isObjectInstruction; }
336 
340  bool IsBehaviorInstruction() const { return isBehaviorInstruction; }
341 
348  InstructionMetadata &SetRequiresBaseObjectCapability(
349  const gd::String &capability);
350 
356  return requiredBaseObjectCapability;
357  }
358 
363  usageComplexity = 2;
364  return *this;
365  }
366 
372  usageComplexity = 7;
373  return *this;
374  }
375 
380  usageComplexity = 9;
381  return *this;
382  }
383 
388  int GetUsageComplexity() const { return usageComplexity; }
389 
394  public:
395  enum AccessType { Reference, MutatorAndOrAccessor, Mutators };
396  ExtraInformation() : accessType(Reference), hasCustomCodeGenerator(false){};
397  virtual ~ExtraInformation(){};
398 
399  // TODO Move these attributes to InstructionMetadata.
400  gd::String functionCallName;
401  gd::String asyncFunctionCallName;
402  gd::String type;
403  AccessType accessType;
404  gd::String optionalAssociatedInstruction;
405  std::map<gd::String, gd::String> optionalMutators;
406  bool hasCustomCodeGenerator;
407  std::function<gd::String(Instruction &instruction,
408  gd::EventsCodeGenerator &codeGenerator,
410  customCodeGenerator;
411  std::vector<gd::String> includeFiles;
412  };
415 
420  InstructionMetadata &SetFunctionName(const gd::String &functionName_) override {
421  codeExtraInformation.functionCallName = functionName_;
422  return *this;
423  }
424 
434  codeExtraInformation.asyncFunctionCallName = functionName_;
435  return *this;
436  }
437 
442  return codeExtraInformation.functionCallName;
443  }
444 
451  return codeExtraInformation.asyncFunctionCallName;
452  }
453 
461  codeExtraInformation.type = type_;
462  return *this;
463  }
464 
471  return codeExtraInformation.type;
472  }
473 
498  codeExtraInformation.optionalAssociatedInstruction = getter;
499  codeExtraInformation.accessType = codeExtraInformation.MutatorAndOrAccessor;
500  return *this;
501  }
502 
503  InstructionMetadata &SetMutators(
504  const std::map<gd::String, gd::String> &mutators) {
505  codeExtraInformation.optionalMutators = mutators;
506  codeExtraInformation.accessType = codeExtraInformation.Mutators;
507  return *this;
508  }
509 
515  InstructionMetadata &SetIncludeFile(const gd::String &includeFile) override {
516  codeExtraInformation.includeFiles.clear();
517  codeExtraInformation.includeFiles.push_back(includeFile);
518  return *this;
519  }
520 
524  InstructionMetadata &AddIncludeFile(const gd::String &includeFile) override {
525  if (std::find(codeExtraInformation.includeFiles.begin(), codeExtraInformation.includeFiles.end(), includeFile) ==
526  codeExtraInformation.includeFiles.end())
527  codeExtraInformation.includeFiles.push_back(includeFile);
528 
529  return *this;
530  }
531 
535  const std::vector<gd::String> &GetIncludeFiles() const override {
536  return codeExtraInformation.includeFiles;
537  };
538 
539  InstructionMetadata &SetCustomCodeGenerator(
540  std::function<gd::String(Instruction &instruction,
541  gd::EventsCodeGenerator &codeGenerator,
543  codeGenerator) {
544  codeExtraInformation.hasCustomCodeGenerator = true;
545  codeExtraInformation.customCodeGenerator = codeGenerator;
546  return *this;
547  }
548 
549  InstructionMetadata &RemoveCustomCodeGenerator() {
550  codeExtraInformation.hasCustomCodeGenerator = false;
551  std::function<gd::String(Instruction & instruction,
552  gd::EventsCodeGenerator & codeGenerator,
554  emptyFunction;
555  codeExtraInformation.customCodeGenerator = emptyFunction;
556  return *this;
557  }
558 
559  bool HasCustomCodeGenerator() const { return codeExtraInformation.hasCustomCodeGenerator; }
560 
568 
569  ParameterMetadataContainer parameters;
570 
571  private:
572  gd::String fullname;
573  gd::String description;
574  gd::String helpPath;
575  gd::String sentence;
576  gd::String group;
577  gd::String iconFilename;
578  gd::String smallIconFilename;
579  bool canHaveSubInstructions;
580  gd::String extensionNamespace;
581  bool hidden;
582  int usageComplexity;
584  bool isPrivate;
585  bool isObjectInstruction;
586  bool isBehaviorInstruction;
587  gd::String requiredBaseObjectCapability;
588  gd::String relevantContext;
589 };
590 
591 } // namespace gd
Describe user-friendly information about an expression or an instruction (action or condition),...
Definition: AbstractFunctionMetadata.h:36
Used to manage the context when generating code for events.
Definition: EventsCodeGenerationContext.h:27
Internal class used to generate code from events.
Definition: EventsCodeGenerator.h:41
An instruction is a member of an event: It can be a condition or an action.
Definition: Instruction.h:30
Defines information about how generate the code for an instruction.
Definition: InstructionMetadata.h:393
Describe user-friendly information about an instruction (action or condition), its parameters and the...
Definition: InstructionMetadata.h:39
bool IsOptionallyAsync() const
Definition: InstructionMetadata.h:181
InstructionMetadata & MarkAsComplex()
Consider that the instruction is complex for a user to understand.
Definition: InstructionMetadata.h:379
InstructionMetadata & SetIsBehaviorInstruction()
Mark the instruction as a behavior instruction. Automatically called when using AddAction/AddConditio...
Definition: InstructionMetadata.h:327
const gd::String & GetHelpPath() const
Definition: InstructionMetadata.h:81
InstructionMetadata & MarkAsSimple()
Consider that the instruction is easy for a user to understand.
Definition: InstructionMetadata.h:362
bool IsRelevantForCustomObjectEvents() const
Definition: InstructionMetadata.h:132
const gd::String & GetManipulatedType() const
Return the type manipulated in a standard way by the instruction.
Definition: InstructionMetadata.h:470
InstructionMetadata & SetDefaultValue(const gd::String &defaultValue_) override
Set the default value used in editor (or if an optional parameter is empty during code generation) fo...
Definition: InstructionMetadata.h:259
InstructionMetadata & SetRelevantForAsynchronousFunctionEventsOnly() override
Definition: InstructionMetadata.h:155
InstructionMetadata & SetAsyncFunctionName(const gd::String &functionName_)
Definition: InstructionMetadata.h:433
InstructionMetadata & SetHelpPath(const gd::String &path)
Definition: InstructionMetadata.h:87
bool IsPrivate() const
Definition: InstructionMetadata.h:96
InstructionMetadata & SetGetter(const gd::String &getter)
Definition: InstructionMetadata.h:497
bool IsRelevantForFunctionEvents() const
Definition: InstructionMetadata.h:117
bool IsRelevantForLayoutEvents() const
Definition: InstructionMetadata.h:110
InstructionMetadata & SetIncludeFile(const gd::String &includeFile) override
Erase any existing include file and add the specified include.
Definition: InstructionMetadata.h:515
InstructionMetadata & GetCodeExtraInformation()
Return the structure containing the information about code generation for the instruction.
Definition: InstructionMetadata.h:567
InstructionMetadata & SetRelevantForLayoutEventsOnly() override
Definition: InstructionMetadata.h:139
InstructionMetadata & SetPrivate() override
Definition: InstructionMetadata.h:102
InstructionMetadata & AddIncludeFile(const gd::String &includeFile) override
Add a file to the already existing include files.
Definition: InstructionMetadata.h:524
const std::vector< gd::String > & GetIncludeFiles() const override
Get the files that must be included to use the instruction.
Definition: InstructionMetadata.h:535
InstructionMetadata & SetParameterExtraInfo(const gd::String &extraInfo) override
Set the additional information, used for some parameters with special type (for example,...
Definition: InstructionMetadata.h:287
InstructionMetadata & SetGroup(const gd::String &str)
Set the group of the instruction in the IDE.
Definition: InstructionMetadata.h:206
bool IsRelevantForAsynchronousFunctionEvents() const
Definition: InstructionMetadata.h:124
InstructionMetadata & SetRelevantForFunctionEventsOnly() override
Definition: InstructionMetadata.h:147
bool IsObjectInstruction() const
Check if the instruction is an object instruction.
Definition: InstructionMetadata.h:335
InstructionMetadata & SetParameterLongDescription(const gd::String &longDescription) override
Set the long description shown in the editor for the last added parameter.
Definition: InstructionMetadata.h:272
int GetUsageComplexity() const
Return the usage complexity of this instruction for the user, from 0 (simple&easy to use) to 10 (comp...
Definition: InstructionMetadata.h:388
InstructionMetadata & SetCanHaveSubInstructions()
Definition: InstructionMetadata.h:188
InstructionMetadata & SetManipulatedType(const gd::String &type_)
Declare if the instruction being declared is somewhat manipulating in a standard way.
Definition: InstructionMetadata.h:460
ExtraInformation codeExtraInformation
Definition: InstructionMetadata.h:413
InstructionMetadata & SetFunctionName(const gd::String &functionName_) override
Definition: InstructionMetadata.h:420
const gd::String & GetAsyncFunctionName()
Definition: InstructionMetadata.h:450
InstructionMetadata & SetRelevantForCustomObjectEventsOnly() override
Definition: InstructionMetadata.h:163
bool IsHidden() const
Return true if the instruction must be hidden in the IDE.
Definition: InstructionMetadata.h:214
InstructionMetadata & MarkAsAdvanced()
Consider that the instruction is harder for a user to understand than a normal instruction.
Definition: InstructionMetadata.h:371
bool IsBehaviorInstruction() const
Check if the instruction is a behavior instruction.
Definition: InstructionMetadata.h:340
bool IsAsync() const
Definition: InstructionMetadata.h:173
const gd::String & GetRequiredBaseObjectCapability() const
Get the required specified capability for this (object) instruction, or an empty string if there is n...
Definition: InstructionMetadata.h:355
InstructionMetadata & SetHidden() override
Set the instruction to be hidden in the IDE.
Definition: InstructionMetadata.h:198
InstructionMetadata & SetIsObjectInstruction()
Mark the instruction as an object instruction. Automatically called when using AddAction/AddCondition...
Definition: InstructionMetadata.h:318
const gd::String & GetFunctionName()
Definition: InstructionMetadata.h:441
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
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24
Definition: ParameterOptions.h:15