File indexing completed on 2025-04-27 11:33:15

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2012 Filip Wieladek <wattos@gmail.com>
0006     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #pragma once
0012 
0013 #include <kwineffects.h>
0014 
0015 namespace KWin
0016 {
0017 
0018 class TouchPointsEffect
0019     : public Effect
0020 {
0021     Q_OBJECT
0022     Q_PROPERTY(qreal lineWidth READ lineWidth)
0023     Q_PROPERTY(int ringLife READ ringLife)
0024     Q_PROPERTY(int ringSize READ ringSize)
0025     Q_PROPERTY(int ringCount READ ringCount)
0026 public:
0027     TouchPointsEffect();
0028     ~TouchPointsEffect() override;
0029     void prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseconds presentTime) override;
0030     void paintScreen(int mask, const QRegion &region, ScreenPaintData &data) override;
0031     void postPaintScreen() override;
0032     bool isActive() const override;
0033     bool touchDown(qint32 id, const QPointF &pos, std::chrono::microseconds time) override;
0034     bool touchMotion(qint32 id, const QPointF &pos, std::chrono::microseconds time) override;
0035     bool touchUp(qint32 id, std::chrono::microseconds time) override;
0036 
0037     // for properties
0038     qreal lineWidth() const
0039     {
0040         return m_lineWidth;
0041     }
0042     int ringLife() const
0043     {
0044         return m_ringLife;
0045     }
0046     int ringSize() const
0047     {
0048         return m_ringMaxSize;
0049     }
0050     int ringCount() const
0051     {
0052         return m_ringCount;
0053     }
0054 
0055 private:
0056     inline void drawCircle(const QColor &color, float cx, float cy, float r);
0057     inline void paintScreenSetup(int mask, QRegion region, ScreenPaintData &data);
0058     inline void paintScreenFinish(int mask, QRegion region, ScreenPaintData &data);
0059 
0060     void repaint();
0061 
0062     float computeAlpha(int time, int ring);
0063     float computeRadius(int time, bool press, int ring);
0064     void drawCircleGl(const QColor &color, float cx, float cy, float r);
0065     void drawCircleQPainter(const QColor &color, float cx, float cy, float r);
0066     void paintScreenSetupGl(int mask, QRegion region, ScreenPaintData &data);
0067     void paintScreenFinishGl(int mask, QRegion region, ScreenPaintData &data);
0068 
0069     Qt::GlobalColor colorForId(quint32 id);
0070 
0071     int m_ringCount = 2;
0072     float m_lineWidth = 1.0;
0073     int m_ringLife = 300;
0074     float m_ringMaxSize = 20.0;
0075 
0076     struct TouchPoint
0077     {
0078         QPointF pos;
0079         int time = 0;
0080         bool press;
0081         QColor color;
0082     };
0083     QVector<TouchPoint> m_points;
0084     QHash<quint32, QPointF> m_latestPositions;
0085     QHash<quint32, Qt::GlobalColor> m_colors;
0086     std::chrono::milliseconds m_lastPresentTime = std::chrono::milliseconds::zero();
0087 };
0088 
0089 } // namespace