1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// SPDX-License-Identifier: GPL-2.0-only
#include "charball.h"
#include <QRandomGenerator>
CharBall::CharBall(int size, int position, QChar character)
: currentRadius(size / 2)
, destroyed(0)
, color(QRandomGenerator::global()->bounded(128, 256),
QRandomGenerator::global()->bounded(128, 256),
QRandomGenerator::global()->bounded(128, 256))
, currentCharacter(character)
{
setPos(position, -2 - size);
}
QRectF CharBall::boundingRect() const
{
return QRectF(-currentRadius, -currentRadius, (2 * currentRadius),
(2 * currentRadius));
}
QPainterPath CharBall::shape() const
{
QPainterPath path;
path.addRect(
-currentRadius, -currentRadius, 2 * currentRadius, 2 * currentRadius);
return path;
}
void CharBall::paint(
QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
{
// Body
painter->setBrush(color);
painter->drawEllipse(
-currentRadius, -currentRadius, 2 * currentRadius, 2 * currentRadius);
if (destroyed != 0) {
destroying();
}
}
QChar CharBall::character() { return currentCharacter; }
void CharBall::destroy()
{
destroyed = 56;
update();
}
void CharBall::destroying()
{
color = QColor(0, 0, 0);
destroyed--;
if (destroyed < 2) {
destroyed = 0;
this->setVisible(false); // delete this;
return;
}
update();
}
|