Skip to content

Commit 19283ed

Browse files
committed
Load comments
1 parent a148c4b commit 19283ed

File tree

5 files changed

+113
-2
lines changed

5 files changed

+113
-2
lines changed

src/engine/internal/engine.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <scratchcpp/block.h>
1414
#include <scratchcpp/variable.h>
1515
#include <scratchcpp/list.h>
16+
#include <scratchcpp/comment.h>
1617
#include <scratchcpp/costume.h>
1718
#include <scratchcpp/keyevent.h>
1819
#include <cassert>
@@ -84,6 +85,14 @@ void Engine::resolveIds()
8485

8586
block->updateInputMap();
8687
block->updateFieldMap();
88+
89+
auto comment = getComment(block->commentId());
90+
block->setComment(comment);
91+
92+
if (comment) {
93+
comment->setBlock(block);
94+
assert(comment->blockId() == block->id());
95+
}
8796
}
8897
}
8998
}
@@ -1078,6 +1087,21 @@ std::shared_ptr<Broadcast> Engine::getBroadcast(const std::string &id)
10781087
return nullptr;
10791088
}
10801089

1090+
// Returns the comment with the given ID.
1091+
std::shared_ptr<Comment> Engine::getComment(const std::string &id)
1092+
{
1093+
if (id.empty())
1094+
return nullptr;
1095+
1096+
for (auto target : m_targets) {
1097+
int index = target->findComment(id);
1098+
if (index != -1)
1099+
return target->commentAt(index);
1100+
}
1101+
1102+
return nullptr;
1103+
}
1104+
10811105
// Returns the entity with the given ID. \see IEntity
10821106
std::shared_ptr<Entity> Engine::getEntity(const std::string &id)
10831107
{

src/engine/internal/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class Engine : public IEngine
143143
std::shared_ptr<Variable> getVariable(const std::string &id);
144144
std::shared_ptr<List> getList(const std::string &id);
145145
std::shared_ptr<Broadcast> getBroadcast(const std::string &id);
146+
std::shared_ptr<Comment> getComment(const std::string &id);
146147
std::shared_ptr<Entity> getEntity(const std::string &id);
147148
std::shared_ptr<IBlockSection> blockSection(const std::string &opcode) const;
148149

src/internal/scratch3reader.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <scratchcpp/variable.h>
99
#include <scratchcpp/list.h>
1010
#include <scratchcpp/costume.h>
11+
#include <scratchcpp/comment.h>
1112
#include <scratchcpp/sound.h>
1213
#include <scratchcpp/stage.h>
1314
#include <scratchcpp/sprite.h>
@@ -188,10 +189,37 @@ bool Scratch3Reader::load()
188189
READER_STEP(step, "target -> block -> shadow");
189190
block->setShadow(blockInfo["shadow"]);
190191

192+
// comment
193+
READER_STEP(step, "target -> block -> comment");
194+
if (!blockInfo["comment"].is_null())
195+
block->setCommentId(blockInfo["comment"]);
196+
191197
target->addBlock(block);
192198
}
193199

194-
// TODO: Add comments
200+
// comments
201+
READER_STEP(step, "target -> comments");
202+
auto comments = jsonTarget["comments"];
203+
for (json::iterator it = comments.begin(); it != comments.end(); ++it) {
204+
auto commentInfo = it.value();
205+
READER_STEP(step, "target -> comment -> { id, x, y }");
206+
auto comment = std::make_shared<Comment>(it.key(), jsonToValue(commentInfo["x"]).toDouble(), jsonToValue(commentInfo["y"]).toDouble());
207+
READER_STEP(step, "target -> comment -> blockId");
208+
209+
if (!commentInfo["blockId"].is_null())
210+
comment->setBlockId(commentInfo["blockId"]);
211+
212+
READER_STEP(step, "target -> comment -> width");
213+
comment->setWidth(jsonToValue(commentInfo["width"]).toDouble());
214+
READER_STEP(step, "target -> comment -> height");
215+
comment->setHeight(jsonToValue(commentInfo["height"]).toDouble());
216+
READER_STEP(step, "target -> comment -> minimized");
217+
comment->setMinimized(commentInfo["minimized"]);
218+
READER_STEP(step, "target -> comment -> text");
219+
comment->setText(commentInfo["text"]);
220+
221+
target->addComment(comment);
222+
}
195223

