77#include < scratchcpp/input.h>
88#include < scratchcpp/field.h>
99#include < scratchcpp/costume.h>
10+ #include < scratchcpp/scratchconfiguration.h>
1011
1112#include " looksblocks.h"
1213#include " ../engine/internal/randomgenerator.h"
@@ -25,6 +26,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
2526 // Blocks
2627 engine->addCompileFunction (this , " looks_show" , &compileShow);
2728 engine->addCompileFunction (this , " looks_hide" , &compileHide);
29+ engine->addCompileFunction (this , " looks_changeeffectby" , &compileChangeEffectBy);
2830 engine->addCompileFunction (this , " looks_changesizeby" , &compileChangeSizeBy);
2931 engine->addCompileFunction (this , " looks_setsizeto" , &compileSetSizeTo);
3032 engine->addCompileFunction (this , " looks_size" , &compileSize);
@@ -44,10 +46,18 @@ void LooksBlocks::registerBlocks(IEngine *engine)
4446
4547 // Fields
4648 engine->addField (this , " NUMBER_NAME" , NUMBER_NAME);
49+ engine->addField (this , " EFFECT" , EFFECT);
4750
4851 // Field values
4952 engine->addFieldValue (this , " number" , Number);
5053 engine->addFieldValue (this , " name" , Name);
54+ engine->addFieldValue (this , " COLOR" , ColorEffect);
55+ engine->addFieldValue (this , " FISHEYE" , FisheyeEffect);
56+ engine->addFieldValue (this , " WHIRL" , WhirlEffect);
57+ engine->addFieldValue (this , " PIXELATE" , PixelateEffect);
58+ engine->addFieldValue (this , " MOSAIC" , MosaicEffect);
59+ engine->addFieldValue (this , " BRIGHTNESS" , BrightnessEffect);
60+ engine->addFieldValue (this , " GHOST" , GhostEffect);
5161}
5262
5363void LooksBlocks::compileShow (Compiler *compiler)
@@ -60,6 +70,89 @@ void LooksBlocks::compileHide(Compiler *compiler)
6070 compiler->addFunctionCall (&hide);
6171}
6272
73+ void LooksBlocks::compileChangeEffectBy (Compiler *compiler)
74+ {
75+ int option = compiler->field (EFFECT)->specialValueId ();
76+
77+ switch (option) {
78+ case ColorEffect:
79+ if (!m_colorEffect)
80+ m_colorEffect = ScratchConfiguration::getGraphicsEffect (" color" );
81+
82+ compiler->addInput (CHANGE);
83+ compiler->addFunctionCall (&changeColorEffectBy);
84+ break ;
85+
86+ case FisheyeEffect:
87+ if (!m_fisheyeEffect)
88+ m_fisheyeEffect = ScratchConfiguration::getGraphicsEffect (" fisheye" );
89+
90+ compiler->addInput (CHANGE);
91+ compiler->addFunctionCall (&changeFisheyeEffectBy);
92+ break ;
93+
94+ case WhirlEffect:
95+ if (!m_whirlEffect)
96+ m_whirlEffect = ScratchConfiguration::getGraphicsEffect (" whirl" );
97+
98+ compiler->addInput (CHANGE);
99+ compiler->addFunctionCall (&changeWhirlEffectBy);
100+ break ;
101+
102+ case PixelateEffect:
103+ if (!m_pixelateEffect)
104+ m_pixelateEffect = ScratchConfiguration::getGraphicsEffect (" pixelate" );
105+
106+ compiler->addInput (CHANGE);
107+ compiler->addFunctionCall (&changePixelateEffectBy);
108+ break ;
109+
110+ case MosaicEffect:
111+ if (!m_mosaicEffect)
112+ m_mosaicEffect = ScratchConfiguration::getGraphicsEffect (" mosaic" );
113+
114+ compiler->addInput (CHANGE);
115+ compiler->addFunctionCall (&changeMosaicEffectBy);
116+ break ;
117+
118+ case BrightnessEffect:
119+ if (!m_brightnessEffect)
120+ m_brightnessEffect = ScratchConfiguration::getGraphicsEffect (" brightness" );
121+
122+ compiler->addInput (CHANGE);
123+ compiler->addFunctionCall (&changeBrightnessEffectBy);
124+ break ;
125+
126+ case GhostEffect:
127+ if (!m_ghostEffect)
128+ m_ghostEffect = ScratchConfiguration::getGraphicsEffect (" ghost" );
129+
130+ compiler->addInput (CHANGE);
131+ compiler->addFunctionCall (&changeGhostEffectBy);
132+ break ;
133+
134+ default :
135+ IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect (compiler->field (EFFECT)->value ().toString ());
136+
137+ if (effect) {
138+ auto it = std::find (m_customGraphicsEffects.begin (), m_customGraphicsEffects.end (), effect);
139+ size_t index;
140+
141+ if (it == m_customGraphicsEffects.end ()) {
142+ index = m_customGraphicsEffects.size ();
143+ m_customGraphicsEffects.push_back (effect);
144+ } else
145+ index = it - m_customGraphicsEffects.begin ();
146+
147+ compiler->addConstValue (index);
148+ compiler->addInput (CHANGE);
149+ compiler->addFunctionCall (&changeEffectBy);
150+ }
151+
152+ break ;
153+ }
154+ }
155+
63156void LooksBlocks::compileChangeSizeBy (Compiler *compiler)
64157{
65158 compiler->addInput (CHANGE);
@@ -254,6 +347,88 @@ unsigned int LooksBlocks::hide(VirtualMachine *vm)
254347 return 0 ;
255348}
256349
350+ unsigned int LooksBlocks::changeEffectBy (VirtualMachine *vm)
351+ {
352+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
353+
354+ if (sprite) {
355+ IGraphicsEffect *effect = m_customGraphicsEffects[vm->getInput (0 , 2 )->toLong ()];
356+ sprite->setGraphicsEffectValue (effect, sprite->graphicsEffectValue (effect) + vm->getInput (1 , 2 )->toDouble ());
357+ }
358+
359+ return 2 ;
360+ }
361+
362+ unsigned int LooksBlocks::changeColorEffectBy (VirtualMachine *vm)
363+ {
364+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
365+
366+ if (sprite)
367+ sprite->setGraphicsEffectValue (m_colorEffect, sprite->graphicsEffectValue (m_colorEffect) + vm->getInput (0 , 1 )->toDouble ());
368+
369+ return 1 ;
370+ }
371+
372+ unsigned int LooksBlocks::changeFisheyeEffectBy (VirtualMachine *vm)
373+ {
374+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
375+
376+ if (sprite)
377+ sprite->setGraphicsEffectValue (m_fisheyeEffect, sprite->graphicsEffectValue (m_fisheyeEffect) + vm->getInput (0 , 1 )->toDouble ());
378+
379+ return 1 ;
380+ }
381+
382+ unsigned int LooksBlocks::changeWhirlEffectBy (VirtualMachine *vm)
383+ {
384+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
385+
386+ if (sprite)
387+ sprite->setGraphicsEffectValue (m_whirlEffect, sprite->graphicsEffectValue (m_whirlEffect) + vm->getInput (0 , 1 )->toDouble ());
388+
389+ return 1 ;
390+ }
391+
392+ unsigned int LooksBlocks::changePixelateEffectBy (VirtualMachine *vm)
393+ {
394+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
395+
396+ if (sprite)
397+ sprite->setGraphicsEffectValue (m_pixelateEffect, sprite->graphicsEffectValue (m_pixelateEffect) + vm->getInput (0 , 1 )->toDouble ());
398+
399+ return 1 ;
400+ }
401+
402+ unsigned int LooksBlocks::changeMosaicEffectBy (VirtualMachine *vm)
403+ {
404+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
405+
406+ if (sprite)
407+ sprite->setGraphicsEffectValue (m_mosaicEffect, sprite->graphicsEffectValue (m_mosaicEffect) + vm->getInput (0 , 1 )->toDouble ());
408+
409+ return 1 ;
410+ }
411+
412+ unsigned int LooksBlocks::changeBrightnessEffectBy (VirtualMachine *vm)
413+ {
414+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
415+
416+ if (sprite)
417+ sprite->setGraphicsEffectValue (m_brightnessEffect, sprite->graphicsEffectValue (m_brightnessEffect) + vm->getInput (0 , 1 )->toDouble ());
418+
419+ return 1 ;
420+ }
421+
422+ unsigned int LooksBlocks::changeGhostEffectBy (VirtualMachine *vm)
423+ {
424+ Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
425+
426+ if (sprite)
427+ sprite->setGraphicsEffectValue (m_ghostEffect, sprite->graphicsEffectValue (m_ghostEffect) + vm->getInput (0 , 1 )->toDouble ());
428+
429+ return 1 ;
430+ }
431+
257432unsigned int LooksBlocks::changeSizeBy (VirtualMachine *vm)
258433{
259434 Sprite *sprite = dynamic_cast <Sprite *>(vm->target ());
0 commit comments