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
|
package coffee.liz.abstractionengine.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
enum class Direction {
UP, DOWN, LEFT, RIGHT
}
@Composable
fun DPad(
onDirectionPressed: (Direction) -> Unit,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
.size(140.dp)
.background(
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.6f),
shape = CircleShape
)
.border(1.dp, MaterialTheme.colorScheme.outline, CircleShape)
) {
// Up button
DirectionButton(
text = "▲",
onClick = { onDirectionPressed(Direction.UP) },
modifier = Modifier
.align(Alignment.TopCenter)
.offset(y = 20.dp)
)
// Down button
DirectionButton(
text = "▼",
onClick = { onDirectionPressed(Direction.DOWN) },
modifier = Modifier
.align(Alignment.BottomCenter)
.offset(y = (-20).dp)
)
// Left button
DirectionButton(
text = "◀",
onClick = { onDirectionPressed(Direction.LEFT) },
modifier = Modifier
.align(Alignment.CenterStart)
.offset(x = 20.dp)
)
// Right button
DirectionButton(
text = "▶",
onClick = { onDirectionPressed(Direction.RIGHT) },
modifier = Modifier
.align(Alignment.CenterEnd)
.offset(x = (-20).dp)
)
// Center circle
Box(
modifier = Modifier
.align(Alignment.Center)
.size(38.dp)
.background(
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.5f),
shape = CircleShape
)
.border(1.dp, MaterialTheme.colorScheme.outline, CircleShape)
)
}
}
@Composable
private fun DirectionButton(
text: String,
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
Button(
onClick = onClick,
modifier = modifier.size(38.dp),
shape = RoundedCornerShape(4.dp),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.6f),
contentColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f)
),
contentPadding = PaddingValues(0.dp)
) {
Text(
text = text,
fontSize = 14.sp,
style = MaterialTheme.typography.headlineMedium
)
}
}
|