196224
// costumes
197225
READER_STEP(step, "target -> costumes");

test/load_project/load_project_test.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <scratchcpp/sound.h>
1212
#include <scratchcpp/sprite.h>
1313
#include <scratchcpp/inputvalue.h>
14+
#include <scratchcpp/comment.h>
1415

1516
#include "../common.h"
1617

@@ -39,7 +40,7 @@ TEST(LoadProjectTest, EmptyProject)
3940
ASSERT_EQ(stage->lists().size(), 0);
4041
ASSERT_EQ(stage->blocks().size(), 0);
4142
ASSERT_EQ(stage->costumes().size(), 1);
42-
// TODO: Add comments
43+
ASSERT_TRUE(stage->comments().empty());
4344
ASSERT_EQ(stage->costumeIndex(), 0);
4445
ASSERT_EQ(stage->sounds().size(), 0);
4546
ASSERT_EQ(stage->layerOrder(), 0);
@@ -82,9 +83,22 @@ TEST(LoadProjectTest, LoadTestProject)
8283

8384
// Stage
8485
Stage *stage = engine->stage();
86+
ASSERT_EQ(stage->comments().size(), 1);
8587
ASSERT_EQ(stage->variables().size(), 2);
8688
ASSERT_EQ(stage->lists().size(), 1);
8789

90+
// Stage: comments
91+
auto comment = stage->commentAt(0);
92+
ASSERT_EQ(comment->id(), "y");
93+
ASSERT_EQ(comment->blockId(), "");
94+
ASSERT_EQ(comment->block(), nullptr);
95+
ASSERT_EQ(comment->x(), 202.96296296296296);
96+
ASSERT_EQ(comment->y(), 293.3333333333335);
97+
ASSERT_EQ(comment->width(), 124.4444580078125);
98+
ASSERT_EQ(comment->height(), 114.0740966796875);
99+
ASSERT_EQ(comment->minimized(), false);
100+
ASSERT_EQ(comment->text(), "TEST");
101+
88102
// Stage: variables
89103
ASSERT_VAR(stage, "var1");
90104
ASSERT_EQ(GET_VAR(stage, "var1")->value().toString(), "Hello, world!");
@@ -104,6 +118,7 @@ TEST(LoadProjectTest, LoadTestProject)
104118
ASSERT_EQ(sprite1->lists().size(), 1);
105119
ASSERT_EQ(sprite1->blocks().size(), 18);
106120
ASSERT_EQ(sprite1->costumes().size(), 2);
121+
ASSERT_EQ(sprite1->comments().size(), 1);
107122
ASSERT_EQ(sprite1->costumeIndex(), 1);
108123
ASSERT_EQ(sprite1->sounds().size(), 1);
109124
ASSERT_TRUE(sprite1->visible());
@@ -115,6 +130,18 @@ TEST(LoadProjectTest, LoadTestProject)
115130
ASSERT_EQ(sprite1->rotationStyleStr(), "all around");
116131
ASSERT_EQ(sprite1->rotationStyle(), Sprite::RotationStyle::AllAround);
117132

