Skip to content

Commit 8c6a502

Browse files
committed
Add getBoundsForBubble() method to IRenderedTarget
1 parent a98a222 commit 8c6a502

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

src/irenderedtarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class IRenderedTarget : public QNanoQuickItem
7070
virtual void setHeight(qreal width) = 0;
7171

7272
virtual libscratchcpp::Rect getBounds() const = 0;
73+
virtual QRectF getBoundsForBubble() const = 0;
7374

7475
virtual QPointF mapFromScene(const QPointF &point) const = 0;
7576

src/renderedtarget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,19 @@ Rect RenderedTarget::getBounds() const
366366
return Rect(left + m_x, top + m_y, right + m_x, bottom + m_y);
367367
}
368368

369+
QRectF RenderedTarget::getBoundsForBubble() const
370+
{
371+
// https://github.com/scratchfoundation/scratch-render/blob/86dcb0151a04bc8c1ff39559e8531e7921102b56/src/Drawable.js#L536-L551
372+
Rect rect = getBounds();
373+
const int slice = 8; // px, how tall the top slice to measure should be
374+
375+
if (rect.height() > slice)
376+
rect.setBottom(rect.top() - slice);
377+
378+
Q_ASSERT(rect.height() <= 8);
379+
return QRectF(QPointF(rect.left(), rect.top()), QPointF(rect.right(), rect.bottom()));
380+
}
381+
369382
QPointF RenderedTarget::mapFromScene(const QPointF &point) const
370383
{
371384
return QNanoQuickItem::mapFromScene(point);

src/renderedtarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class RenderedTarget : public IRenderedTarget
7575
void setHeight(qreal height) override;
7676

7777
libscratchcpp::Rect getBounds() const override;
78+
Q_INVOKABLE QRectF getBoundsForBubble() const override;
7879

7980
QPointF mapFromScene(const QPointF &point) const override;
8081

test/mocks/renderedtargetmock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class RenderedTargetMock : public IRenderedTarget
5656
MOCK_METHOD(QPointF, mapFromScene, (const QPointF &), (const, override));
5757

5858
MOCK_METHOD(libscratchcpp::Rect, getBounds, (), (const, override));
59+
MOCK_METHOD(QRectF, getBoundsForBubble, (), (const, override));
5960

6061
MOCK_METHOD(bool, mirrorHorizontally, (), (const, override));
6162

test/renderedtarget/renderedtarget_test.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ TEST_F(RenderedTargetTest, GetBounds)
674674
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 66.72);
675675
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -125.11);
676676

677+
QRectF bubbleBounds = target.getBoundsForBubble();
678+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 66.13);
679+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -124.52);
680+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 66.72);
681+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -125.11);
682+
677683
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
678684
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
679685
target.updateRotationStyle(Sprite::RotationStyle::LeftRight);
@@ -684,6 +690,12 @@ TEST_F(RenderedTargetTest, GetBounds)
684690
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.29);
685691
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -110.89);
686692

693+
bubbleBounds = target.getBoundsForBubble();
694+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 71.87);
695+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -110.47);
696+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 72.29);
697+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -110.89);
698+
687699
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
688700
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
689701
target.setStageScale(20.75);
@@ -694,5 +706,27 @@ TEST_F(RenderedTargetTest, GetBounds)
694706
ASSERT_EQ(std::round(bounds.right() * 100) / 100, 72.29);
695707
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, -110.89);
696708

709+
bubbleBounds = target.getBoundsForBubble();
710+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, 71.87);
711+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, -110.47);
712+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, 72.29);
713+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, -110.89);
714+
715+
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
716+
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
717+
target.updateSize(9780.6);
718+
719+
bounds = target.getBounds();
720+
ASSERT_EQ(std::round(bounds.left() * 100) / 100, -466.05);
721+
ASSERT_EQ(std::round(bounds.top() * 100) / 100, 1294.13);
722+
ASSERT_EQ(std::round(bounds.right() * 100) / 100, -405.87);
723+
ASSERT_EQ(std::round(bounds.bottom() * 100) / 100, 1233.94);
724+
725+
bubbleBounds = target.getBoundsForBubble();
726+
ASSERT_EQ(std::round(bubbleBounds.left() * 100) / 100, -466.05);
727+
ASSERT_EQ(std::round(bubbleBounds.top() * 100) / 100, 1294.13);
728+
ASSERT_EQ(std::round(bubbleBounds.right() * 100) / 100, -405.87);
729+
ASSERT_EQ(std::round(bubbleBounds.bottom() * 100) / 100, 1286.13);
730+
697731
context.doneCurrent();
698732
}

0 commit comments

Comments
 (0)