/
IndentRectShape.kt
128 lines (118 loc) · 3.98 KB
/
IndentRectShape.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
package com.exyte.animatednavbar.shape
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import com.exyte.animatednavbar.animation.indendshape.ShapeCornerRadius
class IndentRectShape(
private val indentShapeData: IndentShapeData,
) : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline =
Outline.Generic(
Path().addRoundRectWithIndent(size, indentShapeData, layoutDirection)
)
fun copy(
cornerRadius: ShapeCornerRadius = indentShapeData.cornerRadius,
xIndent: Float = indentShapeData.xIndent,
yIndent: Float = indentShapeData.height,
ballSize: Float = indentShapeData.ballOffset,
) = IndentRectShape(
indentShapeData.copy(
cornerRadius = cornerRadius,
xIndent = xIndent,
height = yIndent,
ballOffset = ballSize
)
)
}
fun Path.addRoundRectWithIndent(
size: Size,
indentShapeData: IndentShapeData,
layoutDirection: LayoutDirection,
): Path {
val width = size.width
val height = size.height
val cornerRadius = indentShapeData.cornerRadius
val sweepAngleDegrees = 90f
return apply {
moveTo(chooseCornerSize(size.height, cornerRadius.topLeft), 0f)
val xOffset =
if (layoutDirection == LayoutDirection.Ltr) {
indentShapeData.xIndent - indentShapeData.width / 2
} else {
size.width - indentShapeData.xIndent - indentShapeData.width / 2
}
if (xOffset > cornerRadius.topLeft / 4) {
addPath(
IndentPath(
Rect(
Offset(
x = xOffset,
y = 0f
),
Size(indentShapeData.width, indentShapeData.height)
)
).createPath()
)
}
lineTo(width - cornerRadius.topRight, 0f)
arcTo(
rect = Rect(
offset = Offset(width - cornerRadius.topRight, 0f),
size = Size(cornerRadius.topRight, cornerRadius.topRight)
),
startAngleDegrees = 270f,
sweepAngleDegrees = sweepAngleDegrees,
forceMoveTo = false
)
lineTo(width, height - cornerRadius.bottomRight)
arcTo(
rect = Rect(
offset = Offset(
width - cornerRadius.bottomRight,
height - cornerRadius.bottomRight
),
size = Size(cornerRadius.bottomRight, cornerRadius.bottomRight)
),
startAngleDegrees = 0f,
sweepAngleDegrees = sweepAngleDegrees,
forceMoveTo = false
)
lineTo(width - cornerRadius.bottomLeft, height)
arcTo(
rect = Rect(
offset = Offset(0f, height - cornerRadius.bottomLeft),
size = Size(cornerRadius.bottomLeft, cornerRadius.bottomLeft)
),
startAngleDegrees = 90f,
sweepAngleDegrees = sweepAngleDegrees,
forceMoveTo = false
)
lineTo(0f, cornerRadius.topLeft)
arcTo(
rect = Rect(
offset = Offset(0f, 0f),
size = Size(cornerRadius.topLeft, cornerRadius.topLeft)
),
startAngleDegrees = 180f,
sweepAngleDegrees = sweepAngleDegrees,
forceMoveTo = false
)
close()
}
}
fun chooseCornerSize(sizeHeight: Float, cornerRadius: Float): Float {
return if (sizeHeight > cornerRadius) {
cornerRadius
} else {
sizeHeight
}
}