|
| 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 | +} |
0 commit comments