Skip to content

Commit cc14eae

Browse files
committed
Use TextBubble class for text bubbles
1 parent d68cc9e commit cc14eae

22 files changed

+160
-387
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ 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-
6862
/*! Used to get the current costume width. */
6963
virtual int costumeWidth() const = 0;
7064

include/scratchcpp/istagehandler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
4040
/*! Called when all graphics effects are cleared. */
4141
virtual void onGraphicsEffectsCleared() = 0;
4242

43-
/*! Called when the bubble type changes. */
44-
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;
45-
46-
/*! Called when the bubble text changes. */
47-
virtual void onBubbleTextChanged(const std::string &text) = 0;
48-
4943
/*! Used to get the current costume width. */
5044
virtual int costumeWidth() const = 0;
5145

include/scratchcpp/sprite.h

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

8989
void clearGraphicsEffects() override;
9090

91-
virtual void setBubbleType(Target::BubbleType type) override;
92-
virtual void setBubbleText(const std::string &text) override;
93-
9491
private:
9592
Target *dataSource() const override;
9693
bool touchingClones(const std::vector<Sprite *> &clones) const override;

include/scratchcpp/stage.h

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

6363
void clearGraphicsEffects() override;
6464

65-
virtual void setBubbleType(Target::BubbleType type) override;
66-
virtual void setBubbleText(const std::string &text) override;
67-
6865
private:
6966
bool touchingClones(const std::vector<Sprite *> &clones) const override;
7067

include/scratchcpp/target.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@ class Comment;
1919
class Costume;
2020
class Sound;
2121
class Sprite;
22+
class TextBubble;
2223
class IGraphicsEffect;
2324
class TargetPrivate;
2425

2526
/*! \brief The Target class is the Stage or a Sprite. */
2627
class LIBSCRATCHCPP_EXPORT Target : public Drawable
2728
{
2829
public:
29-
enum class BubbleType
30-
{
31-
Say,
32-
Think
33-
};
34-
3530
Target();
3631
Target(const Target &) = delete;
3732
virtual ~Target() { }
@@ -104,11 +99,8 @@ class LIBSCRATCHCPP_EXPORT Target : public Drawable
10499

105100
virtual void clearGraphicsEffects();
106101

107-
BubbleType bubbleType() const;
108-
virtual void setBubbleType(BubbleType type);
109-
110-
const std::string &bubbleText() const;
111-
virtual void setBubbleText(const std::string &text);
102+
TextBubble *bubble();
103+
const TextBubble *bubble() const;
112104

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

src/blocks/looksblocks.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void LooksBlocks::onInit(IEngine *engine)
115115
const auto &targets = engine->targets();
116116

117117
for (auto target : targets) {
118-
target->setBubbleText("");
118+
target->bubble()->setText("");
119119
target->clearGraphicsEffects();
120120
}
121121
});
@@ -520,13 +520,13 @@ bool LooksBlocks::wait(VirtualMachine *vm)
520520
}
521521
}
522522

523-
void LooksBlocks::showBubble(VirtualMachine *vm, Target::BubbleType type, const std::string &text)
523+
void LooksBlocks::showBubble(VirtualMachine *vm, TextBubble::Type type, const std::string &text)
524524
{
525525
Target *target = vm->target();
526526

527527
if (target) {
528-
target->setBubbleType(type);
529-
target->setBubbleText(text);
528+
target->bubble()->setType(type);
529+
target->bubble()->setText(text);
530530
m_waitingBubbles.erase(target);
531531
}
532532
}
@@ -536,7 +536,7 @@ void LooksBlocks::hideBubble(Target *target)
536536
if (!target)
537537
return;
538538

539-
target->setBubbleText("");
539+
target->bubble()->setText("");
540540
m_waitingBubbles.erase(target);
541541
}
542542

@@ -545,7 +545,7 @@ unsigned int LooksBlocks::startSayForSecs(VirtualMachine *vm)
545545
Target *target = vm->target();
546546

