Skip to content

Commit

Permalink
button frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Duron27 committed Nov 1, 2024
1 parent ed5cdf5 commit 2d672d4
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 205 deletions.
282 changes: 246 additions & 36 deletions app/src/main/java/org/openmw/EngineActivity.kt

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion app/src/main/java/org/openmw/SettingScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.openmw.utils.BouncingBackground
import org.openmw.utils.ExpandableBox
import org.openmw.utils.ReadAndDisplayIniValues
import org.openmw.utils.exportCrashAndLogcatFiles
import org.openmw.utils.exportFile
import org.openmw.utils.exportFilesAndDirectories
import org.openmw.utils.importFilesAndDirectories
import org.openmw.utils.importSpecificFile
Expand All @@ -58,6 +59,7 @@ import java.io.File
fun SettingScreen(context: Context, navigateToHome: () -> Unit) {
val transparentBlack = Color(alpha = 0.6f, red = 0f, green = 0f, blue = 0f)
var showDialog = remember { mutableStateOf(false) }
val scrollState = rememberScrollState()

Scaffold(
topBar = {
Expand All @@ -67,12 +69,14 @@ fun SettingScreen(context: Context, navigateToHome: () -> Unit) {
BouncingBackground()
Box(
modifier = Modifier
.verticalScroll(scrollState)
.wrapContentHeight()
.padding(top = 40.dp, bottom = 80.dp),
) {
Column(
modifier = Modifier
.padding(top = 40.dp, bottom = 60.dp),
.padding(top = 40.dp, bottom = 60.dp)
.wrapContentHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down Expand Up @@ -119,6 +123,30 @@ fun SettingScreen(context: Context, navigateToHome: () -> Unit) {
) {
Text(text = "Import settings.cfg", color = Color.White)
}
Button(
onClick = { importSpecificFile(context, "UI.cfg") },
modifier = Modifier
.fillMaxWidth()
.height(56.dp),
shape = RectangleShape,
colors = ButtonDefaults.buttonColors(
containerColor = Color(alpha = 0.6f, red = 0f, green = 0f, blue = 0f)
)
) {
Text(text = "Import Controls Layout", color = Color.White)
}
Button(
onClick = { exportFile(context, "UI.cfg") },
modifier = Modifier
.fillMaxWidth()
.height(56.dp),
shape = RectangleShape,
colors = ButtonDefaults.buttonColors(
containerColor = Color(alpha = 0.6f, red = 0f, green = 0f, blue = 0f)
)
) {
Text(text = "Export Controls Layout", color = Color.White)
}

Button(
onClick = { importSpecificFile(context, """.*\.omwsave$""") },
Expand Down
66 changes: 27 additions & 39 deletions app/src/main/java/org/openmw/ui/controls/DynamicButtons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
Expand All @@ -33,39 +31,38 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import org.libsdl.app.SDLActivity.onNativeKeyDown
import org.libsdl.app.SDLActivity.onNativeKeyUp
import org.openmw.utils.vibrate
import kotlin.math.roundToInt

@Composable
fun ResizableDraggableButton(
context: Context,
id: Int,
keyCode: Int,
editMode: Boolean,
isDragging: Boolean,
onDelete: () -> Unit,
customCursorView: CustomCursorView
) {
var buttonState by remember {
mutableStateOf(
loadButtonState(context).find { it.id == id }
?: ButtonState(id, 100f, 0f, 0f, false, keyCode)
)
}
val buttonState = UIStateManager.buttonStates.getOrPut(id) {
mutableStateOf(ButtonState(id, 100f, 0f, 0f, false, keyCode))
}.value
var buttonSize by remember { mutableStateOf(buttonState.size.dp) }
var offsetX by remember { mutableFloatStateOf(buttonState.offsetX) }
var offsetY by remember { mutableFloatStateOf(buttonState.offsetY) }
var isDragging by remember { mutableStateOf(false) }
val offsetX = remember { mutableFloatStateOf(buttonState.offsetX) }
val offsetY = remember { mutableFloatStateOf(buttonState.offsetY) }
var isPressed by remember { mutableStateOf(false) }
val density = LocalDensity.current
val visible = UIStateManager.visible
val saveState = {
val updatedState = ButtonState(id, buttonSize.value, offsetX, offsetY, buttonState.isLocked, keyCode)
val allStates = loadButtonState(context).filter { it.id != id } + updatedState
saveButtonState(context, allStates)
val updatedState = buttonState.copy(
size = buttonSize.value,
offsetX = offsetX.floatValue,
offsetY = offsetY.floatValue
)
UIStateManager.buttonStates[id]?.value = updatedState
saveButtonState(context, UIStateManager.buttonStates.values.map { it.value })
}

AnimatedVisibility(
Expand All @@ -90,49 +87,36 @@ fun ResizableDraggableButton(
)
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.background(Color.Transparent)
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(buttonSize)
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.background(Color.Transparent)
.then(
if (editMode) {
Modifier.pointerInput(Unit) {
detectDragGestures(
onDragStart = { isDragging = true },
onDrag = { change, dragAmount ->
offsetX += dragAmount.x
offsetY += dragAmount.y
},
onDragEnd = {
isDragging = false
saveState()
}
)
}
} else Modifier
)
.border(2.dp, if (isDragging) Color.Red else Color.Black, shape = CircleShape)
) {
// Main button
Box(
modifier = Modifier
.fillMaxSize()
.background(
if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT) {
if (isPressed) Color.Green else Color(alpha = 0.6f, red = 0f, green = 0f, blue = 0f)
if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT ||
keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT) {
if (isPressed) Color(alpha = 0.25f, red = 0f, green = 10f, blue = 0f) else Color(alpha = 0.25f, red = 0f, green = 0f, blue = 0f)
} else {
Color(alpha = 0.25f, red = 0f, green = 0f, blue = 0f)
}, shape = CircleShape
},
shape = CircleShape
)
.pointerInput(Unit) {
detectTapGestures(
onPress = {
if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT) {
if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT ||
keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT) {
isPressed = !isPressed
if (isPressed) {
onNativeKeyDown(keyCode)
Expand All @@ -145,6 +129,7 @@ fun ResizableDraggableButton(
}
if (keyCode == KeyEvent.KEYCODE_Z) {
onNativeKeyDown(keyCode)
onNativeKeyDown(KeyEvent.KEYCODE_ENTER)
if (UIStateManager.isCustomCursorEnabled) {
customCursorView.performMouseClick()
} else {
Expand All @@ -160,7 +145,8 @@ fun ResizableDraggableButton(
}
}
awaitRelease()
if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT) {
if (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT || keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT ||
keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT) {
// Do nothing for SHIFT keys as they toggle
} else {
onNativeKeyUp(keyCode)
Expand Down Expand Up @@ -239,6 +225,8 @@ fun keyCodeToChar(keyCode: Int): String {
KeyEvent.KEYCODE_SHIFT_RIGHT -> "Shift-R"
KeyEvent.KEYCODE_CTRL_LEFT -> "Ctrl-L"
KeyEvent.KEYCODE_CTRL_RIGHT -> "Ctrl-R"
KeyEvent.KEYCODE_ALT_LEFT -> "Alt-L"
KeyEvent.KEYCODE_ALT_RIGHT -> "Alt-R"
KeyEvent.KEYCODE_SPACE -> "Space"
KeyEvent.KEYCODE_ESCAPE -> "Escape"
KeyEvent.KEYCODE_ENTER -> "Enter"
Expand Down
49 changes: 23 additions & 26 deletions app/src/main/java/org/openmw/ui/controls/DynamicLeftThumbstick.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import org.libsdl.app.SDLActivity.onNativeKeyUp
import org.openmw.ui.controls.UIStateManager.isThumbDragging
import kotlin.math.abs
import kotlin.math.hypot
import kotlin.math.roundToInt
import android.util.Log

@OptIn(ExperimentalComposeUiApi::class)
@Composable
Expand All @@ -55,29 +54,29 @@ fun ResizableDraggableThumbstick(
onDClick: () -> Unit,
onRelease: () -> Unit
) {
var thumbstickState by remember {
mutableStateOf(loadButtonState(context).find { it.id == id } ?: ButtonState(
id,
200f,
0f,
0f,
false,
keyCode
))
}
var buttonSize by remember { mutableStateOf(thumbstickState.size.dp) }
var offsetX by remember { mutableFloatStateOf(thumbstickState.offsetX) }
var offsetY by remember { mutableFloatStateOf(thumbstickState.offsetY) }
val buttonState = UIStateManager.buttonStates.getOrPut(id) {
mutableStateOf(ButtonState(id, 100f, 0f, 0f, false, keyCode))
}.value

val buttonSize = remember { mutableStateOf(buttonState.size.dp) }
val offsetX = remember { mutableFloatStateOf(buttonState.offsetX) }
val offsetY = remember { mutableFloatStateOf(buttonState.offsetY) }
val visible = UIStateManager.visible
val density = LocalDensity.current
val radiusPx = with(density) { (buttonSize / 2).toPx() }
val radiusPx = with(density) { (buttonSize.value / 2).toPx() }
val deadZone = 0.2f * radiusPx
var touchState by remember { mutableStateOf(Offset(0f, 0f)) }

val saveState = {
val updatedState = ButtonState(id, buttonSize.value, offsetX, offsetY, false, keyCode)
val allStates = loadButtonState(context).filter { it.id != id } + updatedState
saveButtonState(context, allStates)
val updatedState = buttonState.copy(
size = buttonSize.value.value,
offsetX = offsetX.floatValue,
offsetY = offsetY.floatValue
)
UIStateManager.buttonStates[id]?.value = updatedState
saveButtonState(context, UIStateManager.buttonStates.values.map { it.value })
}

AnimatedVisibility(
visible = visible,
enter = slideInVertically(
Expand Down Expand Up @@ -107,10 +106,10 @@ fun ResizableDraggableThumbstick(
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(buttonSize)
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.size(buttonSize.value)
.background(Color.Transparent)
.border(2.dp, if (isThumbDragging) Color.Red else Color.Black, shape = CircleShape)
.align(Alignment.Center)
) {
Box(
contentAlignment = Alignment.Center,
Expand Down Expand Up @@ -193,24 +192,22 @@ fun ResizableDraggableThumbstick(
.size(30.dp)
.background(Color.Black, shape = CircleShape)
.clickable {
buttonSize += 20.dp
buttonSize.value += 20.dp
saveState()
}
.border(2.dp, Color.White, shape = CircleShape),
contentAlignment = Alignment.Center
) {
Text(text = "+", color = Color.White, fontWeight = FontWeight.Bold)
}

// - button
Box(
modifier = Modifier
.align(Alignment.BottomEnd)
.size(30.dp)
.background(Color.Black, shape = CircleShape)
.clickable {
buttonSize -= 20.dp
if (buttonSize < 50.dp) buttonSize = 50.dp
buttonSize.value -= 20.dp
if (buttonSize.value < 50.dp) buttonSize.value = 50.dp
saveState()
}
.border(2.dp, Color.White, shape = CircleShape),
Expand Down
Loading

0 comments on commit 2d672d4

Please sign in to comment.