Skip to content

Commit 1cb6a63

Browse files
authored
Merge pull request #465 from scratchcpp/bubble_api
Add API for bubbles
2 parents 89240e7 + 60e9fc9 commit 1cb6a63

File tree

16 files changed

+221
-0
lines changed

16 files changed

+221
-0
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
5959
/*! Called when all graphics effects are cleared. */
6060
virtual void onGraphicsEffectsCleared() = 0;
6161

62+
/*! Called when the bubble type changes. */
63+
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;
64+
65+
/*! Called when the bubble text changes. */
66+
virtual void onBubbleTextChanged(const std::string &text) = 0;
67+
6268
/*!
6369
* Used to get the bounding rectangle of the sprite.
6470
* \note The rectangle must be relative to the stage, so make sure to use the sprite's coordinates.

include/scratchcpp/istagehandler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
3737

3838
/*! Called when all graphics effects are cleared. */
3939
virtual void onGraphicsEffectsCleared() = 0;
40+
41+
/*! Called when the bubble type changes. */
42+
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;
43+
44+
/*! Called when the bubble text changes. */
45+
virtual void onBubbleTextChanged(const std::string &text) = 0;
4046
};
4147

4248
} // namespace libscratchcpp

include/scratchcpp/sprite.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class LIBSCRATCHCPP_EXPORT Sprite
7676

7777
void clearGraphicsEffects() override;
7878

79+
virtual void setBubbleType(Target::BubbleType type) override;
80+
virtual void setBubbleText(const std::string &text) override;
81+
7982
private:
8083
Target *dataSource() const override;
8184
void setXY(double x, double y);

include/scratchcpp/stage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
5252

5353
void clearGraphicsEffects() override;
5454

55+
virtual void setBubbleType(Target::BubbleType type) override;
56+
virtual void setBubbleText(const std::string &text) override;
57+
5558
private:
5659
spimpl::unique_impl_ptr<StagePrivate> impl;
5760
};

include/scratchcpp/target.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class TargetPrivate;
2424
class LIBSCRATCHCPP_EXPORT Target
2525
{
2626
public:
27+
enum class BubbleType
28+
{
29+
Say,
30+
Think
31+
};
32+
2733
Target();
2834
Target(const Target &) = delete;
2935
virtual ~Target() { }
@@ -83,6 +89,12 @@ class LIBSCRATCHCPP_EXPORT Target
8389

8490
virtual void clearGraphicsEffects();
8591

92+
BubbleType bubbleType() const;
93+
virtual void setBubbleType(BubbleType type);
94+
95+
const std::string &bubbleText() const;
96+
virtual void setBubbleText(const std::string &text);
97+
8698
IEngine *engine() const;
8799
void setEngine(IEngine *engine);
88100

src/scratch/sprite.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,31 @@ void Sprite::clearGraphicsEffects()
429429
impl->iface->onGraphicsEffectsCleared();
430430
}
431431

432+
/*! Overrides Target#setBubbleType(). */
433+
void Sprite::setBubbleType(BubbleType type)
434+
{
435+
Target::setBubbleType(type);
436+
437+
if (impl->iface)
438+
impl->iface->onBubbleTypeChanged(type);
439+
}
440+
441+
/*! Overrides Target#setBubbleText(). */
442+
void Sprite::setBubbleText(const std::string &text)
443+
{
444+
Target::setBubbleText(text);
445+
446+
if (impl->visible && !text.empty()) {
447+
IEngine *eng = engine();
448+
449+
if (eng)
450+
eng->requestRedraw();
451+
}
452+
453+
if (impl->iface)
454+
impl->iface->onBubbleTextChanged(text);
455+
}
456+
432457
Target *Sprite::dataSource() const
433458
{
434459
return impl->cloneSprite;

src/scratch/stage.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,28 @@ void Stage::clearGraphicsEffects()
156156
if (impl->iface)
157157
impl->iface->onGraphicsEffectsCleared();
158158
}
159+
160+
/*! Overrides Target#setBubbleType(). */
161+
void Stage::setBubbleType(BubbleType type)
162+
{
163+
Target::setBubbleType(type);
164+
165+
if (impl->iface)
166+
impl->iface->onBubbleTypeChanged(type);
167+
}
168+
169+
/*! Overrides Target#setBubbleText(). */
170+
void Stage::setBubbleText(const std::string &text)
171+
{
172+
Target::setBubbleText(text);
173+
174+
if (!text.empty()) {
175+
IEngine *eng = engine();
176+
177+
if (eng)
178+
eng->requestRedraw();
179+
}
180+
181+
if (impl->iface)
182+
impl->iface->onBubbleTextChanged(text);
183+
}

src/scratch/target.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,36 @@ void Target::clearGraphicsEffects()
439439
impl->graphicsEffects.clear();
440440
}
441441

442+
/*! Returns the type of the bubble (say or think). */
443+
Target::BubbleType Target::bubbleType() const
444+
{
445+
return impl->bubbleType;
446+
}
447+
448+
/*! Sets the type of the bubble (say or think). */
449+
void Target::setBubbleType(BubbleType type)
450+
{
451+
impl->bubbleType = type;
452+
}
453+
454+
/*!
455+
* Returns the text of the bubble.
456+
* \note If the text is an empty string, the bubble is supposed to be hidden.
457+
*/
458+
const std::string &Target::bubbleText() const
459+
{
460+
return impl->bubbleText;
461+
}
462+
463+
/*!
464+
* Sets the text of the bubble.
465+
* \note If the text is an empty string, the bubble is supposed to be hidden.
466+
*/
467+
void Target::setBubbleText(const std::string &text)
468+
{
469+
impl->bubbleText = text;
470+
}
471+
442472
/*! Returns the engine. */
443473
IEngine *Target::engine() const
444474
{

src/scratch/target_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <unordered_map>
99
#include <scratchcpp/costume.h>
1010
#include <scratchcpp/sound.h>
11+
#include <scratchcpp/target.h>
1112

1213
namespace libscratchcpp
1314
{
@@ -36,6 +37,8 @@ struct TargetPrivate
3637
int layerOrder = 0;
3738
double volume = 100;
3839
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
40+
Target::BubbleType bubbleType = Target::BubbleType::Say;
41+
std::string bubbleText;
3942
};
4043

4144
} // namespace libscratchcpp

test/mocks/spritehandlermock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class SpriteHandlerMock : public ISpriteHandler
2525
MOCK_METHOD(void, onLayerOrderChanged, (int), (override));
2626
MOCK_METHOD(void, onGraphicsEffectChanged, (IGraphicsEffect *, double), (override));
2727
MOCK_METHOD(void, onGraphicsEffectsCleared, (), (override));
28+
MOCK_METHOD(void, onBubbleTypeChanged, (Target::BubbleType), (override));
29+
MOCK_METHOD(void, onBubbleTextChanged, (const std::string &), (override));
2830

2931
MOCK_METHOD(Rect, boundingRect, (), (const, override));
3032
};

0 commit comments

Comments
 (0)