547547
if (target) {
548-
showBubble(vm, Target::BubbleType::Say, vm->getInput(0, 2)->toString());
548+
showBubble(vm, TextBubble::Type::Say, vm->getInput(0, 2)->toString());
549549
m_waitingBubbles[target] = vm;
550550
startWait(vm, vm->getInput(1, 2)->toDouble());
551551
}
@@ -572,7 +572,7 @@ unsigned int LooksBlocks::sayForSecs(VirtualMachine *vm)
572572

573573
unsigned int LooksBlocks::say(VirtualMachine *vm)
574574
{
575-
showBubble(vm, Target::BubbleType::Say, vm->getInput(0, 1)->toString());
575+
showBubble(vm, TextBubble::Type::Say, vm->getInput(0, 1)->toString());
576576
return 1;
577577
}
578578

@@ -581,7 +581,7 @@ unsigned int LooksBlocks::startThinkForSecs(VirtualMachine *vm)
581581
Target *target = vm->target();
582582

583583
if (target) {
584-
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 2)->toString());
584+
showBubble(vm, TextBubble::Type::Think, vm->getInput(0, 2)->toString());
585585
m_waitingBubbles[target] = vm;
586586
startWait(vm, vm->getInput(1, 2)->toDouble());
587587
}
@@ -596,7 +596,7 @@ unsigned int LooksBlocks::thinkForSecs(VirtualMachine *vm)
596596

597597
unsigned int LooksBlocks::think(VirtualMachine *vm)
598598
{
599-
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 1)->toString());
599+
showBubble(vm, TextBubble::Type::Think, vm->getInput(0, 1)->toString());
600600
return 1;
601601
}
602602

src/blocks/looksblocks.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <scratchcpp/iextension.h>
66
#include <scratchcpp/target.h>
7+
#include <scratchcpp/textbubble.h>
78
#include <vector>
89
#include <unordered_map>
910
#include <chrono>
@@ -91,7 +92,7 @@ class LooksBlocks : public IExtension
9192

9293
static void startWait(VirtualMachine *vm, double secs);
9394
static bool wait(VirtualMachine *vm);
94-
static void showBubble(VirtualMachine *vm, Target::BubbleType type, const std::string &text);
95+
static void showBubble(VirtualMachine *vm, TextBubble::Type type, const std::string &text);
9596
static void hideBubble(Target *target);
9697

9798
static unsigned int startSayForSecs(VirtualMachine *vm);

