File: charball.cpp

package info (click to toggle)
tipp10 3.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,984 kB
  • sloc: cpp: 8,343; xml: 70; ansic: 60; makefile: 11
file content (67 lines) | stat: -rw-r--r-- 1,403 bytes parent folder | download | duplicates (2)
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();
}