Skip to content

Commit fb4a422

Browse files
committed
Implement event_whenkeypressed
1 parent 1f6f89b commit fb4a422

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
@@ -34,6 +34,7 @@ void EventBlocks::registerBlocks(IEngine *engine)
3434
engine->addCompileFunction(this, "event_whengreaterthan", &compileWhenGreaterThan);
3535
engine->addCompileFunction(this, "event_broadcast", &compileBroadcast);
3636
engine->addCompileFunction(this, "event_broadcastandwait", &compileBroadcastAndWait);
37+
engine->addCompileFunction(this, "event_whenkeypressed", &compileWhenKeyPressed);
3738
}
3839

3940
CompilerValue *EventBlocks::compileWhenTouchingObject(Compiler *compiler)
@@ -106,6 +107,17 @@ CompilerValue *EventBlocks::compileBroadcastAndWait(Compiler *compiler)
106107
return nullptr;
107108
}
108109

110+
CompilerValue *EventBlocks::compileWhenKeyPressed(Compiler *compiler)
111+
{
112+
auto block = compiler->block();
113+
Field *field = compiler->field("KEY_OPTION");
114+
115+
if (field)
116+
compiler->engine()->addKeyPressScript(block, field);
117+
118+
return nullptr;
119+
}
120+
109121
extern "C" void event_broadcast(ExecutionContext *ctx, const char *name, bool wait)
110122
{
111123
Thread *thread = ctx->thread();

src/dev/blocks/eventblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class EventBlocks : public IExtension
2525
static CompilerValue *compileWhenGreaterThan(Compiler *compiler);
2626
static CompilerValue *compileBroadcast(Compiler *compiler);
2727
static CompilerValue *compileBroadcastAndWait(Compiler *compiler);
28+
static CompilerValue *compileWhenKeyPressed(Compiler *compiler);
2829
};
2930

3031
} // namespace libscratchcpp

test/dev/blocks/event_blocks_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,17 @@ TEST_F(EventBlocksTest, BroadcastAndWait)
229229
thread.run();
230230
}
231231
}
232+
233+
TEST_F(EventBlocksTest, WhenKeyPressed)
234+
{
235+
auto target = std::make_shared<Sprite>();
236+
ScriptBuilder builder(m_extension.get(), m_engine, target);
237+
238+
builder.addBlock("event_whenkeypressed");
239+
builder.addDropdownField("KEY_OPTION", "a");
240+
auto block = builder.currentBlock();
241+
242+
Compiler compiler(&m_engineMock, target.get());
243+
EXPECT_CALL(m_engineMock, addKeyPressScript(block, block->fieldAt(0).get()));
244+
compiler.compile(block);
245+
}

0 commit comments

Comments
 (0)