Skip to content

Commit afb72ae

Browse files
committed
Implement looks_backdropnumbername block
1 parent 5fad56b commit afb72ae

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <scratchcpp/stage.h>
1212
#include <scratchcpp/costume.h>
1313
#include <scratchcpp/stringptr.h>
14+
#include <scratchcpp/string_pool.h>
1415
#include <scratchcpp/value.h>
1516
#include <scratchcpp/input.h>
1617
#include <scratchcpp/field.h>
@@ -56,6 +57,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
5657
engine->addCompileFunction(this, "looks_switchbackdropto", &compileSwitchBackdropTo);
5758
engine->addCompileFunction(this, "looks_gotofrontback", &compileGoToFrontBack);
5859
engine->addCompileFunction(this, "looks_goforwardbackwardlayers", &compileGoForwardBackwardLayers);
60+
engine->addCompileFunction(this, "looks_backdropnumbername", &compileBackdropNumberName);
5961
}
6062

6163
void LooksBlocks::onInit(IEngine *engine)
@@ -277,6 +279,23 @@ CompilerValue *LooksBlocks::compileGoForwardBackwardLayers(Compiler *compiler)
277279
return nullptr;
278280
}
279281

282+
CompilerValue *LooksBlocks::compileBackdropNumberName(Compiler *compiler)
283+
{
284+
Field *field = compiler->field("NUMBER_NAME");
285+
286+
if (!field)
287+
return nullptr;
288+
289+
const std::string &option = field->value().toString();
290+
291+
if (option == "number")
292+
return compiler->addFunctionCallWithCtx("looks_backdrop_number", Compiler::StaticType::Number);
293+
else if (option == "name")
294+
return compiler->addFunctionCallWithCtx("looks_backdrop_name", Compiler::StaticType::String);
295+
else
296+
return compiler->addConstValue(Value());
297+
}
298+
280299
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
281300
{
282301
ctx->stackTimer()->start(duration);
@@ -494,3 +513,16 @@ extern "C" void looks_move_backward_layers(ExecutionContext *ctx, double layers)
494513
Target *target = ctx->thread()->target();
495514
ctx->engine()->moveDrawableBackwardLayers(target, layers);
496515
}
516+
517+
extern "C" double looks_backdrop_number(ExecutionContext *ctx)
518+
{
519+
return ctx->engine()->stage()->costumeIndex() + 1;
520+
}
521+
522+
extern "C" StringPtr *looks_backdrop_name(ExecutionContext *ctx)
523+
{
524+
const std::string &name = ctx->engine()->stage()->currentCostume()->name();
525+
StringPtr *ret = string_pool_new();
526+
string_assign_cstring(ret, name.c_str());
527+
return ret;
528+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class LooksBlocks : public IExtension
4444
static CompilerValue *compileSwitchBackdropTo(Compiler *compiler);
4545
static CompilerValue *compileGoToFrontBack(Compiler *compiler);
4646
static CompilerValue *compileGoForwardBackwardLayers(Compiler *compiler);
47+
static CompilerValue *compileBackdropNumberName(Compiler *compiler);
4748
};
4849