133+
// Sprite1: comments
134+
comment = sprite1->commentAt(0);
135+
ASSERT_EQ(comment->id(), "z");
136+
ASSERT_EQ(comment->blockId(), "");
137+
ASSERT_EQ(comment->block(), nullptr);
138+
ASSERT_EQ(comment->x(), 509.6296296296298);
139+
ASSERT_EQ(comment->y(), 386.66666666666674);
140+
ASSERT_EQ(comment->width(), 171.85186767578125);
141+
ASSERT_EQ(comment->height(), 97.77777099609375);
142+
ASSERT_EQ(comment->minimized(), false);
143+
ASSERT_EQ(comment->text(), "Hello, world!");
144+
118145
// Sprite1: sounds
119146
auto sound = sprite1->soundAt(0);
120147
ASSERT_EQ(sound->name(), "Meow");
@@ -160,6 +187,8 @@ TEST(LoadProjectTest, LoadTestProject)
160187
ASSERT_EQ(GET_FIELD(block, "VARIABLE")->valuePtr(), GET_VAR(stage, "var1"));
161188
ASSERT_INPUT(block, "VALUE");
162189
ASSERT_EQ(GET_INPUT(block, "VALUE")->primaryValue()->value().toInt(), 0);
190+
ASSERT_TRUE(block->commentId().empty());
191+
ASSERT_EQ(block->comment(), nullptr);
163192
block = block->next();
164193
ASSERT_TRUE(block);
165194

@@ -184,6 +213,8 @@ TEST(LoadProjectTest, LoadTestProject)
184213
ASSERT_EQ(GET_INPUT(block, prototype->argumentIds()[0])->primaryValue()->value().toString(), "test");
185214
ASSERT_INPUT(block, prototype->argumentIds()[1]);
186215
ASSERT_TRUE(GET_INPUT(block, prototype->argumentIds()[1])->valueBlock());
216+
ASSERT_TRUE(block->commentId().empty());
217+
ASSERT_EQ(block->comment(), nullptr);
187218

188219
ASSERT_FALSE(block->next());
189220

@@ -234,6 +265,7 @@ TEST(LoadProjectTest, LoadTestProject)
234265
ASSERT_EQ(sprite2->lists().size(), 1);
235266
ASSERT_EQ(sprite2->blocks().size(), 5);
236267
ASSERT_EQ(sprite2->costumes().size(), 3);
268+
ASSERT_EQ(sprite2->comments().size(), 2);
237269
ASSERT_EQ(sprite2->sounds().size(), 1);
238270
ASSERT_FALSE(sprite2->visible());
239271
ASSERT_EQ(std::round(sprite2->x()), 143);
@@ -244,6 +276,32 @@ TEST(LoadProjectTest, LoadTestProject)
244276
ASSERT_EQ(sprite2->rotationStyleStr(), "don't rotate");
245277
ASSERT_EQ(sprite2->rotationStyle(), Sprite::RotationStyle::DoNotRotate);
246278

279+
// Balloon1: comments
280+
comment = sprite2->commentAt(0);
281+
ASSERT_EQ(comment->id(), "A");
282+
ASSERT_EQ(comment->blockId(), "");
283+
ASSERT_EQ(comment->block(), nullptr);
284+
ASSERT_EQ(comment->x(), 459.25925925925924);
285+
ASSERT_EQ(comment->y(), -45.925925925925924);
286+
ASSERT_EQ(comment->width(), 200);
287+
ASSERT_EQ(comment->height(), 200);
288+
ASSERT_EQ(comment->minimized(), true);
289+
ASSERT_EQ(comment->text(), "test");
290+
291+
comment = sprite2->commentAt(1);
292+
auto commentBlock = sprite2->blockAt(sprite2->findBlock("e"));
293+
ASSERT_EQ(comment->id(), "w");
294+
ASSERT_EQ(comment->blockId(), "e");
295+
ASSERT_EQ(comment->block(), commentBlock);
296+
ASSERT_EQ(comment->x(), 247.3981475830078);
297+
ASSERT_EQ(comment->y(), 208);
298+
ASSERT_EQ(comment->width(), 189.62969970703125);
299+
ASSERT_EQ(comment->height(), 93.33334350585938);
300+
ASSERT_EQ(comment->minimized(), false);
301+
ASSERT_EQ(comment->text(), "...");
302+
ASSERT_EQ(commentBlock->commentId(), "w");
303+
ASSERT_EQ(commentBlock->comment(), comment);
304+
247305
// Balloon1: sounds
248306
sound = sprite2->soundAt(0);
249307
ASSERT_EQ(sound->name(), "Pop");

test/load_test.sb3

-367 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)