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 
208  deprecationMessage = message;
209  return *this;
210  }
211 
216  const gd::String &GetDeprecationMessage() const { return deprecationMessage; }
217 
224  hint = hint_;
225  return *this;
226  }
227 
231  const gd::String &GetHint() const { return hint; }
232 
237  group = str;
238  return *this;
239  }
240 
244  bool IsHidden() const { return hidden; }
245 
263  InstructionMetadata &AddParameter(
264  const gd::String &type,
265  const gd::String &label,
266  const gd::String &supplementaryInformation = "",
267  bool parameterIsOptional = false) override;
268 
280  InstructionMetadata &AddCodeOnlyParameter(
281  const gd::String &type, const gd::String &supplementaryInformation) override;
282 
289  InstructionMetadata &SetDefaultValue(const gd::String &defaultValue_) override {
290  if (parameters.GetParametersCount() > 0) {
291  parameters.GetInternalVector().back()->SetDefaultValue(defaultValue_);
292  }
293  return *this;
294  };
295 
303  const gd::String &longDescription) override {
304  if (parameters.GetParametersCount() > 0) {
305  parameters.GetInternalVector().back()->SetLongDescription(longDescription);
306  }
307  return *this;
308  }
309 
319  if (parameters.GetParametersCount() > 0) {
320  parameters.GetInternalVector().back()->SetHint(hint);
321  }
322  return *this;
323  }
324 
333  if (parameters.GetParametersCount() > 0) {
334  parameters.GetInternalVector().back()->SetExtraInfo(extraInfo);
335  }
336  return *this;
337  }
338 
346  InstructionMetadata &UseStandardOperatorParameters(
347  const gd::String &type, const ParameterOptions &options);
348 
356  InstructionMetadata &UseStandardRelationalOperatorParameters(
357  const gd::String &type, const ParameterOptions &options);
358 
364  isObjectInstruction = true;
365  return *this;
366  }
367 
373  isBehaviorInstruction = true;
374  return *this;
375  }
376 
380  bool IsObjectInstruction() const { return isObjectInstruction; }
381 
385  bool IsBehaviorInstruction() const { return isBehaviorInstruction; }
386 
393  InstructionMetadata &SetRequiresBaseObjectCapability(
394  const gd::String &capability);
395 
401  return requiredBaseObjectCapability;
402  }
403 
408  usageComplexity = 2;
409  return *this;
410  }
411 
417  usageComplexity = 7;
418  return *this;
419  }
420 
425  usageComplexity = 9;
426  return *this;
427  }
428 
433  int GetUsageComplexity() const { return usageComplexity; }
434 
439  public:
440  enum AccessType { Reference, MutatorAndOrAccessor, Mutators };
441  ExtraInformation() : accessType(Reference), hasCustomCodeGenerator(false){};
442  virtual ~ExtraInformation(){};
443 
444  // TODO Move these attributes to InstructionMetadata.
445  gd::String functionCallName;
446  gd::String asyncFunctionCallName;
447  gd::String type;
448  AccessType accessType;
449  gd::String optionalAssociatedInstruction;
450  std::map<gd::String, gd::String> optionalMutators;
451  bool hasCustomCodeGenerator;
452  std::function<gd::String(Instruction &instruction,
453  gd::EventsCodeGenerator &codeGenerator,
455  customCodeGenerator;
456  std::vector<gd::String> includeFiles;
457  };
460 
465  InstructionMetadata &SetFunctionName(const gd::String &functionName_) override {
466  codeExtraInformation.functionCallName = functionName_;
467  return *this;
468  }
469 
479  codeExtraInformation.asyncFunctionCallName = functionName_;
480  return *this;
481  }
482 
487  return codeExtraInformation.functionCallName;
488  }
489 
496  return codeExtraInformation.asyncFunctionCallName;
497  }
498 
506  codeExtraInformation.type = type_;
507  return *this;
508  }
509 
516  return codeExtraInformation.type;
517  }
518 
543  codeExtraInformation.optionalAssociatedInstruction = getter;
544  codeExtraInformation.accessType = codeExtraInformation.MutatorAndOrAccessor;
545  return *this;
546  }
547 
548  InstructionMetadata &SetMutators(
549  const std::map<gd::String, gd::String> &mutators) {
550  codeExtraInformation.optionalMutators = mutators;
551  codeExtraInformation.accessType = codeExtraInformation.Mutators;
552  return *this;
553  }
554 
560  InstructionMetadata &SetIncludeFile(const gd::String &includeFile) override {
561  codeExtraInformation.includeFiles.clear();
562  codeExtraInformation.includeFiles.push_back(includeFile);
563  return *this;
564  }
565 
569  InstructionMetadata &AddIncludeFile(const gd::String &includeFile) override {
570  if (std::find(codeExtraInformation.includeFiles.begin(), codeExtraInformation.includeFiles.end(), includeFile) ==
571  codeExtraInformation.includeFiles.end())
572  codeExtraInformation.includeFiles.push_back(includeFile);
573 
574  return *this;
575  }
576 
580  const std::vector<gd::String> &GetIncludeFiles() const override {
581  return codeExtraInformation.includeFiles;
582  };
583 
584  InstructionMetadata &SetCustomCodeGenerator(
585  std::function<gd::String(Instruction &instruction,
586  gd::EventsCodeGenerator &codeGenerator,
588  codeGenerator) {
589  codeExtraInformation.hasCustomCodeGenerator = true;
590  codeExtraInformation.customCodeGenerator = codeGenerator;
591  return *this;
592  }
593 
594  InstructionMetadata &RemoveCustomCodeGenerator() {
595  codeExtraInformation.hasCustomCodeGenerator = false;
596  std::function<gd::String(Instruction & instruction,
597  gd::EventsCodeGenerator & codeGenerator,
599  emptyFunction;
600  codeExtraInformation.customCodeGenerator = emptyFunction;
601  return *this;
602  }
603 
604  bool HasCustomCodeGenerator() const { return codeExtraInformation.hasCustomCodeGenerator; }
605 
613 
614  ParameterMetadataContainer parameters;
615 
616  private:
617  gd::String fullname;
618  gd::String description;
619  gd::String helpPath;
620  gd::String sentence;
621  gd::String group;
622  gd::String iconFilename;
623  gd::String smallIconFilename;
624  bool canHaveSubInstructions;
625  gd::String extensionNamespace;
626  bool hidden;
627  int usageComplexity;
629  bool isPrivate;
630  bool isObjectInstruction;
631  bool isBehaviorInstruction;
632  gd::String requiredBaseObjectCapability;
633  gd::String relevantContext;
634  gd::String deprecationMessage;
635  gd::String hint;
636 };
637 
638 } // 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:438
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:424
InstructionMetadata & SetIsBehaviorInstruction()
Mark the instruction as a behavior instruction. Automatically called when using AddAction/AddConditio...
Definition: InstructionMetadata.h:372
InstructionMetadata & SetDeprecationMessage(const gd::String &message) override
Set the deprecation message that explains why the instruction is deprecated and what to use instead.
Definition: InstructionMetadata.h:207
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:407
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:515
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:289
InstructionMetadata & SetRelevantForAsynchronousFunctionEventsOnly() override
Definition: InstructionMetadata.h:155
InstructionMetadata & SetAsyncFunctionName(const gd::String &functionName_)
Definition: InstructionMetadata.h:478
InstructionMetadata & SetHelpPath(const gd::String &path)
Definition: InstructionMetadata.h:87
InstructionMetadata & SetHint(const gd::String &hint_)
Set a hint attached to the instruction itself. Hints are short reminders about how the instruction sh...
Definition: InstructionMetadata.h:223
bool IsPrivate() const
Definition: InstructionMetadata.h:96
InstructionMetadata & SetGetter(const gd::String &getter)
Definition: InstructionMetadata.h:542
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:560
InstructionMetadata & GetCodeExtraInformation()
Return the structure containing the information about code generation for the instruction.
Definition: InstructionMetadata.h:612
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:569
const std::vector< gd::String > & GetIncludeFiles() const override
Get the files that must be included to use the instruction.
Definition: InstructionMetadata.h:580
InstructionMetadata & SetParameterExtraInfo(const gd::String &extraInfo) override
Set the additional information, used for some parameters with special type (for example,...
Definition: InstructionMetadata.h:332
InstructionMetadata & SetGroup(const gd::String &str)
Set the group of the instruction in the IDE.
Definition: InstructionMetadata.h:236
bool IsRelevantForAsynchronousFunctionEvents() const
Definition: InstructionMetadata.h:124
const gd::String & GetDeprecationMessage() const
Get the deprecation message that explains why the instruction is deprecated and what to use instead.
Definition: InstructionMetadata.h:216
InstructionMetadata & SetRelevantForFunctionEventsOnly() override
Definition: InstructionMetadata.h:147
bool IsObjectInstruction() const
Check if the instruction is an object instruction.
Definition: InstructionMetadata.h:380
InstructionMetadata & SetParameterLongDescription(const gd::String &longDescription) override
Set the long description shown in the editor for the last added parameter.
Definition: InstructionMetadata.h:302
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:433
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:505
const gd::String & GetHint() const
Get the hint attached to the instruction itself. See SetHint.
Definition: InstructionMetadata.h:231
ExtraInformation codeExtraInformation
Definition: InstructionMetadata.h:458
InstructionMetadata & SetFunctionName(const gd::String &functionName_) override
Definition: InstructionMetadata.h:465
const gd::String & GetAsyncFunctionName()
Definition: InstructionMetadata.h:495
InstructionMetadata & SetParameterHint(const gd::String &hint) override
Set a hint attached to the last added parameter. Hints are short reminders about how the parameter sh...
Definition: InstructionMetadata.h:318
InstructionMetadata & SetRelevantForCustomObjectEventsOnly() override
Definition: InstructionMetadata.h:163
bool IsHidden() const
Return true if the instruction must be hidden in the IDE.
Definition: InstructionMetadata.h:244
InstructionMetadata & MarkAsAdvanced()
Consider that the instruction is harder for a user to understand than a normal instruction.
Definition: InstructionMetadata.h:416
bool IsBehaviorInstruction() const
Check if the instruction is a behavior instruction.
Definition: InstructionMetadata.h:385
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:400
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:363
const gd::String & GetFunctionName()
Definition: InstructionMetadata.h:486
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