Skip to content

Commit ed691ee

Browse files
committed
Implement pen_setPenColorParamTo block
1 parent 9da34e4 commit ed691ee

File tree

3 files changed

+730
-4
lines changed

3 files changed

+730
-4
lines changed

src/blocks/penblocks.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void PenBlocks::registerBlocks(IEngine *engine)
8181
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
8282
engine->addCompileFunction(this, "pen_setPenColorToColor", &compileSetPenColorToColor);
8383
engine->addCompileFunction(this, "pen_changePenColorParamBy", &compileChangePenColorParamBy);
84+
engine->addCompileFunction(this, "pen_setPenColorParamTo", &compileSetPenColorParamTo);
8485
}
8586

8687
CompilerValue *PenBlocks::compileClear(Compiler *compiler)
@@ -141,29 +142,39 @@ CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler)
141142
return nullptr;
142143
}
143144

144-
CompilerValue *PenBlocks::compileChangePenColorParamBy(Compiler *compiler)
145+
void PenBlocks::compileSetOrChangePenColorParam(Compiler *compiler, bool change)
145146
{
146147
Input *paramInput = compiler->input("COLOR_PARAM");
147148
CompilerValue *param = compiler->addInput(paramInput);
148149
CompilerValue *value = compiler->addInput("VALUE");
149-
CompilerValue *change = compiler->addConstValue(true);
150+
CompilerValue *changeValue = compiler->addConstValue(change);
150151

151152
if (paramInput->pointsToDropdownMenu()) {
152153
static const std::unordered_set<std::string> options = { "color", "saturation", "brightness", "transparency" };
153154
std::string option = paramInput->selectedMenuItem();
154155

155156
if (options.find(option) != options.cend()) {
156157
std::string f = "pen_set_or_change_" + option;
157-
compiler->addTargetFunctionCall(f, Compiler::StaticType::Void, { Compiler::StaticType::Number, Compiler::StaticType::Bool }, { value, change });
158+
compiler->addTargetFunctionCall(f, Compiler::StaticType::Void, { Compiler::StaticType::Number, Compiler::StaticType::Bool }, { value, changeValue });
158159
}
159160
} else {
160161
compiler->addTargetFunctionCall(
161162
"pen_set_or_change_color_param",
162163
Compiler::StaticType::Void,
163164
{ Compiler::StaticType::String, Compiler::StaticType::Number, Compiler::StaticType::Bool },
164-
{ param, value, change });
165+
{ param, value, changeValue });
165166
}
167+
}
168+
169+
CompilerValue *PenBlocks::compileChangePenColorParamBy(Compiler *compiler)
170+
{
171+
compileSetOrChangePenColorParam(compiler, true);
172+
return nullptr;
173+
}
166174

175+
CompilerValue *PenBlocks::compileSetPenColorParamTo(Compiler *compiler)
176+
{
177+
compileSetOrChangePenColorParam(compiler, false);
167178
return nullptr;
168179
}
169180

src/blocks/penblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class PenBlocks : public libscratchcpp::IExtension
1818
static libscratchcpp::CompilerValue *compilePenDown(libscratchcpp::Compiler *compiler);
1919
static libscratchcpp::CompilerValue *compilePenUp(libscratchcpp::Compiler *compiler);
2020
static libscratchcpp::CompilerValue *compileSetPenColorToColor(libscratchcpp::Compiler *compiler);
21+
static void compileSetOrChangePenColorParam(libscratchcpp::Compiler *compiler, bool change);
2122
static libscratchcpp::CompilerValue *compileChangePenColorParamBy(libscratchcpp::Compiler *compiler);
23+
static libscratchcpp::CompilerValue *compileSetPenColorParamTo(libscratchcpp::Compiler *compiler);
2224
};
2325

2426
} // namespace scratchcpprender

0 commit comments

Comments
 (0)