Skip to content

Commit 33272e7

Browse files
authored
Merge pull request #333 from scratchcpp/refactor_target_interfaces
Refactor target interfaces
2 parents f75a571 + b2d541f commit 33272e7

File tree

13 files changed

+75
-33
lines changed

13 files changed

+75
-33
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,31 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
1414
public:
1515
virtual ~ISpriteHandler() { }
1616

17-
virtual void onSpriteChanged(Sprite *sprite) = 0;
17+
/*! Called when the interface is set on a sprite. */
18+
virtual void init(Sprite *sprite) = 0;
1819

20+
/*! Called when the sprite clones. */
1921
virtual void onCloned(Sprite *clone) = 0;
2022

21-
virtual void onCostumeChanged(const char *data) = 0;
23+
/*! Called when the costume changes. */
24+
virtual void onCostumeChanged(Costume *costume) = 0;
2225

26+
/*! Called when the visibility changes. */
2327
virtual void onVisibleChanged(bool visible) = 0;
28+
29+
/*! Called when the X-coordinate changes. */
2430
virtual void onXChanged(double x) = 0;
31+
32+
/*! Called when the Y-coordinate changes. */
2533
virtual void onYChanged(double y) = 0;
34+
35+
/*! Called when the size changes. */
2636
virtual void onSizeChanged(double size) = 0;
37+
38+
/*! Called when the direction changes. */
2739
virtual void onDirectionChanged(double direction) = 0;
40+
41+
/*! Called when the rotation style changes. */
2842
virtual void onRotationStyleChanged(Sprite::RotationStyle rotationStyle) = 0;
2943
};
3044

include/scratchcpp/istagehandler.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
1414
public:
1515
virtual ~IStageHandler() { }
1616

17-
virtual void onStageChanged(Stage *stage) = 0;
17+
/*! Called when the interface is set on a stage. */
18+
virtual void init(Stage *stage) = 0;
1819

19-
virtual void onCostumeChanged(const char *data) = 0;
20+
/*! Called when the costume changes. */
21+
virtual void onCostumeChanged(Costume *costume) = 0;
2022

23+
/*! Called when the tempo changes. */
2124
virtual void onTempoChanged(int tempo) = 0;
25+
26+
/*! Called when the video state changes. */
2227
virtual void onVideoStateChanged(Stage::VideoState videoState) = 0;
28+
29+
/*! Called when the video transparency changes. */
2330
virtual void onVideoTransparencyChanged(int videoTransparency) = 0;
2431
};
2532

include/scratchcpp/stage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
2929
void setInterface(IStageHandler *newInterface);
3030

3131
bool isStage() const override;
32+
33+
void setCostumeIndex(int newCostumeIndex);
34+
3235
int tempo() const;
3336
void setTempo(int newTempo);
3437

src/scratch/sprite.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Sprite::setInterface(ISpriteHandler *newInterface)
4444
{
4545
assert(newInterface);
4646
impl->iface = newInterface;
47-
impl->iface->onSpriteChanged(this);
47+
impl->iface->init(this);
4848
}
4949

5050
/*! Creates a clone of the sprite. */
@@ -246,6 +246,9 @@ void Sprite::setCostumeIndex(int newCostumeIndex)
246246
}
247247

248248
Target::setCostumeIndex(newCostumeIndex);
249+
250+
if (costume && impl->iface)
251+
impl->iface->onCostumeChanged(costume.get());
249252
}
250253

251254
/*! Returns the direction. */

src/scratch/sprite_p.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ void SpritePrivate::removeClone(Sprite *clone)
3131
}
3232
}
3333

34-
void SpritePrivate::setCostumeData(const char *data)
35-
{
36-
if (iface)
37-
iface->onCostumeChanged(data);
38-
}
39-
4034
void SpritePrivate::getBoundingRect(Rect *out) const
4135
{
4236
assert(out);

src/scratch/sprite_p.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ struct SpritePrivate
1717

1818
void removeClone(Sprite *clone);
1919

20-
void setCostumeData(const char *data);
2120
void getBoundingRect(Rect *out) const;
2221
void getFencedPosition(double inX, double inY, double *outX, double *outY) const;
2322

src/scratch/stage.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void Stage::setInterface(IStageHandler *newInterface)
2020
{
2121
assert(newInterface);
2222
impl->iface = newInterface;
23-
impl->iface->onStageChanged(this);
23+
impl->iface->init(this);
2424
}
2525

2626
/*! Returns true. */
@@ -29,6 +29,16 @@ bool Stage::isStage() const
2929
return true;
3030
}
3131

32+
/*! Overrides Target#setCostumeIndex(). */
33+
void Stage::setCostumeIndex(int newCostumeIndex)
34+
{
35+
Target::setCostumeIndex(newCostumeIndex);
36+
auto costume = costumeAt(newCostumeIndex);
37+
38+
if (impl->iface)
39+
impl->iface->onCostumeChanged(costume.get());
40+
}
41+
3242
/*! Returns the tempo. */
3343
int Stage::tempo() const
3444
{

src/scratch/stage_p.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,3 @@ using namespace libscratchcpp;
99
StagePrivate::StagePrivate()
1010
{
1111
}
12-
13-
void StagePrivate::setCostumeData(const char *data)
14-
{
15-
if (iface)
16-
iface->onCostumeChanged(data);
17-
}

src/scratch/stage_p.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ struct StagePrivate
1212
StagePrivate();
1313
StagePrivate(const StagePrivate &) = delete;
1414

15-
void setCostumeData(const char *data);
16-
1715
IStageHandler *iface = nullptr;
1816
int tempo = 60;
1917
Stage::VideoState videoState = Stage::VideoState::Off;

test/mocks/spritehandlermock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ using namespace libscratchcpp;
88
class SpriteHandlerMock : public ISpriteHandler
99
{
1010
public:
11-
MOCK_METHOD(void, onSpriteChanged, (Sprite *), (override));
11+
MOCK_METHOD(void, init, (Sprite *), (override));
1212

1313
MOCK_METHOD(void, onCloned, (Sprite *), (override));
1414

15-
MOCK_METHOD(void, onCostumeChanged, (const char *), (override));
15+
MOCK_METHOD(void, onCostumeChanged, (Costume *), (override));
1616

1717
MOCK_METHOD(void, onVisibleChanged, (bool), (override));
1818
MOCK_METHOD(void, onXChanged, (double), (override));

0 commit comments

Comments
 (0)