Skip to content

Commit 37f3417

Browse files
committed
Implement looks_costumenumbername block
1 parent afb72ae commit 37f3417

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
5858
engine->addCompileFunction(this, "looks_gotofrontback", &compileGoToFrontBack);
5959
engine->addCompileFunction(this, "looks_goforwardbackwardlayers", &compileGoForwardBackwardLayers);
6060
engine->addCompileFunction(this, "looks_backdropnumbername", &compileBackdropNumberName);
61+
engine->addCompileFunction(this, "looks_costumenumbername", &compileCostumeNumberName);
6162
}
6263

6364
void LooksBlocks::onInit(IEngine *engine)
@@ -296,6 +297,23 @@ CompilerValue *LooksBlocks::compileBackdropNumberName(Compiler *compiler)
296297
return compiler->addConstValue(Value());
297298
}
298299

300+
CompilerValue *LooksBlocks::compileCostumeNumberName(Compiler *compiler)
301+
{
302+
Field *field = compiler->field("NUMBER_NAME");
303+
304+
if (!field)
305+
return nullptr;
306+
307+
const std::string &option = field->value().toString();
308+
309+
if (option == "number")
310+
return compiler->addTargetFunctionCall("looks_costume_number", Compiler::StaticType::Number);
311+
else if (option == "name")
312+
return compiler->addTargetFunctionCall("looks_costume_name", Compiler::StaticType::String);
313+
else
314+
return compiler->addConstValue(Value());
315+
}
316+
299317
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
300318
{
301319
ctx->stackTimer()->start(duration);
@@ -526,3 +544,16 @@ extern "C" StringPtr *looks_backdrop_name(ExecutionContext *ctx)
526544
string_assign_cstring(ret, name.c_str());
527545
return ret;
528546
}
547+
548+
extern "C" double looks_costume_number(Target *target)
549+
{
550+
return target->costumeIndex() + 1;
551+
}
552+
553+
extern "C" StringPtr *looks_costume_name(Target *target)
554+
{
555+
const std::string &name = target->currentCostume()->name();
556+
StringPtr *ret = string_pool_new();
557+
string_assign_cstring(ret, name.c_str());
558+
return ret;
559+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class LooksBlocks : public IExtension
4545
static CompilerValue *compileGoToFrontBack(Compiler *compiler);
4646
static CompilerValue *compileGoForwardBackwardLayers(Compiler *compiler);
4747
static CompilerValue *compileBackdropNumberName(Compiler *compiler);
48+
static CompilerValue *compileCostumeNumberName(Compiler *compiler);
4849
};
4950

5051
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,3 +1964,119 @@ TEST_F(LooksBlocksTest, BackdropNumberName_StageBackdropName)
19641964
ASSERT_EQ(Value(list->data()[0]).toString(), "test");
19651965
ASSERT_EQ(Value(list->data()[1]).toString(), "backdrop1");
19661966
}
1967+
1968+
TEST_F(LooksBlocksTest, CostumeNumberName_SpriteCostumeNumber)
1969+
{
1970+
auto sprite = std::make_shared<Sprite>();
1971+
auto costume1 = std::make_shared<Costume>("costume1", "a", "png");
1972+
auto costume2 = std::make_shared<Costume>("costume2", "b", "png");
1973+
auto testCostume = std::make_shared<Costume>("test", "c", "svg");
1974+
sprite->addCostume(costume1);
1975+
sprite->addCostume(costume2);
1976+
sprite->addCostume(testCostume);
1977+
1978+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1979+
1980+
builder.addBlock("looks_costumenumbername");
1981+
builder.addDropdownField("NUMBER_NAME", "number");
1982+
builder.captureBlockReturnValue();
1983+
builder.build();
1984+
1985+
sprite->setCostumeIndex(2);
1986+
builder.run();
1987+
1988+
sprite->setCostumeIndex(0);
1989+
builder.run();
1990+
1991+
List *list = builder.capturedValues();
1992+
ASSERT_EQ(list->size(), 2);
1993+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 3);
1994+
ASSERT_EQ(Value(list->data()[1]).toDouble(), 1);
1995+
}
1996+
1997+
TEST_F(LooksBlocksTest, CostumeNumberName_SpriteCostumeName)
1998+
{
1999+
auto sprite = std::make_shared<Sprite>();
2000+
auto costume1 = std::make_shared<Costume>("costume1", "a", "png");
2001+
auto costume2 = std::make_shared<Costume>("costume2", "b", "png");
2002+
auto testCostume = std::make_shared<Costume>("test", "c", "svg");
2003+
sprite->addCostume(costume1);
2004+
sprite->addCostume(costume2);
2005+
sprite->addCostume(testCostume);
2006+
2007+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
2008+
2009+
builder.addBlock("looks_costumenumbername");
2010+
builder.addDropdownField("NUMBER_NAME", "name");
2011+
builder.captureBlockReturnValue();
2012+
builder.build();
2013+
2014+
sprite->setCostumeIndex(2);
2015+
builder.run();
2016+
2017+
sprite->setCostumeIndex(0);
2018+
builder.run();
2019+
2020+
List *list = builder.capturedValues();
2021+
ASSERT_EQ(list->size(), 2);
2022+
ASSERT_EQ(Value(list->data()[0]).toString(), "test");
2023+
ASSERT_EQ(Value(list->data()[1]).toString(), "costume1");
2024+
}
2025+
2026+
TEST_F(LooksBlocksTest, CostumeNumberName_StageCostumeNumber)
2027+
{
2028+
auto stage = std::make_shared<Stage>();
2029+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
2030+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
2031+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
2032+
stage->addCostume(backdrop1);
2033+
stage->addCostume(backdrop2);
2034+
stage->addCostume(testBackdrop);
2035+
2036+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
2037+
2038+
builder.addBlock("looks_costumenumbername");
2039+
builder.addDropdownField("NUMBER_NAME", "number");
2040+
builder.captureBlockReturnValue();
2041+
builder.build();
2042+
2043+
stage->setCostumeIndex(2);
2044+
builder.run();
2045+
2046+
stage->setCostumeIndex(0);
2047+
builder.run();
2048+
2049+
List *list = builder.capturedValues();
2050+
ASSERT_EQ(list->size(), 2);
2051+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 3);
2052+
ASSERT_EQ(Value(list->data()[1]).toDouble(), 1);
2053+
}
2054+
2055+
TEST_F(LooksBlocksTest, CostumeNumberName_StageCostumeName)
2056+
{
2057+
auto stage = std::make_shared<Stage>();
2058+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
2059+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
2060+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
2061+
stage->addCostume(backdrop1);
2062+
stage->addCostume(backdrop2);
2063+
stage->addCostume(testBackdrop);
2064+
2065+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
2066+
2067+
builder.addBlock("looks_costumenumbername");
2068+
builder.addDropdownField("NUMBER_NAME", "name");
2069+
builder.captureBlockReturnValue();
2070+
builder.build();
2071+
2072+
stage->setCostumeIndex(2);
2073+
builder.run();
2074+
2075+
stage->setCostumeIndex(0);
2076+
builder.run();
2077+
2078+
List *list = builder.capturedValues();
2079+
ASSERT_EQ(list->size(), 2);
2080+
ASSERT_EQ(Value(list->data()[0]).toString(), "test");
2081+
ASSERT_EQ(Value(list->data()[1]).toString(), "backdrop1");
2082+
}

0 commit comments

Comments
 (0)