Skip to content

Commit a3b1002

Browse files
committed
Implement looks_nextcostume block
1 parent 0e631b1 commit a3b1002

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
4949
engine->addCompileFunction(this, "looks_setsizeto", &compileSetSizeTo);
5050
engine->addCompileFunction(this, "looks_size", &compileSize);
5151
engine->addCompileFunction(this, "looks_switchcostumeto", &compileSwitchCostumeTo);
52+
engine->addCompileFunction(this, "looks_nextcostume", &compileNextCostume);
5253
}
5354

5455
void LooksBlocks::onInit(IEngine *engine)
@@ -211,6 +212,12 @@ CompilerValue *LooksBlocks::compileSwitchCostumeTo(Compiler *compiler)
211212
return nullptr;
212213
}
213214

215+
CompilerValue *LooksBlocks::compileNextCostume(Compiler *compiler)
216+
{
217+
compiler->addTargetFunctionCall("looks_nextcostume");
218+
return nullptr;
219+
}
220+
214221
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
215222
{
216223
ctx->stackTimer()->start(duration);

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class LooksBlocks : public IExtension
4040
static CompilerValue *compileSetSizeTo(Compiler *compiler);
4141
static CompilerValue *compileSize(Compiler *compiler);
4242
static CompilerValue *compileSwitchCostumeTo(Compiler *compiler);
43+
static CompilerValue *compileNextCostume(Compiler *compiler);
4344
};
4445

4546
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,3 +911,58 @@ TEST_F(LooksBlocksTest, SwitchCostumeTo)
911911
ASSERT_EQ(stage->costumeIndex(), 0);
912912
}
913913
}
914+
915+
TEST_F(LooksBlocksTest, NextCostume)
916+
{
917+
{
918+
auto sprite = std::make_shared<Sprite>();
919+
auto costume1 = std::make_shared<Costume>("costume1", "a", "png");
920+
auto costume2 = std::make_shared<Costume>("costume2", "b", "png");
921+
auto testCostume = std::make_shared<Costume>("test", "c", "svg");
922+
sprite->addCostume(costume1);
923+
sprite->addCostume(costume2);
924+
sprite->addCostume(testCostume);
925+
926+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
927+
928+
builder.addBlock("looks_nextcostume");
929+
builder.build();
930+
931+
sprite->setCostumeIndex(0);
932+
builder.run();
933+
ASSERT_EQ(sprite->costumeIndex(), 1);
934+
935+
builder.run();
936+
ASSERT_EQ(sprite->costumeIndex(), 2);
937+
938+
builder.run();
939+
ASSERT_EQ(sprite->costumeIndex(), 0);
940+
}
941+
942+
m_engine->clear();
943+
944+
{
945+
auto stage = std::make_shared<Stage>();
946+
auto costume1 = std::make_shared<Costume>("costume1", "a", "png");
947+
auto costume2 = std::make_shared<Costume>("costume2", "b", "png");
948+
auto testCostume = std::make_shared<Costume>("test", "c", "svg");
949+
stage->addCostume(costume1);
950+
stage->addCostume(costume2);
951+
stage->addCostume(testCostume);
952+
953+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
954+
955+
builder.addBlock("looks_nextcostume");
956+
builder.build();
957+
958+
stage->setCostumeIndex(0);
959+
builder.run();
960+
ASSERT_EQ(stage->costumeIndex(), 1);
961+
962+
builder.run();
963+
ASSERT_EQ(stage->costumeIndex(), 2);
964+
965+
builder.run();
966+
ASSERT_EQ(stage->costumeIndex(), 0);
967+
}
968+
}

0 commit comments

Comments
 (0)