Skip to content

Commit f0b644f

Browse files
committed
Implement looks_seteffectto block
1 parent b39781f commit f0b644f

File tree

3 files changed

+456
-2
lines changed

3 files changed

+456
-2
lines changed

src/blocks/looksblocks.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
2727
engine->addCompileFunction(this, "looks_show", &compileShow);
2828
engine->addCompileFunction(this, "looks_hide", &compileHide);
2929
engine->addCompileFunction(this, "looks_changeeffectby", &compileChangeEffectBy);
30+
engine->addCompileFunction(this, "looks_seteffectto", &compileSetEffectTo);
3031
engine->addCompileFunction(this, "looks_changesizeby", &compileChangeSizeBy);
3132
engine->addCompileFunction(this, "looks_setsizeto", &compileSetSizeTo);
3233
engine->addCompileFunction(this, "looks_size", &compileSize);
@@ -43,6 +44,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
4344
engine->addInput(this, "SIZE", SIZE);
4445
engine->addInput(this, "COSTUME", COSTUME);
4546
engine->addInput(this, "BACKDROP", BACKDROP);
47+
engine->addInput(this, "VALUE", VALUE);
4648

4749
// Fields
4850
engine->addField(this, "NUMBER_NAME", NUMBER_NAME);
@@ -153,6 +155,89 @@ void LooksBlocks::compileChangeEffectBy(Compiler *compiler)
153155
}
154156
}
155157

158+
void LooksBlocks::compileSetEffectTo(Compiler *compiler)
159+
{
160+
int option = compiler->field(EFFECT)->specialValueId();
161+
162+
switch (option) {
163+
case ColorEffect:
164+
if (!m_colorEffect)
165+
m_colorEffect = ScratchConfiguration::getGraphicsEffect("color");
166+
167+
compiler->addInput(CHANGE);
168+
compiler->addFunctionCall(&setColorEffectTo);
169+
break;
170+
171+
case FisheyeEffect:
172+
if (!m_fisheyeEffect)
173+
m_fisheyeEffect = ScratchConfiguration::getGraphicsEffect("fisheye");
174+
175+
compiler->addInput(CHANGE);
176+
compiler->addFunctionCall(&setFisheyeEffectTo);
177+
break;
178+
179+
case WhirlEffect:
180+
if (!m_whirlEffect)
181+
m_whirlEffect = ScratchConfiguration::getGraphicsEffect("whirl");
182+
183+
compiler->addInput(CHANGE);
184+
compiler->addFunctionCall(&setWhirlEffectTo);
185+
break;
186+
187+
case PixelateEffect:
188+
if (!m_pixelateEffect)
189+
m_pixelateEffect = ScratchConfiguration::getGraphicsEffect("pixelate");
190+
191+
compiler->addInput(CHANGE);
192+
compiler->addFunctionCall(&setPixelateEffectTo);
193+
break;
194+
195+
case MosaicEffect:
196+
if (!m_mosaicEffect)
197+
m_mosaicEffect = ScratchConfiguration::getGraphicsEffect("mosaic");
198+
199+
compiler->addInput(CHANGE);
200+
compiler->addFunctionCall(&setMosaicEffectTo);
201+
break;
202+
203+
case BrightnessEffect:
204+
if (!m_brightnessEffect)
205+
m_brightnessEffect = ScratchConfiguration::getGraphicsEffect("brightness");
206+
207+
compiler->addInput(CHANGE);
208+
compiler->addFunctionCall(&setBrightnessEffectTo);
209+
break;
210+
211+
case GhostEffect:
212+
if (!m_ghostEffect)
213+
m_ghostEffect = ScratchConfiguration::getGraphicsEffect("ghost");
214+
215+
compiler->addInput(CHANGE);
216+
compiler->addFunctionCall(&setGhostEffectTo);
217+
break;
218+
219+
default:
220+
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect(compiler->field(EFFECT)->value().toString());
221+
222+
if (effect) {
223+
auto it = std::find(m_customGraphicsEffects.begin(), m_customGraphicsEffects.end(), effect);
224+
size_t index;
225+
226+
if (it == m_customGraphicsEffects.end()) {
227+
index = m_customGraphicsEffects.size();
228+
m_customGraphicsEffects.push_back(effect);
229+
} else
230+
index = it - m_customGraphicsEffects.begin();
231+
232+
compiler->addConstValue(index);
233+
compiler->addInput(CHANGE);
234+
compiler->addFunctionCall(&setEffectTo);
235+
}
236+
237+
break;
238+
}
239+
}
240+
156241
void LooksBlocks::compileChangeSizeBy(Compiler *compiler)
157242
{
158243
compiler->addInput(CHANGE);
@@ -429,6 +514,86 @@ unsigned int LooksBlocks::changeGhostEffectBy(VirtualMachine *vm)
429514
return 1;
430515
}
431516

