Skip to content

Commit 0fef828

Browse files
committed
Implement looks_switchbackdroptoandwait block
1 parent fda3e02 commit 0fef828

File tree

3 files changed

+564
-0
lines changed

3 files changed

+564
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
3131
engine->addCompileFunction(this, "looks_switchcostumeto", &compileSwitchCostumeTo);
3232
engine->addCompileFunction(this, "looks_nextcostume", &compileNextCostume);
3333
engine->addCompileFunction(this, "looks_switchbackdropto", &compileSwitchBackdropTo);
34+
engine->addCompileFunction(this, "looks_switchbackdroptoandwait", &compileSwitchBackdropToAndWait);
3435
engine->addCompileFunction(this, "looks_nextbackdrop", &compileNextBackdrop);
3536
engine->addCompileFunction(this, "looks_costumenumbername", &compileCostumeNumberName);
3637
engine->addCompileFunction(this, "looks_backdropnumbername", &compileBackdropNumberName);
@@ -157,6 +158,47 @@ void LooksBlocks::compileSwitchBackdropTo(Compiler *compiler)
157158
}
158159
}
159160

161+
void LooksBlocks::compileSwitchBackdropToAndWait(Compiler *compiler)
162+
{
163+
Stage *stage = compiler->engine()->stage();
164+
165+
if (!stage)
166+
return;
167+
168+
Input *input = compiler->input(BACKDROP);
169+
170+
if (input->type() != Input::Type::ObscuredShadow) {
171+
assert(input->pointsToDropdownMenu());
172+
std::string value = input->selectedMenuItem();
173+
int index = stage->findCostume(value);
174+
175+
if (index == -1) {
176+
if (value == "next backdrop")
177+
compiler->addFunctionCall(&nextBackdropAndWait);
178+
else if (value == "previous backdrop")
179+
compiler->addFunctionCall(&previousBackdropAndWait);
180+
else if (value == "random backdrop")
181+
compiler->addFunctionCall(&randomBackdropAndWait);
182+
else {
183+
Value v(value);
184+
185+
if (v.type() == Value::Type::Integer) {
186+
compiler->addConstValue(v.toLong() - 1);
187+
compiler->addFunctionCall(&switchBackdropToByIndexAndWait);
188+
}
189+
}
190+
} else {
191+
compiler->addConstValue(index);
192+
compiler->addFunctionCall(&switchBackdropToByIndexAndWait);
193+
}
194+
} else {
195+
compiler->addInput(input);
196+
compiler->addFunctionCall(&switchBackdropToAndWait);
197+
}
198+
199+
compiler->addFunctionCall(&checkBackdropScripts);
200+
}
201+
160202
void LooksBlocks::compileNextBackdrop(Compiler *compiler)
161203
{
162204
compiler->addFunctionCall(&nextBackdrop);
@@ -308,6 +350,12 @@ unsigned int LooksBlocks::previousCostume(VirtualMachine *vm)
308350
return 0;
309351
}
310352

353+
void LooksBlocks::startBackdropScripts(VirtualMachine *vm)
354+
{
355+
if (Stage *stage = vm->engine()->stage())
356+
vm->engine()->broadcastByPtr(stage->costumeAt(stage->currentCostume() - 1)->broadcast(), vm, true);
357+
}
358+
311359
unsigned int LooksBlocks::switchBackdropToByIndex(VirtualMachine *vm)
312360
{
313361
if (Stage *stage = vm->engine()->stage())
@@ -344,6 +392,24 @@ unsigned int LooksBlocks::switchBackdropTo(VirtualMachine *vm)
344392
return 1;
345393
}
346394

395+
unsigned int LooksBlocks::switchBackdropToByIndexAndWait(VirtualMachine *vm)
396+
{
397+
if (Stage *stage = vm->engine()->stage()) {
398+
setCostumeByIndex(stage, vm->getInput(0, 1)->toLong());
399+
startBackdropScripts(vm);
400+
}
401+
402+
return 1;
403+
}
404+
405+
unsigned int LooksBlocks::switchBackdropToAndWait(VirtualMachine *vm)
406+
{
407+
switchBackdropTo(vm);
408+
startBackdropScripts(vm);
409+
410+
return 1;
411+
}
412+
347413
unsigned int LooksBlocks::nextBackdrop(VirtualMachine *vm)
348414
{
349415
if (Stage *stage = vm->engine()->stage())
@@ -352,6 +418,14 @@ unsigned int LooksBlocks::nextBackdrop(VirtualMachine *vm)
352418
return 0;
353419
}
354420

