Add flashy spiral timer to cover
[harbour-pedalo.git] / qml / cover / ClockView.qml
1 import QtQuick 2.0
2 import Sailfish.Silica 1.0
3
4 Item {
5     id: root
6
7     property QtObject timerClock
8
9     // Size of the clock border decoration as a fraction of the radius
10     property real _faceRadiusBase: 84
11     property real _faceHourLength: 14.736 / _faceRadiusBase
12
13     Image {
14         id: clockFace
15
16         anchors.fill: parent
17         fillMode: Image.PreserveAspectFit
18         source: "image://theme/graphic-clock-face-3"
19     }
20
21     ShaderEffect {
22         id: timerVisualization
23
24         anchors.fill: clockFace
25
26         visible: stopwatch.update
27
28         onVisibleChanged: {
29             _updateTimerVisualization()
30         }
31
32         property int _seconds: stopwatch.seconds
33         on_SecondsChanged: _updateTimerVisualization()
34
35         property real outerRadius: 1
36         property real innerRadius: 1 - _faceHourLength
37         property real endAngle
38         property real loopRadius: outerRadius / 10
39         property real count
40
41         property color highlightColor: Theme.rgba(Theme.highlightColor, 0.75)
42
43         function _updateTimerVisualization() {
44             if (stopwatch.fast) {
45                 count = _seconds
46             }
47             else {
48                 count = Math.floor(_seconds / 60)
49             }
50
51             endAngle = 2 * Math.PI * count / 60
52             innerRadius = outerRadius - loopRadius * (parseInt(count / 60) + 1)
53         }
54
55         vertexShader: "
56             uniform highp mat4 qt_Matrix;
57             attribute highp vec4 qt_Vertex;
58             attribute highp vec2 qt_MultiTexCoord0;
59             varying highp vec2 coord;
60             void main() {
61                 coord = qt_MultiTexCoord0;
62                 gl_Position = qt_Matrix * qt_Vertex;
63             }"
64         fragmentShader: "
65             varying highp vec2 coord;
66             uniform lowp float qt_Opacity;
67             uniform lowp float outerRadius;
68             uniform lowp float innerRadius;
69             uniform lowp float endAngle;
70             uniform lowp float loopRadius;
71             uniform lowp vec4 highlightColor;
72             uniform lowp float count;
73
74             lowp float PI = 3.14159265358979323846264;
75
76             void main() {
77                 highp vec2 vector = 2.0*(coord - vec2(0.5, 0.5));
78                 lowp float radius = length(vector);
79                 lowp float angle = atan(vector.y, vector.x) + PI/2.0;
80                 angle += angle < 0.0 ? 2.0*PI : 0.0;
81
82                 lowp float minRadius = outerRadius - ((count / 60.0) + 1.0) * loopRadius + (loopRadius * angle / (2.0 * PI));
83                 lowp float maxRadius = outerRadius - ((mod(count, 60.0) / 60.0) + 1.0) * loopRadius + (loopRadius * angle / (2.0 * PI));
84
85                 if (angle < 2.0 * PI * mod(count, 60.0) / 60.0) {
86                     maxRadius += loopRadius;
87                 }
88
89                 if (radius >= minRadius && radius < maxRadius) {
90                     if (mod(radius - minRadius, loopRadius) > 0.2 * loopRadius) {
91                         gl_FragColor = highlightColor;
92                         return;
93                     }
94                 }
95                 gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
96                 return;
97             }"
98     }
99 }