Skip to content

Commit 003121c

Browse files
committed
Add text bubble shape component
1 parent 8c6a502 commit 003121c

File tree

12 files changed

+513
-0
lines changed

12 files changed

+513
-0
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ qt_add_qml_module(scratchcpp-render
6262
shadermanager.h
6363
graphicseffect.cpp
6464
graphicseffect.h
65+
textbubbleshape.cpp
66+
textbubbleshape.h
67+
textbubblepainter.cpp
68+
textbubblepainter.h
6569
blocks/penextension.cpp
6670
blocks/penextension.h
6771
blocks/penblocks.cpp

src/textbubblepainter.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include "textbubblepainter.h"
4+
#include "textbubbleshape.h"
5+
6+
using namespace scratchcpprender;
7+
8+
// https://github.com/scratchfoundation/scratch-render/blob/ac935423afe3ba79235750eecb1e443474c6eb09/src/TextBubbleSkin.js#L7-L26
9+
static const int STROKE_WIDTH = 4;
10+
static const int CORNER_RADIUS = 16;
11+
static const int TAIL_HEIGHT = 12;
12+
static const QNanoColor BUBBLE_FILL_COLOR = QNanoColor(255, 255, 255);
13+
static const QNanoColor BUBBLE_STROKE_COLOR = QNanoColor(0, 0, 0, 38);
14+
15+
static const double pi = std::acos(-1); // TODO: Use std::numbers::pi in C++20
16+
17+
void TextBubblePainter::paint(QNanoPainter *painter)
18+
{
19+
// https://github.com/scratchfoundation/scratch-render/blob/ac935423afe3ba79235750eecb1e443474c6eb09/src/TextBubbleSkin.js#L149-L242
20+
if (!m_item)
21+
return;
22+
23+
const double scale = m_item->stageScale();
24+
const double width = m_item->nativeWidth() - STROKE_WIDTH;
25+
const double height = m_item->nativeHeight() - STROKE_WIDTH - TAIL_HEIGHT;
26+
27+
painter->resetTransform();
28+
painter->scale(scale, scale);
29+
painter->translate(STROKE_WIDTH * 0.5, STROKE_WIDTH * 0.5);
30+
31+
// If the text bubble points leftward, flip the canvas
32+
painter->save();
33+
34+
if (m_item->onSpriteRight()) {
35+
painter->scale(-1, 1);
36+
painter->translate(-width, 0);
37+
}
38+
39+
// Draw the bubble's rounded borders
40+
painter->beginPath();
41+
painter->moveTo(CORNER_RADIUS, height);
42+
painter->arcTo(0, height, 0, height - CORNER_RADIUS, CORNER_RADIUS);
43+
painter->arcTo(0, 0, width, 0, CORNER_RADIUS);
44+
painter->arcTo(width, 0, width, height, CORNER_RADIUS);
45+
painter->arcTo(width, height, width - CORNER_RADIUS, height, CORNER_RADIUS);
46+
47+
// Translate the canvas so we don't have to do a bunch of width/height arithmetic
48+
painter->save();
49+
painter->translate(width - CORNER_RADIUS, height);
50+
51+
// Draw the bubble's "tail"
52+
if (m_item->type() == TextBubbleShape::Type::Say) {
53+
// For a speech bubble, draw one swoopy thing
54+
painter->bezierTo(0, 4, 4, 8, 4, 10);
55+
painter->arcTo(4, 12, 2, 12, 2);
56+
painter->bezierTo(-1, 12, -11, 8, -16, 0);
57+
58+
painter->closePath();
59+
} else {
60+
// For a thinking bubble, draw a partial circle attached to the bubble...
61+
painter->arc(-16, 0, 4, 0, pi);
62+
63+
painter->closePath();
64+
65+
// and two circles detached from it
66+
painter->moveTo(-7, 7.25);
67+
painter->arc(-9.25, 7.25, 2.25, 0, pi * 2);
68+
69+
painter->moveTo(0, 9.5);
70+
painter->arc(-1.5, 9.5, 1.5, 0, pi * 2);
71+
}
72+
73+
// Un-translate the canvas and fill + stroke the text bubble
74+
painter->restore();
75+
76+
painter->setFillStyle(BUBBLE_FILL_COLOR);
77+
painter->setStrokeStyle(BUBBLE_STROKE_COLOR);
78+
painter->setLineWidth(STROKE_WIDTH);
79+
80+
painter->stroke();
81+
painter->fill();
82+
}
83+
84+
void TextBubblePainter::synchronize(QNanoQuickItem *item)
85+
{
86+
Q_ASSERT(item);
87+
m_item = static_cast<TextBubbleShape *>(item);
88+
}

src/textbubblepainter.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <qnanoquickitempainter.h>
6+
7+
namespace scratchcpprender
8+
{
9+
10+
class TextBubbleShape;
11+
12+
class TextBubblePainter : public QNanoQuickItemPainter
13+
{
14+
public:
15+
void paint(QNanoPainter *painter) override;
16+
void synchronize(QNanoQuickItem *item) override;
17+
18+
private:
19+
TextBubbleShape *m_item = nullptr;
20+
};
21+
22+
} // namespace scratchcpprender

src/textbubbleshape.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include "textbubbleshape.h"
4+
#include "textbubblepainter.h"
5+
6+
using namespace scratchcpprender;
7+
8+
TextBubbleShape::TextBubbleShape(QQuickItem *parent) :
9+
QNanoQuickItem(parent)
10+
{
11+
}
12+
13+
QNanoQuickItemPainter *TextBubbleShape::createItemPainter() const
14+
{
15+
return new TextBubblePainter;
16+
}
17+
18+
TextBubbleShape::Type TextBubbleShape::type() const
19+
{
20+
return m_type;
21+
}
22+
23+
void TextBubbleShape::setType(Type newType)
24+
{
25+
if (m_type == newType)
26+
return;
27+
28+
m_type = newType;
29+
update();
30+
emit typeChanged();
31+
}
32+
33+
bool TextBubbleShape::onSpriteRight() const
34+
{
35+
return m_onSpriteRight;
36+
}
37+
38+
void TextBubbleShape::setOnSpriteRight(bool newOnSpriteRight)
39+
{
40+
if (m_onSpriteRight == newOnSpriteRight)
41+
return;
42+
43+
m_onSpriteRight = newOnSpriteRight;
44+
update();
45+
emit onSpriteRightChanged();
46+
}
47+
48+
double TextBubbleShape::stageScale() const
49+
{
50+
return m_stageScale;
51+
}
52+
53+
void TextBubbleShape::setStageScale(double newStageScale)
54+
{
55+
if (qFuzzyCompare(m_stageScale, newStageScale))
56+
return;
57+
58+
m_stageScale = newStageScale;
59+
setWidth(m_nativeWidth * m_stageScale);
60+
setHeight(m_nativeHeight * m_stageScale);
61+
emit stageScaleChanged();
62+
}
63+
64+
double TextBubbleShape::nativeWidth() const
65+
{
66+
return m_nativeWidth;
67+
}
68+
69+
void TextBubbleShape::setNativeWidth(double newNativeWidth)
70+
{
71+
if (qFuzzyCompare(m_nativeWidth, newNativeWidth))
72+
return;
73+
74+
m_nativeWidth = newNativeWidth;
75+
setWidth(m_nativeWidth * m_stageScale);
76+
emit nativeWidthChanged();
77+
}
78+
79+
double TextBubbleShape::nativeHeight() const
80+
{
81+
return m_nativeHeight;
82+
}
83+
84+
void TextBubbleShape::setNativeHeight(double newNativeHeight)
85+
{
86+
if (qFuzzyCompare(m_nativeHeight, newNativeHeight))
87+
return;
88+
89+
m_nativeHeight = newNativeHeight;
90+
setHeight(m_nativeHeight * m_stageScale);
91+
emit nativeHeightChanged();
92+
}

src/textbubbleshape.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <qnanoquickitem.h>
6+
7+
namespace scratchcpprender
8+
{
9+
10+
class TextBubbleShape : public QNanoQuickItem
11+
{
12+
Q_OBJECT
13+
QML_ELEMENT
14+
Q_PROPERTY(Type type READ type WRITE setType NOTIFY typeChanged)
15+
Q_PROPERTY(bool onSpriteRight READ onSpriteRight WRITE setOnSpriteRight NOTIFY onSpriteRightChanged)
16+
Q_PROPERTY(double stageScale READ stageScale WRITE setStageScale NOTIFY stageScaleChanged)
17+
Q_PROPERTY(double nativeWidth READ nativeWidth WRITE setNativeWidth NOTIFY nativeWidthChanged)
18+
Q_PROPERTY(double nativeHeight READ nativeHeight WRITE setNativeHeight NOTIFY nativeHeightChanged)
19+
20+
public:
21+
enum class Type
22+
{
23+
Say,
24+
Think
25+
};
26+
27+
Q_ENUM(Type)
28+
29+
TextBubbleShape(QQuickItem *parent = nullptr);
30+
31+
Type type() const;
32+
void setType(Type newType);
33+
34+
bool onSpriteRight() const;
35+
void setOnSpriteRight(bool newOnSpriteRight);
36+
37+
double stageScale() const;
38+
void setStageScale(double newStageScale);
39+
40+
double nativeWidth() const;
41+
void setNativeWidth(double newNativeWidth);
42+
43+
double nativeHeight() const;
44+
void setNativeHeight(double newNativeHeight);
45+
46+
signals:
47+
void typeChanged();
48+
void onSpriteRightChanged();
49+
void stageScaleChanged();
50+
void nativeWidthChanged();
51+
void nativeHeightChanged();
52+
53+
protected:
54+
QNanoQuickItemPainter *createItemPainter() const override;
55+
56+
private:
57+
Type m_type = Type::Say;
58+
bool m_onSpriteRight = true;
59+
double m_stageScale = 1;
60+
double m_nativeWidth = 0;
61+
double m_nativeHeight = 0;
62+
};
63+
64+
} // namespace scratchcpprender

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ add_subdirectory(penlayerpainter)
3838
add_subdirectory(blocks)
3939
add_subdirectory(graphicseffect)
4040
add_subdirectory(shadermanager)
41+
add_subdirectory(textbubbleshape)
42+
add_subdirectory(textbubblepainter)

test/say_bubble.png

1.84 KB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_executable(
2+
textbubblepainter_test
3+
textbubblepainter_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
textbubblepainter_test
8+
GTest::gtest_main
9+
scratchcpp-render
10+
${QT_LIBS}
11+
qnanopainter
12+
)
13+
14+
add_test(textbubblepainter_test)
15+
gtest_discover_tests(textbubblepainter_test)

0 commit comments

Comments
 (0)