@@ -350,117 +350,147 @@ unsigned int LooksBlocks::previousCostume(VirtualMachine *vm)
350350 return 0 ;
351351}
352352
353- void LooksBlocks::startBackdropScripts (VirtualMachine *vm)
353+ void LooksBlocks::startBackdropScripts (VirtualMachine *vm, bool wait )
354354{
355- if (Stage *stage = vm->engine ()->stage ())
356- vm->engine ()->broadcastByPtr (stage->costumeAt (stage->currentCostume () - 1 )->broadcast (), vm, true );
355+ if (Stage *stage = vm->engine ()->stage ()) {
356+ if (stage->costumes ().size () > 0 )
357+ vm->engine ()->broadcastByPtr (stage->costumeAt (stage->currentCostume () - 1 )->broadcast (), vm, wait);
358+ }
357359}
358360
359- unsigned int LooksBlocks::switchBackdropToByIndex (VirtualMachine *vm)
361+ void LooksBlocks::switchBackdropToByIndexImpl (VirtualMachine *vm)
360362{
361363 if (Stage *stage = vm->engine ()->stage ())
362364 setCostumeByIndex (stage, vm->getInput (0 , 1 )->toLong ());
363-
364- return 1 ;
365365}
366366
367- unsigned int LooksBlocks::switchBackdropTo (VirtualMachine *vm)
367+ void LooksBlocks::switchBackdropToImpl (VirtualMachine *vm)
368368{
369369 Stage *stage = vm->engine ()->stage ();
370370
371371 if (!stage)
372- return 1 ;
372+ return ;
373373
374374 const Value *name = vm->getInput (0 , 1 );
375375 std::string nameStr = name->toString ();
376376 int index = stage->findCostume (nameStr);
377377
378378 if (index == -1 ) {
379379 if (nameStr == " next backdrop" )
380- nextBackdrop (vm);
380+ nextBackdropImpl (vm);
381381 else if (nameStr == " previous backdrop" )
382- previousBackdrop (vm);
382+ previousBackdropImpl (vm);
383383 else if (nameStr == " random backdrop" ) {
384- randomBackdrop (vm);
384+ randomBackdropImpl (vm);
385385 } else {
386386 if (name->type () == Value::Type::Integer)
387387 setCostumeByIndex (stage, name->toLong () - 1 );
388388 }
389389 } else
390390 setCostumeByIndex (stage, index);
391+ }
391392
392- return 1 ;
393+ void LooksBlocks::nextBackdropImpl (VirtualMachine *vm)
394+ {
395+ if (Stage *stage = vm->engine ()->stage ())
396+ setCostumeByIndex (stage, stage->currentCostume ());
393397}
394398
395- unsigned int LooksBlocks::switchBackdropToByIndexAndWait (VirtualMachine *vm)
399+ void LooksBlocks::previousBackdropImpl (VirtualMachine *vm)
396400{
401+ if (Stage *stage = vm->engine ()->stage ())
402+ setCostumeByIndex (stage, stage->currentCostume () - 2 );
403+ }
404+
405+ void LooksBlocks::randomBackdropImpl (VirtualMachine *vm)
406+ {
407+ if (!rng)
408+ rng = RandomGenerator::instance ().get ();
409+
397410 if (Stage *stage = vm->engine ()->stage ()) {
398- setCostumeByIndex (stage, vm->getInput (0 , 1 )->toLong ());
399- startBackdropScripts (vm);
411+ std::size_t count = stage->costumes ().size ();
412+
413+ if (count > 0 )
414+ stage->setCurrentCostume (rng->randint (1 , count));
400415 }
416+ }
417+
418+ unsigned int LooksBlocks::switchBackdropToByIndex (VirtualMachine *vm)
419+ {
420+ switchBackdropToByIndexImpl (vm);
421+ startBackdropScripts (vm, false );
422+
423+ return 1 ;
424+ }
425+
426+ unsigned int LooksBlocks::switchBackdropTo (VirtualMachine *vm)
427+ {
428+ switchBackdropToImpl (vm);
429+ startBackdropScripts (vm, false );
430+
431+ return 1 ;
432+ }
433+
434+ unsigned int LooksBlocks::switchBackdropToByIndexAndWait (VirtualMachine *vm)
435+ {
436+ switchBackdropToByIndexImpl (vm);
437+ startBackdropScripts (vm, true );
401438
402439 return 1 ;
403440}
404441
405442unsigned int LooksBlocks::switchBackdropToAndWait (VirtualMachine *vm)
406443{
407- switchBackdropTo (vm);
408- startBackdropScripts (vm);
444+ switchBackdropToImpl (vm);
445+ startBackdropScripts (vm, true );
409446
410447 return 1 ;
411448}
412449
413450unsigned int LooksBlocks::nextBackdrop (VirtualMachine *vm)
414451{
415- if (Stage *stage = vm-> engine ()-> stage ())
416- setCostumeByIndex (stage, stage-> currentCostume () );
452+ nextBackdropImpl (vm);
453+ startBackdropScripts (vm, false );
417454
418455 return 0 ;
419456}
420457
421458unsigned int LooksBlocks::nextBackdropAndWait (VirtualMachine *vm)
422459{
423- nextBackdrop (vm);
424- startBackdropScripts (vm);
460+ nextBackdropImpl (vm);
461+ startBackdropScripts (vm, true );
425462
426463 return 0 ;
427464}
428465
429466unsigned int LooksBlocks::previousBackdrop (VirtualMachine *vm)
430467{
431- if (Stage *stage = vm-> engine ()-> stage ())
432- setCostumeByIndex (stage, stage-> currentCostume () - 2 );
468+ previousBackdropImpl (vm);
469+ startBackdropScripts (vm, false );
433470
434471 return 0 ;
435472}
436473
437474unsigned int LooksBlocks::previousBackdropAndWait (VirtualMachine *vm)
438475{
439- previousBackdrop (vm);
440- startBackdropScripts (vm);
476+ previousBackdropImpl (vm);
477+ startBackdropScripts (vm, true );
441478
442479 return 0 ;
443480}
444481
445482unsigned int LooksBlocks::randomBackdrop (VirtualMachine *vm)
446483{
447- if (!rng)
448- rng = RandomGenerator::instance ().get ();
449-
450- if (Stage *stage = vm->engine ()->stage ()) {
451- std::size_t count = stage->costumes ().size ();
452-
453- if (count > 0 )
454- stage->setCurrentCostume (rng->randint (1 , count));
455- }
484+ randomBackdropImpl (vm);
485+ startBackdropScripts (vm, false );
456486
457487 return 0 ;
458488}
459489
460490unsigned int LooksBlocks::randomBackdropAndWait (VirtualMachine *vm)
461491{
462- randomBackdrop (vm);
463- startBackdropScripts (vm);
492+ randomBackdropImpl (vm);
493+ startBackdropScripts (vm, true );
464494
465495 return 0 ;
466496}
0 commit comments