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
6 changes: 6 additions & 0 deletions include/scratchcpp/ispritehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
/*! Called when all graphics effects are cleared. */
virtual void onGraphicsEffectsCleared() = 0;

/*! Called when the bubble type changes. */
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;

/*! Called when the bubble text changes. */
virtual void onBubbleTextChanged(const std::string &text) = 0;

/*!
* Used to get the bounding rectangle of the sprite.
* \note The rectangle must be relative to the stage, so make sure to use the sprite's coordinates.
Expand Down
6 changes: 6 additions & 0 deletions include/scratchcpp/istagehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class LIBSCRATCHCPP_EXPORT IStageHandler

/*! Called when all graphics effects are cleared. */
virtual void onGraphicsEffectsCleared() = 0;

/*! Called when the bubble type changes. */
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;

/*! Called when the bubble text changes. */
virtual void onBubbleTextChanged(const std::string &text) = 0;
};

} // namespace libscratchcpp
3 changes: 3 additions & 0 deletions include/scratchcpp/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class LIBSCRATCHCPP_EXPORT Sprite

void clearGraphicsEffects() override;

virtual void setBubbleType(Target::BubbleType type) override;
virtual void setBubbleText(const std::string &text) override;

private:
Target *dataSource() const override;
void setXY(double x, double y);
Expand Down
3 changes: 3 additions & 0 deletions include/scratchcpp/stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target

void clearGraphicsEffects() override;

virtual void setBubbleType(Target::BubbleType type) override;
virtual void setBubbleText(const std::string &text) override;

private:
spimpl::unique_impl_ptr<StagePrivate> impl;
};
Expand Down
12 changes: 12 additions & 0 deletions include/scratchcpp/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class TargetPrivate;
class LIBSCRATCHCPP_EXPORT Target
{
public:
enum class BubbleType
{
Say,
Think
};

Target();
Target(const Target &) = delete;
virtual ~Target() { }
Expand Down Expand Up @@ -83,6 +89,12 @@ class LIBSCRATCHCPP_EXPORT Target

virtual void clearGraphicsEffects();

BubbleType bubbleType() const;
virtual void setBubbleType(BubbleType type);

const std::string &bubbleText() const;
virtual void setBubbleText(const std::string &text);

IEngine *engine() const;
void setEngine(IEngine *engine);

Expand Down
25 changes: 25 additions & 0 deletions src/scratch/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,31 @@ void Sprite::clearGraphicsEffects()
impl->iface->onGraphicsEffectsCleared();
}

/*! Overrides Target#setBubbleType(). */
void Sprite::setBubbleType(BubbleType type)
{
Target::setBubbleType(type);

if (impl->iface)
impl->iface->onBubbleTypeChanged(type);
}

/*! Overrides Target#setBubbleText(). */
void Sprite::setBubbleText(const std::string &text)
{
Target::setBubbleText(text);

if (impl->visible && !text.empty()) {
IEngine *eng = engine();

if (eng)
eng->requestRedraw();
}

if (impl->iface)
impl->iface->onBubbleTextChanged(text);
}

Target *Sprite::dataSource() const
{
return impl->cloneSprite;
Expand Down
25 changes: 25 additions & 0 deletions src/scratch/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,28 @@ void Stage::clearGraphicsEffects()
if (impl->iface)
impl->iface->onGraphicsEffectsCleared();
}

/*! Overrides Target#setBubbleType(). */
void Stage::setBubbleType(BubbleType type)
{
Target::setBubbleType(type);

if (impl->iface)
impl->iface->onBubbleTypeChanged(type);
}

/*! Overrides Target#setBubbleText(). */
void Stage::setBubbleText(const std::string &text)
{
Target::setBubbleText(text);

if (!text.empty()) {
IEngine *eng = engine();

if (eng)
eng->requestRedraw();
}

if (impl->iface)
impl->iface->onBubbleTextChanged(text);
}
30 changes: 30 additions & 0 deletions src/scratch/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,36 @@ void Target::clearGraphicsEffects()
impl->graphicsEffects.clear();
}

/*! Returns the type of the bubble (say or think). */
Target::BubbleType Target::bubbleType() const
{
return impl->bubbleType;
}

/*! Sets the type of the bubble (say or think). */
void Target::setBubbleType(BubbleType type)
{
impl->bubbleType = type;
}

/*!
* Returns the text of the bubble.
* \note If the text is an empty string, the bubble is supposed to be hidden.
*/
const std::string &Target::bubbleText() const
{
return impl->bubbleText;
}

/*!
* Sets the text of the bubble.
* \note If the text is an empty string, the bubble is supposed to be hidden.
*/
void Target::setBubbleText(const std::string &text)
{
impl->bubbleText = text;
}

/*! Returns the engine. */
IEngine *Target::engine() const
{
Expand Down
3 changes: 3 additions & 0 deletions src/scratch/target_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unordered_map>
#include <scratchcpp/costume.h>
#include <scratchcpp/sound.h>
#include <scratchcpp/target.h>

namespace libscratchcpp
{
Expand Down Expand Up @@ -36,6 +37,8 @@ struct TargetPrivate
int layerOrder = 0;
double volume = 100;
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
Target::BubbleType bubbleType = Target::BubbleType::Say;
std::string bubbleText;
};

} // namespace libscratchcpp
2 changes: 2 additions & 0 deletions test/mocks/spritehandlermock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SpriteHandlerMock : public ISpriteHandler
MOCK_METHOD(void, onLayerOrderChanged, (int), (override));
MOCK_METHOD(void, onGraphicsEffectChanged, (IGraphicsEffect *, double), (override));
MOCK_METHOD(void, onGraphicsEffectsCleared, (), (override));
MOCK_METHOD(void, onBubbleTypeChanged, (Target::BubbleType), (override));
MOCK_METHOD(void, onBubbleTextChanged, (const std::string &), (override));

