-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularSlider.kt
209 lines (185 loc) · 6.57 KB
/
CircularSlider.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.ldl.magnitudo.ui
import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ldl.magnitudo.R
import com.ldl.magnitudo.productSans
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.roundToInt
import kotlin.math.sin
private var stepSize = 1
private var factor = 1f
@Composable
fun CircularSlider(
shapeSize: Dp = 200.dp,
label: String = "",
textColor: Color = colorResource(id = R.color.accent),
pinColor: Color = colorResource(id = R.color.accent),
inactiveBarColor: Color = Color.Black.copy(alpha = 0.10f),
activeBarColor: Color = Color.Red,
initialValue: Any = 0,
strokeWidth: Dp = 6.dp,
valueRange: List<Any> = listOf(),
onValueChange: (Any) -> Unit
) {
stepSize = valueRange.size
factor = (270f / stepSize)
val startAngle = getAngle(valueRange.indexOf(initialValue))
var radius by remember {
mutableStateOf(0f)
}
var shapeCenter by remember {
mutableStateOf(Offset.Zero)
}
var handleCenter by remember {
mutableStateOf(Offset.Zero)
}
var angle by remember {
mutableStateOf(startAngle)
}
Box(contentAlignment = Alignment.Center) {
Canvas(
modifier = Modifier
.size(shapeSize)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
val newOffset = handleCenter + dragAmount
angle = getRotationAngle(newOffset, shapeCenter)
onValueChange.invoke(valueRange[getStep(drawAngle(angle))])
change.consumeAllChanges()
}
}
.padding(30.dp)
) {
shapeCenter = center
radius = size.minDimension / 2
val x = (shapeCenter.x + cos(Math.toRadians(angle)) * radius).toFloat()
val y = (shapeCenter.y + sin(Math.toRadians(angle)) * radius).toFloat()
if (drawAngle(angle) > 0f && drawAngle(angle) < 270f)
handleCenter = Offset(x, y)
else {
if (drawAngle(angle) == 0f) {
val x1 = (shapeCenter.x + cos(Math.toRadians(getAngle(0))) * radius).toFloat()
val y1 = (shapeCenter.y + sin(Math.toRadians(getAngle(0))) * radius).toFloat()
handleCenter = Offset(x1, y1)
}
if (drawAngle(angle) == 270f) {
val x1 = (shapeCenter.x + cos(Math.toRadians(405.0)) * radius).toFloat()
val y1 = (shapeCenter.y + sin(Math.toRadians(405.0)) * radius).toFloat()
handleCenter = Offset(x1, y1)
}
}
drawArc(
color = inactiveBarColor,
startAngle = 135f,
sweepAngle = 270f,
useCenter = false,
size = Size(size.width, size.height),
style = Stroke(strokeWidth.toPx(), cap = StrokeCap.Round)
)
drawArc(
color = activeBarColor,
startAngle = 135f,
sweepAngle = drawAngle(angle = angle),
useCenter = false,
size = Size(size.width, size.height),
style = Stroke(strokeWidth.toPx(), cap = StrokeCap.Round)
)
drawCircle(
color = pinColor,
center = handleCenter,
radius = (strokeWidth.toPx() * 2).dec()
)
}
Canvas(modifier = Modifier
.size(shapeSize / 2)
.pointerInput(Unit) {
}) {
drawCircle(color = Color.Transparent)
}
Column(
modifier = Modifier
.background(Color.Transparent)
.wrapContentSize(),
verticalArrangement = Arrangement.SpaceAround,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = valueRange[getStep(drawAngle(angle))].toString(),
style = TextStyle(
color = textColor,
fontSize = 48.sp,
fontFamily = productSans,
fontWeight = FontWeight.Normal,
textAlign = TextAlign.Start
),
modifier = Modifier.padding(16.dp, 4.dp, 16.dp, 2.dp)
)
Text(
text = label,
style = TextStyle(
color = colorResource(id = R.color.colorPrimaryDark),
fontSize = 14.sp,
fontFamily = productSans,
fontWeight = FontWeight.Normal,
textAlign = TextAlign.Start
)
)
}
Log.i("Value", initialValue.toString())
}
}
private fun drawAngle(angle: Double): Float {
return if (angle > 0 && angle < 45)
(225 + angle).toFloat()
else if (angle >= 45 && angle < 119)
270f
else if (angle >= 119 && angle < 135)
0f
else if (angle == 270.0)
angle.toFloat()
else
angle.toFloat() - 135f
}
private fun getAngle(step: Int): Double {
return if(step == stepSize - 1)
270.0
else
(step * factor + 135f).toDouble()
}
private fun getStep(angle: Float): Int {
val step = (angle / factor).roundToInt()
if (step >= stepSize)
return stepSize -1
return step
}
private fun getRotationAngle(currentPosition: Offset, center: Offset): Double {
val (dx, dy) = currentPosition - center
val theta = atan2(dy, dx).toDouble()
var angle = Math.toDegrees(theta)
if (angle < 0) {
angle += 360.0
}
return angle
}