mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 06:15:58 +00:00
feat(input): Settings Controller is the in-game page for the default, with per-game layouts, Just this game and a direct remap page
This commit is contained in:
1 parent
afa696cef2
commit
446480aa19
15 files changed
+561
-570
No files matched your search
@@ -0,0 +1,115 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
|
||||
/** The on-screen layout each game draws, shared by the in-game Controller page and Settings › Controller. A null game is the default. */
|
||||
object ControlsChoice {
|
||||
const val NEW = "new"
|
||||
const val DEFAULT = "default"
|
||||
|
||||
data class Game(val id: Long, val name: String, val layout: String)
|
||||
|
||||
private fun own(library: InputLibrary, id: String?): String? = id?.takeIf { bindings ->
|
||||
library.bindings.any { it.id == bindings && !it.physical && it.layoutId?.let { l -> library.layouts.any { x -> x.id == l && x.supports(InputRole.PRIMARY) } } == true }
|
||||
}
|
||||
|
||||
private fun own(context: Context, library: InputLibrary, game: Long?) = own(library, GameControls.bindings(context, game))
|
||||
|
||||
/** The input library with [game]'s main-screen layout selected, without saving it. */
|
||||
fun library(context: Context, game: Long?, desktop: Boolean): InputLibrary {
|
||||
val library = InputLibraries.read(context)
|
||||
val chosen = own(context, library, game)
|
||||
return if (chosen == null) library else library.selectPart(desktop, InputRole.PRIMARY, chosen)
|
||||
}
|
||||
|
||||
fun options(context: Context, game: Long?, steamMode: Boolean, gameName: String? = null): List<Pair<String, String>> {
|
||||
val library = InputLibraries.read(context)
|
||||
val own = game != null && game in GameControls.games(context)
|
||||
return (if (own) listOf(DEFAULT to context.getString(R.string.osc_default)) else emptyList()) +
|
||||
listOf(InputLibraries.OVERLAY to context.getString(R.string.osc_layout_gamepad)) +
|
||||
(if (steamMode) listOf(GameControls.STEAM to context.getString(R.string.osc_layout_steam)) else emptyList()) +
|
||||
library.layouts.filter { it.editable && it.supports(InputRole.PRIMARY) && it.id != InputLibraries.OVERLAY }.map { it.id to it.name } +
|
||||
(if (game != null && !gameName.isNullOrBlank()) listOf(NEW to context.getString(R.string.osc_layout_new_for, gameName)) else emptyList())
|
||||
}
|
||||
|
||||
fun current(context: Context, game: Long?, steamMode: Boolean, desktop: Boolean = false): String =
|
||||
if (steamMode && GameControls.steamLayout(context, game)) GameControls.STEAM
|
||||
else library(context, game, desktop).activeLayout(desktop, InputRole.PRIMARY)?.id ?: InputLibraries.OVERLAY
|
||||
|
||||
fun choose(context: Context, game: Long?, id: String, desktop: Boolean, gameName: String? = null) {
|
||||
require(id != NEW)
|
||||
if (id == DEFAULT) { if (game != null) useDefault(context, game); return }
|
||||
val steam = id == GameControls.STEAM
|
||||
val bindings = if (steam) null else {
|
||||
val library = InputLibraries.read(context)
|
||||
val layout = library.layouts.first { it.id == id }
|
||||
library.bindings.firstOrNull { !it.physical && it.compatible(layout) }?.id ?: newInputId().also { newId ->
|
||||
InputLibraries.update(context) { it.withPreset(SavedInputBindings(newId, layout.name, false, layout.id)) }
|
||||
}
|
||||
}
|
||||
if (game != null) GameControls.choose(context, game, bindings, steam, gameName)
|
||||
else {
|
||||
SessionPrefs.setSteamTouchLayout(context, steam)
|
||||
if (bindings != null) InputLibraries.update(context) { it.selectPart(desktop, InputRole.PRIMARY, bindings) }
|
||||
}
|
||||
}
|
||||
|
||||
/** Copies the layout [game] draws now, actions included, into a layout of its own and chooses it for that game only. */
|
||||
fun newForGame(context: Context, game: Long, name: String, desktop: Boolean): Pair<String, String> {
|
||||
val layoutId = newInputId()
|
||||
val bindingsId = newInputId()
|
||||
InputLibraries.update(context) { latest ->
|
||||
val chosen = own(context, latest, game)
|
||||
val part = (if (chosen == null) latest else latest.selectPart(desktop, InputRole.PRIMARY, chosen)).activePart(desktop, InputRole.PRIMARY)
|
||||
val source = latest.layouts.firstOrNull { it.id == part.layout && it.editable } ?: latest.layouts.first { it.id == InputLibraries.OVERLAY }
|
||||
val actions = latest.bindings.firstOrNull { it.id == part.bindings && it.compatible(source) }
|
||||
?: latest.bindings.firstOrNull { !it.physical && it.compatible(source) }
|
||||
?: SavedInputBindings(bindingsId, source.name, false, source.id)
|
||||
val title = name.trim().take(80).ifEmpty { source.name }
|
||||
latest.withLayout(source.derived(layoutId, title)).withPreset(actions.copy(id = bindingsId, name = title, layoutId = layoutId))
|
||||
}
|
||||
GameControls.choose(context, game, bindingsId, steam = false, name = name)
|
||||
return layoutId to bindingsId
|
||||
}
|
||||
|
||||
/** The profile, layout and actions the editor opens for [game], or null when its layout can't be edited here. */
|
||||
fun editTarget(context: Context, game: Long?, desktop: Boolean): Triple<String, String, String>? {
|
||||
val library = library(context, game, desktop)
|
||||
val layout = library.activeLayout(desktop, InputRole.PRIMARY)?.takeIf { it.editable } ?: return null
|
||||
return Triple(library.activeId(desktop), layout.id, library.activePart(desktop, InputRole.PRIMARY).bindings)
|
||||
}
|
||||
|
||||
/** Whether [game] has a layout of its own, rather than the default. */
|
||||
fun hasOwn(context: Context, game: Long?): Boolean = own(context, InputLibraries.read(context), game) != null
|
||||
|
||||
/** Whether saving an edit of [game]'s layout would also change the default or another game. */
|
||||
fun shared(context: Context, game: Long?): Boolean {
|
||||
game ?: return false
|
||||
val library = InputLibraries.read(context)
|
||||
val bindings = own(context, library, game) ?: return true
|
||||
val layout = library.preset(bindings).layoutId
|
||||
fun drawsLayout(id: String) = library.bindings.firstOrNull { it.id == id }?.layoutId == layout
|
||||
return library.usersOfLayout(layout ?: return true).isNotEmpty() ||
|
||||
library.selections.values.any { s -> s.parts[InputRole.PRIMARY]?.let(::drawsLayout) == true } ||
|
||||
GameControls.games(context).any { it != game && own(context, library, it)?.let(::drawsLayout) == true }
|
||||
}
|
||||
|
||||
fun useDefault(context: Context, game: Long) = GameControls.clear(context, game)
|
||||
|
||||
/** Games with a layout of their own, by name. */
|
||||
fun games(context: Context): List<Game> {
|
||||
val library = InputLibraries.read(context)
|
||||
return GameControls.games(context).mapNotNull { id ->
|
||||
val layout = when {
|
||||
GameControls.steamLayout(context, id) -> context.getString(R.string.osc_layout_steam)
|
||||
else -> own(context, library, id)?.let { b -> library.layouts.firstOrNull { it.id == library.preset(b).layoutId }?.name } ?: return@mapNotNull null
|
||||
}
|
||||
val name = GameControls.name(context, id)
|
||||
?: id.takeIf { it in Int.MIN_VALUE..Int.MAX_VALUE }?.let { runCatching { SteamTouchConfig.gameName(context, it.toInt()) }.getOrNull() }
|
||||
?: id.toString()
|
||||
Game(id, name, layout)
|
||||
}.sortedBy { it.name.lowercase() }
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,20 @@ object GameControls {
|
||||
return if (game != null && p.contains("$game.steam")) p.getBoolean("$game.steam", false) else SessionPrefs.steamTouchLayout(context)
|
||||
}
|
||||
|
||||
fun choose(context: Context, game: Long, bindings: String?, steam: Boolean) {
|
||||
fun choose(context: Context, game: Long, bindings: String?, steam: Boolean, name: String? = null) {
|
||||
prefs(context).edit().apply { if (bindings == null) remove("$game.bindings") else putString("$game.bindings", bindings) }
|
||||
.apply { if (!name.isNullOrBlank()) putString("$game.name", name) }
|
||||
.putBoolean("$game.steam", steam).apply()
|
||||
}
|
||||
|
||||
/** Games with their own choice, in the order of their ids. */
|
||||
fun games(context: Context): List<Long> = prefs(context).all.keys
|
||||
.filter { it.endsWith(".steam") || it.endsWith(".bindings") }
|
||||
.mapNotNull { it.substringBeforeLast('.').toLongOrNull() }.distinct().sorted()
|
||||
|
||||
fun name(context: Context, game: Long): String? = prefs(context).getString("$game.name", null)
|
||||
|
||||
fun clear(context: Context, game: Long) {
|
||||
prefs(context).edit().remove("$game.bindings").remove("$game.steam").apply()
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ object GamepadGeometry {
|
||||
|
||||
fun tall(w: Float, h: Float): Boolean = h >= w * 0.45f
|
||||
|
||||
fun layout(w: Float, h: Float, density: Float, portraitDock: Boolean = false, grips: Boolean = true, upright: Boolean = false,
|
||||
fun layout(w: Float, h: Float, density: Float, portraitDock: Boolean = false, grips: Boolean = false, upright: Boolean = false,
|
||||
trackpads: Boolean = false): Map<Slot, Box> {
|
||||
if (w <= 0f || h <= 0f) return emptyMap()
|
||||
if (upright || portraitDock && h >= w * 0.6f && w >= 300 * density && h >= 300 * density) {
|
||||
@@ -72,6 +72,7 @@ object GamepadGeometry {
|
||||
else -> 1
|
||||
}
|
||||
middleGrid(columns, gap - 2 * gutter, shoulderY - buttonH / 2f)
|
||||
if (grips) gripsBetween(boxes, w, gap - 2 * gutter, boxes.values.maxOf { if (it.cx in (w - gap) / 2f..(w + gap) / 2f) it.bottom else 0f } + gutter, h - pad, density)
|
||||
} else {
|
||||
val side = min(min(w * 0.5f, h * 0.70f), (w - 2 * gridButton - 2 * gutter) / 1.42f)
|
||||
val main = side * 0.62f
|
||||
@@ -87,13 +88,30 @@ object GamepadGeometry {
|
||||
val gap = w - 2 * (side * 0.40f + main / 2f)
|
||||
val columns = if (gap >= 2 * gridButton) 2 else 1
|
||||
val rows = (middle.size + columns - 1) / columns
|
||||
middleGrid(columns, gap - 2 * gutter, h - pad - rows * buttonH - (rows - 1) * 10 * density)
|
||||
val gridTop = h - pad - rows * buttonH - (rows - 1) * 10 * density
|
||||
middleGrid(columns, gap - 2 * gutter, gridTop)
|
||||
if (grips) gripsBetween(boxes, w, gap - 2 * gutter, shoulderY + buttonH / 2f + gutter, gridTop - gutter, density)
|
||||
val panelsW = min(112 * density, w - 2 * (side * 0.57f + shoulderW / 2f) - 2 * gutter)
|
||||
if (panelsW >= buttonH) boxes[Slot.PANELS] = Box(w / 2f, shoulderY, panelsW, buttonH)
|
||||
}
|
||||
return boxes
|
||||
}
|
||||
|
||||
/** The back grips in the gap between a landscape panel's two clusters: one row of four, or two rows of two. */
|
||||
private fun gripsBetween(boxes: MutableMap<Slot, Box>, w: Float, span: Float, top: Float, bottom: Float, density: Float) {
|
||||
val gap = 8 * density
|
||||
val minimum = 44 * density
|
||||
val rows = if ((span - 3 * gap) / 4f >= minimum) 1 else if ((span - gap) / 2f >= minimum) 2 else return
|
||||
val perRow = 4 / rows
|
||||
val cellW = ((span - (perRow - 1) * gap) / perRow).coerceAtMost(96 * density)
|
||||
val cellH = ((bottom - top - (rows - 1) * gap) / rows).coerceAtMost(64 * density)
|
||||
if (cellH < minimum) return
|
||||
for ((i, slot) in listOf(Slot.L4, Slot.L5, Slot.R5, Slot.R4).withIndex()) {
|
||||
val col = i % perRow; val row = i / perRow
|
||||
boxes[slot] = Box(w / 2f + (col - (perRow - 1) / 2f) * (cellW + gap), bottom - (rows - 1 - row) * (cellH + gap) - cellH / 2f, cellW, cellH)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The panel under a portrait game: shoulders with View and Menu across the top, the back grips under them when the kit has
|
||||
* them, then the d-pad and face buttons and, along the bottom edge, the sticks or trackpads, each as large as the panel allows.
|
||||
|
||||
@@ -93,12 +93,12 @@ object KitLayout {
|
||||
fun x(ref: Float) = when {
|
||||
ref < REF_W / 3f -> safe.left + ref * unit
|
||||
ref > REF_W * 2f / 3f -> w - safe.right - (REF_W - ref) * unit
|
||||
else -> w / 2f + (ref - REF_W / 2f) * unit
|
||||
else -> (w / 2f + (ref - REF_W / 2f) * unit).between(safe.left + ref * unit, w - safe.right - (REF_W - ref) * unit)
|
||||
}
|
||||
fun y(ref: Float) = when {
|
||||
ref < REF_H / 3f -> safe.top + ref * unit
|
||||
ref > REF_H * 2f / 3f -> h - safe.bottom - (REF_H - ref) * unit
|
||||
else -> h / 2f + (ref - REF_H / 2f) * unit
|
||||
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
|
||||
@@ -204,4 +204,7 @@ object KitLayout {
|
||||
val mm = shortSide / dpi * 25.4f
|
||||
return min(1f, MAX_HEIGHT_MM / max(mm, 1f))
|
||||
}
|
||||
|
||||
/** The middle band never comes closer to an edge than the edge rows' own reference spacing allows. */
|
||||
private fun Float.between(low: Float, high: Float) = coerceIn(low, max(low, high))
|
||||
}
|
||||
Reference in new issue
Block a user