Skip to content

Commit 76ab117

Browse files
committed
Sprite: Add clone info methods
1 parent f3e0aab commit 76ab117

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

include/scratchcpp/sprite.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ class LIBSCRATCHCPP_EXPORT Sprite : public Target
2222
};
2323

2424
Sprite();
25+
~Sprite();
2526

2627
void setInterface(ISpriteHandler *newInterface);
2728

29+
bool isClone() const;
30+
31+
Sprite *cloneRoot() const;
32+
Sprite *cloneParent() const;
33+
2834
bool visible() const;
2935
void setVisible(bool newVisible);
3036

src/scratch/sprite.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Sprite::Sprite() :
1616
{
1717
}
1818

19+
/*! Destroys the Sprite object. */
20+
Sprite::~Sprite()
21+
{
22+
if (isClone()) {
23+
assert(impl->cloneParent);
24+
impl->cloneParent->impl->removeClone(this);
25+
}
26+
}
27+
1928
/*! Sets the sprite interface. */
2029
void Sprite::setInterface(ISpriteHandler *newInterface)
2130
{
@@ -24,6 +33,24 @@ void Sprite::setInterface(ISpriteHandler *newInterface)
2433
impl->iface->onSpriteChanged(this);
2534
}
2635

36+
/*! Returns true if this is a clone. */
37+
bool Sprite::isClone() const
38+
{
39+
return (impl->cloneParent != nullptr);
40+
}
41+
42+
/*! Returns the sprite this clone was created from, or nullptr if this isn't a clone. */
43+
Sprite *Sprite::cloneRoot() const
44+
{
45+
return impl->cloneRoot;
46+
}
47+
48+
/*! Returns the sprite or clone this clone was created from, or nullptr if this isn't a clone. */
49+
Sprite *Sprite::cloneParent() const
50+
{
51+
return impl->cloneParent;
52+
}
53+
2754
/*! Returns true if the sprite is visible. */
2855
bool Sprite::visible() const
2956
{

src/scratch/sprite_p.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ SpritePrivate::SpritePrivate()
1010
{
1111
}
1212

13+
void SpritePrivate::removeClone(Sprite *clone)
14+
{
15+
int index = 0;
16+
for (const auto &child : childClones) {
17+
if (child.get() == clone) {
18+
childClones.erase(childClones.begin() + index);
19+
return;
20+
}
21+
22+
index++;
23+
}
24+
}
25+
1326
void SpritePrivate::setCostumeData(const char *data)
1427
{
1528
if (iface)

src/scratch/sprite_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ struct SpritePrivate
1212
SpritePrivate();
1313
SpritePrivate(const SpritePrivate &) = delete;
1414

15+
void removeClone(Sprite *clone);
16+
1517
void setCostumeData(const char *data);
1618

1719
ISpriteHandler *iface = nullptr;
20+
Sprite *cloneRoot = nullptr;
21+
Sprite *cloneParent = nullptr;
22+
std::vector<std::shared_ptr<Sprite>> childClones;
1823
bool visible = true;
1924
double x = 0;
2025
double y = 0;

0 commit comments

Comments
 (0)