Skip to content

Commit 1f6f89b

Browse files
committed
Implement event_broadcastandwait
1 parent 12e6418 commit 1f6f89b

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/dev/blocks/eventblocks.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/broadcast.h>
88
#include <scratchcpp/dev/executioncontext.h>
99
#include <scratchcpp/thread.h>
10+
#include <scratchcpp/dev/compilerconstant.h>
1011

1112
#include "eventblocks.h"
1213

@@ -32,6 +33,7 @@ void EventBlocks::registerBlocks(IEngine *engine)
3233
engine->addCompileFunction(this, "event_whenbackdropswitchesto", &compileWhenBackdropSwitchesTo);
3334
engine->addCompileFunction(this, "event_whengreaterthan", &compileWhenGreaterThan);
3435
engine->addCompileFunction(this, "event_broadcast", &compileBroadcast);
36+
engine->addCompileFunction(this, "event_broadcastandwait", &compileBroadcastAndWait);
3537
}
3638

3739
CompilerValue *EventBlocks::compileWhenTouchingObject(Compiler *compiler)
@@ -91,16 +93,25 @@ CompilerValue *EventBlocks::compileWhenGreaterThan(Compiler *compiler)
9193
CompilerValue *EventBlocks::compileBroadcast(Compiler *compiler)
9294
{
9395
auto input = compiler->addInput("BROADCAST_INPUT");
94-
compiler->addFunctionCallWithCtx("event_broadcast", Compiler::StaticType::Void, { Compiler::StaticType::String }, { input });
96+
auto wait = compiler->addConstValue(false);
97+
compiler->addFunctionCallWithCtx("event_broadcast", Compiler::StaticType::Void, { Compiler::StaticType::String, Compiler::StaticType::Bool }, { input, wait });
9598
return nullptr;
9699
}
97100

98-
extern "C" void event_broadcast(ExecutionContext *ctx, const char *name)
101+
CompilerValue *EventBlocks::compileBroadcastAndWait(Compiler *compiler)
102+
{
103+
auto input = compiler->addInput("BROADCAST_INPUT");
104+
auto wait = compiler->addConstValue(true);
105+
compiler->addFunctionCallWithCtx("event_broadcast", Compiler::StaticType::Void, { Compiler::StaticType::String, Compiler::StaticType::Bool }, { input, wait });
106+
return nullptr;
107+
}
108+
109+
extern "C" void event_broadcast(ExecutionContext *ctx, const char *name, bool wait)
99110
{
100111
Thread *thread = ctx->thread();
101112
IEngine *engine = thread->engine();
102113
std::vector<int> broadcasts = engine->findBroadcasts(name);
103114

104115
for (int index : broadcasts)
105-
engine->broadcast(index, thread, false);
116+
engine->broadcast(index, thread, wait);
106117
}

src/dev/blocks/eventblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class EventBlocks : public IExtension
2424
static CompilerValue *compileWhenBackdropSwitchesTo(Compiler *compiler);
2525
static CompilerValue *compileWhenGreaterThan(Compiler *compiler);
2626
static CompilerValue *compileBroadcast(Compiler *compiler);
27+
static CompilerValue *compileBroadcastAndWait(Compiler *compiler);
2728
};
2829

2930
} // namespace libscratchcpp

test/dev/blocks/event_blocks_test.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,50 @@ TEST_F(EventBlocksTest, Broadcast)
182182
thread.run();
183183
}
184184
}
185+
186+
TEST_F(EventBlocksTest, BroadcastAndWait)
187+
{
188+
auto broadcast = std::make_shared<Broadcast>("", "test");
189+
m_engine->setBroadcasts({ broadcast });
190+
191+
auto target = std::make_shared<Sprite>();
192+
ScriptBuilder builder(m_extension.get(), m_engine, target);
193+
194+
builder.addBlock("event_broadcastandwait");
195+
builder.addEntityInput("BROADCAST_INPUT", "test", InputValue::Type::Broadcast, broadcast);
196+
auto block1 = builder.currentBlock();
197+
198+
builder.addBlock("event_broadcastandwait");
199+
builder.addNullObscuredInput("BROADCAST_INPUT");
200+
auto block2 = builder.currentBlock();
201+
202+
block1->setNext(nullptr);
203+
block2->setParent(nullptr);
204+
205+
{
206+
Compiler compiler(&m_engineMock, target.get());
207+
auto code = compiler.compile(block1);
208+
Script script(target.get(), block1, &m_engineMock);
209+
script.setCode(code);
210+
Thread thread(target.get(), &m_engineMock, &script);
211+
212+
EXPECT_CALL(m_engineMock, findBroadcasts("test")).WillOnce(Return(std::vector<int>({ 1, 4 })));
213+
EXPECT_CALL(m_engineMock, broadcast(1, &thread, true));
214+
EXPECT_CALL(m_engineMock, broadcast(4, &thread, true));
215+
thread.run();
216+
}
217+
218+
{
219+
Compiler compiler(&m_engineMock, target.get());
220+
auto code = compiler.compile(block2);
221+
Script script(target.get(), block2, &m_engineMock);
222+
script.setCode(code);
223+
Thread thread(target.get(), &m_engineMock, &script);
224+
225+
EXPECT_CALL(m_engineMock, findBroadcasts("0")).WillOnce(Return(std::vector<int>({ 5, 7, 8 })));
226+
EXPECT_CALL(m_engineMock, broadcast(5, &thread, true));
227+
EXPECT_CALL(m_engineMock, broadcast(7, &thread, true));
228+
EXPECT_CALL(m_engineMock, broadcast(8, &thread, true));
229+
thread.run();
230+
}
231+
}

0 commit comments

Comments
 (0)