Skip to content

Commit 1a78883

Browse files
committed
Implement pen_setPenColorParamTo block
1 parent 45ccf41 commit 1a78883

File tree

3 files changed

+377
-0
lines changed

3 files changed

+377
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void PenBlocks::registerBlocks(IEngine *engine)
3535
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
3636
engine->addCompileFunction(this, "pen_setPenColorToColor", &compileSetPenColorToColor);
3737
engine->addCompileFunction(this, "pen_changePenColorParamBy", &compileChangePenColorParamBy);
38+
engine->addCompileFunction(this, "pen_setPenColorParamTo", &compileSetPenColorParamTo);
3839
engine->addCompileFunction(this, "pen_changePenSizeBy", &compileChangePenSizeBy);
3940
engine->addCompileFunction(this, "pen_setPenSizeTo", &compileSetPenSizeTo);
4041
engine->addCompileFunction(this, "pen_changePenShadeBy", &compileChangePenShadeBy);
@@ -101,6 +102,35 @@ void PenBlocks::compileChangePenColorParamBy(libscratchcpp::Compiler *compiler)
101102
}
102103
}
103104

105+
void PenBlocks::compileSetPenColorParamTo(Compiler *compiler)
106+
{
107+
Input *input = compiler->input(COLOR_PARAM);
108+
109+
if (input->type() != Input::Type::ObscuredShadow) {
110+
assert(input->pointsToDropdownMenu());
111+
std::string value = input->selectedMenuItem();
112+
BlockFunc f = nullptr;
113+
114+
if (value == "color")
115+
f = &setPenColorTo;
116+
else if (value == "saturation")
117+
f = &setPenSaturationTo;
118+
else if (value == "brightness")
119+
f = &setPenBrightnessTo;
120+
else if (value == "transparency")
121+
f = &setPenTransparencyTo;
122+
123+
if (f) {
124+
compiler->addInput(VALUE);
125+
compiler->addFunctionCall(f);
126+
}
127+
} else {
128+
compiler->addInput(input);
129+
compiler->addInput(VALUE);
130+
compiler->addFunctionCall(&setPenColorParamTo);
131+
}
132+
}
133+
104134
void PenBlocks::compileChangePenSizeBy(libscratchcpp::Compiler *compiler)
105135
{
106136
compiler->addInput(SIZE);
@@ -343,6 +373,62 @@ unsigned int PenBlocks::changePenTransparencyBy(VirtualMachine *vm)
343373
return 1;
344374
}
345375

376+
unsigned int PenBlocks::setPenColorParamTo(VirtualMachine *vm)
377+
{
378+
SpriteModel *model = getSpriteModel(vm);
379+
380+
if (model) {
381+
const auto it = COLOR_PARAM_MAP.find(vm->getInput(0, 2)->toString());
382+
383+
if (it == COLOR_PARAM_MAP.cend())
384+
return 2;
385+
386+
setOrChangeColorParam(it->second, vm->getInput(1, 2)->toDouble(), model->penState(), false);
387+
}
388+
389+
return 2;
390+
}
391+
392+
unsigned int PenBlocks::setPenColorTo(VirtualMachine *vm)
393+
{
394+
SpriteModel *model = getSpriteModel(vm);
395+
396+
if (model)
397+
setOrChangeColorParam(ColorParam::COLOR, vm->getInput(0, 1)->toDouble(), model->penState(), false);
398+
399+
return 1;
400+
}
401+
402+
unsigned int PenBlocks::setPenSaturationTo(VirtualMachine *vm)
403+
{
404+
SpriteModel *model = getSpriteModel(vm);
405+
406+
if (model)
407+
setOrChangeColorParam(ColorParam::SATURATION, vm->getInput(0, 1)->toDouble(), model->penState(), false);
408+
409+
return 1;
410+
}
411+
412+
unsigned int PenBlocks::setPenBrightnessTo(VirtualMachine *vm)
413+
{
414+
SpriteModel *model = getSpriteModel(vm);
415+
416+
if (model)
417+
setOrChangeColorParam(ColorParam::BRIGHTNESS, vm->getInput(0, 1)->toDouble(), model->penState(), false);
418+
419+
return 1;
420+
}
421+
422+
unsigned int PenBlocks::setPenTransparencyTo(VirtualMachine *vm)
423+
{
424+
SpriteModel *model = getSpriteModel(vm);
425+
426+
if (model)
427+
setOrChangeColorParam(ColorParam::TRANSPARENCY, vm->getInput(0, 1)->toDouble(), model->penState(), false);
428+
429+
return 1;
430+
}
431+
346432
SpriteModel *PenBlocks::getSpriteModel(libscratchcpp::VirtualMachine *vm)
347433
{
348434
Target *target = vm->target();
@@ -373,6 +459,10 @@ void PenBlocks::setOrChangeColorParam(ColorParam param, double value, PenState &
373459
case ColorParam::TRANSPARENCY:
374460
penState.transparency = std::clamp(value + (change ? penState.transparency : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
375461
break;
462+
463+
default:
464+
assert(false);
465+
return;
376466
}
377467

378468
penState.updateColor();

src/blocks/penblocks.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class PenBlocks : public libscratchcpp::IBlockSection
3333
static void compilePenUp(libscratchcpp::Compiler *compiler);
3434
static void compileSetPenColorToColor(libscratchcpp::Compiler *compiler);
3535
static void compileChangePenColorParamBy(libscratchcpp::Compiler *compiler);
36+
static void compileSetPenColorParamTo(libscratchcpp::Compiler *compiler);
3637
static void compileChangePenSizeBy(libscratchcpp::Compiler *compiler);
3738
static void compileSetPenSizeTo(libscratchcpp::Compiler *compiler);
3839
static void compileChangePenShadeBy(libscratchcpp::Compiler *compiler);
@@ -51,6 +52,12 @@ class PenBlocks : public libscratchcpp::IBlockSection
5152
static unsigned int changePenBrightnessBy(libscratchcpp::VirtualMachine *vm);
5253
static unsigned int changePenTransparencyBy(libscratchcpp::VirtualMachine *vm);
5354

55+
static unsigned int setPenColorParamTo(libscratchcpp::VirtualMachine *vm);
56+
static unsigned int setPenColorTo(libscratchcpp::VirtualMachine *vm);
57+
static unsigned int setPenSaturationTo(libscratchcpp::VirtualMachine *vm);
58+
static unsigned int setPenBrightnessTo(libscratchcpp::VirtualMachine *vm);
59+
static unsigned int setPenTransparencyTo(libscratchcpp::VirtualMachine *vm);
60+
5461
static unsigned int changePenSizeBy(libscratchcpp::VirtualMachine *vm);
5562
static unsigned int setPenSizeTo(libscratchcpp::VirtualMachine *vm);
5663
static unsigned int changePenShadeBy(libscratchcpp::VirtualMachine *vm);

0 commit comments

Comments
 (0)