Skip to content

Commit 1e933d8

Browse files
committed
Implement event_whenbroadcastreceived
1 parent 258d028 commit 1e933d8

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/dev/blocks/eventblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include <scratchcpp/iengine.h>
44
#include <scratchcpp/dev/compiler.h>
5+
#include <scratchcpp/block.h>
6+
#include <scratchcpp/field.h>
7+
#include <scratchcpp/broadcast.h>
58

69
#include "eventblocks.h"
710

@@ -23,6 +26,7 @@ void EventBlocks::registerBlocks(IEngine *engine)
2326
engine->addCompileFunction(this, "event_whenflagclicked", &compileWhenFlagClicked);
2427
engine->addCompileFunction(this, "event_whenthisspriteclicked", &compileWhenThisSpriteClicked);
2528
engine->addCompileFunction(this, "event_whenstageclicked", &compileWhenStageClicked);
29+
engine->addCompileFunction(this, "event_whenbroadcastreceived", &compileWhenBroadcastReceived);
2630
}
2731

2832
CompilerValue *EventBlocks::compileWhenTouchingObject(Compiler *compiler)
@@ -48,3 +52,16 @@ CompilerValue *EventBlocks::compileWhenStageClicked(Compiler *compiler)
4852
compiler->engine()->addTargetClickScript(compiler->block());
4953
return nullptr;
5054
}
55+
56+
CompilerValue *EventBlocks::compileWhenBroadcastReceived(Compiler *compiler)
57+
{
58+
auto block = compiler->block();
59+
Field *field = compiler->field("BROADCAST_OPTION");
60+
61+
if (field) {
62+
auto broadcast = std::static_pointer_cast<Broadcast>(field->valuePtr());
63+
compiler->engine()->addBroadcastScript(block, field, broadcast.get());
64+
}
65+
66+
return nullptr;
67+
}

src/dev/blocks/eventblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class EventBlocks : public IExtension
2020
static CompilerValue *compileWhenFlagClicked(Compiler *compiler);
2121
static CompilerValue *compileWhenThisSpriteClicked(Compiler *compiler);
2222
static CompilerValue *compileWhenStageClicked(Compiler *compiler);
23+
static CompilerValue *compileWhenBroadcastReceived(Compiler *compiler);
2324
};
2425

2526
} // namespace libscratchcpp

test/dev/blocks/event_blocks_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <scratchcpp/dev/test/scriptbuilder.h>
33
#include <scratchcpp/dev/compiler.h>
44
#include <scratchcpp/sprite.h>
5+
#include <scratchcpp/broadcast.h>
6+
#include <scratchcpp/block.h>
57
#include <enginemock.h>
68

79
#include "../common.h"
@@ -80,3 +82,20 @@ TEST_F(EventBlocksTest, WhenStageClicked)
8082
EXPECT_CALL(m_engineMock, addTargetClickScript(block));
8183
compiler.compile(block);
8284
}
85+
86+
TEST_F(EventBlocksTest, WhenBroadcastReceived)
87+
{
88+
auto broadcast = std::make_shared<Broadcast>("", "");
89+
m_engine->setBroadcasts({ broadcast });
90+
91+
auto target = std::make_shared<Sprite>();
92+
ScriptBuilder builder(m_extension.get(), m_engine, target);
93+
94+
builder.addBlock("event_whenbroadcastreceived");
95+
builder.addEntityField("BROADCAST_OPTION", broadcast);
96+
auto block = builder.currentBlock();
97+
98+
Compiler compiler(&m_engineMock, target.get());
99+
EXPECT_CALL(m_engineMock, addBroadcastScript(block, block->fieldAt(0).get(), broadcast.get()));
100+
compiler.compile(block);
101+
}

0 commit comments

Comments
 (0)