MOCK_METHOD(Rect, boundingRect, (), (const, override));
};
2 changes: 2 additions & 0 deletions test/mocks/stagehandlermock.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class StageHandlerMock : public IStageHandler
MOCK_METHOD(void, onVideoTransparencyChanged, (int), (override));
MOCK_METHOD(void, onGraphicsEffectChanged, (IGraphicsEffect *, double), (override));
MOCK_METHOD(void, onGraphicsEffectsCleared, (), (override));
MOCK_METHOD(void, onBubbleTypeChanged, (Target::BubbleType), (override));
MOCK_METHOD(void, onBubbleTextChanged, (const std::string &), (override));
};
24 changes: 24 additions & 0 deletions test/scratch_classes/sprite_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,27 @@ TEST(SpriteTest, GraphicsEffects)
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 0);
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), 0);
}

TEST(SpriteTest, BubbleType)
{
Sprite sprite;
ASSERT_EQ(sprite.bubbleType(), Target::BubbleType::Say);

sprite.setBubbleType(Target::BubbleType::Think);
ASSERT_EQ(sprite.bubbleType(), Target::BubbleType::Think);

sprite.setBubbleType(Target::BubbleType::Say);
ASSERT_EQ(sprite.bubbleType(), Target::BubbleType::Say);
}

TEST(SpriteTest, BubbleText)
{
Sprite sprite;
ASSERT_TRUE(sprite.bubbleText().empty());

sprite.setBubbleText("hello");
ASSERT_EQ(sprite.bubbleText(), "hello");

sprite.setBubbleText("world");
ASSERT_EQ(sprite.bubbleText(), "world");
}
24 changes: 24 additions & 0 deletions test/scratch_classes/stage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ TEST(StageTest, GraphicsEffects)
ASSERT_EQ(stage.graphicsEffectValue(&effect2), 0);
}

TEST(StageTest, BubbleType)
{
Stage stage;
ASSERT_EQ(stage.bubbleType(), Target::BubbleType::Say);

stage.setBubbleType(Target::BubbleType::Think);
ASSERT_EQ(stage.bubbleType(), Target::BubbleType::Think);

stage.setBubbleType(Target::BubbleType::Say);
ASSERT_EQ(stage.bubbleType(), Target::BubbleType::Say);
}

TEST(StageTest, BubbleText)
{
Stage stage;
ASSERT_TRUE(stage.bubbleText().empty());

stage.setBubbleText("hello");
ASSERT_EQ(stage.bubbleText(), "hello");

stage.setBubbleText("world");
ASSERT_EQ(stage.bubbleText(), "world");
}

TEST(StageTest, LayerOrder)
{
Stage stage;
Expand Down
24 changes: 24 additions & 0 deletions test/scratch_classes/target_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,30 @@ TEST(TargetTest, GraphicsEffects)
ASSERT_EQ(target.graphicsEffectValue(&effect2), 0);
}

TEST(TargetTest, BubbleType)
{
Target target;
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);

target.setBubbleType(Target::BubbleType::Think);
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Think);

target.setBubbleType(Target::BubbleType::Say);
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);
}

TEST(TargetTest, BubbleText)
{
Target target;
ASSERT_TRUE(target.bubbleText().empty());

target.setBubbleText("hello");
ASSERT_EQ(target.bubbleText(), "hello");

target.setBubbleText("world");
ASSERT_EQ(target.bubbleText(), "world");
}

TEST(TargetTest, Engine)
{
Target target;
Expand Down
16 changes: 16 additions & 0 deletions test/target_interfaces/ispritehandler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ TEST_F(ISpriteHandlerTest, GraphicsEffects)
m_sprite.clearGraphicsEffects();
}

TEST_F(ISpriteHandlerTest, BubbleType)
{
EXPECT_CALL(m_handler, onBubbleTypeChanged(Target::BubbleType::Say));
m_sprite.setBubbleType(Target::BubbleType::Say);

EXPECT_CALL(m_handler, onBubbleTypeChanged(Target::BubbleType::Think));
m_sprite.setBubbleType(Target::BubbleType::Think);
}

TEST_F(ISpriteHandlerTest, BubbleText)
{
EXPECT_CALL(m_handler, onBubbleTextChanged("test"));
EXPECT_CALL(m_engine, requestRedraw());
m_sprite.setBubbleText("test");
}

TEST_F(ISpriteHandlerTest, BoundingRect)
{
EXPECT_CALL(m_handler, boundingRect()).WillOnce(Return(Rect(-44.6, 89.1, 20.5, -0.48)));
Expand Down
16 changes: 16 additions & 0 deletions test/target_interfaces/istagehandler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ TEST_F(IStageHandlerTest, GraphicsEffects)
EXPECT_CALL(m_engine, requestRedraw());
m_stage.clearGraphicsEffects();
}

TEST_F(IStageHandlerTest, BubbleType)
{
EXPECT_CALL(m_handler, onBubbleTypeChanged(Target::BubbleType::Say));
m_stage.setBubbleType(Target::BubbleType::Say);

EXPECT_CALL(m_handler, onBubbleTypeChanged(Target::BubbleType::Think));
m_stage.setBubbleType(Target::BubbleType::Think);
}

TEST_F(IStageHandlerTest, BubbleText)
{
EXPECT_CALL(m_handler, onBubbleTextChanged("test"));
EXPECT_CALL(m_engine, requestRedraw());
m_stage.setBubbleText("test");
}