Skip to content

Commit e1afa60

Browse files
committed
Get and set costumes by index
1 parent d3c71da commit e1afa60

File tree

14 files changed

+197
-190
lines changed

14 files changed

+197
-190
lines changed

include/scratchcpp/sprite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class LIBSCRATCHCPP_EXPORT Sprite : public Target
5050
double size() const;
5151
void setSize(double newSize);
5252

53-
void setCurrentCostume(int newCostume);
53+
void setCostumeIndex(int newCostumeIndex);
5454

5555
double direction() const;
5656
void setDirection(double newDirection);

include/scratchcpp/target.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class LIBSCRATCHCPP_EXPORT Target
5050
int findBlock(const std::string &id) const;
5151
std::vector<std::shared_ptr<Block>> greenFlagBlocks() const;
5252

53-
int currentCostume() const;
54-
void setCurrentCostume(int newCostume);
53+
int costumeIndex() const;
54+
void setCostumeIndex(int newCostumeIndex);
5555

5656
const std::vector<std::shared_ptr<Costume>> &costumes() const;
5757
int addCostume(std::shared_ptr<Costume> costume);

src/blocks/looksblocks.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ void LooksBlocks::setCostumeByIndex(Target *target, long index)
653653
index = std::fmod(index, costumeCount);
654654
}
655655

656-
target->setCurrentCostume(index + 1);
656+
target->setCostumeIndex(index);
657657
}
658658

659659
unsigned int LooksBlocks::switchCostumeToByIndex(VirtualMachine *vm)
@@ -693,15 +693,15 @@ unsigned int LooksBlocks::switchCostumeTo(VirtualMachine *vm)
693693
unsigned int LooksBlocks::nextCostume(VirtualMachine *vm)
694694
{
695695
if (Target *target = vm->target())
696-
setCostumeByIndex(target, target->currentCostume());
696+
setCostumeByIndex(target, target->costumeIndex() + 1);
697697

698698
return 0;
699699
}
700700

701701
unsigned int LooksBlocks::previousCostume(VirtualMachine *vm)
702702
{
703703
if (Target *target = vm->target())
704-
setCostumeByIndex(target, target->currentCostume() - 2);
704+
setCostumeByIndex(target, target->costumeIndex() - 1);
705705

706706
return 0;
707707
}
@@ -710,7 +710,8 @@ void LooksBlocks::startBackdropScripts(VirtualMachine *vm, bool wait)
710710
{
711711
if (Stage *stage = vm->engine()->stage()) {
712712
if (stage->costumes().size() > 0)
713-
vm->engine()->broadcastByPtr(stage->costumeAt(stage->currentCostume() - 1)->broadcast(), vm, wait);
713+
// TODO: Use currentCostume()
714+
vm->engine()->broadcastByPtr(stage->costumeAt(stage->costumeIndex())->broadcast(), vm, wait);
714715
}
715716
}
716717

@@ -749,13 +750,13 @@ void LooksBlocks::switchBackdropToImpl(VirtualMachine *vm)
749750
void LooksBlocks::nextBackdropImpl(VirtualMachine *vm)
750751
{
751752
if (Stage *stage = vm->engine()->stage())
752-
setCostumeByIndex(stage, stage->currentCostume());
753+
setCostumeByIndex(stage, stage->costumeIndex() + 1);
753754
}
754755

755756
void LooksBlocks::previousBackdropImpl(VirtualMachine *vm)
756757
{
757758
if (Stage *stage = vm->engine()->stage())
758-
setCostumeByIndex(stage, stage->currentCostume() - 2);
759+
setCostumeByIndex(stage, stage->costumeIndex() - 1);
759760
}
760761

761762
void LooksBlocks::randomBackdropImpl(VirtualMachine *vm)
@@ -767,7 +768,7 @@ void LooksBlocks::randomBackdropImpl(VirtualMachine *vm)
767768
std::size_t count = stage->costumes().size();
768769

769770
if (count > 0)
770-
stage->setCurrentCostume(rng->randint(1, count));
771+
stage->setCostumeIndex(rng->randint(0, count - 1));
771772
}
772773
}
773774

