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 
222  group = str;
223  return *this;
224  }
225 
229  bool IsHidden() const { return hidden; }
230 
248  InstructionMetadata &AddParameter(
249  const gd::String &type,
250  const gd::String &label,
251  const gd::String &supplementaryInformation = "",
252  bool parameterIsOptional = false) override;
253 
265  InstructionMetadata &AddCodeOnlyParameter(
266  const gd::String &type, const gd::String &supplementaryInformation) override;
267 
274  InstructionMetadata &SetDefaultValue(const gd::String &defaultValue_) override {
275  if (parameters.GetParametersCount() > 0) {
276  parameters.GetInternalVector().back()->SetDefaultValue(defaultValue_);
277  }
278  return *this;
279  };
280 
288  const gd::String &longDescription) override {
289  if (parameters.GetParametersCount() > 0) {
290  parameters.GetInternalVector().back()->SetLongDescription(longDescription);
291  }
292  return *this;
293  }
294 
303  if (parameters.GetParametersCount() > 0) {
304  parameters.GetInternalVector().back()->SetExtraInfo(extraInfo);
305  }
306  return *this;
307  }
308 
316  InstructionMetadata &UseStandardOperatorParameters(
317  const gd::String &type, const ParameterOptions &options);
318 
326  InstructionMetadata &UseStandardRelationalOperatorParameters(
327  const gd::String &type, const ParameterOptions &options);
328 
334  isObjectInstruction = true;
335  return *this;
336  }
337 
343  isBehaviorInstruction = true;
344  return *this;
345  }
346 
350  bool IsObjectInstruction() const { return isObjectInstruction; }
351 
355  bool IsBehaviorInstruction() const { return isBehaviorInstruction; }
356 
363  InstructionMetadata &SetRequiresBaseObjectCapability(
364  const gd::String &capability);
365 
371  return requiredBaseObjectCapability;
372  }
373 
378  usageComplexity = 2;
379  return *this;
380  }
381 
387  usageComplexity = 7;
388  return *this;
389  }
390 
395  usageComplexity = 9;
396  return *this;
397  }
398 
403  int GetUsageComplexity() const { return usageComplexity; }
404 
409  public:
410  enum AccessType { Reference, MutatorAndOrAccessor, Mutators };
411  ExtraInformation() : accessType(Reference), hasCustomCodeGenerator(false){};
412  virtual ~ExtraInformation(){};
413 
414  // TODO Move these attributes to InstructionMetadata.
415  gd::String functionCallName;
416  gd::String asyncFunctionCallName;
417  gd::String type;
418  AccessType accessType;
419  gd::String optionalAssociatedInstruction;
420  std::map<gd::String, gd::String> optionalMutators;
421  bool hasCustomCodeGenerator;
422  std::function<gd::String(Instruction &instruction,
423  gd::EventsCodeGenerator &codeGenerator,
425  customCodeGenerator;
426  std::vector<gd::String> includeFiles;
427  };
430 
435  InstructionMetadata &SetFunctionName(const gd::String &functionName_) override {
436  codeExtraInformation.functionCallName = functionName_;
437  return *this;
438  }
439 
449  codeExtraInformation.asyncFunctionCallName = functionName_;
450  return *this;
451  }
452 
457  return codeExtraInformation.functionCallName;
458  }
459 
466  return codeExtraInformation.asyncFunctionCallName;
467  }
468 
476  codeExtraInformation.type = type_;
477  return *this;
478  }
479 
486  return codeExtraInformation.type;
487  }
488 
513  codeExtraInformation.optionalAssociatedInstruction = getter;
514  codeExtraInformation.accessType = codeExtraInformation.MutatorAndOrAccessor;
515  return *this;
516  }
517 
518  InstructionMetadata &SetMutators(
519  const std::map<gd::String, gd::String> &mutators) {
520  codeExtraInformation.optionalMutators = mutators;
521  codeExtraInformation.accessType = codeExtraInformation.Mutators;
522  return *this;
523  }
524 
530  InstructionMetadata &SetIncludeFile(const gd::String &includeFile) override {
531  codeExtraInformation.includeFiles.clear();
532  codeExtraInformation.includeFiles.push_back(includeFile);
533  return *this;
534  }
535 
539  InstructionMetadata &AddIncludeFile(const gd::String &includeFile) override {
540  if (std::find(codeExtraInformation.includeFiles.begin(), codeExtraInformation.includeFiles.end(), includeFile) ==
541  codeExtraInformation.includeFiles.end())
542  codeExtraInformation.includeFiles.push_back(includeFile);
543 
544  return *this;
545  }
546 
550  const std::vector<gd::String> &GetIncludeFiles() const override {
551  return codeExtraInformation.includeFiles;
552  };
553 
554  InstructionMetadata &SetCustomCodeGenerator(
555  std::function<gd::String(Instruction &instruction,
556  gd::EventsCodeGenerator &codeGenerator,
558  codeGenerator) {
559  codeExtraInformation.hasCustomCodeGenerator = true;
560  codeExtraInformation.customCodeGenerator = codeGenerator;
561  return *this;
562  }
563 
564  InstructionMetadata &RemoveCustomCodeGenerator() {
565  codeExtraInformation.hasCustomCodeGenerator = false;
566  std::function<gd::String(Instruction & instruction,
567  gd::EventsCodeGenerator & codeGenerator,
569  emptyFunction;
570  codeExtraInformation.customCodeGenerator = emptyFunction;
571  return *this;
572  }
573 
574  bool HasCustomCodeGenerator() const { return codeExtraInformation.hasCustomCodeGenerator; }
575 
583 
584  ParameterMetadataContainer parameters;
585 
586  private:
587  gd::String fullname;
588  gd::String description;
589  gd::String helpPath;
590  gd::String sentence;
591  gd::String group;
592  gd::String iconFilename;
593  gd::String smallIconFilename;
594  bool canHaveSubInstructions;
595  gd::String extensionNamespace;
596  bool hidden;
597  int usageComplexity;
599  bool isPrivate;
600  bool isObjectInstruction;
601  bool isBehaviorInstruction;
602  gd::String requiredBaseObjectCapability;
603  gd::String relevantContext;
604  gd::String deprecationMessage;
605 };
606 
607 } // 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:408
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:394
InstructionMetadata & SetIsBehaviorInstruction()
Mark the instruction as a behavior instruction. Automatically called when using AddAction/AddConditio...
Definition: InstructionMetadata.h:342
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:377
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:485
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:274
InstructionMetadata & SetRelevantForAsynchronousFunctionEventsOnly() override
Definition: InstructionMetadata.h:155
InstructionMetadata & SetAsyncFunctionName(const gd::String &functionName_)
Definition: InstructionMetadata.h:448
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:512
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:530
InstructionMetadata & GetCodeExtraInformation()
Return the structure containing the information about code generation for the instruction.
Definition: InstructionMetadata.h:582
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:539
const std::vector< gd::String > & GetIncludeFiles() const override
Get the files that must be included to use the instruction.
Definition: InstructionMetadata.h:550
InstructionMetadata & SetParameterExtraInfo(const gd::String &extraInfo) override
Set the additional information, used for some parameters with special type (for example,...
Definition: InstructionMetadata.h:302
InstructionMetadata & SetGroup(const gd::String &str)
Set the group of the instruction in the IDE.
Definition: InstructionMetadata.h:221
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:350
InstructionMetadata & SetParameterLongDescription(const gd::String &longDescription) override
Set the long description shown in the editor for the last added parameter.
Definition: InstructionMetadata.h:287
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:403
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:475
ExtraInformation codeExtraInformation
Definition: InstructionMetadata.h:428
InstructionMetadata & SetFunctionName(const gd::String &functionName_) override
Definition: InstructionMetadata.h:435
const gd::String & GetAsyncFunctionName()
Definition: InstructionMetadata.h:465
InstructionMetadata & SetRelevantForCustomObjectEventsOnly() override
Definition: InstructionMetadata.h:163
bool IsHidden() const
Return true if the instruction must be hidden in the IDE.
Definition: InstructionMetadata.h:229
InstructionMetadata & MarkAsAdvanced()
Consider that the instruction is harder for a user to understand than a normal instruction.
Definition: InstructionMetadata.h:386
bool IsBehaviorInstruction() const
Check if the instruction is a behavior instruction.
Definition: InstructionMetadata.h:355
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:370
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:333
const gd::String & GetFunctionName()
Definition: InstructionMetadata.h:456
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