517+
unsigned int LooksBlocks::setEffectTo(VirtualMachine *vm)
518+
{
519+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
520+
521+
if (sprite)
522+
sprite->setGraphicsEffectValue(m_customGraphicsEffects[vm->getInput(0, 2)->toLong()], vm->getInput(1, 2)->toDouble());
523+
524+
return 2;
525+
}
526+
527+
unsigned int LooksBlocks::setColorEffectTo(VirtualMachine *vm)
528+
{
529+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
530+
531+
if (sprite)
532+
sprite->setGraphicsEffectValue(m_colorEffect, vm->getInput(0, 1)->toDouble());
533+
534+
return 1;
535+
}
536+
537+
unsigned int LooksBlocks::setFisheyeEffectTo(VirtualMachine *vm)
538+
{
539+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
540+
541+
if (sprite)
542+
sprite->setGraphicsEffectValue(m_fisheyeEffect, vm->getInput(0, 1)->toDouble());
543+
544+
return 1;
545+
}
546+
547+
unsigned int LooksBlocks::setWhirlEffectTo(VirtualMachine *vm)
548+
{
549+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
550+
551+
if (sprite)
552+
sprite->setGraphicsEffectValue(m_whirlEffect, vm->getInput(0, 1)->toDouble());
553+
554+
return 1;
555+
}
556+
557+
unsigned int LooksBlocks::setPixelateEffectTo(VirtualMachine *vm)
558+
{
559+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
560+
561+
if (sprite)
562+
sprite->setGraphicsEffectValue(m_pixelateEffect, vm->getInput(0, 1)->toDouble());
563+
564+
return 1;
565+
}
566+
567+
unsigned int LooksBlocks::setMosaicEffectTo(VirtualMachine *vm)
568+
{
569+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
570+
571+
if (sprite)
572+
sprite->setGraphicsEffectValue(m_mosaicEffect, vm->getInput(0, 1)->toDouble());
573+
574+
return 1;
575+
}
576+
577+
unsigned int LooksBlocks::setBrightnessEffectTo(VirtualMachine *vm)
578+
{
579+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
580+
581+
if (sprite)
582+
sprite->setGraphicsEffectValue(m_brightnessEffect, vm->getInput(0, 1)->toDouble());
583+
584+
return 1;
585+
}
586+
587+
unsigned int LooksBlocks::setGhostEffectTo(VirtualMachine *vm)
588+
{
589+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
590+
591+
if (sprite)
592+
sprite->setGraphicsEffectValue(m_ghostEffect, vm->getInput(0, 1)->toDouble());
593+
594+
return 1;
595+
}
596+
432597
unsigned int LooksBlocks::changeSizeBy(VirtualMachine *vm)
433598
{
434599
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/looksblocks.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class LooksBlocks : public IBlockSection
2323
CHANGE,
2424
SIZE,
2525
COSTUME,
26-
BACKDROP
26+
BACKDROP,
27+
VALUE
2728
};
2829

2930
enum Fields
@@ -52,6 +53,7 @@ class LooksBlocks : public IBlockSection
5253
static void compileShow(Compiler *compiler);
5354
static void compileHide(Compiler *compiler);
5455
static void compileChangeEffectBy(Compiler *compiler);
56+
static void compileSetEffectTo(Compiler *compiler);
5557
static void compileChangeSizeBy(Compiler *compiler);
5658
static void compileSetSizeTo(Compiler *compiler);
5759
static void compileSize(Compiler *compiler);
@@ -75,6 +77,15 @@ class LooksBlocks : public IBlockSection
7577
static unsigned int changeBrightnessEffectBy(VirtualMachine *vm);
7678
static unsigned int changeGhostEffectBy(VirtualMachine *vm);
7779

80+
static unsigned int setEffectTo(VirtualMachine *vm);
81+
static unsigned int setColorEffectTo(VirtualMachine *vm);
82+
static unsigned int setFisheyeEffectTo(VirtualMachine *vm);
83+
static unsigned int setWhirlEffectTo(VirtualMachine *vm);
84+
static unsigned int setPixelateEffectTo(VirtualMachine *vm);
85+
static unsigned int setMosaicEffectTo(VirtualMachine *vm);
86+
static unsigned int setBrightnessEffectTo(VirtualMachine *vm);
87+
static unsigned int setGhostEffectTo(VirtualMachine *vm);
88+
7889
static unsigned int changeSizeBy(VirtualMachine *vm);
7990
static unsigned int setSizeTo(VirtualMachine *vm);
8091
static unsigned int size(VirtualMachine *vm);

0 commit comments

Comments
 (0)