4950
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,3 +1842,125 @@ TEST_F(LooksBlocksTest, GoForwardBackwardLayers_MoveStageBackward)
18421842
builder.run();
18431843
ASSERT_EQ(stage->layerOrder(), 0);
18441844
}
1845+
1846+
TEST_F(LooksBlocksTest, BackdropNumberName_SpriteBackdropNumber)
1847+
{
1848+
auto sprite = std::make_shared<Sprite>();
1849+
1850+
auto stage = std::make_shared<Stage>();
1851+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
1852+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
1853+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
1854+
stage->addCostume(backdrop1);
1855+
stage->addCostume(backdrop2);
1856+
stage->addCostume(testBackdrop);
1857+
1858+
m_engine->setTargets({ stage, sprite });
1859+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1860+
1861+
builder.addBlock("looks_backdropnumbername");
1862+
builder.addDropdownField("NUMBER_NAME", "number");
1863+
builder.captureBlockReturnValue();
1864+
builder.build();
1865+
1866+
stage->setCostumeIndex(2);
1867+
builder.run();
1868+
1869+
stage->setCostumeIndex(0);
1870+
builder.run();
1871+
1872+
List *list = builder.capturedValues();
1873+
ASSERT_EQ(list->size(), 2);
1874+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 3);
1875+
ASSERT_EQ(Value(list->data()[1]).toDouble(), 1);
1876+
}
1877+
1878+
TEST_F(LooksBlocksTest, BackdropNumberName_SpriteBackdropName)
1879+
{
1880+
auto sprite = std::make_shared<Sprite>();
1881+
1882+
auto stage = std::make_shared<Stage>();
1883+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
1884+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
1885+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
1886+
stage->addCostume(backdrop1);
1887+
stage->addCostume(backdrop2);
1888+
stage->addCostume(testBackdrop);
1889+
1890+
m_engine->setTargets({ stage, sprite });
1891+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1892+
1893+
builder.addBlock("looks_backdropnumbername");
1894+
builder.addDropdownField("NUMBER_NAME", "name");
1895+
builder.captureBlockReturnValue();
1896+
builder.build();
1897+
1898+
stage->setCostumeIndex(2);
1899+
builder.run();
1900+
1901+
stage->setCostumeIndex(0);
1902+
builder.run();
1903+
1904+
List *list = builder.capturedValues();
1905+
ASSERT_EQ(list->size(), 2);
1906+
ASSERT_EQ(Value(list->data()[0]).toString(), "test");
1907+
ASSERT_EQ(Value(list->data()[1]).toString(), "backdrop1");
1908+
}
1909+
1910+
TEST_F(LooksBlocksTest, BackdropNumberName_StageBackdropNumber)
1911+
{
1912+
auto stage = std::make_shared<Stage>();
1913+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
1914+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
1915+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
1916+
stage->addCostume(backdrop1);
1917+
stage->addCostume(backdrop2);
1918+
stage->addCostume(testBackdrop);
1919+
1920+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1921+
1922+
builder.addBlock("looks_backdropnumbername");
1923+
builder.addDropdownField("NUMBER_NAME", "number");
1924+
builder.captureBlockReturnValue();
1925+
builder.build();
1926+
1927+
stage->setCostumeIndex(2);
1928+
builder.run();
1929+
1930+
stage->setCostumeIndex(0);
1931+
builder.run();
1932+
1933+
List *list = builder.capturedValues();
1934+
ASSERT_EQ(list->size(), 2);
1935+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 3);
1936+
ASSERT_EQ(Value(list->data()[1]).toDouble(), 1);
1937+
}
1938+
1939+
TEST_F(LooksBlocksTest, BackdropNumberName_StageBackdropName)
1940+
{
1941+
auto stage = std::make_shared<Stage>();
1942+
auto backdrop1 = std::make_shared<Costume>("backdrop1", "a", "png");
1943+
auto backdrop2 = std::make_shared<Costume>("backdrop2", "b", "png");
1944+
auto testBackdrop = std::make_shared<Costume>("test", "c", "svg");
1945+
stage->addCostume(backdrop1);
1946+
stage->addCostume(backdrop2);
1947+
stage->addCostume(testBackdrop);
1948+
1949+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1950+
1951+
builder.addBlock("looks_backdropnumbername");
1952+
builder.addDropdownField("NUMBER_NAME", "name");
1953+
builder.captureBlockReturnValue();
1954+
builder.build();
1955+
1956+
stage->setCostumeIndex(2);
1957+
builder.run();
1958+
1959+
stage->setCostumeIndex(0);
1960+
builder.run();
1961+
1962+
List *list = builder.capturedValues();
1963+
ASSERT_EQ(list->size(), 2);
1964+
ASSERT_EQ(Value(list->data()[0]).toString(), "test");
1965+
ASSERT_EQ(Value(list->data()[1]).toString(), "backdrop1");
1966+
}

0 commit comments

Comments
 (0)