Skip to content

Commit fb2b55d

Browse files
committed
Implement looks_seteffectto block
1 parent 481fb4b commit fb2b55d

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
4848
engine->addCompileFunction(this, "looks_show", &compileShow);
4949
engine->addCompileFunction(this, "looks_hide", &compileHide);
5050
engine->addCompileFunction(this, "looks_changeeffectby", &compileChangeEffectBy);
51+
engine->addCompileFunction(this, "looks_seteffectto", &compileSetEffectTo);
5152
}
5253

5354
void LooksBlocks::onInit(IEngine *engine)
@@ -179,6 +180,24 @@ CompilerValue *LooksBlocks::compileChangeEffectBy(Compiler *compiler)
179180
return nullptr;
180181
}
181182

183+
CompilerValue *LooksBlocks::compileSetEffectTo(Compiler *compiler)
184+
{
185+
Field *field = compiler->field("EFFECT");
186+
187+
if (!field)
188+
return nullptr;
189+
190+
auto index = getEffectIndex(compiler->engine(), field->value().toString());
191+
192+
if (index != -1) {
193+
auto indexValue = compiler->addConstValue(index);
194+
auto value = compiler->addInput("VALUE");
195+
compiler->addTargetFunctionCall("looks_seteffectto", Compiler::StaticType::Void, { Compiler::StaticType::Number, Compiler::StaticType::Number }, { indexValue, value });
196+
}
197+
198+
return nullptr;
199+
}
200+
182201
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
183202
{
184203
ctx->stackTimer()->start(duration);
@@ -238,3 +257,8 @@ extern "C" void looks_changeeffectby(Target *target, double index, double change
238257
IGraphicsEffect *effect = LooksBlocks::getEffect(target->engine(), index);
239258
target->setGraphicsEffectValue(effect, target->graphicsEffectValue(effect) + change);
240259
}
260+
261+
extern "C" void looks_seteffectto(Target *target, double index, double value)
262+
{
263+
target->setGraphicsEffectValue(LooksBlocks::getEffect(target->engine(), index), value);
264+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LooksBlocks : public IExtension
3838
static CompilerValue *compileShow(Compiler *compiler);
3939
static CompilerValue *compileHide(Compiler *compiler);
4040
static CompilerValue *compileChangeEffectBy(Compiler *compiler);
41+
static CompilerValue *compileSetEffectTo(Compiler *compiler);
4142

4243
IEngine *m_engine = nullptr;
4344
std::unordered_map<std::string, long> m_effectMap;

test/blocks/looks_blocks_test.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,94 @@ TEST_F(LooksBlocksTest, ChangeEffectBy)
404404
builder.run();
405405
}
406406
}
407+
408+
TEST_F(LooksBlocksTest, SetEffectTo)
409+
{
410+
// Fisheye
411+
{
412+
auto stage = std::make_shared<Stage>();
413+
stage->setEngine(m_engine);
414+
415+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
416+
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect("FISHEYE");
417+
ASSERT_TRUE(effect);
418+
419+
builder.addBlock("looks_seteffectto");
420+
builder.addDropdownField("EFFECT", "FISHEYE");
421+
builder.addValueInput("VALUE", 45.2);
422+
auto block = builder.currentBlock();
423+
424+
Compiler compiler(m_engine, stage.get());
425+
auto code = compiler.compile(block);
426+
Script script(stage.get(), block, m_engine);
427+
script.setCode(code);
428+
Thread thread(stage.get(), m_engine, &script);
429+
430+
stage->setGraphicsEffectValue(effect, 86.84);
431+
thread.run();
432+
ASSERT_EQ(std::round(stage->graphicsEffectValue(effect) * 100) / 100, 45.2);
433+
}
434+
435+
// Pixelate
436+
{
437+
auto stage = std::make_shared<Stage>();
438+
stage->setEngine(m_engine);
439+
440+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
441+
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect("PIXELATE");
442+
ASSERT_TRUE(effect);
443+
444+
builder.addBlock("looks_seteffectto");
445+
builder.addDropdownField("EFFECT", "PIXELATE");
446+
builder.addValueInput("VALUE", 12.05);
447+
auto block = builder.currentBlock();
448+
449+
Compiler compiler(m_engine, stage.get());
450+
auto code = compiler.compile(block);
451+
Script script(stage.get(), block, m_engine);
452+
script.setCode(code);
453+
Thread thread(stage.get(), m_engine, &script);
454+
455+
thread.run();
456+
ASSERT_EQ(std::round(stage->graphicsEffectValue(effect) * 100) / 100, 12.05);
457+
}
458+
459+
// Mosaic
460+
{
461+
auto sprite = std::make_shared<Sprite>();
462+
sprite->setEngine(m_engine);
463+
464+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
465+
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect("MOSAIC");
466+
ASSERT_TRUE(effect);
467+
468+
builder.addBlock("looks_seteffectto");
469+
builder.addDropdownField("EFFECT", "MOSAIC");
470+
builder.addValueInput("VALUE", -8.06);
471+
auto block = builder.currentBlock();
472+
473+
Compiler compiler(m_engine, sprite.get());
474+
auto code = compiler.compile(block);
475+
Script script(sprite.get(), block, m_engine);
476+
script.setCode(code);
477+
Thread thread(sprite.get(), m_engine, &script);
478+
479+
sprite->setGraphicsEffectValue(effect, 13.12);
480+
thread.run();
481+
ASSERT_EQ(std::round(sprite->graphicsEffectValue(effect) * 100) / 100, -8.06);
482+
}
483+
484+
// Invalid
485+
{
486+
auto stage = std::make_shared<Stage>();
487+
stage->setEngine(m_engine);
488+
489+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
490+
builder.addBlock("looks_seteffectto");
491+
builder.addDropdownField("EFFECT", "INVALID");
492+
builder.addValueInput("VALUE", 8.3);
493+
494+
builder.build();
495+
builder.run();
496+
}
497+
}

0 commit comments

Comments
 (0)