mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
feat(input): controller-specific on-screen controls with editor, rotation and tablet fixes (#2988)
* feat(input): restore controller-specific overlay designs * fix(input): restore portrait controller layouts * fix(input): fix duplicates, rotation and tablet placement in the controller overlays - Hide the below-game controls while the layout editor is open, and give the editor the same cutout insets and optional Steam controls as the game view, so the editor never shows a second copy of a control. - Keep a separate portrait frame for layouts edited in portrait. A layout edited in one orientation keeps the stock kit positions in the other instead of being stretched across. - Anchor the overlay to the bottom of the screen and cap the portrait panel's physical size, so tablets keep the controls in the lower half. - Put L2/L1 and R1/R2 side by side in every kit. - List deleted stock controls in the editor's Add dialog so they can be restored, and keep removed rear buttons and trackpads removed. * fix(input): scale edited overlay layouts around the controller artwork positions * fix(input): keep stock landscape controls, color setting, keyboard action and displayed frame edits consistent * fix(input): draw the layout editor with the kit artwork at the size it will be saved The editor placed its controls in onSizeChanged, which runs during layout, so the preview canvas was rebuilt or remeasured too late: new controls were laid out at 0x0 and existing ones kept stale sizes, losing the kit D-pad artwork. Placement now happens in onMeasure, before the canvas measures its children. The editor preview used the saved button size instead of the slider value, so the slider showed a different layout from the one Done saved. D-pad pieces that joined or left the kit cross were never redrawn, so moving one piece hid all four. * fix(input): keep portrait rear buttons and trackpads where they were saved A portrait save stores the rear buttons and trackpads only in the rotated frame, but loading that frame only moved controls that already existed in the main frame, so they were regenerated at the screen edges. * fix(input): adopt the displayed portrait frame for layouts edited only in landscape Button size and reset positions now switch to the frame on screen whenever the edited layout's orientation differs from the editor, not only when a rotated frame exists, so a portrait change is saved as the portrait frame instead of overwriting the landscape edits. The tools panel stays open while that happens. * fix(input): keep a separate button size for the rotated frame * fix(input): take the shown frame's button size whenever editing switches orientation --------- Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
1 parent
183cbd9351
commit
378175c6ac
22 files changed
+1345
-426
No files matched your search
@@ -132,6 +132,7 @@ class InputLayoutToolsTest {
|
||||
}
|
||||
|
||||
@Test fun aScaledGamepadStaysGeneratedAndRegeneratesOnRotation() {
|
||||
com.droiddeck.launcher.session.SessionPrefs.setSteamController(context, com.droiddeck.launcher.session.SessionPrefs.CONTROLLER_NONE)
|
||||
val (display, metrics) = InputLibraries.primaryDisplay(context)
|
||||
val long = maxOf(metrics.widthPixels, metrics.heightPixels)
|
||||
val short = minOf(metrics.widthPixels, metrics.heightPixels)
|
||||
|
||||
@@ -76,20 +76,21 @@ class OnScreenLayoutTest {
|
||||
assertEquals(base, both.filter { it.control in standard })
|
||||
}
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_XBOX360)
|
||||
assertEquals(original, placed(true, true))
|
||||
assertEquals(original.map { it.id }, placed(true, true).map { it.id })
|
||||
assertEquals(placed(false, false), placed(true, true))
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_DECK)
|
||||
val edited = layout.copy(elements = layout.elements.map { e -> if (e.control == InputControl.START) e.copy(y = .3f) else e })
|
||||
val edited = layout.copy(elements = layout.elements.map { e -> if (e.control == InputControl.START) e.copy(y = .3f) else e }, aspect = w.toFloat() / h)
|
||||
val fitted = OnScreenLayout.elements(context, edited, w, h, safe)
|
||||
assertTrue(fitted.single { it.control == InputControl.START }.y < .5f)
|
||||
}
|
||||
|
||||
@Test fun theBuiltInGamepadIsRegeneratedForEverySizeWithoutStretching() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_STEAM)
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE)
|
||||
val layout = gamepad()
|
||||
for ((w, h) in listOf(1200 to 540, 540 to 1200, 900 to 850)) {
|
||||
val placed = OnScreenLayout.elements(context, layout, w, h, Rect())
|
||||
assertEquals(layout.elements.map { it.id }, placed.map { it.id })
|
||||
assertOnlyShortcutPositionsChanged(LegacyGamepadLayout(context, w, h).regenerate(layout.elements), placed)
|
||||
assertEquals(LegacyGamepadLayout(context, w, h).regenerate(layout.elements), placed)
|
||||
for (id in listOf("legacy:a", "legacy:up", "legacy:guide")) assertSquare(placed.first { it.id == id }, w, h)
|
||||
assertInside(placed, w, h)
|
||||
}
|
||||
@@ -97,10 +98,10 @@ class OnScreenLayoutTest {
|
||||
|
||||
@Test @Config(qualifiers = "w745dp-h343dp-land-420dpi")
|
||||
fun aBuiltInGamepadSavedAtTheDisplaySizeRegeneratesForTheMeasuredSurface() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_STEAM)
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE)
|
||||
val saved = gamepad().copy(elements = LegacyGamepadLayout(context, 2608, 1200).elements(), aspect = 2608f / 1200)
|
||||
val placed = OnScreenLayout.elements(context, saved, 2048, 1200, Rect())
|
||||
assertOnlyShortcutPositionsChanged(LegacyGamepadLayout(context, 2048, 1200).regenerate(saved.elements), placed)
|
||||
assertEquals(LegacyGamepadLayout(context, 2048, 1200).regenerate(saved.elements), placed)
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w745dp-h343dp-land-420dpi")
|
||||
@@ -175,7 +176,7 @@ class OnScreenLayoutTest {
|
||||
}
|
||||
|
||||
@Test fun regenerationKeepsTheUsersButtonStylesAndRemovedButtons() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_STEAM)
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE)
|
||||
val layout = gamepad().let { g -> g.copy(elements = g.elements.filterNot { it.id == "legacy:y" }.map { if (it.id == "legacy:a") it.copy(opacity = .4f, icon = 0, name = "Jump") else it }) }
|
||||
val placed = OnScreenLayout.elements(context, layout, 2000, 1000, Rect())
|
||||
assertNull(placed.firstOrNull { it.id == "legacy:y" })
|
||||
@@ -196,8 +197,10 @@ class OnScreenLayoutTest {
|
||||
assertEquals(control.id, x, cx, .01f)
|
||||
assertEquals(control.id, y, cy, .01f)
|
||||
}
|
||||
assertCenter(InputControl.LT, 140f, 66f)
|
||||
assertCenter(InputControl.LB, 140f, 148f)
|
||||
assertCenter(InputControl.LT, 120f, 96f)
|
||||
assertCenter(InputControl.LB, 310f, 96f)
|
||||
assertCenter(InputControl.RB, 1290f, 96f)
|
||||
assertCenter(InputControl.RT, 1480f, 96f)
|
||||
assertCenter(InputControl.UP, 220f, 242.5f)
|
||||
assertCenter(InputControl.RIGHT, 322.5f, 345f)
|
||||
assertCenter(InputControl.LS, 290f, 640f)
|
||||
@@ -276,6 +279,7 @@ class OnScreenLayoutTest {
|
||||
}
|
||||
|
||||
@Test fun theUnfoldedStripUsesTheDockGeometryInLandscapeAndPortrait() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE)
|
||||
for ((size, portrait) in listOf((2208 to 800) to false, (1080 to 1300) to true)) {
|
||||
val (w, h) = size
|
||||
val placed = OnScreenLayout.elements(context, gamepad(), w, h, Rect(), dock = OnScreenLayout.Dock(portrait, grips = true))
|
||||
@@ -303,6 +307,41 @@ class OnScreenLayoutTest {
|
||||
assertEquals(.1f * 2208f / 1768 / .2f, fitted.width * 2208 / (fitted.height * 800), .01f)
|
||||
}
|
||||
|
||||
@Test fun thePortraitAndBelowGameKitsKeepDirectionalArtClearOfTheSticksAndFaceButtons() {
|
||||
val directions = setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT)
|
||||
val face = setOf(InputControl.A, InputControl.B, InputControl.X, InputControl.Y)
|
||||
val types = listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM, SessionPrefs.CONTROLLER_XBOX360,
|
||||
SessionPrefs.CONTROLLER_DS4, SessionPrefs.CONTROLLER_DUALSENSE)
|
||||
val layout = gamepad()
|
||||
val w = 1080
|
||||
for (type in types) {
|
||||
SessionPrefs.setSteamController(context, type)
|
||||
val steam = type == SessionPrefs.CONTROLLER_DECK || type == SessionPrefs.CONTROLLER_STEAM
|
||||
val deckDock = if (steam) OnScreenLayout.Dock.deck(true, null) else OnScreenLayout.Dock(true, false)
|
||||
for ((h, dock) in listOf(1920 to null, 1300 to deckDock, 385 to deckDock)) {
|
||||
val placed = OnScreenLayout.elements(context, layout, w, h, Rect(), dock = dock,
|
||||
rearButtons = true, trackpads = true)
|
||||
assertInside(placed, w, h)
|
||||
val dpad = placed.filter { it.control in directions }
|
||||
assertEquals(4, dpad.size)
|
||||
val rightEdge = dpad.maxOf { it.x + it.width }
|
||||
val bottomEdge = dpad.maxOf { it.y + it.height }
|
||||
assertTrue(type, placed.filter { it.control in face && it.id != "builtin:keyboard" }.all { it.x > rightEdge })
|
||||
assertTrue(type, placed.filter { it.kind == InputElementKind.STICK || it.kind == InputElementKind.TRACKPAD }.all { it.y > bottomEdge })
|
||||
if (dock == null) {
|
||||
val row = placed.filter { it.control in shortcuts || it.id == "builtin:keyboard" }
|
||||
assertEquals(5, row.size)
|
||||
assertTrue(row.all { it.y > bottomEdge })
|
||||
assertEquals(steam, placed.any { it.kind == InputElementKind.TRACKPAD })
|
||||
} else {
|
||||
assertFalse(placed.any { it.control == InputControl.GUIDE || it.control == InputControl.QAM })
|
||||
assertEquals(steam, placed.any { it.kind == InputElementKind.TRACKPAD })
|
||||
assertEquals(!steam, placed.any { it.control == InputControl.RS })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val legacyKeys = "29+33:0.3,0.7,44::Find::0.6:1:1:1"
|
||||
|
||||
private fun importedOn(library: InputLibrary) = library.activeLayout(false, InputRole.PRIMARY)!!.elements.filter { it.id.startsWith("imported:") }
|
||||
@@ -398,6 +437,7 @@ class OnScreenLayoutTest {
|
||||
}
|
||||
|
||||
@Test fun aDeckStripPutsTheDeckGamepadSurfacesInTheStickSlotsAndOtherPadsKeepSticks() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE)
|
||||
val stock = InputStockLayouts.layouts().first { it.id == DeckPanel.GAMEPAD.layout }
|
||||
val deck = OnScreenLayout.Dock.deck(false, stock)
|
||||
assertEquals(OnScreenLayout.Dock(false, true, InputControl.LS, InputControl.RIGHT_PAD), deck)
|
||||
@@ -454,4 +494,138 @@ class OnScreenLayoutTest {
|
||||
assertEquals(InputStockLayouts.layouts().filter { it.id != DeckPanel.GAMEPAD.layout }.map { it.elements },
|
||||
others.filter { l -> InputStockLayouts.layouts().any { it.id == l.id } }.map { it.elements })
|
||||
}
|
||||
|
||||
private val kits = listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM, SessionPrefs.CONTROLLER_XBOX360,
|
||||
SessionPrefs.CONTROLLER_DS4, SessionPrefs.CONTROLLER_DUALSENSE)
|
||||
|
||||
private fun List<InputSurfaceElement>.at(control: InputControl) = single { it.id == "legacy:${control.id}" }
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun everyKitPutsTriggersBesideTheirBumpers() {
|
||||
for (kit in kits) {
|
||||
SessionPrefs.setSteamController(context, kit)
|
||||
val placed = OnScreenLayout.elements(context, gamepad(), 1600, 900, Rect(), rearButtons = true, trackpads = true)
|
||||
val lt = placed.at(InputControl.LT); val lb = placed.at(InputControl.LB)
|
||||
val rb = placed.at(InputControl.RB); val rt = placed.at(InputControl.RT)
|
||||
for ((outer, inner) in listOf(lt to lb, rt to rb)) assertEquals(kit, outer.y + outer.height / 2f, inner.y + inner.height / 2f, .001f)
|
||||
assertTrue(kit, lt.x + lt.width <= lb.x)
|
||||
assertTrue(kit, rb.x + rb.width <= rt.x)
|
||||
assertInside(placed, 1600, 900)
|
||||
}
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w1280dp-h800dp-land-mdpi")
|
||||
fun tabletOverlaysStayInTheLowerHalfOfTheScreen() {
|
||||
for (kit in kits) {
|
||||
SessionPrefs.setSteamController(context, kit)
|
||||
val placed = OnScreenLayout.elements(context, gamepad(), 1280, 800, Rect(), rearButtons = true, trackpads = true)
|
||||
for (e in placed) assertTrue("$kit ${e.id}", e.y >= .45f)
|
||||
assertInside(placed, 1280, 800)
|
||||
}
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun phoneOverlaysStillUseTheFullHeight() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_DECK)
|
||||
val placed = OnScreenLayout.elements(context, gamepad(), 1600, 900, Rect())
|
||||
assertTrue(placed.at(InputControl.LT).y < .15f)
|
||||
assertTrue(placed.at(InputControl.START).let { it.y + it.height } > .9f)
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun aLayoutEditedInOneOrientationKeepsTheKitInTheOther() {
|
||||
for (kit in kits) {
|
||||
SessionPrefs.setSteamController(context, kit)
|
||||
val layout = gamepad()
|
||||
val landscape = OnScreenLayout.elements(context, layout, 1600, 900, Rect())
|
||||
val moved = landscape.map { if (it.control == InputControl.A) it.copy(x = .5f, y = .5f) else it }
|
||||
val edited = layout.copy(elements = moved, aspect = 1600f / 900f)
|
||||
assertEquals(kit, .5f, OnScreenLayout.elements(context, edited, 1600, 900, Rect()).at(InputControl.A).x, .001f)
|
||||
val kitPortrait = OnScreenLayout.elements(context, layout, 900, 1600, Rect())
|
||||
val editedPortrait = OnScreenLayout.elements(context, edited, 900, 1600, Rect())
|
||||
for (control in listOf(InputControl.A, InputControl.LT, InputControl.RB, InputControl.LS, InputControl.UP))
|
||||
assertEquals("$kit ${control.id}", kitPortrait.at(control), editedPortrait.at(control))
|
||||
}
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun eachOrientationKeepsItsOwnEdits() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_XBOX360)
|
||||
val layout = gamepad()
|
||||
val landscape = OnScreenLayout.elements(context, layout, 1600, 900, Rect()).map { if (it.control == InputControl.A) it.copy(x = .5f, y = .5f) else it }
|
||||
val edited = OnScreenLayout.withFrame(context, layout, landscape, 1600f / 900f)
|
||||
val portrait = OnScreenLayout.elements(context, edited, 900, 1600, Rect()).map { if (it.control == InputControl.B) it.copy(x = .1f, y = .2f) else it }
|
||||
val both = OnScreenLayout.withFrame(context, edited, portrait, 900f / 1600f)
|
||||
assertEquals(landscape.map { it.id to floatArrayOf(it.x, it.y).toList() }, both.elements.map { it.id to floatArrayOf(it.x, it.y).toList() })
|
||||
val wide = OnScreenLayout.elements(context, both, 1600, 900, Rect())
|
||||
val tall = OnScreenLayout.elements(context, both, 900, 1600, Rect())
|
||||
assertEquals(.5f, wide.at(InputControl.A).x, .001f)
|
||||
assertEquals(.1f, tall.at(InputControl.B).x, .001f)
|
||||
assertEquals(.2f, tall.at(InputControl.B).y, .001f)
|
||||
val renamed = OnScreenLayout.withFrame(context, both, both.elements.map { if (it.control == InputControl.A) it.copy(name = "Jump") else it }, both.aspect)
|
||||
assertEquals("Jump", OnScreenLayout.elements(context, renamed, 900, 1600, Rect()).at(InputControl.A).name)
|
||||
assertEquals(.1f, OnScreenLayout.elements(context, renamed, 900, 1600, Rect()).at(InputControl.B).x, .001f)
|
||||
val library = InputLibraries.read(context).withLayout(both)
|
||||
assertEquals(both, InputLibraries.decode(InputLibraries.encode(library))!!.layouts.first { it.id == both.id })
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun aRemovedRearButtonStaysRemovedWhileTheOthersRemain() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_DECK)
|
||||
val layout = gamepad()
|
||||
val displayed = OnScreenLayout.elements(context, layout, 1600, 900, Rect(), rearButtons = true, trackpads = true)
|
||||
val frozen = OnScreenLayout.editorFrame(layout, displayed).filterNot { it.control == InputControl.L4 || it.control == InputControl.LEFT_PAD }
|
||||
val edited = layout.copy(elements = frozen, aspect = 1600f / 900f)
|
||||
for ((w, h) in listOf(1600 to 900, 900 to 1600)) {
|
||||
val placed = OnScreenLayout.elements(context, edited, w, h, Rect(), rearButtons = true, trackpads = true).map { it.control }
|
||||
assertFalse(InputControl.L4 in placed)
|
||||
assertFalse(InputControl.LEFT_PAD in placed)
|
||||
assertTrue(InputControl.R5 in placed)
|
||||
assertTrue(InputControl.RIGHT_PAD in placed)
|
||||
}
|
||||
val hidden = OnScreenLayout.elements(context, edited, 1600, 900, Rect(), rearButtons = false, trackpads = false).map { it.control }
|
||||
assertFalse(InputControl.R5 in hidden)
|
||||
assertFalse(InputControl.RIGHT_PAD in hidden)
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun aPortraitMainLayoutUsesTheStockControlsInLandscape() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_XBOX360)
|
||||
val layout = gamepad()
|
||||
val portrait = OnScreenLayout.elements(context, layout, 900, 1600, Rect()).map { if (it.control == InputControl.A) it.copy(x = .1f, y = .1f) else it }
|
||||
val edited = layout.copy(elements = portrait, aspect = 900f / 1600f)
|
||||
assertEquals(OnScreenLayout.elements(context, layout, 1600, 900, Rect()), OnScreenLayout.elements(context, edited, 1600, 900, Rect()))
|
||||
assertEquals(.1f, OnScreenLayout.elements(context, edited, 900, 1600, Rect()).at(InputControl.A).x, .001f)
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w800dp-h450dp-land-320dpi")
|
||||
fun aPortraitSaveKeepsOptionalControlsInThePortraitFrameOnly() {
|
||||
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_DECK)
|
||||
val layout = gamepad()
|
||||
val landscape = OnScreenLayout.elements(context, layout, 1600, 900, Rect())
|
||||
val edited = OnScreenLayout.withFrame(context, layout, landscape, 1600f / 900f)
|
||||
val portrait = OnScreenLayout.elements(context, edited, 900, 1600, Rect(), rearButtons = true, trackpads = true)
|
||||
.map { if (it.control == InputControl.B) it.copy(x = .1f, y = .2f) else it }
|
||||
assertTrue(portrait.any { it.control == InputControl.L4 })
|
||||
val both = OnScreenLayout.withFrame(context, edited, portrait, 900f / 1600f)
|
||||
assertTrue(both.elements.none { it.control == InputControl.L4 || it.control == InputControl.LEFT_PAD })
|
||||
assertTrue(both.rotated.any { it.control == InputControl.L4 })
|
||||
assertEquals(OnScreenLayout.elements(context, edited, 1600, 900, Rect(), rearButtons = true, trackpads = true),
|
||||
OnScreenLayout.elements(context, both, 1600, 900, Rect(), rearButtons = true, trackpads = true))
|
||||
val tall = OnScreenLayout.elements(context, both, 900, 1600, Rect(), rearButtons = true, trackpads = true)
|
||||
for (control in listOf(InputControl.L4, InputControl.LEFT_PAD, InputControl.B)) assertEquals(control.id, portrait.at(control), tall.at(control))
|
||||
assertTrue(OnScreenLayout.elements(context, both, 900, 1600, Rect(), rearButtons = false, trackpads = false)
|
||||
.none { it.control == InputControl.L4 || it.control == InputControl.LEFT_PAD })
|
||||
}
|
||||
|
||||
@Test fun stockControlsUseTheKitArtworkWhileAddedAndTrackpadControlsStillUseTheTint() {
|
||||
for (kit in listOf(PadKit.XBOX, PadKit.DUALSENSE)) {
|
||||
SessionPrefs.setSteamController(context, if (kit == PadKit.XBOX) SessionPrefs.CONTROLLER_XBOX360 else SessionPrefs.CONTROLLER_DUALSENSE)
|
||||
val layout = gamepad()
|
||||
assertTrue(kit.name, layout.elements.all { kit.artwork(it) })
|
||||
val added = InputSurfaceElement(InputControl.A, 0f, 0f, .1f, .1f, "custom", name = "Fire")
|
||||
assertFalse(kit.artwork(added))
|
||||
assertFalse(kit.artwork(layout.elements.first { it.control == InputControl.A }.copy(image = "picture")))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -522,6 +522,64 @@ class InputSettingsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun buttonSizeShowsTheGamepadWhereItWillBeSaved() {
|
||||
val library = InputLibraries.read(context)
|
||||
val layout = library.layout("game", InputRole.PRIMARY)!!
|
||||
assertEquals(InputLibraries.OVERLAY, layout.id)
|
||||
val round = mutableStateOf(0)
|
||||
compose.setContent { DroidDeckTheme { androidx.compose.runtime.key(round.value) { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY,
|
||||
layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } } }
|
||||
fun editor() = generateSequence(listOf<android.view.View>(ShadowDialog.getLatestDialog().window!!.decorView)) { views ->
|
||||
views.filterIsInstance<android.view.ViewGroup>().flatMap { g -> (0 until g.childCount).map(g::getChildAt) }.ifEmpty { null }
|
||||
}.flatten().filterIsInstance<InputSurfaceEditorView>().first()
|
||||
val stock = compose.runOnIdle { editor().rendered() }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
compose.onNodeWithTag("input-editor-scale").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(95f) }
|
||||
val scaled = compose.runOnIdle { editor().rendered() }
|
||||
assertNotEquals(stock, scaled)
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
compose.runOnIdle { assertEquals(.95f, InputLibraries.read(context).layouts.first { it.id == layout.id }.scale) }
|
||||
compose.runOnIdle { round.value = 1 }
|
||||
compose.runOnIdle { assertEquals(scaled, editor().rendered()) }
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w468dp-h833dp-port-369dpi")
|
||||
fun buttonSizeInPortraitKeepsLandscapeEditsWithoutARotatedFrame() {
|
||||
val face = setOf("legacy:a", "legacy:b", "legacy:x", "legacy:y")
|
||||
val gamepad = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!
|
||||
val landscape = gamepad.copy(elements = gamepad.elements.map { if (it.id in face) it.copy(x = it.x + .012f, y = it.y + .017f) else it },
|
||||
aspect = 1600f / 900f, rotated = emptyList(), rotatedAspect = 0f)
|
||||
InputLibraries.update(context) { it.withLayout(landscape) }
|
||||
val library = InputLibraries.read(context)
|
||||
val round = mutableStateOf(0)
|
||||
compose.setContent { DroidDeckTheme { androidx.compose.runtime.key(round.value) { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY,
|
||||
layoutId = landscape.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } } }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
compose.onNodeWithTag("input-editor-scale").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(90f) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
compose.runOnIdle {
|
||||
val saved = InputLibraries.read(context).layouts.first { it.id == landscape.id }
|
||||
assertEquals(landscape.aspect, saved.aspect)
|
||||
for (id in face) assertEquals(landscape.elements.first { it.id == id }.let { it.x to it.y }, saved.elements.first { it.id == id }.let { it.x to it.y })
|
||||
assertTrue(saved.rotated.isNotEmpty() && saved.rotatedAspect < 1f)
|
||||
assertEquals(landscape.scale, saved.scale)
|
||||
assertEquals(.9f, saved.rotatedScale)
|
||||
assertEquals(.9f, OnScreenLayout.oriented(saved, 900, 1600).scale)
|
||||
assertEquals(landscape.scale, OnScreenLayout.oriented(saved, 1600, 900).scale)
|
||||
}
|
||||
compose.runOnIdle { round.value = 1 }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
val slider = compose.onNodeWithTag("input-editor-scale").performScrollTo()
|
||||
assertEquals(90f, slider.fetchSemanticsNode().config[androidx.compose.ui.semantics.SemanticsProperties.ProgressBarRangeInfo].current, .5f)
|
||||
slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(80f) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
compose.runOnIdle {
|
||||
val saved = InputLibraries.read(context).layouts.first { it.id == landscape.id }
|
||||
assertEquals(landscape.scale, saved.scale)
|
||||
assertEquals(.8f, saved.rotatedScale)
|
||||
}
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w833dp-h468dp-land-369dpi")
|
||||
fun buttonSizeKeepsMovedGamepadGroupsInShape() {
|
||||
val face = setOf("legacy:a", "legacy:b", "legacy:x", "legacy:y")
|
||||
|
||||
@@ -1,15 +1,29 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.view.InputDevice
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.droiddeck.launcher.input.InputControl
|
||||
import com.droiddeck.launcher.input.InputLibraries
|
||||
import com.droiddeck.launcher.input.InputSurfaceElement
|
||||
import com.droiddeck.launcher.input.OnScreenLayout
|
||||
import com.droiddeck.launcher.input.PadKit
|
||||
import com.droiddeck.launcher.input.SavedInputLayout
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import com.droiddeck.launcher.session.SessionService
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@@ -18,8 +32,17 @@ class InputSurfaceEditorViewTest {
|
||||
private val context get() = RuntimeEnvironment.getApplication()
|
||||
private val layout = SavedInputLayout("custom", "Custom", listOf(InputSurfaceElement(InputControl.A, .1f, .2f, .2f, .2f)), aspect = 2f)
|
||||
|
||||
@Before fun reset() {
|
||||
for (name in listOf("input-profiles", "input-surfaces", "controller", "session")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit()
|
||||
}
|
||||
|
||||
@After fun restoreSession() {
|
||||
SessionState.deckPad = false
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
}
|
||||
|
||||
private fun view() = InputSurfaceEditorView(context).apply {
|
||||
layout(0, 0, 1000, 1000)
|
||||
place(1000, 1000)
|
||||
sourceLayout = layout
|
||||
aspect = layout.aspect
|
||||
elements = layout.elements
|
||||
@@ -27,13 +50,47 @@ class InputSurfaceEditorViewTest {
|
||||
}
|
||||
|
||||
private fun center() = OnScreenLayout.editorElements(context, layout, 1000, 1000).first().let { (it.x + it.width / 2) * 1000 to (it.y + it.height / 2) * 1000 }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun InputSurfaceEditorView.rendered() = javaClass.getDeclaredField("visibleElements").also { it.isAccessible = true }.get(this) as List<InputSurfaceElement>
|
||||
private fun overlay() = InputLibraries.read(context).layouts.first { it.id == InputLibraries.OVERLAY }
|
||||
|
||||
private fun editor(layout: SavedInputLayout, w: Int, h: Int) = InputSurfaceEditorView(context).apply {
|
||||
sourceLayout = layout
|
||||
aspect = layout.aspect
|
||||
elements = layout.elements
|
||||
snap = false
|
||||
place(w, h)
|
||||
}
|
||||
|
||||
private fun field(target: Any, name: String) = target.javaClass.getDeclaredField(name).also { it.isAccessible = true }.get(target)
|
||||
private fun InputSurfaceEditorView.controls() = (field(field(this, "preview")!!, "customCanvas") as ViewGroup).let { canvas -> (0 until canvas.childCount).map(canvas::getChildAt) }
|
||||
private fun InputSurfaceEditorView.artworkDpad() = field(field(this, "preview")!!, "artworkDpad") as List<*>
|
||||
private fun InputSurfaceEditorView.rects() = controls().map { listOf(it.left, it.top, it.width, it.height) }
|
||||
|
||||
private fun InputSurfaceEditorView.assertControlsDrawnWhereDisplayed() {
|
||||
val shown = rendered()
|
||||
val views = controls()
|
||||
assertEquals(shown.size, views.size)
|
||||
for ((e, v) in shown.zip(views)) {
|
||||
assertEquals(e.id, e.x * width, v.left.toFloat(), 1f)
|
||||
assertEquals(e.id, e.y * height, v.top.toFloat(), 1f)
|
||||
assertEquals(e.id, e.width * width, v.width.toFloat(), 1f)
|
||||
assertEquals(e.id, e.height * height, v.height.toFloat(), 1f)
|
||||
}
|
||||
}
|
||||
|
||||
private fun InputSurfaceEditorView.touch(action: Int, x: Float, y: Float) {
|
||||
MotionEvent.obtain(0, 10, action, x, y, 0).also { onTouchEvent(it); it.recycle() }
|
||||
}
|
||||
|
||||
private fun InputSurfaceEditorView.mouse(action: Int, x: Float, y: Float) {
|
||||
val properties = MotionEvent.PointerProperties().apply { id = 0; toolType = MotionEvent.TOOL_TYPE_MOUSE }
|
||||
val coords = MotionEvent.PointerCoords().apply { this.x = x; this.y = y; pressure = 1f }
|
||||
val buttons = if (action == MotionEvent.ACTION_UP) 0 else MotionEvent.BUTTON_PRIMARY
|
||||
MotionEvent.obtain(0, 10, action, 1, arrayOf(properties), arrayOf(coords), 0, buttons, 1f, 1f, 0, 0, InputDevice.SOURCE_MOUSE, 0)
|
||||
.also { dispatchTouchEvent(it); it.recycle() }
|
||||
}
|
||||
|
||||
private fun InputSurfaceEditorView.centre(id: String) = rendered().first { it.id == id }.let { (it.x + it.width / 2) * width to (it.y + it.height / 2) * height }
|
||||
|
||||
@Test fun editingStartsInTheDisplayedFrame() {
|
||||
val view = view()
|
||||
var started: Pair<List<InputSurfaceElement>, Float>? = null
|
||||
@@ -70,7 +127,112 @@ class InputSurfaceEditorViewTest {
|
||||
view.elements = renamed
|
||||
view.aspect = 2f
|
||||
assertEquals(OnScreenLayout.editorElements(context, layout.copy(elements = renamed, aspect = 2f), 1000, 1000), view.rendered())
|
||||
view.layout(0, 0, 800, 1000)
|
||||
view.place(800, 1000)
|
||||
assertEquals(OnScreenLayout.editorElements(context, layout.copy(elements = renamed, aspect = 2f), 800, 1000), view.rendered())
|
||||
}
|
||||
|
||||
@Test fun theOverlayEditorDrawsTheKitArtworkWhereTheSessionPlacesIt() {
|
||||
val layout = overlay()
|
||||
assertNotNull(PadKit.forOverlay(context, layout))
|
||||
val view = editor(layout, 2400, 1504)
|
||||
assertEquals(OnScreenLayout.editorElements(context, layout, 2400, 1504), view.rendered())
|
||||
view.assertControlsDrawnWhereDisplayed()
|
||||
assertEquals(4, view.artworkDpad().size)
|
||||
}
|
||||
|
||||
@Config(qualifiers = "xhdpi")
|
||||
@Test fun controlsAddedForTheScreenAreDrawnOnTheFirstFrameInBothOrientations() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
SessionState.deckPad = true
|
||||
SessionPrefs.setOscRearButtons(context, true)
|
||||
SessionPrefs.setOscTrackpads(context, true)
|
||||
val layout = overlay()
|
||||
for ((w, h) in listOf(2400 to 1504, 1504 to 2400)) {
|
||||
val view = editor(layout, w, h)
|
||||
assertTrue(view.rendered().size > layout.elements.size)
|
||||
view.assertControlsDrawnWhereDisplayed()
|
||||
assertEquals(4, view.artworkDpad().size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun displayedControlsStayPutAcrossRepeatedRefreshesAndResizes() {
|
||||
val view = editor(overlay(), 2400, 1504)
|
||||
val first = view.rendered()
|
||||
val drawn = view.rects()
|
||||
repeat(3) {
|
||||
view.elements = view.elements
|
||||
view.aspect = view.aspect
|
||||
view.sourceLayout = view.sourceLayout
|
||||
view.place(1504, 2400)
|
||||
view.assertControlsDrawnWhereDisplayed()
|
||||
view.place(2400, 1504)
|
||||
assertEquals(first, view.rendered())
|
||||
assertEquals(drawn, view.rects())
|
||||
assertEquals(4, view.artworkDpad().size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun dpadPiecesRedrawWhenTheyLeaveOrRejoinTheKitCross() {
|
||||
val view = editor(overlay(), 2400, 1504)
|
||||
val stock = view.rendered()
|
||||
view.aspect = 2400f / 1504
|
||||
val pieces = view.controls().filter { (it.tag as InputSurfaceElement).id in setOf("legacy:up", "legacy:right", "legacy:down", "legacy:left") }
|
||||
assertEquals(4, pieces.size)
|
||||
for ((moved, expected) in listOf(stock.map { if (it.id == "legacy:up") it.copy(y = it.y - .2f) else it } to 0, stock to 4)) {
|
||||
pieces.forEach { shadowOf(it).clearWasInvalidated() }
|
||||
view.elements = moved
|
||||
view.place(2400, 1504)
|
||||
assertEquals(expected, view.artworkDpad().size)
|
||||
pieces.forEach { assertTrue((it.tag as InputSurfaceElement).id, shadowOf(it).wasInvalidated()) }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun draggingMovesTheTouchedControlByTheDragDistance() {
|
||||
val view = editor(overlay(), 2400, 1504)
|
||||
val a = view.rendered().first { it.id == "legacy:a" }
|
||||
val changes = mutableListOf<InputSurfaceElement>()
|
||||
view.onChange = { changes += it }
|
||||
val (x, y) = view.centre(a.id)
|
||||
view.touch(MotionEvent.ACTION_DOWN, x, y)
|
||||
view.touch(MotionEvent.ACTION_MOVE, x - 240f, y - 150f)
|
||||
view.touch(MotionEvent.ACTION_UP, x - 240f, y - 150f)
|
||||
val moved = changes.last()
|
||||
assertEquals(a.id, moved.id)
|
||||
assertEquals(a.x - 240f / 2400, moved.x, .0001f)
|
||||
assertEquals(a.y - 150f / 1504, moved.y, .0001f)
|
||||
assertEquals(a.width, moved.width, .0001f)
|
||||
}
|
||||
|
||||
@Test fun aMouseSelectsDragsAndResizesLikeATouch() {
|
||||
val view = editor(overlay(), 2400, 1504)
|
||||
val a = view.rendered().first { it.id == "legacy:a" }
|
||||
val changes = mutableListOf<InputSurfaceElement>()
|
||||
var selection: String? = null
|
||||
view.onSelect = { selection = it }
|
||||
view.onChange = { changes += it }
|
||||
val (x, y) = view.centre(a.id)
|
||||
view.mouse(MotionEvent.ACTION_DOWN, x, y)
|
||||
view.mouse(MotionEvent.ACTION_UP, x, y)
|
||||
assertEquals(a.id, selection)
|
||||
view.mouse(MotionEvent.ACTION_DOWN, x, y)
|
||||
view.mouse(MotionEvent.ACTION_MOVE, x - 240f, y)
|
||||
view.mouse(MotionEvent.ACTION_UP, x - 240f, y)
|
||||
assertEquals(a.x - 240f / 2400, changes.last().x, .0001f)
|
||||
val right = (a.x + a.width) * 2400; val bottom = (a.y + a.height) * 1504
|
||||
view.mouse(MotionEvent.ACTION_DOWN, right, bottom)
|
||||
view.mouse(MotionEvent.ACTION_MOVE, right + 60f, bottom + 60f)
|
||||
view.mouse(MotionEvent.ACTION_UP, right + 60f, bottom + 60f)
|
||||
val resized = changes.last()
|
||||
assertEquals(a.id, resized.id)
|
||||
assertEquals(a.width + 60f / 2400, resized.width, .0001f)
|
||||
assertEquals(a.height + 60f / 1504, resized.height, .0001f)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun InputSurfaceEditorView.place(w: Int, h: Int) {
|
||||
measure(View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY))
|
||||
layout(0, 0, w, h)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
internal fun InputSurfaceEditorView.rendered() = javaClass.getDeclaredField("visibleElements").also { it.isAccessible = true }.get(this) as List<InputSurfaceElement>
|
||||
@@ -85,10 +85,12 @@ class InputSurfaceTouchTest {
|
||||
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
surface.updateStyle(InputSurfaceStyle(), steamDeckButtons = true)
|
||||
assertEquals(shown, surface.shownElements)
|
||||
assertTrue(surface.takesTouchAt(800f, 240f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
for (artwork in PadKit.entries) {
|
||||
surface.updateStyle(InputSurfaceStyle(), artwork)
|
||||
assertEquals(shown, surface.shownElements)
|
||||
assertTrue(surface.takesTouchAt(800f, 240f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
}
|
||||
|
||||
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f))
|
||||
assertFalse(state().isDown(PadState.A))
|
||||
|
||||
Reference in new issue
Block a user