diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 7df65cd605..42b2804f7e 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -159,6 +159,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var pipAutoEnter by mutableStateOf(false) private var unfoldedControls by mutableStateOf(false) private var unfoldedControlsWithController by mutableStateOf(false) + private var adaptiveSticks by mutableStateOf(null) private var picturePosition by mutableStateOf("center") private fun applyGameAlignment() { @@ -172,7 +173,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } WaylandCompositor.nativeSetScaleMode(SCALE_FIT, alignment) } - private var unfoldedPanel: com.droiddeck.launcher.ui.InputSurfaceView? = null + private var unfoldedPanel by mutableStateOf(null) private lateinit var sessionRoot: FrameLayout private var inputRouter: InputRouter? = null private var physicalInputBindings: com.droiddeck.launcher.input.PhysicalInputBindings? = null @@ -196,12 +197,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun scroll(steps: Int) { sendSceneWheel(steps) } override fun scrollHorizontal(steps: Int) { sendSceneWheel(steps, horizontal = true) } override fun requestMotion() { inputMotion.request() } - override fun command(command: InputCommand) { + override fun command(command: InputCommand) = command(command, InputRole.PHYSICAL) + override fun command(command: InputCommand, role: InputRole) { uiHandler.post { if (!resumed || isDestroyed || pipUi || SessionState.suspended) return@post when (command) { InputCommand.MENU -> { releasePads(); drawerOpen = true } - InputCommand.KEYBOARD -> if (secondScreenPresentation?.showKeyboard() != true) togglePcKeyboard() + InputCommand.KEYBOARD -> if (role == InputRole.PRIMARY || secondScreenPresentation?.showKeyboard() != true) togglePcKeyboard() InputCommand.STEAM -> tapSteamButton() InputCommand.QUICK_ACCESS -> tapQam() } @@ -244,6 +246,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var secondaryInputLayout by mutableStateOf(null) private var steamTouchControls: com.droiddeck.launcher.input.SteamTouchControls? = null private var steamTouchAvailable by mutableStateOf(false) + private var steamTouchInUse by mutableStateOf(false) private val steamTouchRetry = Runnable { updateOnScreenControls() } private var steamTouchOptions: com.droiddeck.launcher.input.SteamTouchConfig.Options? = null private var routedTouchMode = SessionPrefs.TOUCH_AUTO @@ -1404,6 +1407,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { }, unfoldedControls = unfoldedControls, onUnfoldedControls = ::chooseUnfoldedControls, + adaptiveSticks = adaptiveSticks.takeIf { + unfoldedPanel != null || oscMode != SessionPrefs.OSC_NEVER && oscMode != SessionPrefs.OSC_STEAM_QAM && !steamTouchInUse + }, + onAdaptiveSticks = { on -> + primaryInputLayout?.let { com.droiddeck.launcher.input.InputLibraries.setAdaptiveSticks(this@SessionActivity, it.id, on) } + refreshInputProfile() + }, unfoldedControlsWithController = unfoldedControlsWithController, onUnfoldedControlsWithController = ::chooseUnfoldedControlsWithController, onBackground = { drawerOpen = false; pip.background(); moveTaskToBack(true) }, @@ -3498,6 +3508,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val desktop = loadingMode() == SessionService.MODE_DESKTOP inputProfile = library.compileActive(desktop) primaryInputLayout = library.activeLayout(desktop, InputRole.PRIMARY) + adaptiveSticks = primaryInputLayout?.adaptiveSticks secondaryInputLayout = library.activeLayout(desktop, InputRole.SECONDARY) inputUsesLocalActions = InputControl.padControls.any { inputProfile.action(InputRole.PHYSICAL, it) !is InputAction.Native } inputUsesPointer = inputProfile.bindings.values.any { bindings -> @@ -4212,6 +4223,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val buttonsOnly = forced == SessionPrefs.OSC_STEAM_QAM val wantsSteam = forced == SessionPrefs.OSC_STEAM_TOUCH && SessionState.mode == SessionService.MODE_STEAM val steam = if (wantsSteam) steamTouch() else null + steamTouchInUse = steam != null if (wantsSteam && steam == null && !isFinishing && !isDestroyed) { uiHandler.removeCallbacks(steamTouchRetry) uiHandler.postDelayed(steamTouchRetry, 1000) diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt index 3445375794..44305456ae 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -9,7 +9,7 @@ import org.json.JSONObject import java.util.UUID object InputLibraries { - private const val VERSION = 9 + private const val VERSION = 10 private const val KEY = "library" private const val RECOVERY = "library-recovery." const val OVERLAY = "builtin.overlay" @@ -28,6 +28,7 @@ object InputLibraries { val version = raw?.let { JSONObject(it).getInt("version") } ?: 0 if (version < 5) library = scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, library))) if (version < 9) library = neutralStockLayouts(library) + if (version < 10) library = restyleGamepads(context, library) if (version < 6 || !p.contains(LegacyScreenKeyMigration.BACKUP)) { val legacy = LegacyScreenKeyMigration.snapshot(context) if (legacy != null) { @@ -105,12 +106,39 @@ object InputLibraries { return InputLibrary(profiles, layouts, bindings, selected).validate() } + fun setAdaptiveSticks(context: Context, layoutId: String, on: Boolean): Boolean = runCatching { + update(context) { library -> + library.copy(layouts = library.layouts.map { layout -> + if (layout.id != layoutId || !layout.editable) layout + else layout.copy(elements = layout.elements.map { if (it.kind == InputElementKind.STICK) it.copy(floating = on, hideWhenIdle = on) else it }) + }) + } + }.onSuccess { ControllerPrefs.setAdaptiveSticks(context, on) }.isSuccess + @Suppress("DEPRECATION") - private fun upgradeOverlay(context: Context, library: InputLibrary): InputLibrary { - if (library.layouts.none { it.template == InputLayoutTemplate.OVERLAY }) return library + private fun primaryDisplay(context: Context): Pair { val display = context.getSystemService(DisplayManager::class.java)?.getDisplay(Display.DEFAULT_DISPLAY) val surfaceContext = display?.let(context::createDisplayContext) ?: context - val metrics = DisplayMetrics().also { if (display != null) display.getRealMetrics(it) else it.setTo(context.resources.displayMetrics) } + return surfaceContext to DisplayMetrics().also { if (display != null) display.getRealMetrics(it) else it.setTo(context.resources.displayMetrics) } + } + + private fun restyleGamepads(context: Context, library: InputLibrary): InputLibrary { + if (library.layouts.none { it.id == OVERLAY || it.id.startsWith("migrated.layout.") }) return library + val (surfaceContext, metrics) = primaryDisplay(context) + return library.copy(layouts = library.layouts.map { layout -> + val size = when { + layout.id == OVERLAY -> listOf(metrics.widthPixels, metrics.heightPixels) + layout.id.startsWith("migrated.layout.") -> layout.id.removePrefix("migrated.layout.").split('x').mapNotNull { it.toIntOrNull() } + else -> emptyList() + } + if (size.size != 2 || size.any { it <= 0 }) layout + else layout.copy(elements = LegacyGamepadLayout(surfaceContext, size[0], size[1]).restyle(layout.elements)) + }) + } + + private fun upgradeOverlay(context: Context, library: InputLibrary): InputLibrary { + if (library.layouts.none { it.template == InputLayoutTemplate.OVERLAY }) return library + val (surfaceContext, metrics) = primaryDisplay(context) val settings = ControllerPrefs.read(context) val elements = LegacyGamepadLayout(surfaceContext, metrics.widthPixels.coerceAtLeast(1), metrics.heightPixels.coerceAtLeast(1)).elements() val style = InputSurfaceStyle(settings.tint, settings.opacity) diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt index ea11d22a0d..49ab822b24 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -18,6 +18,7 @@ data class SavedInputLayout( val screen: InputLayoutScreen = InputLayoutScreen.BOTH, ) { val editable get() = template == InputLayoutTemplate.CUSTOM + val adaptiveSticks get() = elements.filter { it.kind == InputElementKind.STICK }.takeIf { editable && it.isNotEmpty() }?.all { it.floating && it.hideWhenIdle } fun supports(role: InputRole) = screen.supports(role) && when (template) { InputLayoutTemplate.OVERLAY -> role == InputRole.PRIMARY InputLayoutTemplate.DECK, InputLayoutTemplate.TRACKPAD -> role == InputRole.SECONDARY diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt index 24b45ed1ef..5d67413141 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt @@ -9,6 +9,7 @@ interface InputOutput { fun scroll(steps: Int) fun scrollHorizontal(steps: Int) {} fun command(command: InputCommand) + fun command(command: InputCommand, role: InputRole) = command(command) fun requestMotion() } @@ -233,7 +234,7 @@ class InputRouter(private val output: InputOutput) { when (a) { is InputAction.Keys -> if (down) a.codes.forEach { key(it, true) } else a.codes.asReversed().forEach { key(it, false) } is InputAction.MouseButton -> button(a.code, down) - is InputAction.Command -> if (down) output.command(a.command) + is InputAction.Command -> if (down) output.command(a.command, role) else -> Unit } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt index 4525fa9d9c..11092cfbee 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt @@ -17,10 +17,10 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid val halfH get() = if (id in shoulderIds) radius * SHOULDER_HEIGHT else radius } private val controls = listOf( - Control("up", "dpad", -1, 26f, 0f, -52f), - Control("right", "dpad", -1, 26f, 52f, 0f), - Control("down", "dpad", -1, 26f, 0f, 52f), - Control("left", "dpad", -1, 26f, -52f, 0f), + Control("up", "dpad", -1, 30f, 0f, -50f), + Control("right", "dpad", -1, 30f, 50f, 0f), + Control("down", "dpad", -1, 30f, 0f, 50f), + Control("left", "dpad", -1, 30f, -50f, 0f), Control("ls", "ls", 0, 48f, 0f, 0f), Control("rs", "rs", 1, 48f, 0f, 0f), Control("a", "face", -1, 30f, 0f, 50f), @@ -188,12 +188,69 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid settings.mapping[it.id] != ControllerPrefs.OFF && (it.id != "guide" || settings.steamButton) && (it.id != "qam" || settings.qamButton) }.map { c -> InputSurfaceElement(requireNotNull(InputControl.fromId(c.id)), (c.cx - c.halfW) / width, (c.cy - c.halfH) / height, - 2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, hideWhenIdle = c.stick >= 0 && settings.adaptiveSticks).bounded() + 2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, hideWhenIdle = c.stick >= 0 && settings.adaptiveSticks, + buttonShape = if (c.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE).let(::widenStick).bounded() }.toMutableList() - if (settings.keyboardButton) result += InputSurfaceElement(InputControl.A, .02f, .9f, .12f, .08f, - "builtin:keyboard", name = "Keyboard") + if (settings.keyboardButton) result += keyboard() return result } + + private fun keyboard(): InputSurfaceElement { + val radius = dp(KEYBOARD_RADIUS_DP) + val cx = safe.left + dp(KEYBOARD_X_DP) + val cy = height - safe.bottom - dp(KEYBOARD_Y_DP) + return InputSurfaceElement(InputControl.A, (cx - radius) / width, (cy - radius) / height, 2 * radius / width, 2 * radius / height, + KEYBOARD_ID, name = "Keyboard", icon = InputButtonIcons.KEYBOARD, buttonShape = InputButtonShape.CIRCLE).bounded() + } + + fun restyle(elements: List): List { + val dpad = elements.filter { it.id == "legacy:${it.control.id}" && it.control in DPAD && it.buttonShape == InputButtonShape.ROUNDED } + val centreX = dpad.map { (it.x + it.width / 2f) * width }.average().toFloat() + val centreY = dpad.map { (it.y + it.height / 2f) * height }.average().toFloat() + val resize = untouchedDpad(dpad, centreX, centreY) + return elements.map { e -> + when { + e.id == KEYBOARD_ID && e.icon == 0 && e.name == "Keyboard" && e.buttonShape == InputButtonShape.ROUNDED && + e.x == .02f && e.y == .9f && e.width == .12f && e.height == .08f -> keyboard() + e.id == "legacy:${e.control.id}" && e.buttonShape == InputButtonShape.ROUNDED -> { + val round = e.copy(buttonShape = if (e.control.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE) + (if (resize && e.control in DPAD) resizeDpad(round, centreX, centreY) else widenStick(round)).bounded() + } + else -> e + } + } + } + + private fun untouchedDpad(dpad: List, centreX: Float, centreY: Float): Boolean { + if (dpad.size != DPAD.size) return false + val radius = dpad[0].width * width / 2f + val tolerance = 1.5f + val offset = radius * LEGACY_DPAD_OFFSET_DP / LEGACY_DPAD_RADIUS_DP + return dpad.all { e -> + val old = controls.first { it.id == e.control.id } + val dx = (e.x + e.width / 2f) * width - centreX + val dy = (e.y + e.height / 2f) * height - centreY + abs(e.width * width / 2f - radius) <= tolerance && abs(e.height * height / 2f - radius) <= tolerance && + abs(dx - offset * sign(old.offsetXDp)) <= tolerance && abs(dy - offset * sign(old.offsetYDp)) <= tolerance + } + } + + private fun resizeDpad(e: InputSurfaceElement, centreX: Float, centreY: Float): InputSurfaceElement { + val old = controls.first { it.id == e.control.id } + val radius = e.width * width / 2f * old.radiusDp / LEGACY_DPAD_RADIUS_DP + val cx = centreX + ((e.x + e.width / 2f) * width - centreX) * abs(old.offsetXDp + old.offsetYDp) / LEGACY_DPAD_OFFSET_DP + val cy = centreY + ((e.y + e.height / 2f) * height - centreY) * abs(old.offsetXDp + old.offsetYDp) / LEGACY_DPAD_OFFSET_DP + return e.copy(x = (cx - radius) / width, y = (cy - radius) / height, width = 2 * radius / width, height = 2 * radius / height) + } + + private fun widenStick(e: InputSurfaceElement): InputSurfaceElement { + val extra = e.width * (STICK_REACH - 1f) + return when (e.control) { + InputControl.LS -> (e.x - extra).coerceAtLeast(0f).let { x -> e.copy(x = x, width = e.x + e.width - x) } + InputControl.RS -> e.copy(width = (e.width + extra).coerceAtMost(1f - e.x)) + else -> e + } + } private companion object { const val MIN_FACE_MM = 9.5f const val FULL_SIZE_HEIGHT_MM = 85f @@ -203,5 +260,13 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid const val SHOULDER_WIDTH = 1.45f const val SHOULDER_HEIGHT = 0.8f val shoulderIds = setOf("lb", "rb", "lt", "rt") + const val STICK_REACH = 1.25f + const val LEGACY_DPAD_RADIUS_DP = 26f + const val LEGACY_DPAD_OFFSET_DP = 52f + val DPAD = setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) + const val KEYBOARD_ID = "builtin:keyboard" + const val KEYBOARD_RADIUS_DP = 18f + const val KEYBOARD_X_DP = 90f + const val KEYBOARD_Y_DP = 34f } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt index 053bc695ed..de05e0b027 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt @@ -1,20 +1,36 @@ package com.droiddeck.launcher.input +import android.annotation.SuppressLint import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Typeface +import android.os.Build +import android.view.Gravity +import android.view.HapticFeedbackConstants import android.view.MotionEvent +import android.view.View +import android.view.WindowInsets import android.widget.FrameLayout import com.droiddeck.launcher.ui.InputSurfaceView class OnScreenControls(context: Context, pad: PadBridge?) : FrameLayout(context) { private val surface = InputSurfaceView(context, pad, inputRole = InputRole.PRIMARY) private var layout: SavedInputLayout? = null + private val quickToggle = QuickToggle(context) var isButtonsOnly = false private set + var isQuickHidden = false + private set var onTouchMiss: ((MotionEvent) -> Unit)? get() = surface.onTouchMiss set(value) { surface.onTouchMiss = value } - init { addView(surface, LayoutParams(-1, -1)) } + init { + addView(surface, LayoutParams(-1, -1)) + val size = (QUICK_HIT_DP * 2 * resources.displayMetrics.density).toInt() + addView(quickToggle, LayoutParams(size, size, Gravity.START or Gravity.BOTTOM)) + } fun updateLayout(value: SavedInputLayout?) { if (layout == value) return @@ -27,18 +43,98 @@ class OnScreenControls(context: Context, pad: PadBridge?) : FrameLayout(context) if (isButtonsOnly == enabled) return releaseAll() isButtonsOnly = enabled + isQuickHidden = false + render() + } + + fun setQuickHidden(hidden: Boolean) { + if (isButtonsOnly || isQuickHidden == hidden) return + releaseAll() + isQuickHidden = hidden render() } private fun render() { val elements = layout?.elements.orEmpty() - surface.updateCustomLayout(if (isButtonsOnly) elements.filter { it.control == InputControl.GUIDE || it.control == InputControl.QAM || it.id == "builtin:keyboard" } else elements) - surface.updateStyle(layout?.style ?: InputSurfaceStyle()) + surface.updateCustomLayout(when { + isButtonsOnly -> elements.filter { it.control == InputControl.GUIDE || it.control == InputControl.QAM || it.id == "builtin:keyboard" } + isQuickHidden -> elements.filter { it.id == "builtin:keyboard" } + else -> elements + }) + val style = layout?.style ?: InputSurfaceStyle() + surface.updateStyle(style) + quickToggle.tint = style.tint + quickToggle.alpha = style.opacity / 100f + quickToggle.visibility = if (isButtonsOnly || layout == null) GONE else VISIBLE + quickToggle.invalidate() } - fun takesTouchAt(x: Float, y: Float) = surface.takesTouchAt(x, y) + fun takesTouchAt(x: Float, y: Float) = surface.takesTouchAt(x, y) || + quickToggle.visibility == VISIBLE && x >= quickToggle.left && x < quickToggle.right && y >= quickToggle.top && y < quickToggle.bottom fun releaseAll() = surface.releaseInputs() fun reload() { releaseAll(); render() } override fun onDetachedFromWindow() { releaseAll(); super.onDetachedFromWindow() } + + override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { + val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null + val offset = ((QUICK_CENTER_DP - QUICK_HIT_DP) * resources.displayMetrics.density).toInt() + val params = quickToggle.layoutParams as LayoutParams + val left = (cutout?.safeInsetLeft ?: 0) + offset + val bottom = (cutout?.safeInsetBottom ?: 0) + offset + if (params.leftMargin != left || params.bottomMargin != bottom) { + params.leftMargin = left + params.bottomMargin = bottom + quickToggle.layoutParams = params + } + return super.onApplyWindowInsets(insets) + } + + private inner class QuickToggle(context: Context) : View(context) { + var tint: Int? = ControllerPrefs.STEAM_BLUE + private val density = resources.displayMetrics.density + private var armed = false + private val fill = Paint(Paint.ANTI_ALIAS_FLAG) + private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; strokeWidth = 1.5f * density } + private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply { + textAlign = Paint.Align.CENTER + typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + textSize = 22 * density + } + + override fun onDraw(canvas: Canvas) { + val x = width / 2f + val y = height / 2f + val radius = QUICK_RADIUS_DP * density + val color = tint + fill.color = if (color != null) InputSurfaceView.shade(color, 190, 0.12f) else (InputSurfaceView.SURFACE and 0x00ffffff) or (190 shl 24) + canvas.drawCircle(x, y, radius, fill) + stroke.color = if (color != null) InputSurfaceView.light(color, 150, 0.25f) else InputSurfaceView.LINE + canvas.drawCircle(x, y, radius, stroke) + text.color = if (color != null) InputSurfaceView.light(color, 210, 0.75f) else InputSurfaceView.TEXT + canvas.drawText(if (isQuickHidden) "+" else "−", x, y + text.textSize * 0.35f, text) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> armed = true + MotionEvent.ACTION_CANCEL -> armed = false + MotionEvent.ACTION_UP -> { + if (armed && event.x in 0f..width.toFloat() && event.y in 0f..height.toFloat()) { + performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + setQuickHidden(!isQuickHidden) + } + armed = false + } + } + return true + } + } + + private companion object { + const val QUICK_CENTER_DP = 34f + const val QUICK_HIT_DP = 26f + const val QUICK_RADIUS_DP = 18f + } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt index c6ff3fc901..a5ab702733 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt @@ -8,6 +8,7 @@ import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint +import android.graphics.Path import android.graphics.RadialGradient import android.graphics.RectF import android.graphics.Shader @@ -128,10 +129,11 @@ class InputSurfaceView( } } } + canvas.clipChildren = customLayout.none { it.kind == InputElementKind.STICK && classic(it) } for (element in customLayout) { val source = pad?.let { InputSurfaceSource(it, inputRole, element).also { s -> elementSources += s } } canvas.addView(when (element.kind) { - InputElementKind.STICK -> StickView(context, source, element.floating, element.hideWhenIdle && pad != null) + InputElementKind.STICK -> StickView(context, element, source, element.floating, element.hideWhenIdle && pad != null) InputElementKind.TRACKPAD -> TrackpadView(context, element.control == InputControl.RIGHT_PAD, source = source) InputElementKind.DPAD -> DirectionPad(context, source) else -> HoldButton(context, element, source) @@ -183,16 +185,50 @@ class InputSurfaceView( color = TEXT_DIM } - protected fun drawSurface(canvas: Canvas, lit: Boolean, shape: InputButtonShape = InputButtonShape.ROUNDED) { - val inset = density + protected fun drawSurface(canvas: Canvas, lit: Boolean, shape: InputButtonShape = InputButtonShape.ROUNDED, classic: Boolean = false) { + val inset = if (classic && tint != null) 0.75f * density else density rect.set(inset, inset, width - inset, height - inset) val radius = when (shape) { InputButtonShape.CIRCLE, InputButtonShape.PILL -> min(width, height) / 2f; InputButtonShape.RECTANGLE -> 2 * density; else -> CORNER * density } + val color = tint + if (classic && color != null) { + val corner = if (shape == InputButtonShape.PILL) radius * 0.55f else radius + fill.color = if (lit) shade(color, 160, 1f) else shade(color, 80, 0.12f) + canvas.drawRoundRect(rect, corner, corner, fill) + stroke.color = if (lit) light(color, 230, 0.7f) else light(color, 150, 0.25f) + stroke.strokeWidth = 1.5f * density + canvas.drawRoundRect(rect, corner, corner, stroke) + return + } fill.color = tint?.let { color -> (color and 0x00ffffff) or ((if (lit) 140 else 48) shl 24) } ?: if (lit) ACCENT_WASH else SURFACE canvas.drawRoundRect(rect, radius, radius, fill) stroke.color = tint?.let { color -> (color and 0x00ffffff) or ((if (lit) 255 else 160) shl 24) } ?: if (lit) ACCENT else LINE stroke.strokeWidth = (if (lit) 2f else 1f) * density canvas.drawRoundRect(rect, radius, radius, stroke) } + + protected fun drawCircle(canvas: Canvas, x: Float, y: Float, radius: Float, fillColor: Int, strokeColor: Int) { + fill.color = fillColor + canvas.drawCircle(x, y, radius, fill) + stroke.color = strokeColor + stroke.strokeWidth = 1.5f * density + canvas.drawCircle(x, y, radius, stroke) + } + + protected fun drawKeyboard(canvas: Canvas, x: Float, y: Float, lit: Boolean) { + val color = tint ?: return + stroke.color = if (lit) light(color, 230, 0.7f) else light(color, 150, 0.25f) + stroke.strokeWidth = 1.5f * density + rect.set(x - 10 * density, y - 7 * density, x + 10 * density, y + 7 * density) + canvas.drawRoundRect(rect, 2 * density, 2 * density, stroke) + for (row in 0..1) for (col in 0..2) { + val kx = x + (-6f + col * 5f) * density + val ky = y + (-3f + row * 4f) * density + canvas.drawLine(kx, ky, kx + 2 * density, ky, stroke) + } + canvas.drawLine(x - 5 * density, y + 4 * density, x + 5 * density, y + 4 * density, stroke) + } + + protected fun textColor(lit: Boolean) = if (lit) Color.WHITE else tint?.let { light(it, 210, 0.75f) } ?: TEXT } private class HoldButton(context: Context, private val element: InputSurfaceElement, private val source: InputSurfaceSource?) : Surface(context) { @@ -212,9 +248,10 @@ class InputSurfaceView( override fun onDraw(canvas: Canvas) { val lit = if (element.toggle) source?.latched == true else down - drawSurface(canvas, lit, element.buttonShape) + val classic = classic(element) + drawSurface(canvas, lit, element.buttonShape, classic) val cx = width / 2f - labelPaint.color = if (lit) ACCENT else TEXT + labelPaint.color = if (classic && tint != null) textColor(lit) else if (lit) ACCENT else TEXT bitmap?.let { val scale = min(width * .84f / it.width, height * .84f / it.height) imageRect.set(cx - it.width * scale / 2, (height - it.height * scale) / 2, @@ -222,8 +259,23 @@ class InputSurfaceView( canvas.drawBitmap(it, null, imageRect, labelPaint) return } + if (classic && element.id == "builtin:keyboard" && element.icon == InputButtonIcons.KEYBOARD) { drawKeyboard(canvas, cx, height / 2f, lit); return } if (element.icon != 0 && InputButtonIcons.draw(canvas, element.icon, cx, height / 2f, min(width, height) * .68f, labelPaint, symbol)) return if (!element.showLabel) return + labelPaint.style = Paint.Style.FILL + if (classic && element.name == element.control.label) { + val radius = min(width, height) / 2f + val direction = DIRECTIONS.indexOf(element.control) + if (direction >= 0) { drawArrow(canvas, cx, height / 2f, radius * 0.34f, direction, labelPaint); return } + val glyph = glyph(element.control) + labelPaint.textSize = when { + element.buttonShape == InputButtonShape.PILL -> height / 2f * 0.8f + glyph.length > 2 -> 11 * density + else -> radius * if (glyph.length == 2) 0.62f else 0.85f + } + canvas.drawText(glyph, cx, height / 2f + labelPaint.textSize * .35f, labelPaint) + return + } labelPaint.textSize = min(height * .3f, 30 * density) val labelWidth = labelPaint.measureText(element.name) if (labelWidth > width * .84f) labelPaint.textSize *= width * .84f / labelWidth @@ -350,7 +402,7 @@ class InputSurfaceView( } } - private inner class StickView(context: Context, private val source: InputSurfaceSource? = null, private val floating: Boolean = true, private val hideWhenIdle: Boolean = false) : Surface(context) { + private inner class StickView(context: Context, private val element: InputSurfaceElement, private val source: InputSurfaceSource? = null, private val floating: Boolean = true, private val hideWhenIdle: Boolean = false) : Surface(context) { private var touching = false private var clicking = false private var lastUp = 0L @@ -368,11 +420,32 @@ class InputSurfaceView( private fun reach() = min(width, height) * 0.30f + private fun centreX() = when { + !classic(element) || width <= height -> width / 2f + element.control == InputControl.LS -> width - height / 2f + else -> height / 2f + } + override fun onDraw(canvas: Canvas) { if (hideWhenIdle && !touching) return - drawSurface(canvas, clicking) - val cx = if (touching) originX else width / 2f + val cx = if (touching) originX else centreX() val cy = if (touching) originY else height / 2f + val color = tint + if (classic(element) && color != null) { + val radius = min(width, height) / 2f - 0.75f * density + drawCircle(canvas, cx, cy, radius, + if (touching) shade(color, 160, 1f) else shade(color, 60, 0.12f), if (touching) light(color, 230, 0.7f) else light(color, 150, 0.25f)) + val knobRadius = radius * 0.46f + drawCircle(canvas, cx + knobX, cy + knobY, knobRadius, + if (touching || clicking) shade(color, 160, 1f) else shade(color, 120, 0.35f), if (touching) light(color, 230, 0.7f) else light(color, 150, 0.25f)) + if (!element.showLabel) return + labelPaint.color = textColor(touching) + labelPaint.textSize = knobRadius * 0.8f + val label = if (element.name == element.control.label) glyph(element.control) else element.name + canvas.drawText(label, cx + knobX, cy + knobY + labelPaint.textSize * 0.35f, labelPaint) + return + } + drawSurface(canvas, clicking) ring.strokeWidth = 2 * density ring.color = if (touching) ACCENT else LINE canvas.drawCircle(cx, cy, reach(), ring) @@ -389,7 +462,7 @@ class InputSurfaceView( downX = event.x downY = event.y travel = 0f - originX = if (floating) event.x else width / 2f + originX = if (floating) event.x else centreX() originY = if (floating) event.y else height / 2f touching = true performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) @@ -508,13 +581,44 @@ class InputSurfaceView( private val ACCENT_WASH = Color.argb(0x2E, 0x1A, 0x9F, 0xFF) private val ACCENT_GLOW = Color.argb(0x66, 0x1A, 0x9F, 0xFF) private val ON_ACCENT = Color.rgb(0x03, 0x11, 0x1F) - private val SURFACE = Color.rgb(0x0A, 0x0B, 0x0D) - private val LINE = Color.rgb(0x26, 0x29, 0x2E) + internal val SURFACE = Color.rgb(0x0A, 0x0B, 0x0D) + internal val LINE = Color.rgb(0x26, 0x29, 0x2E) private val GUIDE = Color.rgb(0x16, 0x18, 0x1B) - private val TEXT = Color.rgb(0xF2, 0xF4, 0xF7) + internal val TEXT = Color.rgb(0xF2, 0xF4, 0xF7) private val TEXT_DIM = Color.rgb(0x7A, 0x80, 0x8A) private val KNOB = Color.rgb(0x2A, 0x2D, 0x33) + private fun classic(element: InputSurfaceElement) = (element.id == "legacy:${element.control.id}" || element.id == "builtin:keyboard") && + (element.buttonShape == InputButtonShape.CIRCLE || element.buttonShape == InputButtonShape.PILL) + + private val DIRECTIONS = listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) + + internal fun shade(tint: Int, alpha: Int, f: Float) = + Color.argb(alpha, (Color.red(tint) * f).toInt(), (Color.green(tint) * f).toInt(), (Color.blue(tint) * f).toInt()) + + internal fun light(tint: Int, alpha: Int, f: Float) = Color.argb(alpha, (Color.red(tint) + (255 - Color.red(tint)) * f).toInt(), + (Color.green(tint) + (255 - Color.green(tint)) * f).toInt(), (Color.blue(tint) + (255 - Color.blue(tint)) * f).toInt()) + + private fun glyph(control: InputControl) = when (control) { + InputControl.LS -> "L" + InputControl.RS -> "R" + InputControl.SELECT -> "⧉" + InputControl.START -> "☰" + InputControl.GUIDE -> "◉" + InputControl.QAM -> "⋯" + else -> control.id.uppercase() + } + + private fun drawArrow(canvas: Canvas, x: Float, y: Float, size: Float, direction: Int, paint: Paint) { + val (fx, fy) = when (direction) { 0 -> 0f to -1f; 1 -> 1f to 0f; 2 -> 0f to 1f; else -> -1f to 0f } + val path = Path() + path.moveTo(x + fx * size, y + fy * size) + path.lineTo(x - fx * size * 0.7f - fy * size, y - fy * size * 0.7f + fx * size) + path.lineTo(x - fx * size * 0.7f + fy * size, y - fy * size * 0.7f - fx * size) + path.close() + canvas.drawPath(path, paint) + } + internal fun dp(context: Context, value: Int): Int = (value * context.resources.displayMetrics.density + 0.5f).toInt() internal fun outline(context: Context, radius: Float, fill: Int) = GradientDrawable().apply { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index ac34b0e92b..53d533e713 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -314,6 +314,8 @@ class DrawerActions( val onPause: () -> Unit = {}, val onPipAutoEnter: (Boolean) -> Unit = {}, val unfoldedControls: Boolean? = null, + val adaptiveSticks: Boolean? = null, + val onAdaptiveSticks: (Boolean) -> Unit = {}, val onUnfoldedControls: (Boolean) -> Unit = {}, val unfoldedControlsWithController: Boolean = false, val onUnfoldedControlsWithController: (Boolean) -> Unit = {}, @@ -636,6 +638,10 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont else listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), if (!a.steam && a.oscMode == SessionPrefs.OSC_STEAM_TOUCH) SessionPrefs.OSC_AUTO else a.oscMode, chipModifier = focus.track(page, "osc"), onPick = a.onOsc) + a.adaptiveSticks?.let { on -> + ToggleRow(host, "adaptive-sticks", stringResource(R.string.ctrl_adaptive), null, on, + chipModifier = focus.track(page, "adaptive-sticks"), onChange = a.onAdaptiveSticks) + } a.onEditSteamTouch?.let { edit -> ActionRow(stringResource(R.string.osc_steam_layout), stringResource(R.string.osc_steam_layout_hint), stringResource(R.string.ctrl_edit), edit) } diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt index 3e60587367..dc63d19dd2 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt @@ -266,12 +266,120 @@ class InputLibraryTest { ControllerPrefs.setTarget(context, "b", ControllerPrefs.OFF) val elements = LegacyGamepadLayout(context, 1200, 800).elements() val stick = elements.first { it.control == InputControl.LS } - assertEquals(.4f, stick.x + stick.width / 2f, .0001f) + assertEquals(.4f, stick.x + stick.width - stick.height * 800f / 1200f / 2f, .0001f) assertEquals(.5f, stick.y + stick.height / 2f, .0001f) assertFalse(elements.any { it.control == InputControl.GUIDE || it.control == InputControl.B }) assertTrue(elements.all { it == it.bounded() }) } + @Test fun gamepadControlsAreRoundWithShoulderPillsAndACompactKeyboard() { + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + val density = context.resources.displayMetrics.density + for (element in elements.filter { it.id.startsWith("legacy:") }) { + val shoulder = element.control in setOf(InputControl.LB, InputControl.RB, InputControl.LT, InputControl.RT) + assertEquals(element.control.id, if (shoulder) InputButtonShape.PILL else InputButtonShape.CIRCLE, element.buttonShape) + } + val keyboard = elements.single { it.id == "builtin:keyboard" } + assertEquals(InputButtonIcons.KEYBOARD, keyboard.icon) + assertEquals(InputButtonShape.CIRCLE, keyboard.buttonShape) + assertEquals(36 * density / 1200f, keyboard.width, .0001f) + assertEquals(90 * density / 1200f, keyboard.x + keyboard.width / 2f, .0001f) + assertEquals(1f - 34 * density / 800f, keyboard.y + keyboard.height / 2f, .0001f) + } + + @Test fun dpadButtonsMatchTheFaceButtonsInSizeAndSpacing() { + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + fun centre(c: InputControl) = elements.single { it.id == "legacy:${c.id}" }.let { (it.x + it.width / 2f) * 1200 to (it.y + it.height / 2f) * 800 } + fun size(c: InputControl) = elements.single { it.id == "legacy:${c.id}" }.width * 1200 + assertEquals(size(InputControl.A), size(InputControl.UP), .01f) + assertEquals(centre(InputControl.A).second - centre(InputControl.Y).second, centre(InputControl.DOWN).second - centre(InputControl.UP).second, .01f) + assertEquals(centre(InputControl.B).first - centre(InputControl.X).first, centre(InputControl.RIGHT).first - centre(InputControl.LEFT).first, .01f) + } + + @Test fun anUntouchedSavedDpadIsResizedAroundItsCentreAndAMovedOneIsKept() { + val fresh = LegacyGamepadLayout(context, 1200, 800) + val target = fresh.elements().filter { it.control in setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) } + val cx = target.map { it.x + it.width / 2f }.average().toFloat() + val cy = target.map { it.y + it.height / 2f }.average().toFloat() + val scale = 26f / 30f + val old = target.map { e -> + val ex = cx + (e.x + e.width / 2f - cx) * 52f / 50f + val ey = cy + (e.y + e.height / 2f - cy) * 52f / 50f + e.copy(x = ex - e.width * scale / 2f, y = ey - e.height * scale / 2f, width = e.width * scale, height = e.height * scale, buttonShape = InputButtonShape.ROUNDED) + } + val restyled = fresh.restyle(old) + for ((want, got) in target.zip(restyled)) { + assertEquals(want.x, got.x, .0005f); assertEquals(want.y, got.y, .0005f) + assertEquals(want.width, got.width, .0005f); assertEquals(InputButtonShape.CIRCLE, got.buttonShape) + } + val moved = old.mapIndexed { i, e -> if (i == 0) e.copy(buttonShape = InputButtonShape.RECTANGLE) else e } + assertEquals(moved.drop(1).map { it.width }, fresh.restyle(moved).drop(1).map { it.width }) + val shifted = old.mapIndexed { i, e -> if (i == 0) e.copy(x = e.x + .03f) else e } + assertEquals(shifted.map { listOf(it.x, it.y, it.width, it.height) }, fresh.restyle(shifted).map { listOf(it.x, it.y, it.width, it.height) }) + } + + @Test fun stickTouchAreasReachFurtherOutwardButKeepTheirInnerEdge() { + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + for (control in listOf(InputControl.LS, InputControl.RS)) { + val stick = elements.single { it.control == control } + assertEquals(control.id, stick.height * 800f * 1.25f, stick.width * 1200f, 1f) + } + val left = elements.single { it.control == InputControl.LS } + val right = elements.single { it.control == InputControl.RS } + ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (.3f to .5f), "rs" to (.7f to .5f))) + val placed = LegacyGamepadLayout(context, 1200, 800).elements() + val ls = placed.single { it.control == InputControl.LS } + val rs = placed.single { it.control == InputControl.RS } + assertEquals(.3f, ls.x + ls.width - ls.height * 800f / 1200f / 2f, .0001f) + assertEquals(.7f, rs.x + rs.height * 800f / 1200f / 2f, .0001f) + assertTrue(left.width > left.height * 800f / 1200f && right.width > right.height * 800f / 1200f) + } + + @Test fun adaptiveSticksToggleChangesOnlyTheSticksOfThatLayout() { + val library = InputLibraries.read(context) + val overlay = library.layouts.single { it.id == InputLibraries.OVERLAY } + assertEquals(true, overlay.adaptiveSticks) + InputLibraries.setAdaptiveSticks(context, overlay.id, false) + val off = InputLibraries.read(context) + val changed = off.layouts.single { it.id == overlay.id } + assertEquals(false, changed.adaptiveSticks) + assertFalse(ControllerPrefs.read(context).adaptiveSticks) + assertEquals(overlay.elements.filter { it.kind != InputElementKind.STICK }, changed.elements.filter { it.kind != InputElementKind.STICK }) + assertEquals(library.layouts.filter { it.id != overlay.id }, off.layouts.filter { it.id != overlay.id }) + assertNull(off.layouts.single { it.id == InputLibraries.DECK }.adaptiveSticks) + assertTrue(InputLibraries.setAdaptiveSticks(context, overlay.id, true)) + assertEquals(true, InputLibraries.read(context).layouts.single { it.id == overlay.id }.adaptiveSticks) + prefs.edit().putString("library", JSONObject().put("version", 99).toString()).commit() + assertFalse(InputLibraries.setAdaptiveSticks(context, overlay.id, false)) + assertTrue(ControllerPrefs.read(context).adaptiveSticks) + } + + @Test fun savedGamepadsFromTheFirstUnifiedBuildGetTheRoundLookOnce() { + val fresh = InputLibraries.read(context) + val overlay = fresh.layouts.single { it.id == InputLibraries.OVERLAY } + val old = overlay.copy(elements = overlay.elements.map { e -> + if (e.id == "builtin:keyboard") InputSurfaceElement(InputControl.A, .02f, .9f, .12f, .08f, "builtin:keyboard", name = "Keyboard") + else if (e.control == InputControl.A) e.copy(buttonShape = InputButtonShape.RECTANGLE, x = .5f) + else e.copy(buttonShape = InputButtonShape.ROUNDED) + }) + val custom = SavedInputLayout("mine", "Mine", listOf(InputSurfaceElement(InputControl.B, .1f, .1f, .1f, .1f))) + val stored = fresh.withLayout(old).withLayout(custom) + val renamed = InputSurfaceElement(InputControl.A, .02f, .9f, .12f, .08f, "builtin:keyboard", name = "Keys") + assertEquals(renamed, LegacyGamepadLayout(context, 1200, 800).restyle(listOf(renamed)).single()) + prefs.edit().putString("library", JSONObject(InputLibraries.encode(stored)).put("version", 8).toString()).commit() + val upgraded = InputLibraries.read(context) + val restyled = upgraded.layouts.single { it.id == InputLibraries.OVERLAY }.elements + assertEquals(overlay.elements.single { it.id == "builtin:keyboard" }, restyled.single { it.id == "builtin:keyboard" }) + assertEquals(InputButtonShape.RECTANGLE, restyled.single { it.control == InputControl.A && it.id.startsWith("legacy:") }.buttonShape) + assertEquals(.5f, restyled.single { it.control == InputControl.A && it.id.startsWith("legacy:") }.x, .0001f) + assertEquals(InputButtonShape.PILL, restyled.single { it.control == InputControl.LB }.buttonShape) + assertEquals(InputButtonShape.CIRCLE, restyled.single { it.control == InputControl.UP }.buttonShape) + assertEquals(InputButtonShape.ROUNDED, upgraded.layouts.single { it.id == "mine" }.elements.single().buttonShape) + assertEquals(upgraded, InputLibraries.read(context)) + for (desktop in listOf(false, true)) + assertEquals(InputAction.Command(InputCommand.KEYBOARD), upgraded.compileActive(desktop).elementAction(InputRole.PRIMARY, "builtin:keyboard", InputControl.A)) + } + @Test fun quickPartsAreModeSpecificAndDoNotEditSavedProfiles() { val original = InputLibraries.read(context) val mixed = original.selectPart(true, InputRole.SECONDARY, InputStockLayouts.TRACKPAD).validate() diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt index 593584875f..9ccbafe5b2 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt @@ -20,6 +20,7 @@ class InputSurfaceSourceTest { override fun pointer(dx: Float, dy: Float) { events += "pointer" } override fun scroll(steps: Int) { events += "scroll" } override fun command(command: InputCommand) { events += command.name } + override fun command(command: InputCommand, role: InputRole) { events += "${command.name}:$role" } override fun requestMotion() {} }) private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "element-input")) @@ -33,6 +34,18 @@ class InputSurfaceSourceTest { } private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState() + @Test fun aCommandCarriesTheRoleOfTheSurfaceThatSentIt() { + val keyboard = InputAction.Command(InputCommand.KEYBOARD) + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.PRIMARY to mapOf("builtin:keyboard" to keyboard), + InputRole.SECONDARY to mapOf("builtin:keyboard" to keyboard)))) + val element = button("builtin:keyboard") + val primary = InputSurfaceSource(bridge, InputRole.PRIMARY, element).also { sources += it } + val secondary = source(element) + primary.update(1f); primary.update(0f) + secondary.update(1f); secondary.update(0f) + assertEquals(listOf("KEYBOARD:PRIMARY", "KEYBOARD:SECONDARY"), events) + } + @Test fun toggleUsesSharedOwnershipAndReleasesOnMenuAndProfileChange() { bind("first" to InputAction.Keys(listOf(29)), "second" to InputAction.Keys(listOf(29))) val a = source(button("first").copy(toggle = true)); val b = source(button("second")) diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt index 5f7b48f1e3..1e317f2398 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt @@ -175,6 +175,33 @@ class OnScreenControlsTest { assertFalse(state().isDown(PadState.L3)) } + @Test fun theQuickToggleHidesEverythingButTheKeyboardAndShowsItAgain() { + val keyboard = InputSurfaceElement(InputControl.A, .4f, .4f, .1f, .1f, "builtin:keyboard", name = "Keyboard") + view.updateLayout(SavedInputLayout("test", "Test", listOf(left, button, keyboard))) + measure(view) + val density = context.resources.displayMetrics.density + val x = 34 * density + val y = 600 - 34 * density + assertTrue(view.takesTouchAt(x, y)) + touch(MotionEvent.ACTION_UP, 4 to (x to y)) + touch(MotionEvent.ACTION_DOWN, 5 to (x to y)); touch(MotionEvent.ACTION_CANCEL, 5 to (x to y)); touch(MotionEvent.ACTION_UP, 5 to (x to y)) + assertFalse(view.isQuickHidden) + touch(MotionEvent.ACTION_DOWN, 1 to (x to y)); touch(MotionEvent.ACTION_UP, 1 to (x to y)) + measure(view) + assertTrue(view.isQuickHidden) + assertFalse(view.takesTouchAt(800f, 250f)) + assertTrue(view.takesTouchAt(450f, 270f)) + touch(MotionEvent.ACTION_DOWN, 2 to (800f to 250f)); touch(MotionEvent.ACTION_UP, 2 to (800f to 250f)) + assertTrue(view.isQuickHidden) + touch(MotionEvent.ACTION_DOWN, 3 to (x to y)); touch(MotionEvent.ACTION_UP, 3 to (x to y)) + measure(view) + assertFalse(view.isQuickHidden) + assertTrue(view.takesTouchAt(800f, 250f)) + view.setButtonsOnly(true) + measure(view) + assertFalse(view.takesTouchAt(x, y)) + } + private fun touch(action: Int, vararg pointers: Pair>, flags: Int = 0): Boolean { val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray() val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() diff --git a/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt b/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt index bcb071e883..cfa85a0de0 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt @@ -405,8 +405,11 @@ class StickDeadZoneTest { view.measure(android.view.View.MeasureSpec.makeMeasureSpec(1200, android.view.View.MeasureSpec.EXACTLY), android.view.View.MeasureSpec.makeMeasureSpec(800, android.view.View.MeasureSpec.EXACTLY)) view.layout(0, 0, 1200, 800) val control = elements.first { it.control.id == id } - val x = (control.x + control.width / 2) * 1200 - val y = (control.y + control.height / 2) * 800 + val left = (control.x * 1200).toInt() + val top = (control.y * 800).toInt() + val height = (control.height * 800).toInt() + val x = left + if (id == "ls") (control.width * 1200).toInt() - height / 2f else height / 2f + val y = top + height / 2f val down = MotionEvent.obtain(1000, 1000, MotionEvent.ACTION_DOWN, x, y, 0) try { assertTrue(view.dispatchTouchEvent(down)) } finally { down.recycle() } assertEquals(0f, if (id == "ls") pad().leftX else pad().rightX, 0f)