-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
127 lines (120 loc) · 5.84 KB
/
Main.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
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material.icons.filled.Clear
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import io.github.markyav.drawbox.box.DrawBox
import io.github.markyav.drawbox.controller.DrawBoxBackground
import io.github.markyav.drawbox.controller.DrawBoxSubscription
import io.github.markyav.drawbox.controller.DrawController
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
val controller = remember { DrawController() }
val bitmap by remember { controller.getBitmap(250, DrawBoxSubscription.DynamicUpdate) }.collectAsState()
val bitmapFinishDrawingUpdate by remember { controller.getBitmap(250, DrawBoxSubscription.FinishDrawingUpdate) }.collectAsState()
val undoCount by controller.undoCount.collectAsState()
val redoCount by controller.redoCount.collectAsState()
val enableUndo by remember { derivedStateOf { undoCount > 0 } }
val enableRedo by remember { derivedStateOf { redoCount > 0 } }
val strokeWith by controller.strokeWidth.collectAsState()
val canvasOpacity by controller.canvasOpacity.collectAsState()
val background by controller.background.collectAsState()
LaunchedEffect(Unit) {
controller.background.value = DrawBoxBackground.ColourBackground(color = Color.Blue, alpha = 0.15f)
controller.canvasOpacity.value = 0.5f
}
Row {
Column(modifier = Modifier.weight(2f, false)) {
Row {
IconButton(onClick = controller::undo, enabled = enableUndo) {
Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "undo")
}
IconButton(onClick = controller::redo, enabled = enableRedo) {
Icon(imageVector = Icons.Default.ArrowForward, contentDescription = "redo")
}
IconButton(onClick = controller::reset, enabled = enableUndo || enableRedo) {
Icon(imageVector = Icons.Default.Clear, contentDescription = "reset")
}
}
Row(modifier = Modifier.padding(end = 8.dp)) {
Column(modifier = Modifier.weight(2f, false)) {
Text("Stroke width")
Slider(
value = strokeWith,
onValueChange = { controller.strokeWidth.value = it },
valueRange = 1f..100f
)
}
Column(modifier = Modifier.weight(2f, false)) {
Text("Canvas opacity")
Slider(
value = canvasOpacity,
onValueChange = { controller.canvasOpacity.value = it },
valueRange = 0f..1f
)
}
}
Spacer(modifier = Modifier.height(8.dp))
Row(modifier = Modifier.padding(end = 8.dp)) {
Column(modifier = Modifier.weight(2f, true)) {
Text("Color")
Row {
TextButton(onClick = { controller.color.value = Color.Red }) {
Text("Red")
}
TextButton(onClick = { controller.color.value = Color.Green }) {
Text("Green")
}
TextButton(onClick = { controller.color.value = Color.Yellow }) {
Text("Yellow")
}
}
}
Column(modifier = Modifier.weight(2f, false)) {
Text("Background opacity")
Slider(
value = (background as? DrawBoxBackground.ColourBackground)?.alpha ?: 0f,
onValueChange = { controller.background.value = DrawBoxBackground.ColourBackground(color = Color.Blue, alpha = it) },
valueRange = 0f..1f
)
}
}
DrawBox(
controller = controller,
modifier = Modifier
.fillMaxHeight()
.aspectRatio(1f)
.padding(100.dp)
.border(width = 1.dp, color = Color.Blue),
)
}
Column(modifier = Modifier.weight(1f, false)) {
Text("DynamicUpdate:")
Spacer(modifier = Modifier.height(10.dp))
Image(
bitmap,
contentDescription = "drawn bitmap",
modifier = Modifier.size(200.dp).border(width = 1.dp, color = Color.Red),
)
Spacer(modifier = Modifier.height(50.dp))
Text("FinishDrawingUpdate:")
Spacer(modifier = Modifier.height(10.dp))
Image(
bitmapFinishDrawingUpdate,
contentDescription = "drawn bitmap",
modifier = Modifier.size(200.dp).border(width = 1.dp, color = Color.Red),
)
}
}
}
}