Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/internal/scratch3reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ bool Scratch3Reader::load()

// TODO: Add comments

// currentCostume
READER_STEP(step, "target -> currentCostume");
target->setCurrentCostume(jsonTarget["currentCostume"]);

// costumes
READER_STEP(step, "target -> costumes");
auto costumes = jsonTarget["costumes"];
Expand All @@ -218,6 +214,10 @@ bool Scratch3Reader::load()
target->addCostume(costume);
}

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

// sounds
READER_STEP(step, "target -> sounds");
auto sounds = jsonTarget["sounds"];
Expand Down
3 changes: 2 additions & 1 deletion src/scratch/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ int Target::currentCostume() const
/*! Sets the ID of the current costume. */
void Target::setCurrentCostume(int newCostume)
{
impl->currentCostume = newCostume;
if (newCostume > 0 && newCostume <= costumes().size())
impl->currentCostume = newCostume;
}

/*! Returns the list of costumes. */
Expand Down
2 changes: 1 addition & 1 deletion src/scratch/target_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct TargetPrivate
std::vector<std::shared_ptr<Variable>> variables;
std::vector<std::shared_ptr<List>> lists;
std::vector<std::shared_ptr<Block>> blocks;
int currentCostume = 1;
int currentCostume = 0;
std::vector<std::shared_ptr<Costume>> costumes;
std::vector<std::shared_ptr<Sound>> sounds;
int layerOrder = 0;
Expand Down
3 changes: 2 additions & 1 deletion test/load_project/load_project_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST(LoadProjectTest, EmptyProject)
ASSERT_EQ(stage->blocks().size(), 0);
ASSERT_EQ(stage->costumes().size(), 1);
// TODO: Add comments
ASSERT_EQ(stage->currentCostume(), 0);
ASSERT_EQ(stage->currentCostume(), 1);
ASSERT_EQ(stage->sounds().size(), 0);
ASSERT_EQ(stage->layerOrder(), 0);
ASSERT_EQ(stage->volume(), 100);
Expand Down Expand Up @@ -104,6 +104,7 @@ TEST(LoadProjectTest, LoadTestProject)
ASSERT_EQ(sprite1->lists().size(), 1);
ASSERT_EQ(sprite1->blocks().size(), 18);
ASSERT_EQ(sprite1->costumes().size(), 2);
ASSERT_EQ(sprite1->currentCostume(), 2);
ASSERT_EQ(sprite1->sounds().size(), 1);
ASSERT_TRUE(sprite1->visible());
ASSERT_EQ(sprite1->x(), 0);
Expand Down
2 changes: 2 additions & 0 deletions test/scratch_classes/sprite_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ TEST(SpriteTest, Clone)
sprite.addList(list1);
sprite.addList(list2);

sprite.addCostume(std::make_shared<Costume>("", "", ""));
sprite.addCostume(std::make_shared<Costume>("", "", ""));
sprite.setCurrentCostume(2);
sprite.setLayerOrder(5);
sprite.setVolume(50);
Expand Down
26 changes: 24 additions & 2 deletions test/scratch_classes/target_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,31 @@ TEST(TargetTest, Blocks)
TEST(TargetTest, CurrentCostume)
{
Target target;
ASSERT_EQ(target.currentCostume(), 0);

target.setCurrentCostume(1);
ASSERT_EQ(target.currentCostume(), 0);

target.setCurrentCostume(2);
ASSERT_EQ(target.currentCostume(), 0);

target.addCostume(std::make_shared<Costume>("", "", ""));
ASSERT_EQ(target.currentCostume(), 0);

target.setCurrentCostume(1);
ASSERT_EQ(target.currentCostume(), 1);

target.setCurrentCostume(2);
ASSERT_EQ(target.currentCostume(), 1);

target.addCostume(std::make_shared<Costume>("", "", ""));
ASSERT_EQ(target.currentCostume(), 1);
target.setCurrentCostume(5);
ASSERT_EQ(target.currentCostume(), 5);

target.setCurrentCostume(2);
ASSERT_EQ(target.currentCostume(), 2);

target.setCurrentCostume(3);
ASSERT_EQ(target.currentCostume(), 2);
}

TEST(TargetTest, Costumes)
Expand Down