-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvariableblocks.cpp
More file actions
110 lines (84 loc) · 2.78 KB
/
variableblocks.cpp
File metadata and controls
110 lines (84 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: Apache-2.0
#include <scratchcpp/iengine.h>
#include <scratchcpp/compiler.h>
#include <scratchcpp/compilerconstant.h>
#include <scratchcpp/block.h>
#include <scratchcpp/input.h>
#include <scratchcpp/field.h>
#include <scratchcpp/variable.h>
#include "variableblocks.h"
using namespace libscratchcpp;
std::string VariableBlocks::name() const
{
return "Variables";
}
std::string VariableBlocks::description() const
{
return "Variable blocks";
}
Rgb VariableBlocks::color() const
{
return rgb(255, 140, 26);
}
void VariableBlocks::registerBlocks(IEngine *engine)
{
// Blocks
engine->addCompileFunction(this, "data_variable", &compileVariable);
engine->addCompileFunction(this, "data_setvariableto", &compileSetVariableTo);
engine->addCompileFunction(this, "data_changevariableby", &compileChangeVariableBy);
// Monitor names
engine->addMonitorNameFunction(this, "data_variable", &variableMonitorName);
// Monitor change functions
engine->addMonitorChangeFunction(this, "data_variable", &changeVariableMonitorValue);
}
CompilerValue *VariableBlocks::compileVariable(Compiler *compiler)
{
Field *varField = compiler->field("VARIABLE");
Variable *var = static_cast<Variable *>(varField->valuePtr().get());
assert(var);
if (var)
return compiler->addVariableValue(var);
else
return compiler->addConstValue(Value());
}
CompilerValue *VariableBlocks::compileSetVariableTo(Compiler *compiler)
{
Field *varField = compiler->field("VARIABLE");
Variable *var = static_cast<Variable *>(varField->valuePtr().get());
assert(var);
if (var)
compiler->createVariableWrite(var, compiler->addInput("VALUE"));
return nullptr;
}
CompilerValue *VariableBlocks::compileChangeVariableBy(Compiler *compiler)
{
Field *varField = compiler->field("VARIABLE");
Variable *var = static_cast<Variable *>(varField->valuePtr().get());
assert(var);
if (var) {
CompilerValue *value = compiler->createAdd(compiler->addVariableValue(var), compiler->addInput("VALUE"));
compiler->createVariableWrite(var, value);
}
return nullptr;
}
const std::string &VariableBlocks::variableMonitorName(Block *block)
{
static const std::string empty = "";
auto field = block->fieldAt(block->findField("VARIABLE"));
if (!field)
return empty;
Variable *var = dynamic_cast<Variable *>(field->valuePtr().get());
if (var)
return var->name();
else
return empty;
}
void VariableBlocks::changeVariableMonitorValue(Block *block, const Value &newValue)
{
auto field = block->fieldAt(block->findField("VARIABLE"));
if (!field)
return;
Variable *var = dynamic_cast<Variable *>(field->valuePtr().get());
if (var)
var->setValue(newValue);
}