Skip to content

Commit acff91c

Browse files
committed
Move target property from Sound to Asset
Resolves: #540
1 parent 37f9604 commit acff91c

File tree

8 files changed

+34
-30
lines changed

8 files changed

+34
-30
lines changed

include/scratchcpp/asset.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace libscratchcpp
1010
{
1111

12+
class Target;
1213
class AssetPrivate;
1314

1415
/*! \brief The Asset class represents a Scratch asset, for example a Costume or a Sound. */
@@ -32,6 +33,9 @@ class LIBSCRATCHCPP_EXPORT Asset : public Entity
3233
unsigned int dataSize() const;
3334
void setData(unsigned int size, void *data);
3435

36+
Target *target() const;
37+
void setTarget(Target *target);
38+
3539
protected:
3640
virtual void processData(unsigned int size, void *data) { }
3741

include/scratchcpp/sound.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace libscratchcpp
1010
{
1111

12-
class Target;
1312
class SoundPrivate;
1413

1514
/*! \brief The Sound class represents a Scratch sound. */
@@ -40,9 +39,6 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset
4039

4140
virtual bool isPlaying() const;
4241

43-
Target *target() const;
44-
void setTarget(Target *target);
45-
4642
std::shared_ptr<Sound> clone() const;
4743

4844
protected:

src/scratch/asset.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ void Asset::setData(unsigned int size, void *data)
5858
impl->data = data;
5959
processData(size, data);
6060
}
61+
62+
/*! Returns the sprite or stage this asset belongs to. */
63+
Target *Asset::target() const
64+
{
65+
return impl->target;
66+
}
67+
68+
/*! Sets the sprite or stage this asset belongs to. */
69+
void Asset::setTarget(Target *target)
70+
{
71+
impl->target = target;
72+
}

src/scratch/asset_p.h

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

10+
class Target;
11+
1012
struct AssetPrivate
1113
{
1214
AssetPrivate(const std::string &name, const std::string &format);
@@ -19,6 +21,7 @@ struct AssetPrivate
1921
std::string fileName;
2022
const void *data = nullptr;
2123
unsigned int dataSize = 0;
24+
Target *target = nullptr;
2225
};
2326

2427
} // namespace libscratchcpp

src/scratch/sound.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ bool Sound::isPlaying() const
8787
return impl->player->isPlaying();
8888
}
8989

90-
/*! Returns the sprite or stage this variable belongs to. */
91-
Target *Sound::target() const
92-
{
93-
return impl->target;
94-
}
95-
96-
/*! Sets the sprite or stage this variable belongs to. */
97-
void Sound::setTarget(Target *target)
98-
{
99-
impl->target = target;
100-
}
101-
10290
/*! Returns an independent copy of the sound which is valid for as long as the original sound exists. */
10391
std::shared_ptr<Sound> Sound::clone() const
10492
{
@@ -130,8 +118,10 @@ void Sound::processData(unsigned int size, void *data)
130118

131119
void Sound::stopCloneSounds()
132120
{
133-
if (impl->target && !impl->target->isStage()) {
134-
Sprite *sprite = static_cast<Sprite *>(impl->target);
121+
Target *target = this->target();
122+
123+
if (target && !target->isStage()) {
124+
Sprite *sprite = static_cast<Sprite *>(target);
135125

136126
if (sprite->isClone())
137127
sprite = sprite->cloneSprite();

src/scratch/sound_p.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace libscratchcpp
1111
{
1212

13-
class Target;
1413
class Sound;
1514

1615
struct SoundPrivate
@@ -22,7 +21,6 @@ struct SoundPrivate
2221
int sampleCount = 0;
2322
static IAudioOutput *audioOutput;
2423
std::shared_ptr<IAudioPlayer> player = nullptr;
25-
Target *target = nullptr;
2624
const Sound *cloneRoot = nullptr;
2725
};
2826

test/assets/asset_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/asset.h>
2+
#include <scratchcpp/target.h>
23

34
#include "../common.h"
45
#include "testasset.h"
@@ -35,3 +36,13 @@ TEST(AssetTest, Data)
3536
ASSERT_EQ(asset.processedData, data);
3637
ASSERT_EQ(asset.callCount, 1);
3738
}
39+
40+
TEST(AssetTest, Target)
41+
{
42+
Asset asset("sound1", "a", "wav");
43+
ASSERT_EQ(asset.target(), nullptr);
44+
45+
Target target;
46+
asset.setTarget(&target);
47+
ASSERT_EQ(asset.target(), &target);
48+
}

test/assets/sound_test.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,6 @@ TEST_F(SoundTest, IsPlaying)
145145
SoundPrivate::audioOutput = nullptr;
146146
}
147147

148-
TEST_F(SoundTest, Target)
149-
{
150-
Sound sound("sound1", "a", "wav");
151-
ASSERT_EQ(sound.target(), nullptr);
152-
153-
Target target;
154-
sound.setTarget(&target);
155-
ASSERT_EQ(sound.target(), &target);
156-
}
157-
158148
TEST_F(SoundTest, Clone)
159149
{
160150
auto sound = std::make_shared<Sound>("sound1", "a", "wav");

0 commit comments

Comments
 (0)