Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions include/scratchcpp/ispritehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
public:
virtual ~ISpriteHandler() { }

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

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

virtual void onCostumeChanged(const char *data) = 0;
/*! Called when the costume changes. */
virtual void onCostumeChanged(Costume *costume) = 0;

/*! Called when the visibility changes. */
virtual void onVisibleChanged(bool visible) = 0;

/*! Called when the X-coordinate changes. */
virtual void onXChanged(double x) = 0;

/*! Called when the Y-coordinate changes. */
virtual void onYChanged(double y) = 0;

/*! Called when the size changes. */
virtual void onSizeChanged(double size) = 0;

/*! Called when the direction changes. */
virtual void onDirectionChanged(double direction) = 0;

/*! Called when the rotation style changes. */
virtual void onRotationStyleChanged(Sprite::RotationStyle rotationStyle) = 0;
};

Expand Down
11 changes: 9 additions & 2 deletions include/scratchcpp/istagehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
public:
virtual ~IStageHandler() { }

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

virtual void onCostumeChanged(const char *data) = 0;
/*! Called when the costume changes. */
virtual void onCostumeChanged(Costume *costume) = 0;

/*! Called when the tempo changes. */
virtual void onTempoChanged(int tempo) = 0;

/*! Called when the video state changes. */
virtual void onVideoStateChanged(Stage::VideoState videoState) = 0;

/*! Called when the video transparency changes. */
virtual void onVideoTransparencyChanged(int videoTransparency) = 0;
};

Expand Down
3 changes: 3 additions & 0 deletions include/scratchcpp/stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
void setInterface(IStageHandler *newInterface);

bool isStage() const override;

void setCostumeIndex(int newCostumeIndex);

int tempo() const;
void setTempo(int newTempo);

Expand Down
5 changes: 4 additions & 1 deletion src/scratch/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void Sprite::setInterface(ISpriteHandler *newInterface)
{
assert(newInterface);
impl->iface = newInterface;
impl->iface->onSpriteChanged(this);
impl->iface->init(this);
}

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

Target::setCostumeIndex(newCostumeIndex);

if (costume && impl->iface)
impl->iface->onCostumeChanged(costume.get());
}

/*! Returns the direction. */
Expand Down
6 changes: 0 additions & 6 deletions src/scratch/sprite_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ void SpritePrivate::removeClone(Sprite *clone)
}
}

void SpritePrivate::setCostumeData(const char *data)
{
if (iface)
iface->onCostumeChanged(data);
}

void SpritePrivate::getBoundingRect(Rect *out) const
{
assert(out);
Expand Down
1 change: 0 additions & 1 deletion src/scratch/sprite_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct SpritePrivate

void removeClone(Sprite *clone);

void setCostumeData(const char *data);
void getBoundingRect(Rect *out) const;
void getFencedPosition(double inX, double inY, double *outX, double *outY) const;

Expand Down
12 changes: 11 additions & 1 deletion src/scratch/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void Stage::setInterface(IStageHandler *newInterface)
{
assert(newInterface);
impl->iface = newInterface;
impl->iface->onStageChanged(this);
impl->iface->init(this);
}

/*! Returns true. */
Expand All @@ -29,6 +29,16 @@ bool Stage::isStage() const
return true;
}

/*! Overrides Target#setCostumeIndex(). */
void Stage::setCostumeIndex(int newCostumeIndex)
{
Target::setCostumeIndex(newCostumeIndex);
auto costume = costumeAt(newCostumeIndex);

if (impl->iface)
impl->iface->onCostumeChanged(costume.get());
}

