generated from android/android-dev-challenge-compose
-
Notifications
You must be signed in to change notification settings - Fork 28
/
ConfigurationHint.kt
170 lines (157 loc) · 5.7 KB
/
ConfigurationHint.kt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("PrivatePropertyName")
package com.mobnetic.newtonstimer.configuration
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.clipPath
import androidx.compose.ui.unit.dp
import com.mobnetic.newtonstimer.balls.BallSize
import com.mobnetic.newtonstimer.timer.NewtonsTimerViewModel
@Composable
fun ConfigurationHint(
ifConfigured: Boolean,
configuredAngle: Float,
ballSize: BallSize,
) {
val color by animateColorAsState(MaterialTheme.colors.onBackground)
val animateStartAngle by animatedHintArcStartAngle()
val animatedAlpha by animateFloatAsState(if (ifConfigured) 0f else 1f)
Box(
Modifier.drawBehind {
if (animatedAlpha == 0f) return@drawBehind
drawHintArc(
ballSize = ballSize,
sweepAngle = NewtonsTimerViewModel.MAX_ANGLE,
color = color,
alpha = animatedAlpha * 0.3f
)
if (configuredAngle > 0f) {
drawHintArc(
ballSize = ballSize,
sweepAngle = configuredAngle,
color = color,
alpha = animatedAlpha
)
} else {
drawAnimatedHintArc(
ballSize = ballSize,
animatedHintStartAngle = animateStartAngle,
color = color,
alpha = animatedAlpha
)
}
}
)
}
private fun DrawScope.drawHintArc(
ballSize: BallSize,
sweepAngle: Float,
color: Color,
alpha: Float = 1f,
) {
val dashInterval = ARC_STROKE_DASH_INTERVAL.toPx()
drawArc(
color = color,
startAngle = 90f,
sweepAngle = sweepAngle,
topLeft = ballSize.arcOffset(),
size = ballSize.arcSize(),
alpha = alpha,
useCenter = false,
style = Stroke(
ARC_STROKE_WIDTH.toPx(),
pathEffect = PathEffect.dashPathEffect(floatArrayOf(dashInterval, dashInterval))
)
)
}
private fun DrawScope.drawAnimatedHintArc(
ballSize: BallSize,
animatedHintStartAngle: Float,
color: Color,
alpha: Float = 1f,
) {
clipPath(animatedHintClipPath(ballSize, animatedStartAngle = animatedHintStartAngle)) {
drawHintArc(
ballSize = ballSize,
sweepAngle = NewtonsTimerViewModel.MAX_ANGLE,
color = color,
alpha = alpha
)
}
}
@Composable
private fun animatedHintArcStartAngle(): State<Float> {
val transition = rememberInfiniteTransition()
return transition.animateFloat(
initialValue = 90f - ARC_ANIMATED_HINT_SWEEP_ANGLE_DEGREES,
targetValue = 90f + NewtonsTimerViewModel.MAX_ANGLE,
animationSpec = infiniteRepeatable(
animation = tween(
durationMillis = ARC_ANIMATED_HINT_ANIM_DURATION,
delayMillis = ARC_ANIMATED_HINT_ANIM_DELAY,
),
repeatMode = RepeatMode.Restart
)
)
}
private fun DrawScope.animatedHintClipPath(
ballSize: BallSize,
animatedStartAngle: Float,
) = Path().apply {
moveTo(0f, 0f)
arcTo(
rect = ballSize.arcRect(additionalRadius = ARC_STROKE_WIDTH.toPx()),
startAngleDegrees = animatedStartAngle,
sweepAngleDegrees = ARC_ANIMATED_HINT_SWEEP_ANGLE_DEGREES,
forceMoveTo = false
)
}
private fun BallSize.arcOffset(additionalRadius: Float = 0f): Offset {
val radius = stringLengthToBallCenter + additionalRadius
return Offset(x = -radius + ballRadius, y = -radius)
}
private fun BallSize.arcSize(additionalRadius: Float = 0f): Size {
val radius = stringLengthToBallCenter + additionalRadius
return Size(width = radius, height = radius).times(2f)
}
private fun BallSize.arcRect(additionalRadius: Float = 0f) =
Rect(arcOffset(additionalRadius), arcSize(additionalRadius))
private val ARC_STROKE_WIDTH = 2.dp
private val ARC_STROKE_DASH_INTERVAL = ARC_STROKE_WIDTH * 4
private const val ARC_ANIMATED_HINT_SWEEP_ANGLE_DEGREES = 5f
private const val ARC_ANIMATED_HINT_ANIM_DURATION = 1200
private const val ARC_ANIMATED_HINT_ANIM_DELAY = 600