Skip to content

Commit df85658

Browse files
committed
Add containsScratchPoint() method
1 parent a2e9a1f commit df85658

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

src/irenderedtarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class IRenderedTarget : public QNanoQuickItem
8484
virtual void clearGraphicEffects() = 0;
8585

8686
virtual const std::vector<QPoint> &hullPoints() const = 0;
87+
88+
virtual bool containsScratchPoint(double x, double y) const = 0;
8789
};
8890

8991
} // namespace scratchcpprender

src/renderedtarget.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,20 @@ bool RenderedTarget::contains(const QPointF &point) const
567567
return *it == intPoint;
568568
}
569569

570+
bool RenderedTarget::containsScratchPoint(double x, double y) const
571+
{
572+
if (!m_engine || !parentItem())
573+
return false;
574+
575+
// contains() expects item coordinates, so translate the Scratch coordinates first
576+
double stageWidth = m_engine->stageWidth();
577+
double stageHeight = m_engine->stageHeight();
578+
x = m_stageScale * (x + stageWidth / 2);
579+
y = m_stageScale * (stageHeight / 2 - y);
580+
581+
return contains(mapFromItem(parentItem(), QPointF(x, y)));
582+
}
583+
570584
void RenderedTarget::calculatePos()
571585
{
572586
if (!m_skin || !m_costume || !m_engine)

src/renderedtarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class RenderedTarget : public IRenderedTarget
9292
const std::vector<QPoint> &hullPoints() const override;
9393

9494
Q_INVOKABLE bool contains(const QPointF &point) const override;
95+
bool containsScratchPoint(double x, double y) const override;
9596

9697
signals:
9798
void engineChanged();

test/mocks/renderedtargetmock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class RenderedTargetMock : public IRenderedTarget
7070
MOCK_METHOD(const std::vector<QPoint> &, hullPoints, (), (const, override));
7171

7272
MOCK_METHOD(bool, contains, (const QPointF &), (const, override));
73+
MOCK_METHOD(bool, containsScratchPoint, (double, double), (const, override));
74+
7375
MOCK_METHOD(QNanoQuickItemPainter *, createItemPainter, (), (const, override));
7476
MOCK_METHOD(void, hoverEnterEvent, (QHoverEvent *), (override));
7577
MOCK_METHOD(void, hoverLeaveEvent, (QHoverEvent *), (override));

test/renderedtarget/renderedtarget_test.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ TEST_F(RenderedTargetTest, HullPoints)
305305
SpriteModel model;
306306
model.init(&sprite);
307307

308-
RenderedTarget target;
308+
QQuickItem parent;
309+
parent.setWidth(200);
310+
parent.setHeight(100);
311+
RenderedTarget target(&parent);
309312
target.setEngine(&engine);
310313
target.setSpriteModel(&model);
311314

@@ -315,14 +318,17 @@ TEST_F(RenderedTargetTest, HullPoints)
315318
createContextAndSurface(&context, &surface);
316319

317320
// Load costume
318-
EXPECT_CALL(engine, stageWidth()).WillOnce(Return(480));
319-
EXPECT_CALL(engine, stageHeight()).WillOnce(Return(360));
321+
EXPECT_CALL(engine, stageWidth()).WillRepeatedly(Return(480));
322+
EXPECT_CALL(engine, stageHeight()).WillRepeatedly(Return(360));
320323
auto costume = std::make_shared<Costume>("", "", "png");
321324
std::string costumeData = readFileStr("image.png");
322325
costume->setData(costumeData.size(), static_cast<void *>(costumeData.data()));
323326
sprite.addCostume(costume);
324327
target.loadCostumes();
325328
target.updateCostume(costume.get());
329+
target.setStageScale(2);
330+
target.setX(25);
331+
target.setY(30);
326332

327333
// Test hull points
328334
target.setWidth(3);
@@ -351,6 +357,28 @@ TEST_F(RenderedTargetTest, HullPoints)
351357
ASSERT_TRUE(target.contains({ 3, 3 }));
352358
ASSERT_FALSE(target.contains({ 3.3, 3.5 }));
353359

360+
// Test containsScratchPoint()
361+
ASSERT_FALSE(target.containsScratchPoint(-227.5, 165)); // [0, 0]
362+
ASSERT_FALSE(target.containsScratchPoint(-226.5, 165)); // [1, 0]
363+
ASSERT_FALSE(target.containsScratchPoint(-225.5, 165)); // [2, 0]
364+
ASSERT_FALSE(target.containsScratchPoint(-224.5, 165)); // [3, 0]
365+
366+
ASSERT_FALSE(target.containsScratchPoint(-227.5, 164)); // [0, 1]
367+
ASSERT_TRUE(target.containsScratchPoint(-226.5, 164)); // [1, 1]
368+
ASSERT_TRUE(target.containsScratchPoint(-226.1, 163.75)); // [1.4, 1.25]
369+
ASSERT_TRUE(target.containsScratchPoint(-225.5, 164)); // [2, 1]
370+
ASSERT_TRUE(target.containsScratchPoint(-224.5, 164)); // [3, 1]
371+
372+
ASSERT_TRUE(target.containsScratchPoint(-226.5, 163)); // [1, 2]
373+
ASSERT_FALSE(target.containsScratchPoint(-225.5, 163)); // [2, 2]
374+
ASSERT_TRUE(target.containsScratchPoint(-224.5, 163)); // [3, 2]
375+
ASSERT_FALSE(target.containsScratchPoint(-224, 162.9)); // [3.5, 2.1]
376+
377+
ASSERT_TRUE(target.containsScratchPoint(-226.5, 162)); // [1, 3]
378+
ASSERT_TRUE(target.containsScratchPoint(-225.5, 162)); // [2, 3]
379+
ASSERT_TRUE(target.containsScratchPoint(-224.5, 162)); // [3, 3]
380+
ASSERT_FALSE(target.containsScratchPoint(-224.2, 161.5)); // [3.3, 3.5]
381+
354382
// Cleanup
355383
context.doneCurrent();
356384
}

0 commit comments

Comments
 (0)