@@ -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+
104134void 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+
346432SpriteModel *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 ();
0 commit comments