|
| 1 | +package rs.ruffle |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.PaddingValues |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.Spacer |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.padding |
| 10 | +import androidx.compose.foundation.layout.width |
| 11 | +import androidx.compose.material.icons.Icons |
| 12 | +import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft |
| 13 | +import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight |
| 14 | +import androidx.compose.material.icons.filled.Check |
| 15 | +import androidx.compose.material.icons.filled.KeyboardArrowDown |
| 16 | +import androidx.compose.material.icons.filled.KeyboardArrowUp |
| 17 | +import androidx.compose.material3.Button |
| 18 | +import androidx.compose.material3.DropdownMenu |
| 19 | +import androidx.compose.material3.DropdownMenuItem |
| 20 | +import androidx.compose.material3.HorizontalDivider |
| 21 | +import androidx.compose.material3.Icon |
| 22 | +import androidx.compose.material3.Surface |
| 23 | +import androidx.compose.material3.Text |
| 24 | +import androidx.compose.runtime.Composable |
| 25 | +import androidx.compose.runtime.getValue |
| 26 | +import androidx.compose.runtime.mutableStateOf |
| 27 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 28 | +import androidx.compose.runtime.setValue |
| 29 | +import androidx.compose.ui.Modifier |
| 30 | +import androidx.compose.ui.graphics.RectangleShape |
| 31 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 32 | +import androidx.compose.ui.tooling.preview.Preview |
| 33 | +import androidx.compose.ui.unit.dp |
| 34 | +import rs.ruffle.ui.theme.RuffleTheme |
| 35 | + |
| 36 | +val BUTTON_ROWS = arrayOf( |
| 37 | + arrayOf( |
| 38 | + KeyboardButton(keyCode = 49, keyChar = '1'), |
| 39 | + KeyboardButton(keyCode = 50, keyChar = '2'), |
| 40 | + KeyboardButton(keyCode = 51, keyChar = '3'), |
| 41 | + KeyboardButton(keyCode = 52, keyChar = '4'), |
| 42 | + KeyboardButton(keyCode = 53, keyChar = '5'), |
| 43 | + KeyboardButton(keyCode = 54, keyChar = '6'), |
| 44 | + KeyboardButton(keyCode = 55, keyChar = '7'), |
| 45 | + KeyboardButton(keyCode = 56, keyChar = '8'), |
| 46 | + KeyboardButton(keyCode = 57, keyChar = '9'), |
| 47 | + KeyboardButton(keyCode = 48, keyChar = '0') |
| 48 | + ), |
| 49 | + arrayOf( |
| 50 | + KeyboardButton(keyCode = 81, keyChar = 'q', text = "Q"), |
| 51 | + KeyboardButton(keyCode = 87, keyChar = 'w', text = "W"), |
| 52 | + KeyboardButton(keyCode = 69, keyChar = 'e', text = "E"), |
| 53 | + KeyboardButton(keyCode = 82, keyChar = 'r', text = "R"), |
| 54 | + KeyboardButton(keyCode = 84, keyChar = 't', text = "T"), |
| 55 | + KeyboardButton(keyCode = 89, keyChar = 'y', text = "Y"), |
| 56 | + KeyboardButton(keyCode = 85, keyChar = 'u', text = "U"), |
| 57 | + KeyboardButton(keyCode = 73, keyChar = 'i', text = "I"), |
| 58 | + KeyboardButton(keyCode = 79, keyChar = 'o', text = "O"), |
| 59 | + KeyboardButton(keyCode = 80, keyChar = 'p', text = "P") |
| 60 | + ), |
| 61 | + arrayOf( |
| 62 | + KeyboardButton(keyCode = 65, keyChar = 'a', text = "A"), |
| 63 | + KeyboardButton(keyCode = 83, keyChar = 's', text = "S"), |
| 64 | + KeyboardButton(keyCode = 68, keyChar = 'd', text = "D"), |
| 65 | + KeyboardButton(keyCode = 70, keyChar = 'f', text = "F"), |
| 66 | + KeyboardButton(keyCode = 71, keyChar = 'g', text = "G"), |
| 67 | + KeyboardButton(keyCode = 72, keyChar = 'h', text = "H"), |
| 68 | + KeyboardButton(keyCode = 74, keyChar = 'j', text = "J"), |
| 69 | + KeyboardButton(keyCode = 75, keyChar = 'k', text = "K"), |
| 70 | + KeyboardButton(keyCode = 76, keyChar = 'l', text = "L") |
| 71 | + ), |
| 72 | + arrayOf( |
| 73 | + KeyboardButton(keyCode = 90, keyChar = 'z', text = "Z"), |
| 74 | + KeyboardButton(keyCode = 88, keyChar = 'x', text = "X"), |
| 75 | + KeyboardButton(keyCode = 67, keyChar = 'c', text = "C"), |
| 76 | + KeyboardButton(keyCode = 86, keyChar = 'v', text = "V"), |
| 77 | + KeyboardButton(keyCode = 66, keyChar = 'b', text = "B"), |
| 78 | + KeyboardButton(keyCode = 78, keyChar = 'n', text = "N"), |
| 79 | + KeyboardButton(keyCode = 77, keyChar = 'm', text = "M") |
| 80 | + ), |
| 81 | + arrayOf( |
| 82 | + KeyboardButton(keyCode = 17, text = "CTRL"), |
| 83 | + KeyboardButton(keyCode = 18, text = "ALT"), |
| 84 | + KeyboardButton(keyCode = 32, keyChar = ' ', text = "␣"), |
| 85 | + KeyboardButton( |
| 86 | + keyCode = 37, |
| 87 | + icon = Icons.AutoMirrored.Filled.KeyboardArrowLeft, |
| 88 | + text = "Left" |
| 89 | + ), |
| 90 | + KeyboardButton(keyCode = 38, icon = Icons.Filled.KeyboardArrowUp, text = "Up"), |
| 91 | + KeyboardButton(keyCode = 40, icon = Icons.Filled.KeyboardArrowDown, text = "Down"), |
| 92 | + KeyboardButton( |
| 93 | + keyCode = 39, |
| 94 | + icon = Icons.AutoMirrored.Filled.KeyboardArrowRight, |
| 95 | + text = "Right" |
| 96 | + ) |
| 97 | + ) |
| 98 | +) |
| 99 | + |
| 100 | +data class KeyboardButton( |
| 101 | + val keyCode: Byte, |
| 102 | + val keyChar: Char = '\u0000', |
| 103 | + val text: String = keyChar.toString(), |
| 104 | + val icon: ImageVector? = null |
| 105 | +) |
| 106 | + |
| 107 | +class ContextMenuItem( |
| 108 | + val text: String, |
| 109 | + val separatorBefore: Boolean, |
| 110 | + val enabled: Boolean, |
| 111 | + val checked: Boolean, |
| 112 | + val onClick: () -> Unit |
| 113 | +) |
| 114 | + |
| 115 | +@Composable |
| 116 | +fun OnScreenControls( |
| 117 | + onKeyClick: (code: Byte, char: Char) -> Unit, |
| 118 | + onShowContextMenu: () -> Unit, |
| 119 | + onHideContextMenu: () -> Unit, |
| 120 | + contextMenuItems: List<ContextMenuItem> |
| 121 | +) { |
| 122 | + var showKeyboard by rememberSaveable { mutableStateOf(true) } |
| 123 | + val menuHasAnyCheckmark = contextMenuItems.any { it.checked } |
| 124 | + |
| 125 | + Surface { |
| 126 | + Column(modifier = Modifier.padding(horizontal = 1.dp)) { |
| 127 | + if (showKeyboard) { |
| 128 | + VirtualKeyboard(onKeyClick) |
| 129 | + } |
| 130 | + |
| 131 | + BottomMenu( |
| 132 | + toggleKeyboard = { showKeyboard = !showKeyboard }, |
| 133 | + onShowContextMenu, |
| 134 | + contextMenuItems, |
| 135 | + onHideContextMenu, |
| 136 | + menuHasAnyCheckmark |
| 137 | + ) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +@Composable |
| 143 | +private fun BottomMenu( |
| 144 | + toggleKeyboard: () -> Unit, |
| 145 | + onShowContextMenu: () -> Unit, |
| 146 | + contextMenuItems: List<ContextMenuItem>, |
| 147 | + onHideContextMenu: () -> Unit, |
| 148 | + menuHasAnyCheckmark: Boolean |
| 149 | +) { |
| 150 | + Row( |
| 151 | + modifier = Modifier.fillMaxWidth(), |
| 152 | + horizontalArrangement = Arrangement.SpaceBetween |
| 153 | + ) { |
| 154 | + Button( |
| 155 | + onClick = { toggleKeyboard() }, |
| 156 | + contentPadding = PaddingValues(1.dp), |
| 157 | + shape = RectangleShape |
| 158 | + ) { |
| 159 | + Text("⌨") |
| 160 | + } |
| 161 | + |
| 162 | + Button( |
| 163 | + onClick = { onShowContextMenu() }, |
| 164 | + contentPadding = PaddingValues(1.dp), |
| 165 | + shape = RectangleShape |
| 166 | + ) { |
| 167 | + DropdownMenu( |
| 168 | + expanded = contextMenuItems.isNotEmpty(), |
| 169 | + onDismissRequest = { onHideContextMenu() } |
| 170 | + ) { |
| 171 | + contextMenuItems.forEach { |
| 172 | + if (it.separatorBefore) { |
| 173 | + HorizontalDivider() |
| 174 | + } |
| 175 | + DropdownMenuItem( |
| 176 | + enabled = it.enabled, |
| 177 | + text = { |
| 178 | + Row { |
| 179 | + if (menuHasAnyCheckmark) { |
| 180 | + if (it.checked) { |
| 181 | + Icon(Icons.Filled.Check, "") |
| 182 | + } else { |
| 183 | + Spacer( |
| 184 | + modifier = Modifier.width( |
| 185 | + Icons.Filled.Check.defaultWidth |
| 186 | + ) |
| 187 | + ) |
| 188 | + } |
| 189 | + } |
| 190 | + Text(it.text) |
| 191 | + } |
| 192 | + }, |
| 193 | + onClick = { it.onClick() } |
| 194 | + ) |
| 195 | + } |
| 196 | + } |
| 197 | + Text("▤") |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +@Composable |
| 203 | +private fun VirtualKeyboard(onKeyClick: (code: Byte, char: Char) -> Unit) { |
| 204 | + Column { |
| 205 | + BUTTON_ROWS.forEach { |
| 206 | + Row( |
| 207 | + modifier = Modifier.fillMaxWidth(), |
| 208 | + horizontalArrangement = Arrangement.spacedBy(3.dp) |
| 209 | + ) { |
| 210 | + it.forEach { |
| 211 | + TextButton( |
| 212 | + modifier = Modifier.weight(1f), |
| 213 | + button = it, |
| 214 | + onKeyClick |
| 215 | + ) |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +@Composable |
| 223 | +fun TextButton( |
| 224 | + modifier: Modifier, |
| 225 | + button: KeyboardButton, |
| 226 | + onKeyClick: (code: Byte, char: Char) -> Unit |
| 227 | +) { |
| 228 | + Button( |
| 229 | + modifier = modifier, |
| 230 | + onClick = { |
| 231 | + onKeyClick(button.keyCode, button.keyChar) |
| 232 | + }, |
| 233 | + contentPadding = PaddingValues(1.dp), |
| 234 | + shape = RectangleShape |
| 235 | + ) { |
| 236 | + if (button.icon != null) { |
| 237 | + Icon(button.icon, contentDescription = button.text) |
| 238 | + } else { |
| 239 | + Text(text = button.text) |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +@Preview |
| 245 | +@Composable |
| 246 | +fun OnScreenControlsPreview() { |
| 247 | + RuffleTheme { |
| 248 | + OnScreenControls( |
| 249 | + onKeyClick = { _: Byte, _: Char -> }, |
| 250 | + onShowContextMenu = {}, |
| 251 | + onHideContextMenu = {}, |
| 252 | + contextMenuItems = listOf() |
| 253 | + ) |
| 254 | + } |
| 255 | +} |
0 commit comments