src/blocks/sensingblocks.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <scratchcpp/field.h>
1010
#include <scratchcpp/sprite.h>
1111
#include <scratchcpp/stage.h>
12+
#include <scratchcpp/textbubble.h>
1213
#include <scratchcpp/costume.h>
1314
#include <scratchcpp/variable.h>
1415
#include <scratchcpp/block.h>
@@ -109,7 +110,7 @@ void SensingBlocks::onInit(IEngine *engine)
109110
if (!m_questionList.empty()) {
110111
// Abort the question of this thread if it's currently being displayed
111112
if (m_questionList.front()->vm == thread->vm()) {
112-
thread->target()->setBubbleText("");
113+
thread->target()->bubble()->setText("");
113114
engine->questionAborted()();
114115
}
115116

@@ -672,7 +673,7 @@ void SensingBlocks::onAnswer(const std::string &answer)
672673

673674
// If the target was visible when asked, hide the say bubble unless the target was the stage
674675
if (question->wasVisible && !question->wasStage)
675-
vm->target()->setBubbleText("");
676+
vm->target()->bubble()->setText("");
676677

677678
m_questionList.erase(m_questionList.begin());
678679
vm->resolvePromise();
@@ -1084,8 +1085,8 @@ void SensingBlocks::askNextQuestion()
10841085
// If the target is visible, emit a blank question and show
10851086
// a bubble unless the target was the stage
10861087
if (question->wasVisible && !question->wasStage) {
1087-
target->setBubbleType(Target::BubbleType::Say);
1088-
target->setBubbleText(question->question);
1088+
target->bubble()->setType(TextBubble::Type::Say);
1089+
target->bubble()->setText(question->question);
10891090

10901091
engine->questionAsked()("");
10911092
} else {

src/scratch/sprite.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <scratchcpp/costume.h>
99
#include <scratchcpp/sound.h>
1010
#include <scratchcpp/rect.h>
11+
#include <scratchcpp/textbubble.h>
1112
#include <cassert>
1213
#include <iostream>
1314

@@ -20,6 +21,14 @@ Sprite::Sprite() :
2021
Target(),
2122
impl(spimpl::make_unique_impl<SpritePrivate>(this))
2223
{
24+
bubble()->textChanged().connect([this](const std::string &text) {
25+
if (impl->visible && !text.empty()) {
26+
IEngine *eng = engine();
27+
28+
if (eng)
29+
eng->requestRedraw();
30+
}
31+
});
2332
}
2433

2534
/*! Sets the sprite interface. */
@@ -544,32 +553,6 @@ void Sprite::clearGraphicsEffects()
544553
impl->iface->onGraphicsEffectsCleared();
545554
}
546555

547-
/*! Overrides Target#setBubbleType(). */
548-
void Sprite::setBubbleType(BubbleType type)
549-
{
550-
Target::setBubbleType(type);
551-
552-
if (impl->iface)
553-
impl->iface->onBubbleTypeChanged(type);
554-
}
555-
556-
/*! Overrides Target#setBubbleText(). */
557-
void Sprite::setBubbleText(const std::string &text)
558-
{
559-
Target::setBubbleText(impl->visible ? text : "");
560-
const std::string &finalText = bubbleText();
561-
562-
if (impl->visible && !finalText.empty()) {
563-
IEngine *eng = engine();
564-
565-
if (eng)
566-
eng->requestRedraw();
567-
}
568-
569-
if (impl->iface)
570-
impl->iface->onBubbleTextChanged(impl->visible ? finalText : "");
571-
}
572-
573556
Target *Sprite::dataSource() const
574557
{
575558
return impl->cloneSprite;

src/scratch/stage.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/stage.h>
44
#include <scratchcpp/istagehandler.h>
55
#include <scratchcpp/iengine.h>
6+
#include <scratchcpp/textbubble.h>
67
#include <cassert>
78

89
#include "stage_p.h"
@@ -14,6 +15,14 @@ Stage::Stage() :
1415
Target(),
1516
impl(spimpl::make_unique_impl<StagePrivate>())
1617
{
18+
bubble()->textChanged().connect([this](const std::string &text) {
19+
if (!text.empty()) {
20+
IEngine *eng = engine();
21+
22+
if (eng)
23+
eng->requestRedraw();
24+
}
25+
});
1726
}
1827

1928
/*! Sets the stage interface. */
@@ -220,32 +229,6 @@ void Stage::clearGraphicsEffects()
220229
impl->iface->onGraphicsEffectsCleared();
221230
}
222231

223-
/*! Overrides Target#setBubbleType(). */
224-
void Stage::setBubbleType(BubbleType type)
225-
{
226-
Target::setBubbleType(type);
227-
228-
if (impl->iface)
229-
impl->iface->onBubbleTypeChanged(type);
230-
}
231-
232-
/*! Overrides Target#setBubbleText(). */
233-
void Stage::setBubbleText(const std::string &text)
234-
{
235-
Target::setBubbleText(text);
236-
const std::string &finalText = bubbleText();
237-
238-
if (!finalText.empty()) {
239-
IEngine *eng = engine();
240-
241-
if (eng)
242-
eng->requestRedraw();
243-
}
244-
245-
if (impl->iface)
246-
impl->iface->onBubbleTextChanged(finalText);
247-
}
248-
249232
bool Stage::touchingClones(const std::vector<Sprite *> &clones) const
250233
{
251234
if (!impl->iface)

0 commit comments

Comments
 (0)