Skip to content

Commit c91cba6

Browse files
committed
Implement looks_switchbackdroptoandwait block
1 parent 37f3417 commit c91cba6

File tree

3 files changed

+760
-0
lines changed

3 files changed

+760
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/istacktimer.h>
88
#include <scratchcpp/irandomgenerator.h>
99
#include <scratchcpp/thread.h>
10+
#include <scratchcpp/promise.h>
1011
#include <scratchcpp/sprite.h>
1112
#include <scratchcpp/stage.h>
1213
#include <scratchcpp/costume.h>
@@ -59,6 +60,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
5960
engine->addCompileFunction(this, "looks_goforwardbackwardlayers", &compileGoForwardBackwardLayers);
6061
engine->addCompileFunction(this, "looks_backdropnumbername", &compileBackdropNumberName);
6162
engine->addCompileFunction(this, "looks_costumenumbername", &compileCostumeNumberName);
63+
engine->addCompileFunction(this, "looks_switchbackdroptoandwait", &compileSwitchBackdropToAndWait);
6264
}
6365

6466
void LooksBlocks::onInit(IEngine *engine)
@@ -314,6 +316,21 @@ CompilerValue *LooksBlocks::compileCostumeNumberName(Compiler *compiler)
314316
return compiler->addConstValue(Value());
315317
}
316318

319+
CompilerValue *LooksBlocks::compileSwitchBackdropToAndWait(Compiler *compiler)
320+
{
321+
auto backdrop = compiler->addInput("BACKDROP");
322+
auto wait = compiler->addConstValue(true);
323+
compiler->addFunctionCallWithCtx("looks_switchbackdropto", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { backdrop });
324+
compiler->addFunctionCallWithCtx("looks_start_backdrop_scripts", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { wait });
325+
326+
auto hasBackdrops = compiler->addFunctionCallWithCtx("looks_backdrop_promise", Compiler::StaticType::Bool);
327+
compiler->beginIfStatement(hasBackdrops);
328+
compiler->createYield();
329+
compiler->endIf();
330+
331+
return nullptr;
332+
}
333+
317334
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
318335
{
319336
ctx->stackTimer()->start(duration);
@@ -557,3 +574,13 @@ extern "C" StringPtr *looks_costume_name(Target *target)
557574
string_assign_cstring(ret, name.c_str());
558575
return ret;
559576
}
577+
578+
extern "C" bool looks_backdrop_promise(ExecutionContext *ctx)
579+
{
580+
if (ctx->engine()->stage()->costumes().size() > 0) {
581+
ctx->setPromise(std::make_shared<Promise>());
582+
return true;
583+
}
584+
585+
return false;
586+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class LooksBlocks : public IExtension
4646
static CompilerValue *compileGoForwardBackwardLayers(Compiler *compiler);
4747
static CompilerValue *compileBackdropNumberName(Compiler *compiler);
4848
static CompilerValue *compileCostumeNumberName(Compiler *compiler);
49+
static CompilerValue *compileSwitchBackdropToAndWait(Compiler *compiler);
4950
};
5051

5152
} // namespace libscratchcpp

0 commit comments

Comments
 (0)