Skip to content

Commit 146a4bf

Browse files
committed
Use Entity as the base class of Asset
And use shared_ptr for assets
1 parent 97819b3 commit 146a4bf

File tree

15 files changed

+89
-97
lines changed

15 files changed

+89
-97
lines changed

include/scratchcpp/asset.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
#pragma once
44

5-
#include <string>
5+
#include "entity.h"
66
#include "spimpl.h"
7-
87
#include "global.h"
98

109
namespace libscratchcpp
@@ -13,12 +12,13 @@ namespace libscratchcpp
1312
class AssetPrivate;
1413

1514
/*! \brief The Asset class represents a Scratch asset, for example a Costume or a Sound. */
16-
class LIBSCRATCHCPP_EXPORT Asset
15+
class LIBSCRATCHCPP_EXPORT Asset : public Entity
1716
{
1817
public:
1918
Asset(const std::string &name, const std::string &id, const std::string &format);
19+
Asset(const Asset &) = delete;
2020

21-
const std::string &assetId() const;
21+
void setId(const std::string &id);
2222

2323
const std::string &name() const;
2424

include/scratchcpp/costume.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class LIBSCRATCHCPP_EXPORT Costume : public Asset
1616
{
1717
public:
1818
Costume(const std::string &name, const std::string &id, const std::string &format);
19+
Costume(const Costume &) = delete;
1920

2021
double bitmapResolution() const;
2122
void setBitmapResolution(double newBitmapResolution);

include/scratchcpp/sound.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset
1616
{
1717
public:
1818
Sound(const std::string &name, const std::string &id, const std::string &format);
19+
Sound(const Sound &) = delete;
1920

2021
int rate() const;
2122
void setRate(int newRate);

include/scratchcpp/target.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ class LIBSCRATCHCPP_EXPORT Target
5151
int currentCostume() const;
5252
void setCurrentCostume(int newCostume);
5353

54-
const std::vector<Costume> &costumes() const;
55-
int addCostume(const Costume &costume);
56-
const Costume &costumeAt(int index) const;
54+
const std::vector<std::shared_ptr<Costume>> &costumes() const;
55+
int addCostume(std::shared_ptr<Costume> costume);
56+
std::shared_ptr<Costume> costumeAt(int index) const;
5757
int findCostume(const std::string &costumeName) const;
5858

59-
const std::vector<Sound> &sounds() const;
60-
int addSound(const Sound &sound);
61-
const Sound &soundAt(int index) const;
59+
const std::vector<std::shared_ptr<Sound>> &sounds() const;
60+
int addSound(std::shared_ptr<Sound> sound);
61+
std::shared_ptr<Sound> soundAt(int index) const;
6262
int findSound(const std::string &soundName) const;
6363

6464
int layerOrder() const;

src/internal/scratch3reader.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,14 @@ bool Scratch3Reader::load()
203203
auto costumes = jsonTarget["costumes"];
204204
for (auto jsonCostume : costumes) {
205205
READER_STEP(step, "target -> costume -> { name, assetId, dataFormat }");
206-
Costume costume(jsonCostume["name"], jsonCostume["assetId"], jsonCostume["dataFormat"]);
206+
auto costume = std::make_shared<Costume>(jsonCostume["name"], jsonCostume["assetId"], jsonCostume["dataFormat"]);
207207
READER_STEP(step, "target -> costume -> bitmapResolution");
208208
if (jsonCostume.contains("bitmapResolution"))
209-
costume.setBitmapResolution(jsonCostume["bitmapResolution"]);
209+
costume->setBitmapResolution(jsonCostume["bitmapResolution"]);
210210
READER_STEP(step, "target -> costume -> rotationCenterX");
211-
costume.setRotationCenterX(jsonCostume["rotationCenterX"]);
211+
costume->setRotationCenterX(jsonCostume["rotationCenterX"]);
212212
READER_STEP(step, "target -> costume -> rotationCenterY");
213-
costume.setRotationCenterY(jsonCostume["rotationCenterY"]);
213+
costume->setRotationCenterY(jsonCostume["rotationCenterY"]);
214214
target->addCostume(costume);
215215
}
216216

@@ -219,11 +219,11 @@ bool Scratch3Reader::load()
219219
auto sounds = jsonTarget["sounds"];
220220
for (auto jsonSound : sounds) {
221221
READER_STEP(step, "target -> sound -> { name, assetId, dataFormat }");
222-
Sound sound(jsonSound["name"], jsonSound["assetId"], jsonSound["dataFormat"]);
222+
auto sound = std::make_shared<Sound>(jsonSound["name"], jsonSound["assetId"], jsonSound["dataFormat"]);
223223
READER_STEP(step, "target -> sound -> rate");
224-
sound.setRate(jsonSound["rate"]);
224+
sound->setRate(jsonSound["rate"]);
225225
READER_STEP(step, "target -> sound -> sampleCount");
226-
sound.setSampleCount(jsonSound["sampleCount"]);
226+
sound->setSampleCount(jsonSound["sampleCount"]);
227227
target->addSound(sound);
228228
}
229229

src/scratch/asset.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ using namespace libscratchcpp;
88

99
/*! Constructs Asset. */
1010
Asset::Asset(const std::string &name, const std::string &id, const std::string &format) :
11-
impl(spimpl::make_impl<AssetPrivate>(name, id, format))
11+
Entity(id),
12+
impl(spimpl::make_impl<AssetPrivate>(name, format))
1213
{
14+
impl->updateFileName(id);
1315
}
1416

15-
/*! Returns the MD5 hash of the asset file. */
16-
const std::string &Asset::assetId() const
17+
/*! Sets the ID (MD5 hash) of the asset file. */
18+
void Asset::setId(const std::string &id)
1719
{
18-
return impl->assetId;
20+
Entity::setId(id);
21+
impl->updateFileName(id);
1922
}
2023

2124
/*! Returns the name of the asset. */

src/scratch/asset_p.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
using namespace libscratchcpp;
66

7-
AssetPrivate::AssetPrivate(const std::string &name, const std::string &id, const std::string &format) :
7+
AssetPrivate::AssetPrivate(const std::string &name, const std::string &format) :
88
name(name),
9-
assetId(id),
109
dataFormat(format)
1110
{
12-
updateFileName();
1311
}
1412

15-
void AssetPrivate::updateFileName()
13+
void AssetPrivate::updateFileName(const std::string &id)
1614
{
17-
// NOTE: fileName depends on assetId and dataFormat
18-
fileName = assetId + "." + dataFormat;
15+
// NOTE: fileName depends on id and dataFormat
16+
fileName = id + "." + dataFormat;
1917
}

src/scratch/asset_p.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ namespace libscratchcpp
99

1010
struct AssetPrivate
1111
{
12-
AssetPrivate(const std::string &name, const std::string &id, const std::string &format);
12+
AssetPrivate(const std::string &name, const std::string &format);
1313

14-
void updateFileName();
14+
void updateFileName(const std::string &id);
1515

16-
std::string assetId;
1716
std::string name;
1817
std::string dataFormat;
1918
std::string fileName;

src/scratch/target.cpp

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -193,35 +193,25 @@ void Target::setCurrentCostume(int newCostume)
193193
}
194194

195195
/*! Returns the list of costumes. */
196-
const std::vector<Costume> &Target::costumes() const
196+
const std::vector<std::shared_ptr<Costume>> &Target::costumes() const
197197
{
198198
return impl->costumes;
199199
}
200200

201201
/*! Adds a costume and returns its index. */
202-
int Target::addCostume(const Costume &costume)
202+
int Target::addCostume(std::shared_ptr<Costume> costume)
203203
{
204-
// TODO: Use shared_ptr for assets
205-
/*auto it = std::find(impl->costumes.begin(), impl->costumes.end(), costume);
204+
auto it = std::find(impl->costumes.begin(), impl->costumes.end(), costume);
206205

207-
if (it != impl->blocks.end())
208-
return it - impl->blocks.begin();*/
209-
210-
int i = 0;
211-
212-
for (const auto &c : impl->costumes) {
213-
if (c.name() == costume.name())
214-
return i;
215-
216-
i++;
217-
}
206+
if (it != impl->costumes.end())
207+
return it - impl->costumes.begin();
218208

219209
impl->costumes.push_back(costume);
220210
return impl->costumes.size() - 1;
221211
}
222212

223213
/*! Returns the costume at index. */
224-
const libscratchcpp::Costume &Target::costumeAt(int index) const
214+
std::shared_ptr<Costume> Target::costumeAt(int index) const
225215
{
226216
// TODO: Add range check
227217
return impl->costumes[index];
@@ -232,42 +222,33 @@ int Target::findCostume(const std::string &costumeName) const
232222
{
233223
int i = 0;
234224
for (auto costume : impl->costumes) {
235-
if (costume.name() == costumeName)
225+
if (costume->name() == costumeName)
236226
return i;
237227
i++;
238228
}
239229
return -1;
240230
}
241231

242232
/*! Returns the list of sounds. */
243-
const std::vector<Sound> &Target::sounds() const
233+
const std::vector<std::shared_ptr<Sound>> &Target::sounds() const
244234
{
245235
return impl->sounds;
246236
}
247237

248238
/*! Adds a sound and returns its index. */
249-
int Target::addSound(const Sound &sound)
239+
int Target::addSound(std::shared_ptr<Sound> sound)
250240
{
251-
// TODO: Use shared_ptr for assets
252-
/*auto it = std::find(impl->sounds.begin(), impl->sounds.end(), sound);
241+
auto it = std::find(impl->sounds.begin(), impl->sounds.end(), sound);
253242

254243
if (it != impl->sounds.end())
255-
return it - impl->sounds.begin();*/
256-
257-
int i = 0;
244+
return it - impl->sounds.begin();
258245

259-
for (const auto &s : impl->sounds) {
260-
if (s.name() == sound.name())
261-
return i;
262-
263-
i++;
264-
}
265246
impl->sounds.push_back(sound);
266247
return impl->sounds.size() - 1;
267248
}
268249

269250
/*! Returns the sound at index. */
270-
const libscratchcpp::Sound &Target::soundAt(int index) const
251+
std::shared_ptr<Sound> Target::soundAt(int index) const
271252
{
272253
// TODO: Add range check
273254
return impl->sounds[index];
@@ -278,7 +259,7 @@ int Target::findSound(const std::string &soundName) const
278259
{
279260
int i = 0;
280261
for (auto sound : impl->sounds) {
281-
if (sound.name() == soundName)
262+
if (sound->name() == soundName)
282263
return i;
283264
i++;
284265
}

src/scratch/target_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ struct TargetPrivate
2525
std::vector<std::shared_ptr<List>> lists;
2626
std::vector<std::shared_ptr<Block>> blocks;
2727
int currentCostume = 1;
28-
std::vector<Costume> costumes;
29-
std::vector<Sound> sounds;
28+
std::vector<std::shared_ptr<Costume>> costumes;
29+
std::vector<std::shared_ptr<Sound>> sounds;
3030
int layerOrder = 0;
3131
int volume = 100;
3232
};

0 commit comments

Comments
 (0)