1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #ifndef STROKEMANAGER_H
19 #define STROKEMANAGER_H
20 
21 #include <ctime>
22 #include <QQueue>
23 #include <QPointF>
24 #include <QList>
25 #include <QTimer>
26 #include <QElapsedTimer>
27 #include "object.h"
28 
29 
30 class PointerEvent;
31 
32 class StrokeManager : public QObject
33 {
34 public:
35     StrokeManager();
36 
37     void pointerPressEvent(PointerEvent* event);
38     void pointerMoveEvent(PointerEvent* event);
39     void pointerReleaseEvent(PointerEvent* event);
40     void setPressure(float pressure);
41     void setStabilizerLevel(int level);
42 
getPressure()43     float getPressure() { return mTabletPressure; }
getStabilizerLevel()44     int getStabilizerLevel() { return mStabilizerLevel; }
isTabletInUse()45     bool isTabletInUse() { return mTabletInUse; }
setTabletInUse(bool inUse)46     void setTabletInUse(bool inUse) { mTabletInUse = inUse; }
isActive()47     bool isActive() { return mStrokeStarted; }
48 
49     QList<QPointF> interpolateStroke();
50     void interpolatePoll();
51     QPointF interpolateStart(QPointF firstPoint);
52     void interpolatePollAndPaint();
53     void interpolateEnd();
54     void smoothMousePos(QPointF pos);
55     QList<QPointF> meanInpolOp(QList<QPointF> points, qreal x, qreal y, qreal pressure);
56     QList<QPointF> noInpolOp(QList<QPointF> points);
57     QList<QPointF> tangentInpolOp(QList<QPointF> points);
58 
getLastPressPixel()59     QPointF getLastPressPixel() const { return mLastPressPixel; }
getCurrentPixel()60     QPointF getCurrentPixel() const { return mCurrentPixel; }
getLastPixel()61     QPointF getLastPixel() const { return mLastPixel; }
getLastMeanPixel()62     QPointF getLastMeanPixel() const { return mLastInterpolated; }
getMousePos()63     QPointF getMousePos() const { return mousePos; }
getCurrentPressPixel()64     QPointF getCurrentPressPixel() const { return mCurrentPressPixel; }
65 
66 private:
67     static const int STROKE_QUEUE_LENGTH = 3; // 4 points for cubic bezier
68 
69     void reset();
70 
71     float pressure = 1.0f; // last pressure
72     QQueue<QPointF> strokeQueue;
73     QQueue<qreal> pressureQueue;
74 
75     QTimer timer;
76 
77     QElapsedTimer mSingleshotTime;
78     QPointF mCurrentPressPixel = { 0, 0 };
79     QPointF mLastPressPixel2 = { 0, 0 };
80     QPointF mLastPressPixel = { 0, 0 };
81     QPointF mCurrentPixel = { 0, 0 };
82     QPointF mLastPixel = { 0, 0 };
83     QPointF mLastInterpolated = { 0, 0 };
84     QPointF mousePos = { 0, 0 };
85 
86     QPointF m_previousTangent;
87     bool    mHasTangent = false;
88     int     previousTime = 0;
89     bool    mStrokeStarted = false;
90     bool    mTabletInUse = false;
91     float   mTabletPressure = 1.f;
92     int     mStabilizerLevel = 0;
93 
94     clock_t m_timeshot;
95 };
96 
97 #endif // STROKEMANAGER_H
98