Skip to content

Commit 30f9b09

Browse files
committed
TextBubble: Add owner property
1 parent 69c5643 commit 30f9b09

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

include/scratchcpp/textbubble.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace libscratchcpp
99
{
1010

11+
class Thread;
1112
class TextBubblePrivate;
1213

1314
/*! \brief The TextBubble class represents a text bubble created using say or think block. */
@@ -31,8 +32,11 @@ class LIBSCRATCHCPP_EXPORT TextBubble : public Drawable
3132

3233
const std::string &text() const;
3334
virtual void setText(const std::string &text);
35+
virtual void setText(const std::string &text, Thread *owner);
3436
sigslot::signal<const std::string &> &textChanged() const;
3537

38+
Thread *owner() const;
39+
3640
private:
3741
spimpl::unique_impl_ptr<TextBubblePrivate> impl;
3842
};

src/scratch/textbubble.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ const std::string &TextBubble::text() const
5353
* \note If the text is an empty string, the TextBubble is supposed to be hidden.
5454
*/
5555
void TextBubble::setText(const std::string &text)
56+
{
57+
setText(text, nullptr);
58+
}
59+
60+
void TextBubble::setText(const std::string &text, Thread *owner)
5661
{
5762
// https://github.com/scratchfoundation/scratch-vm/blob/7313ce5199f8a3da7850085d0f7f6a3ca2c89bf6/src/blocks/scratch3_looks.js#L251-L257
5863
const Value v(text);
5964
std::string converted = text;
65+
impl->owner = owner;
6066

6167
// Non-integers should be rounded to 2 decimal places (no more, no less), unless they're small enough that rounding would display them as 0.00.
6268
if (v.isValidNumber()) {
@@ -87,3 +93,9 @@ sigslot::signal<const std::string &> &TextBubble::textChanged() const
8793
{
8894
return impl->textChanged;
8995
}
96+
97+
/*! Returns the thread which currently owns the text bubble. */
98+
Thread *TextBubble::owner() const
99+
{
100+
return impl->owner;
101+
}

src/scratch/textbubble_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct TextBubblePrivate
1111
{
1212
TextBubble::Type type = TextBubble::Type::Say;
1313
std::string text;
14+
Thread *owner = nullptr;
1415
mutable sigslot::signal<TextBubble::Type> typeChanged;
1516
mutable sigslot::signal<const std::string &> textChanged;
1617
};

test/scratch_classes/textbubble_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/textbubble.h>
2+
#include <scratchcpp/thread.h>
23
#include <enginemock.h>
34

45
#include "../common.h"
@@ -33,16 +34,24 @@ TEST(TextBubbleTest, BubbleText)
3334
{
3435
TextBubble bubble;
3536
ASSERT_TRUE(bubble.text().empty());
37+
ASSERT_EQ(bubble.owner(), nullptr);
3638

3739
bubble.setText("hello");
3840
ASSERT_EQ(bubble.text(), "hello");
41+
ASSERT_EQ(bubble.owner(), nullptr);
42+
43+
Thread thread(nullptr, nullptr, nullptr);
44+
bubble.setText("test", &thread);
45+
ASSERT_EQ(bubble.text(), "test");
46+
ASSERT_EQ(bubble.owner(), &thread);
3947

4048
EngineMock engine;
4149
bubble.setEngine(&engine);
4250
EXPECT_CALL(engine, moveDrawableToFront(&bubble));
4351
EXPECT_CALL(engine, requestRedraw());
4452
bubble.setText("world");
4553
ASSERT_EQ(bubble.text(), "world");
54+
ASSERT_EQ(bubble.owner(), nullptr);
4655
bubble.setEngine(nullptr);
4756

4857
// longstr.length = 384, should be limited to 330 in bubble text

0 commit comments

Comments
 (0)