@@ -854,7 +855,8 @@ unsigned int LooksBlocks::randomBackdropAndWait(VirtualMachine *vm)
854855
unsigned int LooksBlocks::checkBackdropScripts(VirtualMachine *vm)
855856
{
856857
if (Stage *stage = vm->engine()->stage()) {
857-
if ((stage->costumes().size() > 0) && vm->engine()->broadcastByPtrRunning(stage->costumeAt(stage->currentCostume() - 1)->broadcast(), vm))
858+
// TODO: Use currentCostume()
859+
if ((stage->costumes().size() > 0) && vm->engine()->broadcastByPtrRunning(stage->costumeAt(stage->costumeIndex())->broadcast(), vm))
858860
vm->stop(true, true, true);
859861
}
860862

@@ -864,7 +866,7 @@ unsigned int LooksBlocks::checkBackdropScripts(VirtualMachine *vm)
864866
unsigned int LooksBlocks::costumeNumber(VirtualMachine *vm)
865867
{
866868
if (Target *target = vm->target())
867-
vm->addReturnValue(target->currentCostume());
869+
vm->addReturnValue(target->costumeIndex() + 1);
868870
else
869871
vm->addReturnValue(0);
870872

@@ -874,7 +876,8 @@ unsigned int LooksBlocks::costumeNumber(VirtualMachine *vm)
874876
unsigned int LooksBlocks::costumeName(VirtualMachine *vm)
875877
{
876878
if (Target *target = vm->target()) {
877-
auto costume = target->costumeAt(target->currentCostume() - 1);
879+
// TODO: Use currentCostume()
880+
auto costume = target->costumeAt(target->costumeIndex());
878881

879882
if (costume)
880883
vm->addReturnValue(costume->name());
@@ -889,7 +892,7 @@ unsigned int LooksBlocks::costumeName(VirtualMachine *vm)
889892
unsigned int LooksBlocks::backdropNumber(VirtualMachine *vm)
890893
{
891894
if (Stage *stage = vm->engine()->stage())
892-
vm->addReturnValue(stage->currentCostume());
895+
vm->addReturnValue(stage->costumeIndex() + 1);
893896
else
894897
vm->addReturnValue(0);
895898

@@ -899,7 +902,8 @@ unsigned int LooksBlocks::backdropNumber(VirtualMachine *vm)
899902
unsigned int LooksBlocks::backdropName(VirtualMachine *vm)
900903
{
901904
if (Stage *stage = vm->engine()->stage()) {
902-
auto costume = stage->costumeAt(stage->currentCostume() - 1);
905+
// TODO: Use currentCostume()
906+
auto costume = stage->costumeAt(stage->costumeIndex());
903907

904908
if (costume)
905909
vm->addReturnValue(costume->name());

src/blocks/sensingblocks.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ unsigned int SensingBlocks::costumeNumberOfSprite(VirtualMachine *vm)
475475
Sprite *sprite = dynamic_cast<Sprite *>(target);
476476

477477
if (sprite)
478-
vm->replaceReturnValue(sprite->currentCostume(), 1);
478+
vm->replaceReturnValue(sprite->costumeIndex() + 1, 1);
479479
else
480480
vm->replaceReturnValue(0, 1);
481481

@@ -488,7 +488,7 @@ unsigned int SensingBlocks::costumeNumberOfSpriteByIndex(VirtualMachine *vm)
488488
Sprite *sprite = dynamic_cast<Sprite *>(target);
489489

490490
if (sprite)
491-
vm->replaceReturnValue(sprite->currentCostume(), 1);
491+
vm->replaceReturnValue(sprite->costumeIndex() + 1, 1);
492492
else
493493
vm->replaceReturnValue(0, 1);
494494

@@ -501,7 +501,8 @@ unsigned int SensingBlocks::costumeNameOfSprite(VirtualMachine *vm)
501501
Sprite *sprite = dynamic_cast<Sprite *>(target);
502502

503503
if (sprite)
504-
vm->replaceReturnValue(sprite->costumeAt(sprite->currentCostume() - 1)->name(), 1);
504+
// TODO: Use currentCostume()
505+
vm->replaceReturnValue(sprite->costumeAt(sprite->costumeIndex())->name(), 1);
505506
else
506507
vm->replaceReturnValue(0, 1);
507508

@@ -514,7 +515,8 @@ unsigned int SensingBlocks::costumeNameOfSpriteByIndex(VirtualMachine *vm)
514515
Sprite *sprite = dynamic_cast<Sprite *>(target);
515516

516517
if (sprite)
517-
vm->replaceReturnValue(sprite->costumeAt(sprite->currentCostume() - 1)->name(), 1);
518+
// TODO: Use currentCostume()
519+
vm->replaceReturnValue(sprite->costumeAt(sprite->costumeIndex())->name(), 1);
518520
else
519521
vm->replaceReturnValue(0, 1);
520522

@@ -577,7 +579,7 @@ unsigned int SensingBlocks::backdropNumberOfStage(VirtualMachine *vm)
577579
Stage *stage = dynamic_cast<Stage *>(target);
578580

579581
if (stage)
580-
vm->replaceReturnValue(stage->currentCostume(), 1);
582+
vm->replaceReturnValue(stage->costumeIndex() + 1, 1);
581583
else
582584
vm->replaceReturnValue(0, 1);
583585

@@ -590,7 +592,7 @@ unsigned int SensingBlocks::backdropNumberOfStageByIndex(VirtualMachine *vm)
590592
Stage *stage = dynamic_cast<Stage *>(target);
591593

592594
if (stage)
593-
vm->replaceReturnValue(stage->currentCostume(), 1);
595+
vm->replaceReturnValue(stage->costumeIndex() + 1, 1);
594596
else
595597
vm->replaceReturnValue(0, 1);
596598

@@ -603,7 +605,8 @@ unsigned int SensingBlocks::backdropNameOfStage(VirtualMachine *vm)
603605
Stage *stage = dynamic_cast<Stage *>(target);
604606

605607
if (stage)
606-
vm->replaceReturnValue(stage->costumeAt(stage->currentCostume() - 1)->name(), 1);
608+
// TODO: Use currentCostume()
609+
vm->replaceReturnValue(stage->costumeAt(stage->costumeIndex())->name(), 1);
607610
else
608611
vm->replaceReturnValue(0, 1);
609612

@@ -616,7 +619,8 @@ unsigned int SensingBlocks::backdropNameOfStageByIndex(VirtualMachine *vm)
616619
Stage *stage = dynamic_cast<Stage *>(target);
617620

618621
if (stage)
619-
vm->replaceReturnValue(stage->costumeAt(stage->currentCostume() - 1)->name(), 1);
622+
// TODO: Use currentCostume()
623+
vm->replaceReturnValue(stage->costumeAt(stage->costumeIndex())->name(), 1);
620624
else
621625
vm->replaceReturnValue(0, 1);
622626

src/internal/scratch3reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ bool Scratch3Reader::load()
216216

217217
// currentCostume
218218
READER_STEP(step, "target -> currentCostume");
219-
target->setCurrentCostume(jsonToValue(jsonTarget["currentCostume"]).toInt() + 1);
219+
target->setCostumeIndex(jsonToValue(jsonTarget["currentCostume"]).toInt());
220220

221221
// sounds
222222
READER_STEP(step, "target -> sounds");

src/scratch/sprite.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ std::shared_ptr<Sprite> Sprite::clone()
7676
for (auto list : l)
7777
clone->addList(list->clone());
7878

79-
clone->setCurrentCostume(currentCostume());
79+
clone->setCostumeIndex(costumeIndex());
8080
clone->setLayerOrder(layerOrder());
8181
clone->setVolume(volume());
8282
clone->setEngine(engine());
@@ -197,17 +197,16 @@ void Sprite::setSize(double newSize)
197197
impl->iface->onSizeChanged(impl->size);
198198

199199
// TODO: Make currentCostume() return the costume (not index)
200-
auto costume = costumeAt(currentCostume() - 1);
200+
auto costume = costumeAt(costumeIndex());
201201

202202
if (costume)
203203
costume->setScale(newSize / 100);
204204
}
205205

206-
/*! Overrides Target#setCurrentCostume(). */
207-
void Sprite::setCurrentCostume(int newCostume)
206+
/*! Overrides Target#setCostumeIndex(). */
207+
void Sprite::setCostumeIndex(int newCostumeIndex)
208208
{
209-
// TODO: Make currentCostume() return the costume (not index)
210-
auto costume = costumeAt(newCostume - 1);
209+
auto costume = costumeAt(newCostumeIndex);
211210

212211
if (costume) {
213212
costume->setScale(impl->size / 100);
@@ -217,7 +216,7 @@ void Sprite::setCurrentCostume(int newCostume)
217216
costume->setGraphicsEffectValue(effect, value);
218217
}
219218

220-
Target::setCurrentCostume(newCostume);
219+
Target::setCostumeIndex(newCostumeIndex);
221220
}
222221

223222
/*! Returns the direction. */
@@ -277,7 +276,7 @@ void Sprite::setRotationStyle(RotationStyle newRotationStyle)
277276
{
278277
impl->rotationStyle = newRotationStyle;
279278
// TODO: Make currentCostume() return the costume (not index)
280-
auto costume = costumeAt(currentCostume() - 1);
279+
auto costume = costumeAt(costumeIndex());
281280

282281
if (costume)
283282
costume->setMirrorHorizontally(newRotationStyle == RotationStyle::LeftRight);
@@ -329,7 +328,7 @@ void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
329328
impl->graphicsEffects[effect] = value;
330329

331330
// TODO: Make currentCostume() return the costume (not index)
332-
auto costume = costumeAt(currentCostume() - 1);
331+
auto costume = costumeAt(costumeIndex());
333332

334333
if (costume)
335334
costume->setGraphicsEffectValue(effect, value);
@@ -341,7 +340,7 @@ void Sprite::clearGraphicsEffects()
341340
impl->graphicsEffects.clear();
342341

343342
// TODO: Make currentCostume() return the costume (not index)
344-
auto costume = costumeAt(currentCostume() - 1);
343+
auto costume = costumeAt(costumeIndex());
345344

346345
if (costume)
347346
costume->clearGraphicsEffects();

src/scratch/sprite_p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void SpritePrivate::getBoundingRect(Rect *out) const
4242
assert(out);
4343
assert(sprite);
4444
// TODO: Make currentCostume() return the costume
45-
auto costume = sprite->costumeAt(sprite->currentCostume() - 1);
45+
auto costume = sprite->costumeAt(sprite->costumeIndex());
4646

4747
if (!costume) {
4848
out->setLeft(x);

src/scratch/target.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,17 @@ std::vector<std::shared_ptr<Block>> Target::greenFlagBlocks() const
198198
return ret;
199199
}
200200

201-
/*! Returns the ID of the current costume. */
202-
int Target::currentCostume() const
201+
/*! Returns the index of the current costume. */
202+
int Target::costumeIndex() const
203203
{
204-
return impl->currentCostume;
204+
return impl->costumeIndex;
205205
}
206206

207-
/*! Sets the ID of the current costume. */
208-
void Target::setCurrentCostume(int newCostume)
207+
/*! Sets the index of the current costume. */
208+
void Target::setCostumeIndex(int newCostumeIndex)
209209
{
210-
if (newCostume > 0 && newCostume <= costumes().size())
211-
impl->currentCostume = newCostume;
210+
if (newCostumeIndex >= 0 && newCostumeIndex < costumes().size())
211+
impl->costumeIndex = newCostumeIndex;
212212
}
213213

214214
/*! Returns the list of costumes. */

src/scratch/target_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct TargetPrivate
2626
std::vector<std::shared_ptr<Variable>> variables;
2727
std::vector<std::shared_ptr<List>> lists;
2828
std::vector<std::shared_ptr<Block>> blocks;
29-
int currentCostume = 0;
29+
int costumeIndex = -1;
3030
std::vector<std::shared_ptr<Costume>> costumes;
3131
std::vector<std::shared_ptr<Sound>> sounds;
3232
int layerOrder = 0;

0 commit comments

Comments
 (0)