68 lines
1.7 KiB
C++
68 lines
1.7 KiB
C++
#include "blobshape2.hpp"
|
|
#include "blobgroup2.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
BlobShape2::BlobShape2(QQuickItem* parent) : QQuickItem(parent) {}
|
|
|
|
void BlobShape2::setGroup(BlobGroup2* g) {
|
|
if (m_group == g) return;
|
|
if (m_group && isComponentComplete()) unregisterFromGroup();
|
|
m_group = g;
|
|
if (m_group && isComponentComplete()) registerWithGroup();
|
|
emit groupChanged();
|
|
if (m_group) m_group->markDirty();
|
|
}
|
|
|
|
void BlobShape2::setRadius(qreal r) {
|
|
if (qFuzzyCompare(m_radius, r)) return;
|
|
m_radius = r;
|
|
emit radiusChanged();
|
|
if (m_group) m_group->markDirty();
|
|
}
|
|
|
|
void BlobShape2::componentComplete() {
|
|
QQuickItem::componentComplete();
|
|
if (m_group) registerWithGroup();
|
|
}
|
|
|
|
void BlobShape2::geometryChange(
|
|
const QRectF& newGeometry, const QRectF& oldGeometry) {
|
|
QQuickItem::geometryChange(newGeometry, oldGeometry);
|
|
updateCenteredDeformMatrix();
|
|
|
|
// One draw covers the whole group, so any shape change is a group change.
|
|
if (m_group) m_group->markDirty();
|
|
}
|
|
|
|
void BlobShape2::updateCenteredDeformMatrix() {
|
|
const auto cx = static_cast<float>(width()) * 0.5f;
|
|
const auto cy = static_cast<float>(height()) * 0.5f;
|
|
QMatrix4x4 result;
|
|
result.translate(cx, cy);
|
|
result *= m_deformMatrix;
|
|
result.translate(-cx, -cy);
|
|
if (m_centeredDeformMatrix != result) {
|
|
m_centeredDeformMatrix = result;
|
|
emit deformMatrixChanged();
|
|
}
|
|
}
|
|
|
|
void BlobShape2::cornerRadii(float out[4]) const {
|
|
const auto maxR = static_cast<float>(std::min(width(), height())) * 0.5f;
|
|
const auto r = std::min(static_cast<float>(m_radius), maxR);
|
|
out[0] = r;
|
|
out[1] = r;
|
|
out[2] = r;
|
|
out[3] = r;
|
|
}
|
|
|
|
void BlobShape2::registerWithGroup() {
|
|
if (m_group) m_group->addShape(this);
|
|
}
|
|
|
|
void BlobShape2::unregisterFromGroup() {
|
|
if (m_group) m_group->removeShape(this);
|
|
}
|