Skip to content

Commit d6b9fe6

Browse files
committed
Implement event_whenthisspriteclicked block
1 parent de07290 commit d6b9fe6

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/blocks/eventblocks.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void EventBlocks::registerBlocks(IEngine *engine)
2222
{
2323
// Blocks
2424
engine->addCompileFunction(this, "event_whenflagclicked", &compileWhenFlagClicked);
25+
engine->addCompileFunction(this, "event_whenthisspriteclicked", &compileWhenThisSpriteClicked);
2526
engine->addCompileFunction(this, "event_broadcast", &compileBroadcast);
2627
engine->addCompileFunction(this, "event_broadcastandwait", &compileBroadcastAndWait);
2728
engine->addCompileFunction(this, "event_whenbroadcastreceived", &compileWhenBroadcastReceived);
@@ -42,6 +43,11 @@ void EventBlocks::compileWhenFlagClicked(Compiler *compiler)
4243
compiler->engine()->addGreenFlagScript(compiler->block());
4344
}
4445

46+
void EventBlocks::compileWhenThisSpriteClicked(Compiler *compiler)
47+
{
48+
compiler->engine()->addTargetClickScript(compiler->block());
49+
}
50+
4551
void EventBlocks::compileBroadcast(Compiler *compiler)
4652
{
4753
auto input = compiler->input(BROADCAST_INPUT);

src/blocks/eventblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class EventBlocks : public IBlockSection
3131
void registerBlocks(IEngine *engine) override;
3232

3333
static void compileWhenFlagClicked(Compiler *compiler);
34+
static void compileWhenThisSpriteClicked(Compiler *compiler);
3435
static void compileBroadcast(Compiler *compiler);
3536
static void compileBroadcastAndWait(Compiler *compiler);
3637
static void compileWhenBroadcastReceived(Compiler *compiler);

test/blocks/event_blocks_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ TEST_F(EventBlocksTest, RegisterBlocks)
8484
{
8585
// Blocks
8686
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenflagclicked", &EventBlocks::compileWhenFlagClicked));
87+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenthisspriteclicked", &EventBlocks::compileWhenThisSpriteClicked));
8788
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_broadcast", &EventBlocks::compileBroadcast));
8889
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_broadcastandwait", &EventBlocks::compileBroadcastAndWait));
8990
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenbroadcastreceived", &EventBlocks::compileWhenBroadcastReceived));
@@ -121,6 +122,26 @@ TEST_F(EventBlocksTest, WhenFlagClicked)
121122
ASSERT_TRUE(compiler.lists().empty());
122123
}
123124

125+
TEST_F(EventBlocksTest, WhenThisSpriteClicked)
126+
{
127+
Compiler compiler(&m_engineMock);
128+
129+
auto block = createEventBlock("a", "event_whenthisspriteclicked");
130+
131+
compiler.init();
132+
133+
EXPECT_CALL(m_engineMock, addTargetClickScript(block));
134+
compiler.setBlock(block);
135+
EventBlocks::compileWhenThisSpriteClicked(&compiler);
136+
137+
compiler.end();
138+
139+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_HALT }));
140+
ASSERT_TRUE(compiler.constValues().empty());
141+
ASSERT_TRUE(compiler.variables().empty());
142+
ASSERT_TRUE(compiler.lists().empty());
143+
}
144+
124145
TEST_F(EventBlocksTest, Broadcast)
125146
{
126147
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)