Skip to content

Commit 90b566c

Browse files
committed
Implement control_wait_until
1 parent 880e30e commit 90b566c

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

src/dev/blocks/controlblocks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void ControlBlocks::registerBlocks(IEngine *engine)
3232
engine->addCompileFunction(this, "control_if_else", &compileIfElse);
3333
engine->addCompileFunction(this, "control_stop", &compileStop);
3434
engine->addCompileFunction(this, "control_wait", &compileWait);
35+
engine->addCompileFunction(this, "control_wait_until", &compileWaitUntil);
3536
}
3637

3738
CompilerValue *ControlBlocks::compileForever(Compiler *compiler)
@@ -96,6 +97,14 @@ CompilerValue *ControlBlocks::compileWait(Compiler *compiler)
9697
return nullptr;
9798
}
9899

100+
CompilerValue *ControlBlocks::compileWaitUntil(Compiler *compiler)
101+
{
102+
compiler->beginLoopCondition();
103+
compiler->beginRepeatUntilLoop(compiler->addInput("CONDITION"));
104+
compiler->endLoop();
105+
return nullptr;
106+
}
107+
99108
extern "C" void control_stop_all(ExecutionContext *ctx)
100109
{
101110
ctx->engine()->stop();

src/dev/blocks/controlblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ControlBlocks : public IExtension
2222
static CompilerValue *compileIfElse(Compiler *compiler);
2323
static CompilerValue *compileStop(Compiler *compiler);
2424
static CompilerValue *compileWait(Compiler *compiler);
25+
static CompilerValue *compileWaitUntil(Compiler *compiler);
2526
};
2627

2728
} // namespace libscratchcpp

test/dev/blocks/control_blocks_test.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,65 @@ TEST_F(ControlBlocksTest, Wait)
410410
ASSERT_TRUE(code->isFinished(ctx.get()));
411411
}
412412
}
413+
414+
TEST_F(ControlBlocksTest, WaitUntil)
415+
{
416+
auto target = std::make_shared<Sprite>();
417+
418+
{
419+
ScriptBuilder builder(m_extension.get(), m_engine, target);
420+
421+
builder.addBlock("control_wait_until");
422+
builder.addValueInput("CONDITION", false);
423+
builder.build();
424+
m_engine->start();
425+
426+
m_engine->step();
427+
ASSERT_TRUE(m_engine->isRunning());
428+
429+
m_engine->step();
430+
ASSERT_TRUE(m_engine->isRunning());
431+
}
432+
433+
m_engine->clear();
434+
target = std::make_shared<Sprite>();
435+
436+
{
437+
ScriptBuilder builder(m_extension.get(), m_engine, target);
438+
439+
builder.addBlock("control_wait_until");
440+
builder.addValueInput("CONDITION", true);
441+
builder.build();
442+
m_engine->start();
443+
444+
m_engine->step();
445+
m_engine->step();
446+
ASSERT_FALSE(m_engine->isRunning());
447+
}
448+
449+
m_engine->clear();
450+
target = std::make_shared<Sprite>();
451+
452+
{
453+
ScriptBuilder builder(m_extension.get(), m_engine, target);
454+
455+
builder.addBlock("control_wait_until");
456+
auto block = std::make_shared<Block>("", "test_condition");
457+
builder.addObscuredInput("CONDITION", block);
458+
builder.build();
459+
460+
conditionReturnValue = false;
461+
m_engine->start();
462+
463+
m_engine->step();
464+
ASSERT_TRUE(m_engine->isRunning());
465+
466+
m_engine->step();
467+
ASSERT_TRUE(m_engine->isRunning());
468+
469+
conditionReturnValue = true;
470+
m_engine->step();
471+
m_engine->step();
472+
ASSERT_FALSE(m_engine->isRunning());
473+
}
474+
}

test/dev/blocks/util.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ void registerBlocks(IEngine *engine, IExtension *extension)
2828
compiler->addFunctionCall("test_print", Compiler::StaticType::Void, { Compiler::StaticType::String }, { input });
2929
return nullptr;
3030
});
31+
32+
engine->addCompileFunction(extension, "test_condition", [](Compiler *compiler) -> CompilerValue * { return compiler->addFunctionCall("test_condition", Compiler::StaticType::Bool); });
3133
}
3234

3335
extern "C" void test_print(const char *str)
3436
{
3537
std::cout << str << std::endl;
3638
}
3739

40+
extern "C" bool test_condition()
41+
{
42+
return conditionReturnValue;
43+
}
44+
3845
} // namespace libscratchcpp

test/dev/blocks/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace libscratchcpp
66
class IEngine;
77
class IExtension;
88

9+
bool conditionReturnValue = false;
10+
911
void registerBlocks(IEngine *engine, IExtension *extension);
1012

1113
} // namespace libscratchcpp

0 commit comments

Comments
 (0)