Skip to content

Commit d669ce5

Browse files
committed
Add touchingSprite() method
1 parent b0609ad commit d669ce5

File tree

14 files changed

+175
-0
lines changed

14 files changed

+175
-0
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
7878
*/
7979
virtual Rect fastBoundingRect() const = 0;
8080

81+
/*! Used to check whether the sprite touches any of the given sprite clones. */
82+
virtual bool touchingClones(const std::vector<Sprite *> &clones) const = 0;
83+
8184
/*! Used to check whether the sprite touches the given point (in Scratch coordinates). */
8285
virtual bool touchingPoint(double x, double y) const = 0;
8386
};

include/scratchcpp/istagehandler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace libscratchcpp
99
{
1010

11+
class Sprite;
12+
1113
/*! \brief The IStageHandler class provides a stage interface for Scratch project players. */
1214
class LIBSCRATCHCPP_EXPORT IStageHandler
1315
{
@@ -53,6 +55,9 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
5355
*/
5456
virtual Rect fastBoundingRect() const = 0;
5557

58+
/*! Used to check whether the stage touches any of the given sprite clones. */
59+
virtual bool touchingClones(const std::vector<Sprite *> &clones) const = 0;
60+
5661
/*! Used to check whether the stage touches the given point (in Scratch coordinates). */
5762
virtual bool touchingPoint(double x, double y) const = 0;
5863
};

include/scratchcpp/sprite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class LIBSCRATCHCPP_EXPORT Sprite
8383

8484
private:
8585
Target *dataSource() const override;
86+
bool touchingClones(const std::vector<Sprite *> &clones) const override;
8687
void setXY(double x, double y);
8788

8889
spimpl::unique_impl_ptr<SpritePrivate> impl;

include/scratchcpp/stage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
6161
virtual void setBubbleText(const std::string &text) override;
6262

6363
private:
64+
bool touchingClones(const std::vector<Sprite *> &clones) const override;
65+
6466
spimpl::unique_impl_ptr<StagePrivate> impl;
6567
};
6668

include/scratchcpp/target.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Block;
1818
class Comment;
1919
class Costume;
2020
class Sound;
21+
class Sprite;
2122
class IGraphicsEffect;
2223
class TargetPrivate;
2324

@@ -88,6 +89,7 @@ class LIBSCRATCHCPP_EXPORT Target
8889
virtual Rect boundingRect() const;
8990
virtual Rect fastBoundingRect() const;
9091

92+
bool touchingSprite(Sprite *sprite) const;
9193
virtual bool touchingPoint(double x, double y) const;
9294
bool touchingEdge() const;
9395

@@ -109,6 +111,9 @@ class LIBSCRATCHCPP_EXPORT Target
109111
/*! Override this method to set a custom data source for blocks, assets, comments, etc. */
110112
virtual Target *dataSource() const { return nullptr; }
111113

114+
/*! Override this method to check whether the target touches the given sprite clones. */
115+
virtual bool touchingClones(const std::vector<Sprite *> &clones) const { return false; }
116+
112117
private:
113118
spimpl::unique_impl_ptr<TargetPrivate> impl;
114119
};

src/scratch/sprite.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,14 @@ Target *Sprite::dataSource() const
477477
return impl->cloneSprite;
478478
}
479479

480+
bool Sprite::touchingClones(const std::vector<Sprite *> &clones) const
481+
{
482+
if (!impl->iface)
483+
return false;
484+
485+
return impl->iface->touchingClones(clones);
486+
}
487+
480488
void Sprite::setXY(double x, double y)
481489
{
482490
IEngine *eng = engine();

src/scratch/stage.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,11 @@ void Stage::setBubbleText(const std::string &text)
208208
if (impl->iface)
209209
impl->iface->onBubbleTextChanged(text);
210210
}
211+
212+
bool Stage::touchingClones(const std::vector<Sprite *> &clones) const
213+
{
214+
if (!impl->iface)
215+
return false;
216+
217+
return impl->iface->touchingClones(clones);
218+
}

src/scratch/target.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include <scratchcpp/target.h>
4+
#include <scratchcpp/sprite.h>
45
#include <scratchcpp/variable.h>
56
#include <scratchcpp/list.h>
67
#include <scratchcpp/block.h>
@@ -431,6 +432,28 @@ Rect Target::fastBoundingRect() const
431432
return Rect();
432433
}
433434

435+
/*! Returns true if the Target is touching the given Sprite (or its clones). */
436+
bool Target::touchingSprite(Sprite *sprite) const
437+
{
438+
// https://github.com/scratchfoundation/scratch-vm/blob/8dbcc1fc8f8d8c4f1e40629fe8a388149d6dfd1c/src/sprites/rendered-target.js#L792-L805
439+
if (!sprite)
440+
return false;
441+
442+
Sprite *firstClone = sprite->isClone() ? sprite->cloneSprite() : sprite;
443+
assert(firstClone);
444+
std::vector<Sprite *> clones;
445+
446+
if (true) // TODO: Filter clones that are being dragged, including firstClone
447+
clones.push_back(firstClone);
448+
449+
for (auto clone : firstClone->clones()) {
450+
if (true) // TODO: Filter clones that are being dragged
451+
clones.push_back(clone.get());
452+
}
453+
454+
return touchingClones(clones);
455+
}
456+
434457
/*! Returns true if the Target is touching the given point (in Scratch coordinates). */
435458
bool Target::touchingPoint(double x, double y) const
436459
{

test/mocks/spritehandlermock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ class SpriteHandlerMock : public ISpriteHandler
3131

3232
MOCK_METHOD(Rect, boundingRect, (), (const, override));
3333
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
34+
35+
MOCK_METHOD(bool, touchingClones, (const std::vector<Sprite *> &), (const, override));
3436
MOCK_METHOD(bool, touchingPoint, (double, double), (const, override));
3537
};

test/mocks/stagehandlermock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ class StageHandlerMock : public IStageHandler
2222

2323
MOCK_METHOD(Rect, boundingRect, (), (const, override));
2424
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
25+
26+
MOCK_METHOD(bool, touchingClones, (const std::vector<Sprite *> &), (const, override));
2527
MOCK_METHOD(bool, touchingPoint, (double, double), (const, override));
2628
};

0 commit comments

Comments
 (0)