|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | + |
| 3 | +#include "penlayer.h" |
| 4 | +#include "penlayerpainter.h" |
| 5 | +#include "penattributes.h" |
| 6 | + |
| 7 | +using namespace scratchcpprender; |
| 8 | + |
| 9 | +std::unordered_map<libscratchcpp::IEngine *, IPenLayer *> PenLayer::m_projectPenLayers; |
| 10 | + |
| 11 | +PenLayer::PenLayer(QNanoQuickItem *parent) : |
| 12 | + IPenLayer(parent) |
| 13 | +{ |
| 14 | + m_fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); |
| 15 | + m_fboFormat.setSamples(m_antialiasingEnabled ? 4 : 0); |
| 16 | +} |
| 17 | + |
| 18 | +PenLayer::~PenLayer() |
| 19 | +{ |
| 20 | + if (m_engine) |
| 21 | + m_projectPenLayers.erase(m_engine); |
| 22 | +} |
| 23 | + |
| 24 | +bool PenLayer::antialiasingEnabled() const |
| 25 | +{ |
| 26 | + return m_antialiasingEnabled; |
| 27 | +} |
| 28 | + |
| 29 | +void PenLayer::setAntialiasingEnabled(bool enabled) |
| 30 | +{ |
| 31 | + m_antialiasingEnabled = enabled; |
| 32 | + m_fboFormat.setSamples(enabled ? 4 : 0); |
| 33 | +} |
| 34 | + |
| 35 | +libscratchcpp::IEngine *PenLayer::engine() const |
| 36 | +{ |
| 37 | + return m_engine; |
| 38 | +} |
| 39 | + |
| 40 | +void PenLayer::setEngine(libscratchcpp::IEngine *newEngine) |
| 41 | +{ |
| 42 | + if (m_engine == newEngine) |
| 43 | + return; |
| 44 | + |
| 45 | + if (m_engine) |
| 46 | + m_projectPenLayers.erase(m_engine); |
| 47 | + |
| 48 | + m_engine = newEngine; |
| 49 | + |
| 50 | + if (m_engine) { |
| 51 | + m_projectPenLayers[m_engine] = this; |
| 52 | + m_fbo = std::make_unique<QOpenGLFramebufferObject>(m_engine->stageWidth(), m_engine->stageHeight(), m_fboFormat); |
| 53 | + Q_ASSERT(m_fbo->isValid()); |
| 54 | + |
| 55 | + m_paintDevice = std::make_unique<QOpenGLPaintDevice>(m_fbo->size()); |
| 56 | + clear(); |
| 57 | + } |
| 58 | + |
| 59 | + emit engineChanged(); |
| 60 | +} |
| 61 | + |
| 62 | +void scratchcpprender::PenLayer::clear() |
| 63 | +{ |
| 64 | + if (!m_fbo) |
| 65 | + return; |
| 66 | + |
| 67 | + m_fbo->bind(); |
| 68 | + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 69 | + glClear(GL_COLOR_BUFFER_BIT); |
| 70 | + m_fbo->release(); |
| 71 | + |
| 72 | + update(); |
| 73 | +} |
| 74 | + |
| 75 | +void scratchcpprender::PenLayer::drawPoint(const PenAttributes &penAttributes, double x, double y) |
| 76 | +{ |
| 77 | + drawLine(penAttributes, x, y, x, y); |
| 78 | +} |
| 79 | + |
| 80 | +void scratchcpprender::PenLayer::drawLine(const PenAttributes &penAttributes, double x0, double y0, double x1, double y1) |
| 81 | +{ |
| 82 | + if (!m_fbo || !m_paintDevice || !m_engine) |
| 83 | + return; |
| 84 | + |
| 85 | + // Begin painting |
| 86 | + m_fbo->bind(); |
| 87 | + QPainter painter(m_paintDevice.get()); |
| 88 | + painter.beginNativePainting(); |
| 89 | + painter.setRenderHint(QPainter::Antialiasing, m_antialiasingEnabled); |
| 90 | + painter.setRenderHint(QPainter::SmoothPixmapTransform, false); |
| 91 | + |
| 92 | + // Translate to Scratch coordinate system |
| 93 | + double stageWidthHalf = m_engine->stageWidth() / 2; |
| 94 | + double stageHeightHalf = m_engine->stageHeight() / 2; |
| 95 | + x0 += stageWidthHalf; |
| 96 | + y0 = stageHeightHalf - y0; |
| 97 | + x1 += stageWidthHalf; |
| 98 | + y1 = stageHeightHalf - y1; |
| 99 | + |
| 100 | + // Set pen attributes |
| 101 | + QPen pen(penAttributes.color); |
| 102 | + pen.setWidthF(penAttributes.diameter); |
| 103 | + pen.setCapStyle(Qt::RoundCap); |
| 104 | + painter.setPen(pen); |
| 105 | + |
| 106 | + // If the start and end coordinates are the same, draw a point, otherwise draw a line |
| 107 | + if (x0 == x1 && y0 == y1) |
| 108 | + painter.drawPoint(x0, y0); |
| 109 | + else |
| 110 | + painter.drawLine(x0, y0, x1, y1); |
| 111 | + |
| 112 | + // End painting |
| 113 | + painter.endNativePainting(); |
| 114 | + painter.end(); |
| 115 | + m_fbo->release(); |
| 116 | + |
| 117 | + update(); |
| 118 | +} |
| 119 | + |
| 120 | +QOpenGLFramebufferObject *PenLayer::framebufferObject() const |
| 121 | +{ |
| 122 | + return m_fbo.get(); |
| 123 | +} |
| 124 | + |
| 125 | +IPenLayer *PenLayer::getProjectPenLayer(libscratchcpp::IEngine *engine) |
| 126 | +{ |
| 127 | + auto it = m_projectPenLayers.find(engine); |
| 128 | + |
| 129 | + if (it != m_projectPenLayers.cend()) |
| 130 | + return it->second; |
| 131 | + |
| 132 | + return nullptr; |
| 133 | +} |
| 134 | + |
| 135 | +QNanoQuickItemPainter *PenLayer::createItemPainter() const |
| 136 | +{ |
| 137 | + return new PenLayerPainter; |
| 138 | +} |
0 commit comments