421+
unsigned int LooksBlocks::nextBackdropAndWait(VirtualMachine *vm)
422+
{
423+
nextBackdrop(vm);
424+
startBackdropScripts(vm);
425+
426+
return 0;
427+
}
428+
355429
unsigned int LooksBlocks::previousBackdrop(VirtualMachine *vm)
356430
{
357431
if (Stage *stage = vm->engine()->stage())
@@ -360,6 +434,14 @@ unsigned int LooksBlocks::previousBackdrop(VirtualMachine *vm)
360434
return 0;
361435
}
362436

437+
unsigned int LooksBlocks::previousBackdropAndWait(VirtualMachine *vm)
438+
{
439+
previousBackdrop(vm);
440+
startBackdropScripts(vm);
441+
442+
return 0;
443+
}
444+
363445
unsigned int LooksBlocks::randomBackdrop(VirtualMachine *vm)
364446
{
365447
if (!rng)
@@ -375,6 +457,24 @@ unsigned int LooksBlocks::randomBackdrop(VirtualMachine *vm)
375457
return 0;
376458
}
377459

460+
unsigned int LooksBlocks::randomBackdropAndWait(VirtualMachine *vm)
461+
{
462+
randomBackdrop(vm);
463+
startBackdropScripts(vm);
464+
465+
return 0;
466+
}
467+
468+
unsigned int LooksBlocks::checkBackdropScripts(VirtualMachine *vm)
469+
{
470+
Stage *stage = vm->engine()->stage();
471+
472+
if (stage && vm->engine()->broadcastByPtrRunning(stage->costumeAt(stage->currentCostume() - 1)->broadcast(), vm))
473+
vm->stop(true, true, true);
474+
475+
return 0;
476+
}
477+
378478
unsigned int LooksBlocks::costumeNumber(VirtualMachine *vm)
379479
{
380480
if (Target *target = vm->target())

src/blocks/looksblocks.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace libscratchcpp
88
{
99

1010
class Target;
11+
class Stage;
12+
class Value;
1113
class IRandomGenerator;
1214

1315
/*! \brief The LooksBlocks class contains the implementation of looks blocks. */
@@ -45,6 +47,7 @@ class LooksBlocks : public IBlockSection
4547
static void compileSwitchCostumeTo(Compiler *compiler);
4648
static void compileNextCostume(Compiler *compiler);
4749
static void compileSwitchBackdropTo(Compiler *compiler);
50+
static void compileSwitchBackdropToAndWait(Compiler *compiler);
4851
static void compileNextBackdrop(Compiler *compiler);
4952
static void compileCostumeNumberName(Compiler *compiler);
5053
static void compileBackdropNumberName(Compiler *compiler);
@@ -61,11 +64,18 @@ class LooksBlocks : public IBlockSection
6164
static unsigned int nextCostume(VirtualMachine *vm);
6265
static unsigned int previousCostume(VirtualMachine *vm);
6366

67+
static void startBackdropScripts(VirtualMachine *vm);
6468
static unsigned int switchBackdropToByIndex(VirtualMachine *vm);
6569
static unsigned int switchBackdropTo(VirtualMachine *vm);
70+
static unsigned int switchBackdropToByIndexAndWait(VirtualMachine *vm);
71+
static unsigned int switchBackdropToAndWait(VirtualMachine *vm);
6672
static unsigned int nextBackdrop(VirtualMachine *vm);
73+
static unsigned int nextBackdropAndWait(VirtualMachine *vm);
6774
static unsigned int previousBackdrop(VirtualMachine *vm);
75+
static unsigned int previousBackdropAndWait(VirtualMachine *vm);
6876
static unsigned int randomBackdrop(VirtualMachine *vm);
77+
static unsigned int randomBackdropAndWait(VirtualMachine *vm);
78+
static unsigned int checkBackdropScripts(VirtualMachine *vm);
6979

7080
static unsigned int costumeNumber(VirtualMachine *vm);
7181
static unsigned int costumeName(VirtualMachine *vm);

0 commit comments

Comments
 (0)