Skip to content

Commit 01c544f

Browse files
committed
Add top level block position API
1 parent 319055d commit 01c544f

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

include/scratchcpp/block.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
5656

5757
bool topLevel() const;
5858

59+
int x() const;
60+
void setX(int x);
61+
62+
int y() const;
63+
void setY(int y);
64+
5965
std::shared_ptr<Comment> comment() const;
6066
const std::string &commentId() const;
6167
void setComment(std::shared_ptr<Comment> comment);

src/scratch/block.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,36 @@ bool Block::topLevel() const
313313
return (impl->parentId == "" && !impl->parent);
314314
}
315315

316+
/*! Returns the X-coordinate of the block in the code area (only for top level blocks). */
317+
int Block::x() const
318+
{
319+
return impl->x;
320+
}
321+
322+
/*! Sets the X-coordinate of the block in the code area (only for top level blocks). */
323+
void Block::setX(int x)
324+
{
325+
if (!topLevel())
326+
std::cout << "warning: setting X-coordinate of a block which is not a top level block" << std::endl;
327+
328+
impl->x = x;
329+
}
330+
331+
/*! Returns the Y-coordinate of the block in the code area (only for top level blocks). */
332+
int Block::y() const
333+
{
334+
return impl->y;
335+
}
336+
337+
/*! Sets the Y-coordinate of the block in the code area (only for top level blocks). */
338+
void Block::setY(int y)
339+
{
340+
if (!topLevel())
341+
std::cout << "warning: setting Y-coordinate of a block which is not a top level block" << std::endl;
342+
343+
impl->y = y;
344+
}
345+
316346
/*! Returns the comment which is attached to this block. */
317347
std::shared_ptr<Comment> Block::comment() const
318348
{

src/scratch/block_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct BlockPrivate
3333
std::vector<std::shared_ptr<Field>> fields;
3434
std::unordered_map<int, Field *> fieldMap;
3535
bool shadow = false;
36+
int x = 0;
37+
int y = 0;
3638
std::string commentId;
3739
std::shared_ptr<Comment> comment = nullptr;
3840
IEngine *engine = nullptr;

test/scratch_classes/block_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ TEST_F(BlockTest, TopLevel)
194194
ASSERT_TRUE(block.topLevel());
195195
}
196196

197+
TEST_F(BlockTest, X)
198+
{
199+
Block block("", "");
200+
ASSERT_EQ(block.x(), 0);
201+
202+
block.setX(12);
203+
ASSERT_EQ(block.x(), 12);
204+
}
205+
206+
TEST_F(BlockTest, Y)
207+
{
208+
Block block("", "");
209+
ASSERT_EQ(block.y(), 0);
210+
211+
block.setY(8);
212+
ASSERT_EQ(block.y(), 8);
213+
}
214+
197215
TEST_F(BlockTest, Comment)
198216
{
199217
Block block("", "");

0 commit comments

Comments
 (0)