mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 21:35:59 +00:00
fix(input): declutter the floating gamepad overlay (#2933)
This commit is contained in:
1 parent
f5d879f222
commit
e58b463f54
9 files changed
+244
-30
No files matched your search
@@ -260,7 +260,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
private var steamController: SteamControllerBle? = null
|
||||
private var onScreenControls: OnScreenControls? = null
|
||||
private var bottomInputControls: OnScreenControls? = null
|
||||
private var primaryInputLayout: com.droiddeck.launcher.input.SavedInputLayout? = null
|
||||
private var primaryInputLayout by mutableStateOf<com.droiddeck.launcher.input.SavedInputLayout?>(null)
|
||||
private var secondaryInputLayout by mutableStateOf<com.droiddeck.launcher.input.SavedInputLayout?>(null)
|
||||
private var steamTouchControls: com.droiddeck.launcher.input.SteamTouchControls? = null
|
||||
private var steamTouchAvailable by mutableStateOf(false)
|
||||
@@ -643,6 +643,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
private var suspendPolicy by mutableStateOf(SessionPrefs.SUSPEND_MANUAL)
|
||||
private var steamDownloadsInBackground by mutableStateOf(false)
|
||||
private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO)
|
||||
private var oscRearButtons by mutableStateOf(false)
|
||||
private var oscTrackpads by mutableStateOf(false)
|
||||
private var onScreenButtonsVisible by mutableStateOf(false)
|
||||
private var layoutOptions by mutableStateOf<List<Pair<String, String>>>(emptyList())
|
||||
private var layoutChoice by mutableStateOf("")
|
||||
@@ -1745,6 +1747,23 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
layoutOptions = layoutOptions,
|
||||
layoutChoice = layoutChoice,
|
||||
onLayout = ::chooseLayout,
|
||||
oscRearButtons = oscRearButtons.takeIf {
|
||||
unfoldedPanel == null && !usesSteamTouchLayout() && com.droiddeck.launcher.input.PadKit.current(this).steam &&
|
||||
primaryInputLayout?.let(com.droiddeck.launcher.input.OnScreenLayout::hasBuiltInKit) == true
|
||||
},
|
||||
oscTrackpads = oscTrackpads,
|
||||
onOscRearButtons = { shown ->
|
||||
SessionPrefs.setOscRearButtons(this, shown)
|
||||
oscRearButtons = shown
|
||||
onScreenControls?.reload()
|
||||
bottomInputControls?.reload()
|
||||
},
|
||||
onOscTrackpads = { shown ->
|
||||
SessionPrefs.setOscTrackpads(this, shown)
|
||||
oscTrackpads = shown
|
||||
onScreenControls?.reload()
|
||||
bottomInputControls?.reload()
|
||||
},
|
||||
onEditLayout = when {
|
||||
layoutChoice == com.droiddeck.launcher.input.GameControls.STEAM -> steamTouchControls?.takeIf { steamTouchAvailable && onScreenButtonsVisible && it.visibility == View.VISIBLE }?.let { view -> {
|
||||
drawerOpen = false
|
||||
@@ -2556,6 +2575,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
|
||||
steamDownloadsInBackground = SessionPrefs.steamDownloadsInBackground(this)
|
||||
oscMode = sessionOscMode()
|
||||
oscRearButtons = SessionPrefs.oscRearButtons(this)
|
||||
oscTrackpads = SessionPrefs.oscTrackpads(this)
|
||||
backHold = SessionPrefs.backHold(this)
|
||||
backActionsInverted = SessionPrefs.backActionsInverted(this)
|
||||
physicalShortcuts.bindings = PhysicalShortcuts.read(this)
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Rect
|
||||
import android.graphics.RectF
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
@@ -18,6 +19,12 @@ object KitLayout {
|
||||
/** Whether [id] names one of a kit's own controls rather than a key or control the player added. */
|
||||
fun isKit(id: String) = id.startsWith("legacy:") || id == DPAD_ID || id == KEYBOARD_ID
|
||||
|
||||
internal fun visibleOnOverlay(control: InputControl, rearButtons: Boolean, trackpads: Boolean): Boolean = when (control) {
|
||||
InputControl.L4, InputControl.L5, InputControl.R4, InputControl.R5 -> rearButtons
|
||||
InputControl.LEFT_PAD, InputControl.RIGHT_PAD -> trackpads
|
||||
else -> true
|
||||
}
|
||||
|
||||
/** The control a kit id stands for in a layout that keeps no kit positions, so bindings saved for it stay valid. */
|
||||
fun element(id: String): InputSurfaceElement? = when {
|
||||
id == DPAD_ID -> InputSurfaceElement(InputControl.LS, 0f, 0f, .1f, .1f, id, InputElementKind.DPAD)
|
||||
@@ -72,22 +79,24 @@ object KitLayout {
|
||||
dpad(220f, 345f, 150f)) + face(1375f, 345f, 58f, 96f) + listOf(
|
||||
Spec(InputControl.LS, 290f, 640f, 96f), Spec(InputControl.RS, 1310f, 640f, 96f),
|
||||
Spec(InputControl.LEFT_PAD, 470f, 360f, 88f, shape = InputButtonShape.ROUNDED), Spec(InputControl.RIGHT_PAD, 1130f, 360f, 88f, shape = InputButtonShape.ROUNDED),
|
||||
Spec(InputControl.SELECT, 660f, 150f, 46f, 26f, shape = InputButtonShape.PILL), Spec(InputControl.START, 940f, 150f, 46f, 26f, shape = InputButtonShape.PILL),
|
||||
Spec(InputControl.SELECT, 660f, 850f, 46f, 26f, shape = InputButtonShape.PILL), Spec(InputControl.START, 940f, 850f, 46f, 26f, shape = InputButtonShape.PILL),
|
||||
Spec(InputControl.GUIDE, 370f, 852f, 66f, 30f, shape = InputButtonShape.PILL), Spec(InputControl.QAM, 1230f, 852f, 66f, 30f, shape = InputButtonShape.PILL),
|
||||
keyboard(136f))
|
||||
kit.playStation -> shoulders() + listOf(dpad(250f, 395f, 125f)) + face(1350f, 395f, 58f, 96f) + listOf(
|
||||
Spec(InputControl.LS, 565f, 690f, 100f), Spec(InputControl.RS, 1035f, 690f, 100f),
|
||||
Spec(InputControl.L3, 430f, 590f, 42f, ax = 565f, ay = 690f), Spec(InputControl.R3, 1170f, 590f, 42f, ax = 1035f, ay = 690f),
|
||||
Spec(InputControl.SELECT, 640f, 150f, 50f, 30f, shape = InputButtonShape.PILL), Spec(InputControl.START, 960f, 150f, 50f, 30f, shape = InputButtonShape.PILL),
|
||||
Spec(InputControl.SELECT, 640f, 850f, 50f, 30f, shape = InputButtonShape.PILL), Spec(InputControl.START, 960f, 850f, 50f, 30f, shape = InputButtonShape.PILL),
|
||||
Spec(InputControl.GUIDE, 800f, 250f, 46f), Spec(InputControl.QAM, 220f, 848f, 46f), keyboard(136f))
|
||||
else -> shoulders() + listOf(Spec(InputControl.LS, 235f, 400f, 104f), dpad(470f, 675f, 118f)) + face(1375f, 400f, 58f, 94f) + listOf(
|
||||
Spec(InputControl.RS, 1130f, 690f, 104f), Spec(InputControl.L3, 385f, 290f, 42f, ax = 235f, ay = 400f), Spec(InputControl.R3, 1290f, 790f, 42f, ax = 1130f, ay = 690f),
|
||||
Spec(InputControl.SELECT, 690f, 162f, 38f), Spec(InputControl.GUIDE, 800f, 162f, 54f), Spec(InputControl.START, 910f, 162f, 38f),
|
||||
Spec(InputControl.SELECT, 690f, 850f, 38f), Spec(InputControl.GUIDE, 800f, 162f, 54f), Spec(InputControl.START, 910f, 850f, 38f),
|
||||
Spec(InputControl.QAM, 220f, 848f, 46f), keyboard(136f))
|
||||
}
|
||||
|
||||
/** The default controls for [kit] on a [width] × [height] screen. */
|
||||
fun elements(context: Context, kit: PadKit, width: Int, height: Int, inset: Rect = Rect(), scale: Float = 1f): List<InputSurfaceElement> {
|
||||
fun elements(context: Context, kit: PadKit, width: Int, height: Int, inset: Rect = Rect(), scale: Float = 1f,
|
||||
cutouts: List<Rect> = emptyList(), rearButtons: Boolean = true, trackpads: Boolean = true,
|
||||
addedControls: List<InputSurfaceElement> = emptyList()): List<InputSurfaceElement> {
|
||||
if (width <= 0 || height <= 0) return emptyList()
|
||||
// Insets stay as reported unless what's left is too small to keep every control above the minimum element size.
|
||||
val smallestUnit = InputSurfaceElement.MIN_SIZE * max(width, height) / (2 * SMALLEST_HALF)
|
||||
@@ -100,6 +109,29 @@ object KitLayout {
|
||||
val (top, bottom) = fit(inset.top, inset.bottom, height, REF_H)
|
||||
val safe = Rect(left, top, right, bottom)
|
||||
if (height > width) return portrait(context, kit, width, height, safe, scale)
|
||||
.filter { visibleOnOverlay(it.control, rearButtons, trackpads) }
|
||||
val controls = specs(kit).filter { visibleOnOverlay(it.control, rearButtons, trackpads) }
|
||||
val leftCutouts = cutouts.filter { inset.left > 0 && it.right <= inset.left }
|
||||
val rightCutouts = cutouts.filter { inset.right > 0 && it.left >= width - inset.right }
|
||||
if ((leftCutouts.isNotEmpty() || rightCutouts.isNotEmpty()) &&
|
||||
(left == 0 || leftCutouts.isNotEmpty()) && (right == 0 || rightCutouts.isNotEmpty())) {
|
||||
val corner = cornerInset(safe, cutouts, height, context.resources.displayMetrics.density)
|
||||
val fullWidth = landscape(context, kit, width, height, Rect(0, top, 0, bottom), scale, corner, controls)
|
||||
avoidSideCutouts(fullWidth, leftCutouts, rightCutouts, width, height, addedControls)?.let { return it }
|
||||
}
|
||||
return landscape(context, kit, width, height, safe, scale, left, controls)
|
||||
}
|
||||
|
||||
internal fun cornerInset(inset: Rect, cutouts: List<Rect>, height: Int, density: Float): Int {
|
||||
val leftCutouts = cutouts.filter { inset.left > 0 && it.right <= inset.left }
|
||||
val floor = height - inset.bottom
|
||||
return if (height <= 0 || leftCutouts.isEmpty() ||
|
||||
leftCutouts.any { it.top < floor && it.bottom > floor - CORNER_DP * density && it.left < CORNER_DP * density })
|
||||
inset.left else 0
|
||||
}
|
||||
|
||||
private fun landscape(context: Context, kit: PadKit, width: Int, height: Int, safe: Rect, scale: Float,
|
||||
cornerInset: Int, controls: List<Spec>): List<InputSurfaceElement> {
|
||||
val w = width.toFloat(); val h = height.toFloat()
|
||||
val unit = (min(w / REF_W, h / REF_H) * cap(context, min(width, height)) * scale).coerceAtMost(min((w - safe.left - safe.right).coerceAtLeast(1f) / MIN_SPAN, (h - safe.top - safe.bottom).coerceAtLeast(1f) / REF_H))
|
||||
fun x(ref: Float) = when {
|
||||
@@ -113,22 +145,66 @@ object KitLayout {
|
||||
else -> (h / 2f + (ref - REF_H / 2f) * unit).between(safe.top + ref * unit, h - safe.bottom - (REF_H - ref) * unit)
|
||||
}
|
||||
val density = context.resources.displayMetrics.density
|
||||
var edge = safe.left + CORNER_DP * density
|
||||
val specs = specs(kit)
|
||||
val row = specs.filter { it.cy > REF_H * .88f && it.cx < REF_W / 3f }.sortedBy { it.cx }.associateWith { s ->
|
||||
var edge = cornerInset + CORNER_DP * density
|
||||
val row = controls.filter { it.cy > REF_H * .88f && it.cx < REF_W / 3f }.sortedBy { it.cx }.associateWith { s ->
|
||||
val hw = s.hw * unit
|
||||
max(x(s.cx), edge + hw).also { edge = it + hw + GAP_DP * density }
|
||||
}
|
||||
return specs.map { s ->
|
||||
val cx = row[s] ?: (x(s.ax) + (s.cx - s.ax) * unit); val cy = y(s.ay) + (s.cy - s.ay) * unit; val hw = s.hw * unit; val hh = s.hh * unit
|
||||
return controls.map { s ->
|
||||
val cx = when (s.control) {
|
||||
InputControl.START, InputControl.SELECT -> (w + safe.left - safe.right) / 2f + (s.cx - REF_W / 2f) * unit
|
||||
else -> row[s] ?: (x(s.ax) + (s.cx - s.ax) * unit)
|
||||
}
|
||||
val cy = y(s.ay) + (s.cy - s.ay) * unit; val hw = s.hw * unit; val hh = s.hh * unit
|
||||
InputSurfaceElement(s.control, (cx - hw) / w, (cy - hh) / h, 2 * hw / w, 2 * hh / h, s.id, s.kind, if (s.name == s.control.label) kit.name(s.control) else s.name,
|
||||
floating = s.kind == InputElementKind.STICK, buttonShape = s.shape, icon = s.icon).bounded()
|
||||
}
|
||||
}
|
||||
|
||||
private fun avoidSideCutouts(elements: List<InputSurfaceElement>, leftCutouts: List<Rect>, rightCutouts: List<Rect>,
|
||||
width: Int, height: Int, addedControls: List<InputSurfaceElement>): List<InputSurfaceElement>? {
|
||||
fun bounds(e: InputSurfaceElement) = RectF(e.x * width, e.y * height, (e.x + e.width) * width, (e.y + e.height) * height)
|
||||
val boxes = elements.map(::bounds)
|
||||
val addedBoxes = addedControls.map(::bounds)
|
||||
// Move a face-button cross or a stick and its click button together.
|
||||
val groups = elements.indices.groupBy { index ->
|
||||
val element = elements[index]
|
||||
when (element.control) {
|
||||
InputControl.A, InputControl.B, InputControl.X, InputControl.Y -> if (element.id == KEYBOARD_ID) element.id else "face"
|
||||
InputControl.L3 -> "legacy:ls"
|
||||
InputControl.R3 -> "legacy:rs"
|
||||
else -> element.id
|
||||
}
|
||||
}.values
|
||||
val shifts = FloatArray(elements.size)
|
||||
val left = leftCutouts.map(::RectF)
|
||||
val right = rightCutouts.map(::RectF)
|
||||
val obstacles = left + right
|
||||
for (group in groups) {
|
||||
val leftEdge = left.filter { cutout -> group.any { RectF.intersects(boxes[it], cutout) } }.maxOfOrNull { it.right }
|
||||
val rightEdge = right.filter { cutout -> group.any { RectF.intersects(boxes[it], cutout) } }.minOfOrNull { it.left }
|
||||
val shift = when {
|
||||
leftEdge != null && rightEdge != null -> return null
|
||||
leftEdge != null -> leftEdge - group.minOf { boxes[it].left } + 1f
|
||||
rightEdge != null -> rightEdge - group.maxOf { boxes[it].right } - 1f
|
||||
else -> continue
|
||||
}
|
||||
for (index in group) shifts[index] = shift
|
||||
}
|
||||
val moved = boxes.mapIndexed { index, box -> RectF(box).apply { offset(shifts[index], 0f) } }
|
||||
for (i in moved.indices) {
|
||||
if (moved[i].left < 0f || moved[i].right > width || obstacles.any { RectF.intersects(moved[i], it) }) return null
|
||||
if (shifts[i] != 0f && addedBoxes.any { RectF.intersects(moved[i], it) }) return null
|
||||
for (j in 0 until i) {
|
||||
if (shifts[i] != shifts[j] && RectF.intersects(moved[i], moved[j]) && !RectF.intersects(boxes[i], boxes[j])) return null
|
||||
}
|
||||
}
|
||||
return elements.mapIndexed { index, e -> e.copy(x = e.x + shifts[index] / width).bounded() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A portrait surface gets the same pad as the panel under the game, in the lower part of the screen, with the keyboard,
|
||||
* Steam, Quick Access and stick-click buttons in a row along the bottom edge that starts after DroidDeck's corner button.
|
||||
* Steam, Quick Access, Start, Select and stick-click buttons in a row along the bottom edge.
|
||||
*/
|
||||
private fun portrait(context: Context, kit: PadKit, width: Int, height: Int, safe: Rect, scale: Float): List<InputSurfaceElement> {
|
||||
val w = width.toFloat(); val h = height.toFloat()
|
||||
@@ -137,15 +213,16 @@ object KitLayout {
|
||||
val inner = (w - safe.left - safe.right).coerceAtLeast(1f)
|
||||
val area = (min(h * .6f, inner * 1.15f) * scale).coerceAtMost(h - safe.top - safe.bottom - row).coerceAtLeast(1f)
|
||||
val top = h - safe.bottom - row - area
|
||||
val pad = dock(context, kit, inner.toInt(), area.toInt(), true, InputControl.LS, InputControl.RS, upright = true).map { e ->
|
||||
val pad = dock(context, kit, inner.toInt(), area.toInt(), true, InputControl.LS, InputControl.RS, upright = true)
|
||||
.filterNot { it.control == InputControl.SELECT || it.control == InputControl.START }.map { e ->
|
||||
e.copy(x = (safe.left + e.x * inner) / w, y = (top + e.y * area) / h, width = e.width * inner / w, height = e.height * area / h,
|
||||
floating = e.kind == InputElementKind.STICK)
|
||||
}
|
||||
val specs = specs(kit)
|
||||
fun spec(control: InputControl, id: String = "legacy:${control.id}") = specs.firstOrNull { it.control == control && it.id == id }
|
||||
val gap = GAP_DP * density
|
||||
val leftRow = listOfNotNull(spec(InputControl.A, KEYBOARD_ID), spec(InputControl.GUIDE).takeIf { kit.steam }, spec(InputControl.L3))
|
||||
val rightRow = listOfNotNull(spec(InputControl.QAM), spec(InputControl.GUIDE).takeIf { !kit.steam }, spec(InputControl.R3))
|
||||
val leftRow = listOfNotNull(spec(InputControl.A, KEYBOARD_ID), spec(InputControl.GUIDE).takeIf { kit.steam }, spec(InputControl.L3), spec(InputControl.SELECT))
|
||||
val rightRow = listOfNotNull(spec(InputControl.QAM), spec(InputControl.GUIDE).takeIf { !kit.steam }, spec(InputControl.R3), spec(InputControl.START))
|
||||
val start = safe.left + CORNER_DP * density
|
||||
val span = (w - safe.right - gap - start - gap * (leftRow.size + rightRow.size - 1)).coerceAtLeast(1f)
|
||||
val size = min(BUTTON_DP * density, span / (leftRow + rightRow).sumOf { (it.hw / it.hh).toDouble() }.toFloat())
|
||||
|
||||
@@ -15,6 +15,7 @@ import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import android.view.WindowInsets
|
||||
import android.widget.FrameLayout
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import com.droiddeck.launcher.ui.InputSurfaceView
|
||||
import com.droiddeck.launcher.ui.PadArt
|
||||
|
||||
@@ -25,6 +26,7 @@ class OnScreenControls(context: Context, pad: PadBridge?, val dock: OnScreenLayo
|
||||
private val surface = InputSurfaceView(context, pad, inputRole = InputRole.PRIMARY, haptics = dock != null)
|
||||
private var layout: SavedInputLayout? = null
|
||||
private val safe = Rect()
|
||||
private var cutouts = emptyList<Rect>()
|
||||
private var viewWidth = 0
|
||||
private var viewHeight = 0
|
||||
private val corner = CornerButton(context)
|
||||
@@ -62,11 +64,22 @@ class OnScreenControls(context: Context, pad: PadBridge?, val dock: OnScreenLayo
|
||||
}
|
||||
|
||||
private fun render() {
|
||||
val placed = OnScreenLayout.elements(context, layout, viewWidth, viewHeight, safe, dock)
|
||||
val placed = OnScreenLayout.elements(context, layout, viewWidth, viewHeight, safe, dock, cutouts,
|
||||
rearButtons = SessionPrefs.oscRearButtons(context), trackpads = SessionPrefs.oscTrackpads(context))
|
||||
surface.updateCustomLayout(if (hidden != null) placed.filter { it.alwaysShown } else placed)
|
||||
surface.updateStyle(layout?.style ?: InputSurfaceStyle())
|
||||
corner.visibility = if (layout == null || dock != null) GONE else VISIBLE
|
||||
corner.alpha = (layout?.style?.opacity ?: 100) / 100f
|
||||
val density = resources.displayMetrics.density
|
||||
val margin = (CORNER_MARGIN_DP * density).toInt()
|
||||
val params = corner.layoutParams as LayoutParams
|
||||
val leftInset = if (dock == null && viewWidth >= viewHeight && layout?.let(OnScreenLayout::generated) == true)
|
||||
KitLayout.cornerInset(safe, cutouts, viewHeight, density) else safe.left
|
||||
if (params.leftMargin != leftInset + margin || params.bottomMargin != safe.bottom + margin) {
|
||||
params.leftMargin = leftInset + margin
|
||||
params.bottomMargin = safe.bottom + margin
|
||||
corner.layoutParams = params
|
||||
}
|
||||
corner.invalidate()
|
||||
}
|
||||
|
||||
@@ -91,18 +104,11 @@ class OnScreenControls(context: Context, pad: PadBridge?, val dock: OnScreenLayo
|
||||
|
||||
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
|
||||
val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null
|
||||
val margin = (CORNER_MARGIN_DP * resources.displayMetrics.density).toInt()
|
||||
val params = corner.layoutParams as LayoutParams
|
||||
val left = (cutout?.safeInsetLeft ?: 0) + margin
|
||||
val bottom = (cutout?.safeInsetBottom ?: 0) + margin
|
||||
if (params.leftMargin != left || params.bottomMargin != bottom) {
|
||||
params.leftMargin = left
|
||||
params.bottomMargin = bottom
|
||||
corner.layoutParams = params
|
||||
}
|
||||
val fresh = Rect(cutout?.safeInsetLeft ?: 0, if (dock == null) cutout?.safeInsetTop ?: 0 else 0, cutout?.safeInsetRight ?: 0, cutout?.safeInsetBottom ?: 0)
|
||||
if (fresh != safe) {
|
||||
val bounds = cutout?.boundingRects.orEmpty()
|
||||
if (fresh != safe || bounds != cutouts) {
|
||||
safe.set(fresh)
|
||||
cutouts = bounds
|
||||
render()
|
||||
}
|
||||
return super.onApplyWindowInsets(insets)
|
||||
|
||||
@@ -23,19 +23,25 @@ object OnScreenLayout {
|
||||
* The built-in gamepad keeps no kit positions until its kit is edited; until then the kit follows the virtual controller and
|
||||
* the layout stores only the keys and controls the player added on top.
|
||||
*/
|
||||
fun generated(layout: SavedInputLayout) = (layout.id == InputLibraries.OVERLAY || layout.origin == InputLibraries.OVERLAY) &&
|
||||
!layout.kitless && layout.elements.none { KitLayout.isKit(it.id) }
|
||||
fun generated(layout: SavedInputLayout) = hasBuiltInKit(layout) && layout.elements.none { KitLayout.isKit(it.id) }
|
||||
|
||||
fun elements(context: Context, layout: SavedInputLayout?, width: Int, height: Int, safe: Rect, dock: Dock? = null): List<InputSurfaceElement> {
|
||||
fun hasBuiltInKit(layout: SavedInputLayout) =
|
||||
!layout.kitless && (layout.id == InputLibraries.OVERLAY || layout.origin == InputLibraries.OVERLAY)
|
||||
|
||||
fun elements(context: Context, layout: SavedInputLayout?, width: Int, height: Int, safe: Rect, dock: Dock? = null,
|
||||
cutouts: List<Rect> = emptyList(), rearButtons: Boolean = true, trackpads: Boolean = true): List<InputSurfaceElement> {
|
||||
layout ?: return emptyList()
|
||||
if (width <= 0 || height <= 0) return layout.elements
|
||||
if (generated(layout)) {
|
||||
val kit = PadKit.current(context)
|
||||
val extras = if (layout.elements.isEmpty()) emptyList() else fit(layout.elements, aspect(context, layout), 1f, width, height, safe)
|
||||
return (if (dock != null) KitLayout.dock(context, kit, width, height, dock.portrait, dock.left, dock.right)
|
||||
else KitLayout.elements(context, kit, width, height, safe, layout.scale)) + extras
|
||||
else KitLayout.elements(context, kit, width, height, safe, layout.scale, cutouts, rearButtons, trackpads, addedControls = extras)) + extras
|
||||
}
|
||||
return fit(layout.elements, aspect(context, layout), 1f, width, height, safe)
|
||||
val elements = if (dock == null && hasBuiltInKit(layout)) layout.elements.filter { element ->
|
||||
!KitLayout.isKit(element.id) || KitLayout.visibleOnOverlay(element.control, rearButtons, trackpads)
|
||||
} else layout.elements
|
||||
return fit(elements, aspect(context, layout), 1f, width, height, safe)
|
||||
}
|
||||
|
||||
fun editorElements(context: Context, layout: SavedInputLayout, width: Int, height: Int, edited: Boolean = false): List<InputSurfaceElement> =
|
||||
|
||||
@@ -678,6 +678,18 @@ object SessionPrefs {
|
||||
editor.putString("osc", mode).apply()
|
||||
}
|
||||
|
||||
fun oscRearButtons(context: Context): Boolean = prefs(context).getBoolean("oscRearButtons", false)
|
||||
|
||||
fun setOscRearButtons(context: Context, shown: Boolean) {
|
||||
prefs(context).edit().putBoolean("oscRearButtons", shown).apply()
|
||||
}
|
||||
|
||||
fun oscTrackpads(context: Context): Boolean = prefs(context).getBoolean("oscTrackpads", false)
|
||||
|
||||
fun setOscTrackpads(context: Context, shown: Boolean) {
|
||||
prefs(context).edit().putBoolean("oscTrackpads", shown).apply()
|
||||
}
|
||||
|
||||
/** Draw Steam's touch layout for the game instead of DroidDeck's own. */
|
||||
fun steamTouchLayout(context: Context): Boolean =
|
||||
prefs(context).getBoolean("steamTouchLayout", prefs(context).getString("osc", null) == OSC_STEAM_TOUCH)
|
||||
|
||||
@@ -295,6 +295,10 @@ class DrawerActions(
|
||||
val layoutChoice: String = "",
|
||||
val onLayout: (String) -> Unit = {},
|
||||
val onEditLayout: (() -> Unit)? = null,
|
||||
val oscRearButtons: Boolean? = null,
|
||||
val oscTrackpads: Boolean = false,
|
||||
val onOscRearButtons: (Boolean) -> Unit = {},
|
||||
val onOscTrackpads: (Boolean) -> Unit = {},
|
||||
val controller: com.droiddeck.launcher.input.ControllerPrefs.Settings? = null,
|
||||
val onRumble: (Boolean) -> Unit = {},
|
||||
val onSuspendPolicy: (String) -> Unit,
|
||||
@@ -676,6 +680,12 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
|
||||
SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), a.oscMode,
|
||||
tabModifier = { focus.track(page, "osc-$it") }, onPick = a.onOsc)
|
||||
}
|
||||
a.oscRearButtons?.let { rearButtons ->
|
||||
ToggleRow(host, "osc-rear-buttons", stringResource(R.string.osc_rear_buttons), null, rearButtons,
|
||||
chipModifier = focus.track(page, "osc-rear-buttons"), onChange = a.onOscRearButtons)
|
||||
ToggleRow(host, "osc-trackpads", stringResource(R.string.osc_trackpads), null, a.oscTrackpads,
|
||||
chipModifier = focus.track(page, "osc-trackpads"), onChange = a.onOscTrackpads)
|
||||
}
|
||||
ChoiceRow(host, "touch", stringResource(R.string.input_touches), null,
|
||||
listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.drawer_touch_auto, a.touchAuto), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_mouse),
|
||||
SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_GESTURES to stringResource(R.string.mode_touch_gestures),
|
||||
|
||||
@@ -330,6 +330,8 @@
|
||||
<string name="osc_layout_steam">Steam layout</string>
|
||||
<string name="osc_edit_layout">Edit layout</string>
|
||||
<string name="osc_show">Show</string>
|
||||
<string name="osc_rear_buttons">Rear buttons</string>
|
||||
<string name="osc_trackpads">Trackpads</string>
|
||||
<string name="osc_more">More controller settings</string>
|
||||
<string name="osc_more_detail">Shortcuts, latency and the PC keyboard</string>
|
||||
<string name="input_touches">Touches elsewhere</string>
|
||||
|
||||
@@ -113,6 +113,56 @@ class KitLayoutTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun aSideCutoutOnlyMovesTheOverlappingCluster() {
|
||||
val w = 2400
|
||||
val h = 1080
|
||||
val plain = KitLayout.elements(context, PadKit.STEAM_DECK, w, h, rearButtons = false, trackpads = false)
|
||||
val left = Rect(10, 380, 120, 480)
|
||||
val right = Rect(w - 120, 380, w, 480)
|
||||
val adjusted = KitLayout.elements(context, PadKit.STEAM_DECK, w, h, Rect(120, 0, 120, 0),
|
||||
cutouts = listOf(left, right), rearButtons = false, trackpads = false)
|
||||
val face = setOf("legacy:a", "legacy:b", "legacy:x", "legacy:y")
|
||||
val xOffsets = plain.zip(adjusted).associate { (before, after) ->
|
||||
assertEquals(before.id, after.id)
|
||||
assertEquals(before.y, after.y, .00001f)
|
||||
assertEquals(before.width, after.width, .00001f)
|
||||
assertEquals(before.height, after.height, .00001f)
|
||||
assertTrue(after.x * w >= left.right || (after.x + after.width) * w <= left.left ||
|
||||
after.y * h >= left.bottom || (after.y + after.height) * h <= left.top)
|
||||
assertTrue(after.x * w >= right.right || (after.x + after.width) * w <= right.left ||
|
||||
after.y * h >= right.bottom || (after.y + after.height) * h <= right.top)
|
||||
before.id to (after.x - before.x) * w
|
||||
}
|
||||
assertTrue(xOffsets.getValue(KitLayout.DPAD_ID) in 1f..100f)
|
||||
assertTrue(xOffsets.getValue("legacy:b") in -100f..-1f)
|
||||
for ((id, offset) in xOffsets) if (id in face) assertEquals(xOffsets.getValue("legacy:b"), offset, .01f)
|
||||
else if (id != KitLayout.DPAD_ID) assertEquals(id, 0f, offset, .01f)
|
||||
assertTrue(overlapping(adjusted, w, h).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aCutoutShiftDoesNotCoverAnAddedControl() {
|
||||
val w = 2400
|
||||
val h = 1080
|
||||
val safe = Rect(120, 0, 0, 0)
|
||||
val extra = InputSurfaceElement(InputControl.B, .05f, .4f, .025f, .06f, id = "added")
|
||||
val layout = SavedInputLayout(InputLibraries.OVERLAY, "Gamepad", listOf(extra), aspect = w.toFloat() / h)
|
||||
val fallback = OnScreenLayout.elements(context, layout, w, h, safe, rearButtons = false, trackpads = false)
|
||||
val placed = OnScreenLayout.elements(context, layout, w, h, safe, cutouts = listOf(Rect(0, 380, 120, 480)),
|
||||
rearButtons = false, trackpads = false)
|
||||
assertEquals(fallback, placed)
|
||||
assertTrue(overlapping(placed, w, h).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun startAndSelectAreOnTheFloatingOverlayBottomRow() {
|
||||
for (kit in PadKit.entries) for ((w, h) in sizes) {
|
||||
val buttons = KitLayout.elements(context, kit, w, h)
|
||||
val keyboard = buttons.single { it.id == KitLayout.KEYBOARD_ID }
|
||||
for (button in buttons.filter { it.control == InputControl.SELECT || it.control == InputControl.START }) {
|
||||
assertTrue("$kit $w x $h ${button.id}", button.y + button.height / 2 in keyboard.y..(keyboard.y + keyboard.height))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun widerScreensSpreadTheControlsInsteadOfStretchingThem() {
|
||||
for (kit in PadKit.entries) {
|
||||
val narrow = KitLayout.elements(context, kit, 1920, 1080).associateBy { it.id }
|
||||
|
||||
@@ -32,6 +32,36 @@ class ShowLayoutTest {
|
||||
view.releaseInputs()
|
||||
}
|
||||
|
||||
@Test fun overlayExtrasAreIndependentAndKeepTheirPreferencesWhenRecreated() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_DECK)
|
||||
val kit = SavedInputLayout(InputLibraries.OVERLAY, "Gamepad")
|
||||
val grips = setOf(InputControl.L4, InputControl.L5, InputControl.R4, InputControl.R5)
|
||||
val trackpads = setOf(InputControl.LEFT_PAD, InputControl.RIGHT_PAD)
|
||||
fun controls(dock: OnScreenLayout.Dock? = null, layout: SavedInputLayout = kit): OnScreenControls =
|
||||
OnScreenControls(context, null, dock).also { it.updateLayout(layout); sized(it, 1920, 1080) }
|
||||
fun shown(view: OnScreenControls) = view.inputSurface.shownElements.map { it.control }.toSet()
|
||||
val overlay = controls()
|
||||
assertTrue(shown(overlay).intersect(grips + trackpads).isEmpty())
|
||||
SessionPrefs.setOscRearButtons(context, true)
|
||||
overlay.reload()
|
||||
assertTrue(shown(overlay).containsAll(grips))
|
||||
assertTrue(shown(overlay).intersect(trackpads).isEmpty())
|
||||
SessionPrefs.setOscTrackpads(context, true)
|
||||
SessionPrefs.setOscRearButtons(context, false)
|
||||
assertTrue(shown(controls()).containsAll(trackpads))
|
||||
assertTrue(shown(controls()).intersect(grips).isEmpty())
|
||||
|
||||
SessionPrefs.setOscTrackpads(context, false)
|
||||
val customPad = InputSurfaceElement(InputControl.LEFT_PAD, .4f, .4f, .15f, .15f, id = "custom-pad")
|
||||
val edited = kit.copy(elements = listOf(customPad,
|
||||
InputSurfaceElement(InputControl.RIGHT_PAD, .6f, .4f, .15f, .15f, id = "legacy:right-pad")))
|
||||
assertEquals(listOf(customPad.id), controls(layout = edited).inputSurface.shownElements.map { it.id })
|
||||
|
||||
val dock = OnScreenLayout.Dock(portrait = true, grips = true)
|
||||
val panel = OnScreenControls(context, null, dock).also { it.updateLayout(kit); sized(it, 1080, 1300) }
|
||||
assertTrue(shown(panel).containsAll(grips + trackpads))
|
||||
}
|
||||
|
||||
@Test fun aSavedLayoutIsDrawnAsSaved() {
|
||||
val saved = listOf(InputSurfaceElement(InputControl.A, .4f, .4f, .1f, .1f))
|
||||
val view = InputSurfaceView(context)
|
||||
|
||||
Reference in new issue
Block a user