Advertisement
Artyom_Kopan

view/ButtonItem.kt

May 30th, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.89 KB | None | 0 0
  1. package view
  2.  
  3. import androidx.compose.runtime.*
  4. import androidx.compose.foundation.layout.height
  5. import androidx.compose.foundation.layout.padding
  6. import androidx.compose.foundation.layout.width
  7. import androidx.compose.material.Button
  8. import androidx.compose.material.ButtonDefaults
  9. import androidx.compose.material.Text
  10. import androidx.compose.runtime.Composable
  11. import androidx.compose.ui.Modifier
  12. import androidx.compose.ui.graphics.Color
  13. import androidx.compose.ui.text.font.FontWeight
  14. import androidx.compose.ui.text.style.TextAlign
  15. import androidx.compose.ui.unit.dp
  16. import androidx.compose.ui.unit.sp
  17.  
  18. @Suppress("FunctionNaming")
  19. @Composable
  20. internal fun ButtonItem(state: ViewModel.State, buttonId: Int, onClick: (Int) -> Unit) {
  21.     val button = state.buttons[buttonId]
  22.     var text by remember { mutableStateOf(button.symbol?.toString() ?: " ") }
  23.  
  24.     fun onAdvancedClick() {
  25.         onClick(button.id)
  26.         if (state.gameMode == GameMode.BOT && state.winStatus == WinStatus.CONTINUES) {
  27.             val numberBotButton = state.unusedButtons[state.unusedButtons.indices.random()]
  28.             onClick(numberBotButton)
  29.             /* отладочный вывод */ println("${button.id} $numberBotButton")
  30.         }
  31.     }
  32.  
  33.     Button(
  34.         onClick = { onAdvancedClick(); text = button.symbol.toString() },
  35.         modifier = Modifier.width(200.dp).height(200.dp).padding(10.dp),
  36.         colors = ButtonDefaults.buttonColors(backgroundColor = Color(0, 0, 0, 0))
  37.     ) {
  38.         val outputSymbol = if (text == "CROSS") "X" else if (text == "NOUGHT") "O" else " "
  39.         val textColor = if (outputSymbol == "X") Color.Red else Color.Blue
  40.         Text(
  41.             outputSymbol,
  42.             fontSize = 60.sp,
  43.             textAlign = TextAlign.Center,
  44.             color = textColor,
  45.             fontWeight = FontWeight.Bold
  46.         )
  47.  
  48.         CheckWin(state)
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement