Skip to content

Commit 33193cc

Browse files
committed
Implement looks_nextbackdrop block
1 parent c91cba6 commit 33193cc

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
6161
engine->addCompileFunction(this, "looks_backdropnumbername", &compileBackdropNumberName);
6262
engine->addCompileFunction(this, "looks_costumenumbername", &compileCostumeNumberName);
6363
engine->addCompileFunction(this, "looks_switchbackdroptoandwait", &compileSwitchBackdropToAndWait);
64+
engine->addCompileFunction(this, "looks_nextbackdrop", &compileNextBackdrop);
6465
}
6566

6667
void LooksBlocks::onInit(IEngine *engine)
@@ -331,6 +332,12 @@ CompilerValue *LooksBlocks::compileSwitchBackdropToAndWait(Compiler *compiler)
331332
return nullptr;
332333
}
333334

335+
CompilerValue *LooksBlocks::compileNextBackdrop(Compiler *compiler)
336+
{
337+
compiler->addFunctionCallWithCtx("looks_nextbackdrop");
338+
return nullptr;
339+
}
340+
334341
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
335342
{
336343
ctx->stackTimer()->start(duration);
@@ -584,3 +591,8 @@ extern "C" bool looks_backdrop_promise(ExecutionContext *ctx)
584591

585592
return false;
586593
}
594+
595+
extern "C" void looks_nextbackdrop(ExecutionContext *ctx)
596+
{
597+
looks_nextcostume(ctx->engine()->stage());
598+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class LooksBlocks : public IExtension
4747
static CompilerValue *compileBackdropNumberName(Compiler *compiler);
4848
static CompilerValue *compileCostumeNumberName(Compiler *compiler);
4949
static CompilerValue *compileSwitchBackdropToAndWait(Compiler *compiler);
50+
static CompilerValue *compileNextBackdrop(Compiler *compiler);
5051
};
5152

5253
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,3 +2812,67 @@ TEST_F(LooksBlocksTest, SwitchBackdropToAndWait_Stage)
28122812
thread.run();
28132813
ASSERT_TRUE(thread.isFinished());
28142814
}
2815+
2816+
TEST_F(LooksBlocksTest, NextBackdrop_Sprite)
2817+
{
2818+
auto sprite = std::make_shared<Sprite>();
2819+
auto costume1 = std::make_shared<Costume>("costume1", "a", "png");
2820+
auto testCostume = std::make_shared<Costume>("test", "c", "svg");
2821+
sprite->addCostume(costume1);
2822+
sprite->addCostume(testCostume);
2823+
2824+
auto stage = std::make_shared<Stage>();
2825+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
2826+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
2827+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
2828+
stage->addCostume(backdrop1);
2829+
stage->addCostume(backdrop2);
2830+
stage->addCostume(testBackdrop);
2831+
2832+
m_engine->setTargets({ stage, sprite });
2833+
2834+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
2835+
2836+
builder.addBlock("looks_nextbackdrop");
2837+
builder.build();
2838+
2839+
sprite->setCostumeIndex(0);
2840+
stage->setCostumeIndex(0);
2841+
builder.run();
2842+
ASSERT_EQ(sprite->costumeIndex(), 0);
2843+
ASSERT_EQ(stage->costumeIndex(), 1);
2844+
2845+
builder.run();
2846+
ASSERT_EQ(sprite->costumeIndex(), 0);
2847+
ASSERT_EQ(stage->costumeIndex(), 2);
2848+
2849+
builder.run();
2850+
ASSERT_EQ(sprite->costumeIndex(), 0);
2851+
ASSERT_EQ(stage->costumeIndex(), 0);
2852+
}
2853+
2854+
TEST_F(LooksBlocksTest, NextBackdrop_Stage)
2855+
{
2856+
auto stage = std::make_shared<Stage>();
2857+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
2858+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
2859+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
2860+
stage->addCostume(backdrop1);
2861+
stage->addCostume(backdrop2);
2862+
stage->addCostume(testBackdrop);
2863+
2864+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
2865+
2866+
builder.addBlock("looks_nextbackdrop");
2867+
builder.build();
2868+
2869+
stage->setCostumeIndex(0);
2870+
builder.run();
2871+
ASSERT_EQ(stage->costumeIndex(), 1);
2872+
2873+
builder.run();
2874+
ASSERT_EQ(stage->costumeIndex(), 2);
2875+
2876+
builder.run();
2877+
ASSERT_EQ(stage->costumeIndex(), 0);
2878+
}

0 commit comments

Comments
 (0)