/*! Returns the tempo. */
int Stage::tempo() const
{
Expand Down
6 changes: 0 additions & 6 deletions src/scratch/stage_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ using namespace libscratchcpp;
StagePrivate::StagePrivate()
{
}

void StagePrivate::setCostumeData(const char *data)
{
if (iface)
iface->onCostumeChanged(data);
}
2 changes: 0 additions & 2 deletions src/scratch/stage_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ struct StagePrivate
StagePrivate();
StagePrivate(const StagePrivate &) = delete;

void setCostumeData(const char *data);

IStageHandler *iface = nullptr;
int tempo = 60;
Stage::VideoState videoState = Stage::VideoState::Off;
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/spritehandlermock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ using namespace libscratchcpp;
class SpriteHandlerMock : public ISpriteHandler
{
public:
MOCK_METHOD(void, onSpriteChanged, (Sprite *), (override));
MOCK_METHOD(void, init, (Sprite *), (override));

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

MOCK_METHOD(void, onCostumeChanged, (const char *), (override));
MOCK_METHOD(void, onCostumeChanged, (Costume *), (override));

MOCK_METHOD(void, onVisibleChanged, (bool), (override));
MOCK_METHOD(void, onXChanged, (double), (override));
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/stagehandlermock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ using namespace libscratchcpp;
class StageHandlerMock : public IStageHandler
{
public:
MOCK_METHOD(void, onStageChanged, (Stage *), (override));
MOCK_METHOD(void, init, (Stage *), (override));

MOCK_METHOD(void, onCostumeChanged, (const char *), (override));
MOCK_METHOD(void, onCostumeChanged, (Costume *), (override));

MOCK_METHOD(void, onTempoChanged, (int), (override));
MOCK_METHOD(void, onVideoStateChanged, (Stage::VideoState), (override));
Expand Down
19 changes: 15 additions & 4 deletions test/target_interfaces/ispritehandler_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <scratchcpp/costume.h>
#include <spritehandlermock.h>
#include <enginemock.h>

Expand All @@ -14,7 +15,7 @@ class ISpriteHandlerTest : public testing::Test
public:
void SetUp() override
{
EXPECT_CALL(m_handler, onSpriteChanged(&m_sprite)).Times(1);
EXPECT_CALL(m_handler, init(&m_sprite)).Times(1);
m_sprite.setInterface(&m_handler);
m_sprite.setEngine(&m_engine);
}
Expand Down Expand Up @@ -110,8 +111,18 @@ TEST_F(ISpriteHandlerTest, RotationStyle)
m_sprite.setRotationStyle(Sprite::RotationStyle::LeftRight);
}

TEST_F(ISpriteHandlerTest, CostumeData)
TEST_F(ISpriteHandlerTest, Costume)
{
// TODO: Add test for costume data
// EXPECT_CALL(m_handler, onCostumeChanged(...)).Times(1);
auto costume1 = std::make_shared<Costume>("", "", "");
auto costume2 = std::make_shared<Costume>("", "", "");
m_sprite.addCostume(costume1);
m_sprite.addCostume(costume2);

EXPECT_CALL(m_handler, onCostumeChanged(costume1.get()));
EXPECT_CALL(m_engine, requestRedraw());
m_sprite.setCostumeIndex(0);

EXPECT_CALL(m_handler, onCostumeChanged(costume2.get()));
EXPECT_CALL(m_engine, requestRedraw());
m_sprite.setCostumeIndex(1);
}
17 changes: 13 additions & 4 deletions test/target_interfaces/istagehandler_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <scratchcpp/stage.h>
#include <scratchcpp/costume.h>
#include <stagehandlermock.h>

#include "../common.h"
Expand All @@ -10,7 +11,7 @@ class IStageHandlerTest : public testing::Test
public:
void SetUp() override
{
EXPECT_CALL(m_handler, onStageChanged(&m_stage)).Times(1);
EXPECT_CALL(m_handler, init(&m_stage)).Times(1);
m_stage.setInterface(&m_handler);
}

Expand All @@ -33,8 +34,16 @@ TEST_F(IStageHandlerTest, VideoState)
m_stage.setVideoState(Stage::VideoState::OnFlipped);
}

TEST_F(IStageHandlerTest, CostumeData)
TEST_F(IStageHandlerTest, Costume)
{
// TODO: Add test for costume data
// EXPECT_CALL(m_handler, onCostumeChanged(...)).Times(1);
auto costume1 = std::make_shared<Costume>("", "", "");
auto costume2 = std::make_shared<Costume>("", "", "");
m_stage.addCostume(costume1);
m_stage.addCostume(costume2);

EXPECT_CALL(m_handler, onCostumeChanged(costume1.get()));
m_stage.setCostumeIndex(0);

EXPECT_CALL(m_handler, onCostumeChanged(costume2.get()));
m_stage.setCostumeIndex(1);
}