Skip to content

Commit 715a95e

Browse files
committed
Implement event_whenbackropswitchesto
1 parent 1e933d8 commit 715a95e

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/dev/blocks/eventblocks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void EventBlocks::registerBlocks(IEngine *engine)
2727
engine->addCompileFunction(this, "event_whenthisspriteclicked", &compileWhenThisSpriteClicked);
2828
engine->addCompileFunction(this, "event_whenstageclicked", &compileWhenStageClicked);
2929
engine->addCompileFunction(this, "event_whenbroadcastreceived", &compileWhenBroadcastReceived);
30+
engine->addCompileFunction(this, "event_whenbackdropswitchesto", &compileWhenBackdropSwitchesTo);
3031
}
3132

3233
CompilerValue *EventBlocks::compileWhenTouchingObject(Compiler *compiler)
@@ -65,3 +66,14 @@ CompilerValue *EventBlocks::compileWhenBroadcastReceived(Compiler *compiler)
6566

6667
return nullptr;
6768
}
69+
70+
CompilerValue *EventBlocks::compileWhenBackdropSwitchesTo(Compiler *compiler)
71+
{
72+
auto block = compiler->block();
73+
Field *field = compiler->field("BACKDROP");
74+
75+
if (field)
76+
compiler->engine()->addBackdropChangeScript(block, field);
77+
78+
return nullptr;
79+
}

src/dev/blocks/eventblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class EventBlocks : public IExtension
2121
static CompilerValue *compileWhenThisSpriteClicked(Compiler *compiler);
2222
static CompilerValue *compileWhenStageClicked(Compiler *compiler);
2323
static CompilerValue *compileWhenBroadcastReceived(Compiler *compiler);
24+
static CompilerValue *compileWhenBackdropSwitchesTo(Compiler *compiler);
2425
};
2526

2627
} // namespace libscratchcpp

test/dev/blocks/event_blocks_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,17 @@ TEST_F(EventBlocksTest, WhenBroadcastReceived)
9999
EXPECT_CALL(m_engineMock, addBroadcastScript(block, block->fieldAt(0).get(), broadcast.get()));
100100
compiler.compile(block);
101101
}
102+
103+
TEST_F(EventBlocksTest, WhenBackdropSwitchesTo)
104+
{
105+
auto target = std::make_shared<Sprite>();
106+
ScriptBuilder builder(m_extension.get(), m_engine, target);
107+
108+
builder.addBlock("event_whenbackdropswitchesto");
109+
builder.addDropdownField("BACKDROP", "backdrop2");
110+
auto block = builder.currentBlock();
111+
112+
Compiler compiler(&m_engineMock, target.get());
113+
EXPECT_CALL(m_engineMock, addBackdropChangeScript(block, block->fieldAt(0).get()));
114+
compiler.compile(block);
115+
}

0 commit comments

Comments
 (0)