mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
feat: on-screen keys for any key or key combo (#1305)
This commit is contained in:
1 parent
6e67dbb5be
commit
9f4794769c
48 files changed
+3169
-47
No files matched your search
@@ -75,8 +75,10 @@ import com.droiddeck.launcher.input.OnScreenControls
|
||||
import com.droiddeck.launcher.input.PadBridge
|
||||
import com.droiddeck.launcher.input.PadMouse
|
||||
import com.droiddeck.launcher.input.PadState
|
||||
import com.droiddeck.launcher.input.ScreenKeys
|
||||
import com.droiddeck.launcher.input.SteamControllerBle
|
||||
import com.droiddeck.launcher.input.DirectTouchHold
|
||||
import com.droiddeck.launcher.input.GestureTouchStreamGate
|
||||
import com.droiddeck.launcher.input.PointerGestures
|
||||
import com.droiddeck.launcher.input.SecondScreenDisplay
|
||||
import com.droiddeck.launcher.input.SecondScreenDisplays
|
||||
@@ -199,10 +201,20 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
private var gestureConfig = com.droiddeck.launcher.input.TouchGestureConfig()
|
||||
private var gestureGame: String? = null
|
||||
private var gestureSettingsOpen by mutableStateOf(false)
|
||||
private var screenKeyPickerOpen by mutableStateOf(false)
|
||||
private var screenKeyBind: List<Int>? = null
|
||||
private var screenKeyDir: Int? = null
|
||||
private var screenKeyTextFocus = false
|
||||
private var screenKeyEdit by mutableStateOf<com.droiddeck.launcher.input.ScreenKey?>(null)
|
||||
private var screenLayoutsOpen by mutableStateOf(false)
|
||||
private var screenKeysShown by mutableStateOf(true)
|
||||
private var screenLayoutNames by mutableStateOf(listOf<String>())
|
||||
private var screenKeys: ScreenKeys? = null
|
||||
private var recognizedGesture by mutableStateOf<TouchscreenGestures.Gesture?>(null)
|
||||
private var gesturePointerX = 0f
|
||||
private var gesturePointerY = 0f
|
||||
private var gesturePointerReady = false
|
||||
private val gestureTouchStreamGate = GestureTouchStreamGate()
|
||||
private val clearRecognizedGesture = Runnable { recognizedGesture = null }
|
||||
private val gestureKeysDown = mutableSetOf<Int>()
|
||||
private lateinit var scrollTouch: ScrollTouchGestures
|
||||
@@ -536,6 +548,21 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
cursorVisible = false
|
||||
}
|
||||
onScreenControls = OnScreenControls(this, bridge, onKeyboard = ::togglePcKeyboard).also { root.addView(it) }
|
||||
screenKeysShown = com.droiddeck.launcher.input.ControllerPrefs.screenKeysShown(this)
|
||||
screenKeys = ScreenKeys(this) { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down) }.also { keys ->
|
||||
keys.onTouchMiss = miss@{ event ->
|
||||
if (routedTouchMode == SessionPrefs.TOUCH_GESTURES &&
|
||||
!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, event)) return@miss
|
||||
event.offsetLocation(keys.left - surfaceView.left.toFloat(), keys.top - surfaceView.top.toFloat())
|
||||
onTouchEvent(event)
|
||||
}
|
||||
keys.onAdd = { screenKeyPickerOpen = true }
|
||||
keys.onSelect = { codes -> screenKeyEdit = screenKeys?.stateOf(codes) }
|
||||
keys.onEditingChanged = { if (!it) screenKeyEdit = null }
|
||||
keys.steamStyle = { steamTouchControls?.style }
|
||||
keys.visibility = if (screenKeysShown) View.VISIBLE else View.GONE
|
||||
root.addView(keys)
|
||||
}
|
||||
keyboard = KeyboardHost(this).also { root.addView(it) }
|
||||
touchpad = TouchpadGestures(PointerGestures.slop(this), pointerListener)
|
||||
directTouchHold = DirectTouchHold(PointerGestures.slop(this), pointerListener) {
|
||||
@@ -550,7 +577,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
// game surface and on-screen buttons, in either finger-down order.
|
||||
root.isMotionEventSplittingEnabled = true
|
||||
surfaceView.setOnTouchListener { _, event ->
|
||||
routedTouchMode == SessionPrefs.TOUCH_GESTURES && onTouchEvent(event)
|
||||
if (routedTouchMode != SessionPrefs.TOUCH_GESTURES) {
|
||||
gestureTouchStreamGate.reset()
|
||||
false
|
||||
} else if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SURFACE, event)) {
|
||||
false
|
||||
} else onTouchEvent(event)
|
||||
}
|
||||
padMouse = PadMouse(padMouseListener)
|
||||
bridge.setTouchRoute(object : PadBridge.TouchRoute {
|
||||
@@ -741,6 +773,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, gestureSettingsOpen, pipUi, SessionState.suspended) {
|
||||
if (drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || pipUi || SessionState.suspended) {
|
||||
cancelTouchGestures()
|
||||
screenKeys?.stopEditing()
|
||||
}
|
||||
}
|
||||
com.droiddeck.launcher.ui.GestureFeedback(recognizedGesture)
|
||||
@@ -749,6 +782,84 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
onDismiss = { gestureSettingsOpen = false },
|
||||
onSaved = { gestureSettingsOpen = false; refreshGestureConfig() },
|
||||
)
|
||||
if (screenKeyPickerOpen) {
|
||||
val dir = screenKeyDir
|
||||
com.droiddeck.launcher.ui.ScreenKeyPicker(
|
||||
initial = if (dir != null) {
|
||||
listOfNotNull((screenKeyEdit?.codes?.getOrNull(dir) ?: 0).takeIf { it != 0 })
|
||||
} else {
|
||||
screenKeyBind ?: emptyList()
|
||||
},
|
||||
onDismiss = { screenKeyPickerOpen = false; screenKeyBind = null; screenKeyDir = null },
|
||||
onAdd = { codes ->
|
||||
val bind = screenKeyBind
|
||||
if (dir != null && bind != null) {
|
||||
screenKeys?.setDirection(bind, dir, codes.lastOrNull() ?: 0)?.let { screenKeyEdit = it }
|
||||
} else if (bind != null) {
|
||||
screenKeys?.setCodes(bind, codes)?.let { screenKeyEdit = it }
|
||||
} else {
|
||||
screenKeys?.add(codes)
|
||||
screenKeyEdit = screenKeys?.stateOf(codes)
|
||||
}
|
||||
screenKeyPickerOpen = false
|
||||
screenKeyBind = null
|
||||
screenKeyDir = null
|
||||
},
|
||||
)
|
||||
}
|
||||
screenKeyEdit?.let { key ->
|
||||
com.droiddeck.launcher.ui.ScreenKeyEditor(
|
||||
codes = key.codes, keyY = key.y,
|
||||
size = key.size, opacity = key.opacity, shape = key.shape, toggle = key.toggle,
|
||||
labels = key.labels,
|
||||
text = key.text, hasImage = key.image.isNotEmpty(),
|
||||
onResize = { screenKeys?.resize(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onOpacity = { screenKeys?.setOpacity(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onShape = { screenKeys?.setShape(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onToggle = { on -> screenKeys?.setToggle(key.codes, on); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onLabels = { on -> screenKeys?.setLabels(key.codes, on); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onDirection = { index -> screenKeyBind = key.codes; screenKeyDir = index; screenKeyPickerOpen = true },
|
||||
onText = { screenKeys?.setText(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onImage = { screenKeys?.setImage(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onClearImage = { screenKeys?.setImage(key.codes, ""); screenKeyEdit = screenKeys?.stateOf(key.codes) },
|
||||
onBind = { screenKeyBind = key.codes; screenKeyDir = null; screenKeyPickerOpen = true },
|
||||
onTextFocus = { screenKeyTextFocus = true; keyboard?.hide() },
|
||||
onTextBlur = { screenKeyTextFocus = false },
|
||||
onRemove = { screenKeys?.remove(key.codes); screenKeyEdit = null },
|
||||
onDismiss = { screenKeyEdit = null; screenKeyTextFocus = false; screenKeys?.deselect() },
|
||||
)
|
||||
}
|
||||
if (screenLayoutsOpen) com.droiddeck.launcher.ui.ScreenLayoutPanel(
|
||||
layouts = screenLayoutNames,
|
||||
onSave = { name ->
|
||||
screenKeys?.let { keys ->
|
||||
com.droiddeck.launcher.input.ControllerPrefs.saveScreenLayout(this@SessionActivity, name, keys.export())
|
||||
}
|
||||
screenLayoutNames = layoutNames()
|
||||
},
|
||||
onLoad = { name ->
|
||||
com.droiddeck.launcher.input.ControllerPrefs.screenLayouts(this@SessionActivity)[name]?.let { screenKeys?.load(it) }
|
||||
screenLayoutsOpen = false
|
||||
},
|
||||
onDelete = { name ->
|
||||
com.droiddeck.launcher.input.ControllerPrefs.deleteScreenLayout(this@SessionActivity, name)
|
||||
screenLayoutNames = layoutNames()
|
||||
},
|
||||
onImport = { text ->
|
||||
val width = (screenKeys?.width ?: 0).takeIf { it > 0 } ?: resources.displayMetrics.widthPixels
|
||||
val keys = com.droiddeck.launcher.input.importScreenLayout(text, width, resources.displayMetrics.density)
|
||||
if (keys != null) {
|
||||
screenKeys?.load(com.droiddeck.launcher.input.ControllerPrefs.encodeScreenKeys(keys))
|
||||
screenLayoutsOpen = false
|
||||
}
|
||||
},
|
||||
exportText = {
|
||||
val width = (screenKeys?.width ?: 0).takeIf { it > 0 } ?: resources.displayMetrics.widthPixels
|
||||
val keys = com.droiddeck.launcher.input.ControllerPrefs.decodeScreenKeys(screenKeys?.export().orEmpty())
|
||||
com.droiddeck.launcher.input.exportScreenLayout(keys, width, resources.displayMetrics.density)
|
||||
},
|
||||
onDismiss = { screenLayoutsOpen = false },
|
||||
)
|
||||
androidx.compose.runtime.LaunchedEffect(drawerOpen) {
|
||||
if (drawerOpen) {
|
||||
syncLossless()
|
||||
@@ -939,6 +1050,19 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
},
|
||||
onTouch = { v -> SessionPrefs.setTouchMode(this@SessionActivity, v); readPrefs() },
|
||||
onGestureSettings = { drawerOpen = false; gestureSettingsOpen = true },
|
||||
onScreenKeys = { drawerOpen = false; screenKeys?.startEditing() },
|
||||
screenKeysShown = screenKeysShown,
|
||||
onScreenKeysShown = { shown ->
|
||||
screenKeysShown = shown
|
||||
com.droiddeck.launcher.input.ControllerPrefs.setScreenKeysShown(this@SessionActivity, shown)
|
||||
screenKeys?.stopEditing()
|
||||
screenKeys?.visibility = if (shown) View.VISIBLE else View.GONE
|
||||
},
|
||||
onScreenLayouts = {
|
||||
drawerOpen = false
|
||||
screenLayoutNames = layoutNames()
|
||||
screenLayoutsOpen = true
|
||||
},
|
||||
onFexPreset = { v -> SessionPrefs.setFexPreset(this@SessionActivity, v); readPrefs() },
|
||||
onSecondScreenMode = ::selectSecondScreenMode,
|
||||
onSecondScreenDisplay = ::selectSecondScreenDisplay,
|
||||
@@ -1495,7 +1619,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
if (nextTouchMode == SessionPrefs.TOUCH_OFF || !usingTouchpad()) cursorVisible = false
|
||||
}
|
||||
touchMode = nextTouchMode
|
||||
routedTouchMode = effectiveTouchMode()
|
||||
val nextRoutedTouchMode = effectiveTouchMode()
|
||||
if (routedTouchMode != nextRoutedTouchMode) gestureTouchStreamGate.reset()
|
||||
routedTouchMode = nextRoutedTouchMode
|
||||
frameGen = FrameGen.mode(this)
|
||||
fexPreset = SessionPrefs.fexPreset(this)
|
||||
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
|
||||
@@ -2053,6 +2179,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
return super.dispatchKeyEvent(event)
|
||||
}
|
||||
if (pipUi) return true
|
||||
if (screenKeyTextFocus) return super.dispatchKeyEvent(event)
|
||||
val pad = com.droiddeck.launcher.input.JoyCon.normalize(event)
|
||||
if (pad !== event) return dispatchKeyEvent(pad)
|
||||
val fromController = PadBridge.isControllerKeyEvent(event)
|
||||
@@ -2254,8 +2381,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
super.dispatchKeyEvent(event)
|
||||
}
|
||||
|
||||
private fun layoutNames(): List<String> =
|
||||
com.droiddeck.launcher.input.ControllerPrefs.screenLayouts(this).keys.sorted()
|
||||
|
||||
private fun refreshGestureConfig() {
|
||||
gestureConfig = com.droiddeck.launcher.input.GesturePrefs.read(this, gestureGame)
|
||||
gestureTouchStreamGate.reset()
|
||||
if (::touchscreenGestures.isInitialized) touchscreenGestures.configure(gestureConfig)
|
||||
gesturePointerReady = false
|
||||
recognizedGesture = null
|
||||
@@ -2544,6 +2675,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
private fun refreshControls() {
|
||||
applyUnfoldedControls()
|
||||
updateOnScreenControls()
|
||||
screenKeys?.refreshAppearance()
|
||||
}
|
||||
|
||||
private fun applyUnfoldedControls() {
|
||||
@@ -2629,6 +2761,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
if (::scrollTouch.isInitialized) scrollTouch.cancel()
|
||||
if (CompositorHost.isStarted) WaylandCompositor.nativeSendTouch(3, -1, 0, 0)
|
||||
if (next == SessionPrefs.TOUCH_OFF) cursorVisible = false
|
||||
gestureTouchStreamGate.reset()
|
||||
routedTouchMode = next
|
||||
}
|
||||
|
||||
@@ -2819,6 +2952,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
device.plugged = false
|
||||
uiHandler.postDelayed({ updateOnScreenControls() }, 700)
|
||||
}
|
||||
view.onStyle = { screenKeys?.refreshAppearance() }
|
||||
root.addView(view, root.indexOfChild(onScreenControls) + 1)
|
||||
steamTouchControls = view
|
||||
steamTouchAvailable = true
|
||||
@@ -2849,6 +2983,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
controls.visibility = View.GONE
|
||||
steamTouchControls?.let { it.releaseAll(); it.visibility = View.GONE }
|
||||
com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false
|
||||
screenKeys?.followSteam = false
|
||||
applyTouchRouting()
|
||||
return
|
||||
}
|
||||
@@ -2877,6 +3012,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
onScreenButtonsVisible = show
|
||||
val drawn = show && !keyboardCoversPad()
|
||||
if (steam != null) {
|
||||
screenKeys?.followSteam = drawn
|
||||
if (controls.visibility == View.VISIBLE) { controls.releaseAll(); controls.visibility = View.GONE }
|
||||
if (!drawn) steam.releaseAll()
|
||||
steam.visibility = if (drawn) View.VISIBLE else View.GONE
|
||||
@@ -2885,6 +3021,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
Log.i(TAG, "on-screen controls: Steam's touch controller " + if (drawn) "shown" else "hidden")
|
||||
return
|
||||
}
|
||||
screenKeys?.followSteam = false
|
||||
if (drawn == (controls.visibility == View.VISIBLE)) return
|
||||
if (!drawn) controls.releaseAll()
|
||||
controls.visibility = if (drawn) View.VISIBLE else View.GONE
|
||||
@@ -2970,6 +3107,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
pendingBackAction = null
|
||||
releaseDrawerDirection()
|
||||
onScreenControls?.releaseAll()
|
||||
screenKeys?.releaseAll()
|
||||
releasePads()
|
||||
touchpad.cancel()
|
||||
scrollTouch.cancel()
|
||||
|
||||
@@ -157,6 +157,56 @@ object ControllerPrefs {
|
||||
prefs(context).edit().remove("layout.${width}x$height").apply()
|
||||
}
|
||||
|
||||
fun screenKeysShown(context: Context): Boolean = prefs(context).getBoolean("screenKeysShown", true)
|
||||
|
||||
fun setScreenKeysShown(context: Context, shown: Boolean) {
|
||||
prefs(context).edit().putBoolean("screenKeysShown", shown).apply()
|
||||
}
|
||||
|
||||
fun screenKeys(context: Context): List<ScreenKey> = decodeScreenKeys(prefs(context).getString("screenKeys", null).orEmpty())
|
||||
|
||||
fun setScreenKeys(context: Context, keys: List<ScreenKey>) {
|
||||
prefs(context).edit().putString("screenKeys", encodeScreenKeys(keys)).apply()
|
||||
}
|
||||
|
||||
fun encodeScreenKeys(keys: List<ScreenKey>): String = keys.joinToString(";") {
|
||||
"${it.codes.joinToString("+")}:${it.x},${it.y},${it.size}:${escape(it.glyph)}:${escape(it.text)}:${escape(it.image)}:${it.opacity}:${it.shape}:${if (it.toggle) 1 else 0}:${if (it.labels) 1 else 0}"
|
||||
}
|
||||
|
||||
fun decodeScreenKeys(raw: String): List<ScreenKey> = raw.split(';').mapNotNull { entry ->
|
||||
val parts = entry.split(':')
|
||||
if (parts.size < 2) return@mapNotNull null
|
||||
val keys = parts[0].split('+').mapNotNull { it.toIntOrNull() }.takeIf { it.isNotEmpty() } ?: return@mapNotNull null
|
||||
val nums = parts[1].split(',').mapNotNull { it.toFloatOrNull() }.takeIf { it.size == 3 } ?: return@mapNotNull null
|
||||
ScreenKey(
|
||||
keys, nums[0], nums[1], nums[2].toInt(),
|
||||
parts.getOrNull(2)?.let(::unescape).orEmpty(),
|
||||
parts.getOrNull(3)?.let(::unescape).orEmpty(),
|
||||
parts.getOrNull(4)?.let(::unescape).orEmpty(),
|
||||
parts.getOrNull(5)?.toFloatOrNull()?.coerceIn(0.2f, 1f) ?: 1f,
|
||||
parts.getOrNull(6)?.toIntOrNull()?.coerceIn(SCREEN_KEY_PILL, SCREEN_KEY_STICK) ?: SCREEN_KEY_PILL,
|
||||
parts.getOrNull(7) == "1",
|
||||
parts.getOrNull(8) != "0",
|
||||
)
|
||||
}
|
||||
|
||||
fun screenLayouts(context: Context): Map<String, String> =
|
||||
prefs(context).all.mapNotNull { (key, value) ->
|
||||
if (!key.startsWith("screenLayout.") || value !is String) null else key.removePrefix("screenLayout.") to value
|
||||
}.toMap()
|
||||
|
||||
fun saveScreenLayout(context: Context, name: String, raw: String) {
|
||||
prefs(context).edit().putString("screenLayout.$name", raw).apply()
|
||||
}
|
||||
|
||||
fun deleteScreenLayout(context: Context, name: String) {
|
||||
prefs(context).edit().remove("screenLayout.$name").apply()
|
||||
}
|
||||
|
||||
private fun escape(value: String) = value.replace("%", "%25").replace(";", "%3B").replace(":", "%3A").replace(",", "%2C")
|
||||
|
||||
private fun unescape(value: String) = value.replace("%2C", ",").replace("%3A", ":").replace("%3B", ";").replace("%25", "%")
|
||||
|
||||
fun resetAll(context: Context) {
|
||||
prefs(context).edit().clear().putBoolean("rumble", true).apply()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.view.MotionEvent
|
||||
|
||||
/** Keeps split Android touch targets from restarting one shared gesture recognizer. */
|
||||
internal class GestureTouchStreamGate {
|
||||
enum class Source { SURFACE, SCREEN_KEYS }
|
||||
|
||||
private var owner: Source? = null
|
||||
|
||||
fun accepts(source: Source, event: MotionEvent): Boolean {
|
||||
if (owner == null && event.actionMasked == MotionEvent.ACTION_DOWN) owner = source
|
||||
val accepted = owner == source
|
||||
if (accepted && (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL)) {
|
||||
owner = null
|
||||
}
|
||||
return accepted
|
||||
}
|
||||
|
||||
fun reset() { owner = null }
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.StringRes
|
||||
import com.droiddeck.launcher.R
|
||||
|
||||
data class PcKey(
|
||||
val label: String,
|
||||
val code: Int,
|
||||
val weight: Float = 1f,
|
||||
val shifted: String? = null,
|
||||
@StringRes val labelRes: Int = 0,
|
||||
)
|
||||
|
||||
internal fun k(label: String, code: Int, weight: Float = 1f, shifted: String? = null) = PcKey(label, code, weight, shifted)
|
||||
|
||||
internal fun k(@StringRes labelRes: Int, code: Int, weight: Float = 1f) = PcKey("", code, weight, null, labelRes)
|
||||
|
||||
val PC_KEY_ROWS: List<List<PcKey>> = listOf(
|
||||
listOf(
|
||||
k(R.string.pckb_key_esc, 1), k("F1", 59), k("F2", 60), k("F3", 61), k("F4", 62), k("F5", 63), k("F6", 64),
|
||||
k("F7", 65), k("F8", 66), k("F9", 67), k("F10", 68), k("F11", 87), k("F12", 88),
|
||||
k(R.string.pckb_key_ins, 110), k(R.string.pckb_key_del, 111), k(R.string.pckb_key_home, 102), k(R.string.pckb_key_end, 107), k(R.string.pckb_key_pgup, 104), k(R.string.pckb_key_pgdn, 109),
|
||||
),
|
||||
listOf(
|
||||
k("`", 41, shifted = "~"), k("1", 2, shifted = "!"), k("2", 3, shifted = "@"), k("3", 4, shifted = "#"),
|
||||
k("4", 5, shifted = "$"), k("5", 6, shifted = "%"), k("6", 7, shifted = "^"), k("7", 8, shifted = "&"),
|
||||
k("8", 9, shifted = "*"), k("9", 10, shifted = "("), k("0", 11, shifted = ")"), k("-", 12, shifted = "_"),
|
||||
k("=", 13, shifted = "+"), k(R.string.pckb_key_backspace, 14, 2f),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_tab, 15, 1.5f), k("q", 16), k("w", 17), k("e", 18), k("r", 19), k("t", 20), k("y", 21),
|
||||
k("u", 22), k("i", 23), k("o", 24), k("p", 25), k("[", 26, shifted = "{"), k("]", 27, shifted = "}"),
|
||||
k("\\", 43, 1.5f, shifted = "|"),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_caps, 58, 1.75f), k("a", 30), k("s", 31), k("d", 32), k("f", 33), k("g", 34), k("h", 35),
|
||||
k("j", 36), k("k", 37), k("l", 38), k(";", 39, shifted = ":"), k("'", 40, shifted = "\""),
|
||||
k(R.string.pckb_key_enter, 28, 2.25f),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_shift, 42, 2.25f), k("z", 44), k("x", 45), k("c", 46), k("v", 47), k("b", 48), k("n", 49),
|
||||
k("m", 50), k(",", 51, shifted = "<"), k(".", 52, shifted = ">"), k("/", 53, shifted = "?"),
|
||||
k(R.string.pckb_key_shift, 54, 1.75f), k("↑", 103),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_ctrl, 29, 1.5f), k(R.string.pckb_key_super, 125, 1.25f), k(R.string.pckb_key_alt, 56, 1.25f), k(R.string.pckb_key_space, 57, 6.25f),
|
||||
k(R.string.pckb_key_altgr, 100, 1.25f), k(R.string.pckb_key_ctrl, 97, 1.25f), k("←", 105), k("↓", 108), k("→", 106),
|
||||
),
|
||||
)
|
||||
|
||||
val PC_MODIFIERS: Set<Int> = setOf(42, 54, 29, 97, 56, 100, 125)
|
||||
|
||||
val PC_KEYS: Map<Int, PcKey> = PC_KEY_ROWS.flatten().associateBy { it.code }
|
||||
|
||||
fun pcKeyLabel(context: Context, code: Int): String {
|
||||
val key = PC_KEYS[code] ?: return code.toString()
|
||||
val text = if (key.labelRes != 0) context.getString(key.labelRes) else key.label
|
||||
return if (text.length == 1 && text[0].isLetter()) text.uppercase() else text
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
|
||||
fun screenKeyLabel(context: Context, codes: List<Int>): String =
|
||||
codes.filter { it != 0 }.joinToString("+") { short(context, it) }
|
||||
|
||||
private fun short(context: Context, code: Int): String = when (code) {
|
||||
28 -> "⏎"
|
||||
14 -> "⌫"
|
||||
15 -> "Tab"
|
||||
57 -> "Space"
|
||||
58 -> "Caps"
|
||||
42, 54 -> "Shift"
|
||||
29, 97 -> "Ctrl"
|
||||
56 -> "Alt"
|
||||
100 -> "AltGr"
|
||||
125 -> "Super"
|
||||
else -> pcKeyLabel(context, code)
|
||||
}
|
||||
|
||||
const val SCREEN_KEY_PILL = 0
|
||||
const val SCREEN_KEY_ROUND = 1
|
||||
const val SCREEN_KEY_CIRCLE = 2
|
||||
const val SCREEN_KEY_RECT = 3
|
||||
const val SCREEN_KEY_TRIGGER = 4
|
||||
const val SCREEN_KEY_DPAD = 5
|
||||
const val SCREEN_KEY_STICK = 6
|
||||
|
||||
val SCREEN_KEY_DPAD_DIRS = listOf("↑", "→", "↓", "←")
|
||||
val SCREEN_KEY_STICK_DIRS = listOf("↑", "↗", "→", "↘", "↓", "↙", "←", "↖")
|
||||
|
||||
fun screenKeyDirections(shape: Int): List<String> = when (shape) {
|
||||
SCREEN_KEY_DPAD -> SCREEN_KEY_DPAD_DIRS
|
||||
SCREEN_KEY_STICK -> SCREEN_KEY_STICK_DIRS
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
val SCREEN_KEY_SHAPES =
|
||||
listOf(
|
||||
SCREEN_KEY_PILL, SCREEN_KEY_ROUND, SCREEN_KEY_CIRCLE, SCREEN_KEY_RECT,
|
||||
SCREEN_KEY_TRIGGER, SCREEN_KEY_DPAD, SCREEN_KEY_STICK,
|
||||
)
|
||||
|
||||
data class ScreenKey(
|
||||
val codes: List<Int>,
|
||||
val x: Float,
|
||||
val y: Float,
|
||||
val size: Int,
|
||||
val glyph: String = "",
|
||||
val text: String = "",
|
||||
val image: String = "",
|
||||
val opacity: Float = 1f,
|
||||
val shape: Int = SCREEN_KEY_PILL,
|
||||
val toggle: Boolean = false,
|
||||
val labels: Boolean = true,
|
||||
) {
|
||||
fun shownLabel(context: Context): String = when {
|
||||
text.isNotEmpty() -> text
|
||||
glyph.isNotEmpty() -> glyph
|
||||
else -> screenKeyLabel(context, codes)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Path
|
||||
|
||||
object ScreenKeyIcons {
|
||||
fun has(code: Int): Boolean = code in SET
|
||||
|
||||
private val SET = setOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106)
|
||||
|
||||
fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint): Boolean {
|
||||
if (!has(code)) return false
|
||||
val h = box / 2f
|
||||
val stroke = box * 0.085f
|
||||
val path = Path()
|
||||
paint.style = Paint.Style.STROKE
|
||||
paint.strokeWidth = stroke
|
||||
paint.strokeCap = Paint.Cap.ROUND
|
||||
paint.strokeJoin = Paint.Join.ROUND
|
||||
when (code) {
|
||||
28 -> {
|
||||
path.moveTo(cx - 0.34f * h, cy - 0.22f * h)
|
||||
path.lineTo(cx + 0.30f * h, cy - 0.22f * h)
|
||||
path.lineTo(cx + 0.30f * h, cy + 0.24f * h)
|
||||
path.lineTo(cx - 0.04f * h, cy + 0.24f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx + 0.12f * h, cy + 0.06f * h)
|
||||
path.lineTo(cx - 0.04f * h, cy + 0.24f * h)
|
||||
path.lineTo(cx + 0.12f * h, cy + 0.42f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
14 -> {
|
||||
path.moveTo(cx - 0.72f * h, cy)
|
||||
path.lineTo(cx - 0.10f * h, cy - 0.50f * h)
|
||||
path.lineTo(cx + 0.74f * h, cy - 0.50f * h)
|
||||
path.lineTo(cx + 0.74f * h, cy + 0.50f * h)
|
||||
path.lineTo(cx - 0.10f * h, cy + 0.50f * h)
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx + 0.10f * h, cy - 0.20f * h)
|
||||
path.lineTo(cx + 0.44f * h, cy + 0.20f * h)
|
||||
path.moveTo(cx + 0.44f * h, cy - 0.20f * h)
|
||||
path.lineTo(cx + 0.10f * h, cy + 0.20f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
15 -> {
|
||||
path.moveTo(cx - 0.72f * h, cy)
|
||||
path.lineTo(cx + 0.44f * h, cy)
|
||||
path.moveTo(cx + 0.14f * h, cy - 0.30f * h)
|
||||
path.lineTo(cx + 0.46f * h, cy)
|
||||
path.lineTo(cx + 0.14f * h, cy + 0.30f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx + 0.70f * h, cy - 0.48f * h)
|
||||
path.lineTo(cx + 0.70f * h, cy + 0.48f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
57 -> {
|
||||
path.moveTo(cx - 0.64f * h, cy - 0.22f * h)
|
||||
path.lineTo(cx - 0.64f * h, cy + 0.20f * h)
|
||||
path.lineTo(cx + 0.64f * h, cy + 0.20f * h)
|
||||
path.lineTo(cx + 0.64f * h, cy - 0.22f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
42, 54 -> {
|
||||
paint.style = Paint.Style.FILL
|
||||
path.moveTo(cx, cy - 0.55f * h)
|
||||
path.lineTo(cx + 0.62f * h, cy + 0.02f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.02f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.32f * h)
|
||||
path.lineTo(cx - 0.26f * h, cy + 0.32f * h)
|
||||
path.lineTo(cx - 0.26f * h, cy + 0.02f * h)
|
||||
path.lineTo(cx - 0.62f * h, cy + 0.02f * h)
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx - 0.26f * h, cy + 0.44f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.44f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.58f * h)
|
||||
path.lineTo(cx - 0.26f * h, cy + 0.58f * h)
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
29, 97 -> {
|
||||
path.moveTo(cx - 0.58f * h, cy + 0.26f * h)
|
||||
path.lineTo(cx, cy - 0.28f * h)
|
||||
path.lineTo(cx + 0.58f * h, cy + 0.26f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
125 -> {
|
||||
paint.style = Paint.Style.FILL
|
||||
val gap = 0.07f * h
|
||||
val side = 0.34f * h
|
||||
for (dxi in -1..1 step 2) for (dyi in -1..1 step 2) {
|
||||
val left = cx + dxi * (gap + side / 2f) - side / 2f
|
||||
val top = cy + dyi * (gap + side / 2f) - side / 2f
|
||||
path.addRect(left, top, left + side, top + side, Path.Direction.CW)
|
||||
}
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
103, 108, 105, 106 -> {
|
||||
paint.style = Paint.Style.FILL
|
||||
val r = 0.52f * h
|
||||
when (code) {
|
||||
103 -> {
|
||||
path.moveTo(cx, cy - r)
|
||||
path.lineTo(cx + r * 0.80f, cy + r * 0.55f)
|
||||
path.lineTo(cx - r * 0.80f, cy + r * 0.55f)
|
||||
}
|
||||
|
||||
108 -> {
|
||||
path.moveTo(cx, cy + r)
|
||||
path.lineTo(cx + r * 0.80f, cy - r * 0.55f)
|
||||
path.lineTo(cx - r * 0.80f, cy - r * 0.55f)
|
||||
}
|
||||
|
||||
105 -> {
|
||||
path.moveTo(cx - r, cy)
|
||||
path.lineTo(cx + r * 0.55f, cy - r * 0.80f)
|
||||
path.lineTo(cx + r * 0.55f, cy + r * 0.80f)
|
||||
}
|
||||
|
||||
else -> {
|
||||
path.moveTo(cx + r, cy)
|
||||
path.lineTo(cx - r * 0.55f, cy - r * 0.80f)
|
||||
path.lineTo(cx - r * 0.55f, cy + r * 0.80f)
|
||||
}
|
||||
}
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
}
|
||||
paint.style = Paint.Style.FILL
|
||||
paint.strokeCap = Paint.Cap.BUTT
|
||||
paint.strokeJoin = Paint.Join.MITER
|
||||
return true
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,140 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private val ICP_NAMES: Map<Int, String> = mapOf(
|
||||
103 to "KEY_UP", 106 to "KEY_RIGHT", 108 to "KEY_DOWN", 105 to "KEY_LEFT",
|
||||
28 to "KEY_ENTER", 1 to "KEY_ESC", 14 to "KEY_BKSP", 111 to "KEY_DEL", 15 to "KEY_TAB",
|
||||
57 to "KEY_SPACE", 29 to "KEY_CTRL_L", 97 to "KEY_CTRL_R", 110 to "KEY_INSERT",
|
||||
42 to "KEY_SHIFT_L", 54 to "KEY_SHIFT_R", 56 to "KEY_ALT_L", 100 to "KEY_ALT_R",
|
||||
102 to "KEY_HOME", 107 to "KEY_END", 99 to "KEY_PRTSCN", 104 to "KEY_PG_UP", 109 to "KEY_PG_DOWN",
|
||||
58 to "KEY_CAPS_LOCK", 69 to "KEY_NUM_LOCK", 41 to "KEY_GRAVE", 12 to "KEY_MINUS",
|
||||
11 to "KEY_0", 2 to "KEY_1", 3 to "KEY_2", 4 to "KEY_3", 5 to "KEY_4", 6 to "KEY_5",
|
||||
7 to "KEY_6", 8 to "KEY_7", 9 to "KEY_8", 10 to "KEY_9",
|
||||
30 to "KEY_A", 48 to "KEY_B", 46 to "KEY_C", 32 to "KEY_D", 18 to "KEY_E", 33 to "KEY_F",
|
||||
34 to "KEY_G", 35 to "KEY_H", 23 to "KEY_I", 36 to "KEY_J", 37 to "KEY_K", 38 to "KEY_L",
|
||||
50 to "KEY_M", 49 to "KEY_N", 24 to "KEY_O", 25 to "KEY_P", 16 to "KEY_Q", 19 to "KEY_R",
|
||||
31 to "KEY_S", 20 to "KEY_T", 22 to "KEY_U", 47 to "KEY_V", 17 to "KEY_W", 45 to "KEY_X",
|
||||
21 to "KEY_Y", 44 to "KEY_Z",
|
||||
26 to "KEY_BRACKET_LEFT", 27 to "KEY_BRACKET_RIGHT", 43 to "KEY_BACKSLASH", 53 to "KEY_SLASH",
|
||||
39 to "KEY_SEMICOLON", 51 to "KEY_COMMA", 52 to "KEY_PERIOD", 40 to "KEY_APOSTROPHE",
|
||||
59 to "KEY_F1", 60 to "KEY_F2", 61 to "KEY_F3", 62 to "KEY_F4", 63 to "KEY_F5", 64 to "KEY_F6",
|
||||
65 to "KEY_F7", 66 to "KEY_F8", 67 to "KEY_F9", 68 to "KEY_F10", 87 to "KEY_F11", 88 to "KEY_F12",
|
||||
79 to "KEY_KP_1", 80 to "KEY_KP_2", 81 to "KEY_KP_3", 75 to "KEY_KP_4", 76 to "KEY_KP_5",
|
||||
77 to "KEY_KP_6", 71 to "KEY_KP_7", 72 to "KEY_KP_8", 73 to "KEY_KP_9", 82 to "KEY_KP_0",
|
||||
)
|
||||
|
||||
private val ICP_CODES: Map<String, Int> = ICP_NAMES.entries.associate { (code, name) -> name to code }
|
||||
|
||||
private fun icpShape(shape: Int): String = when (shape) {
|
||||
SCREEN_KEY_CIRCLE -> "CIRCLE"
|
||||
SCREEN_KEY_RECT -> "RECT"
|
||||
SCREEN_KEY_ROUND -> "ROUND_RECT"
|
||||
SCREEN_KEY_DPAD -> "ROUND_RECT"
|
||||
SCREEN_KEY_STICK -> "ROUND_RECT"
|
||||
SCREEN_KEY_TRIGGER -> "ROUND_RECT"
|
||||
else -> "ROUND_RECT"
|
||||
}
|
||||
|
||||
private fun screenKeyShape(shape: String): Int = when (shape) {
|
||||
"CIRCLE" -> SCREEN_KEY_CIRCLE
|
||||
"RECT" -> SCREEN_KEY_RECT
|
||||
"SQUARE" -> SCREEN_KEY_ROUND
|
||||
else -> SCREEN_KEY_PILL
|
||||
}
|
||||
|
||||
fun exportScreenLayout(keys: List<ScreenKey>, widthPx: Int, density: Float): String {
|
||||
val snap = widthPx / 100f
|
||||
val elements = JSONArray()
|
||||
keys.forEach { key ->
|
||||
val binds = JSONArray()
|
||||
val directional = key.shape == SCREEN_KEY_DPAD || key.shape == SCREEN_KEY_STICK
|
||||
key.codes.forEach { code ->
|
||||
val name = ICP_NAMES[code]
|
||||
if (name != null) binds.put(name) else if (directional) binds.put("")
|
||||
}
|
||||
if (binds.length() == 0) return@forEach
|
||||
val shape = icpShape(key.shape)
|
||||
val sizePx = key.size * density
|
||||
val scale = when {
|
||||
key.shape == SCREEN_KEY_DPAD -> sizePx * 2.3f / (14f * snap)
|
||||
key.shape == SCREEN_KEY_STICK -> sizePx * 2.3f / (14f * snap)
|
||||
shape == "CIRCLE" -> sizePx / (6f * snap)
|
||||
else -> sizePx / (4f * snap)
|
||||
}
|
||||
elements.put(
|
||||
JSONObject()
|
||||
.put(
|
||||
"type",
|
||||
when (key.shape) {
|
||||
SCREEN_KEY_DPAD -> "D_PAD"
|
||||
SCREEN_KEY_STICK -> "STICK"
|
||||
else -> "BUTTON"
|
||||
},
|
||||
)
|
||||
.put("shape", shape)
|
||||
.put("customColor", 0)
|
||||
.put("bindings", binds)
|
||||
.put("scale", scale)
|
||||
.put("x", key.x)
|
||||
.put("y", key.y)
|
||||
.put("toggleSwitch", key.toggle)
|
||||
.put("swipeable", false)
|
||||
.put("text", key.text)
|
||||
.put("iconId", 0)
|
||||
.apply { if (key.opacity < 1f) put("opacity", key.opacity) },
|
||||
)
|
||||
}
|
||||
return JSONObject()
|
||||
.put("id", 0)
|
||||
.put("name", "DroidDeck")
|
||||
.put("cursorSpeed", 1.5f)
|
||||
.put("elements", elements)
|
||||
.toString()
|
||||
}
|
||||
|
||||
fun importScreenLayout(raw: String, widthPx: Int, density: Float): List<ScreenKey>? {
|
||||
val root = runCatching { JSONObject(raw) }.getOrNull() ?: return null
|
||||
val elements = root.optJSONArray("elements") ?: return null
|
||||
val snap = widthPx / 100f
|
||||
val keys = mutableListOf<ScreenKey>()
|
||||
for (i in 0 until elements.length()) {
|
||||
val element = elements.optJSONObject(i) ?: continue
|
||||
val type = element.optString("type", "BUTTON")
|
||||
if (type != "BUTTON" && type != "D_PAD" && type != "STICK") continue
|
||||
val binds = element.optJSONArray("bindings") ?: continue
|
||||
val raw = (0 until binds.length()).map { ICP_CODES[binds.optString(it)] ?: 0 }
|
||||
val codes = if (type == "D_PAD" || type == "STICK") {
|
||||
raw.dropLastWhile { it == 0 }.ifEmpty { listOf(0) }
|
||||
} else {
|
||||
raw.filter { it != 0 }
|
||||
}
|
||||
if (codes.isEmpty()) continue
|
||||
val dpad = type == "D_PAD"
|
||||
val stick = type == "STICK"
|
||||
val shape = when {
|
||||
dpad -> SCREEN_KEY_DPAD
|
||||
stick -> SCREEN_KEY_STICK
|
||||
else -> screenKeyShape(element.optString("shape"))
|
||||
}
|
||||
val scale = element.optDouble("scale", 1.0).toFloat().takeIf { it > 0f } ?: 1f
|
||||
val sizePx = when {
|
||||
dpad || stick -> 14f * snap * scale / 2.3f
|
||||
shape == SCREEN_KEY_CIRCLE -> 6f * snap * scale
|
||||
else -> 4f * snap * scale
|
||||
}
|
||||
keys += ScreenKey(
|
||||
codes = codes,
|
||||
x = element.optDouble("x", 0.5).toFloat().coerceIn(0f, 1f),
|
||||
y = element.optDouble("y", 0.5).toFloat().coerceIn(0f, 1f),
|
||||
size = (sizePx / density).roundToInt().coerceIn(ScreenKeys.MIN_SIZE, ScreenKeys.MAX_SIZE),
|
||||
text = element.optString("text").take(24),
|
||||
opacity = element.optDouble("opacity", 1.0).toFloat().coerceIn(0.2f, 1f),
|
||||
shape = shape,
|
||||
toggle = element.optBoolean("toggleSwitch"),
|
||||
)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
@@ -93,7 +93,10 @@ class SteamTouchControls(
|
||||
private var opened = 0
|
||||
private var elements: List<Element> = emptyList()
|
||||
private var tint = Color.WHITE
|
||||
private var alphaScale = 0.45f
|
||||
private var alphaScale = 0.99f
|
||||
var style: Pair<Int, Float> = Color.WHITE to 0.99f
|
||||
private set
|
||||
var onStyle: (() -> Unit)? = null
|
||||
|
||||
private class Visual(val ref: SteamTouchBindings.Ref?, val binding: SteamTouchBindings.Binding?)
|
||||
private var visuals: Map<Int, List<Visual>> = emptyMap()
|
||||
@@ -191,6 +194,8 @@ class SteamTouchControls(
|
||||
private fun applyColor(color: FloatArray?) {
|
||||
tint = if (color != null) Color.rgb((color[0] * 255).toInt(), (color[1] * 255).toInt(), (color[2] * 255).toInt()) else Color.WHITE
|
||||
alphaScale = ((color?.get(3) ?: 0.45f) * 2.2f).coerceIn(0.15f, 1f)
|
||||
style = tint to alphaScale
|
||||
onStyle?.invoke()
|
||||
}
|
||||
|
||||
private fun rebuildVisuals() {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import com.droiddeck.launcher.R
|
||||
import androidx.annotation.StringRes
|
||||
import com.droiddeck.launcher.input.PC_KEY_ROWS
|
||||
import com.droiddeck.launcher.input.PC_MODIFIERS
|
||||
import com.droiddeck.launcher.input.PcKey
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import android.view.KeyEvent
|
||||
import androidx.compose.animation.animateColorAsState
|
||||
@@ -54,46 +56,6 @@ import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
private data class PcKey(val label: String, val code: Int, val weight: Float = 1f, val shifted: String? = null, @StringRes val labelRes: Int = 0)
|
||||
|
||||
private fun k(label: String, code: Int, weight: Float = 1f, shifted: String? = null) = PcKey(label, code, weight, shifted)
|
||||
|
||||
private fun k(@StringRes labelRes: Int, code: Int, weight: Float = 1f) = PcKey("", code, weight, null, labelRes)
|
||||
|
||||
private val ROWS: List<List<PcKey>> = listOf(
|
||||
listOf(
|
||||
k(R.string.pckb_key_esc, 1), k("F1", 59), k("F2", 60), k("F3", 61), k("F4", 62), k("F5", 63), k("F6", 64),
|
||||
k("F7", 65), k("F8", 66), k("F9", 67), k("F10", 68), k("F11", 87), k("F12", 88),
|
||||
k(R.string.pckb_key_ins, 110), k(R.string.pckb_key_del, 111), k(R.string.pckb_key_home, 102), k(R.string.pckb_key_end, 107), k(R.string.pckb_key_pgup, 104), k(R.string.pckb_key_pgdn, 109),
|
||||
),
|
||||
listOf(
|
||||
k("`", 41, shifted = "~"), k("1", 2, shifted = "!"), k("2", 3, shifted = "@"), k("3", 4, shifted = "#"),
|
||||
k("4", 5, shifted = "$"), k("5", 6, shifted = "%"), k("6", 7, shifted = "^"), k("7", 8, shifted = "&"),
|
||||
k("8", 9, shifted = "*"), k("9", 10, shifted = "("), k("0", 11, shifted = ")"), k("-", 12, shifted = "_"),
|
||||
k("=", 13, shifted = "+"), k(R.string.pckb_key_backspace, 14, 2f),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_tab, 15, 1.5f), k("q", 16), k("w", 17), k("e", 18), k("r", 19), k("t", 20), k("y", 21),
|
||||
k("u", 22), k("i", 23), k("o", 24), k("p", 25), k("[", 26, shifted = "{"), k("]", 27, shifted = "}"),
|
||||
k("\\", 43, 1.5f, shifted = "|"),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_caps, 58, 1.75f), k("a", 30), k("s", 31), k("d", 32), k("f", 33), k("g", 34), k("h", 35),
|
||||
k("j", 36), k("k", 37), k("l", 38), k(";", 39, shifted = ":"), k("'", 40, shifted = "\""),
|
||||
k(R.string.pckb_key_enter, 28, 2.25f),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_shift, 42, 2.25f), k("z", 44), k("x", 45), k("c", 46), k("v", 47), k("b", 48), k("n", 49),
|
||||
k("m", 50), k(",", 51, shifted = "<"), k(".", 52, shifted = ">"), k("/", 53, shifted = "?"),
|
||||
k(R.string.pckb_key_shift, 54, 1.75f), k("↑", 103),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_ctrl, 29, 1.5f), k(R.string.pckb_key_super, 125, 1.25f), k(R.string.pckb_key_alt, 56, 1.25f), k(R.string.pckb_key_space, 57, 6.25f),
|
||||
k(R.string.pckb_key_altgr, 100, 1.25f), k(R.string.pckb_key_ctrl, 97, 1.25f), k("←", 105), k("↓", 108), k("→", 106),
|
||||
),
|
||||
)
|
||||
|
||||
private val MODIFIERS = setOf(42, 54, 29, 97, 56, 100, 125)
|
||||
private const val MOD_OFF = 0
|
||||
private const val MOD_ONCE = 1
|
||||
private const val MOD_LOCKED = 2
|
||||
@@ -115,7 +77,7 @@ fun PcKeyboard(
|
||||
for ((code, state) in mods.toMap()) if (state == MOD_ONCE) { sendKey(code, false); mods[code] = MOD_OFF }
|
||||
}
|
||||
fun press(key: PcKey) {
|
||||
if (key.code in MODIFIERS) {
|
||||
if (key.code in PC_MODIFIERS) {
|
||||
when (mods[key.code] ?: MOD_OFF) {
|
||||
MOD_OFF -> { sendKey(key.code, true); mods[key.code] = MOD_ONCE }
|
||||
MOD_ONCE -> mods[key.code] = MOD_LOCKED
|
||||
@@ -126,7 +88,7 @@ fun PcKeyboard(
|
||||
sendKey(key.code, true)
|
||||
}
|
||||
fun release(key: PcKey) {
|
||||
if (key.code in MODIFIERS) return
|
||||
if (key.code in PC_MODIFIERS) return
|
||||
sendKey(key.code, false)
|
||||
if (key.code == 58) capsOn = !capsOn
|
||||
releaseOnceModifiers()
|
||||
@@ -153,7 +115,7 @@ fun PcKeyboard(
|
||||
Spacer(Modifier.width(8.dp))
|
||||
HeaderButton(stringResource(R.string.pckb_hide), onClose)
|
||||
}
|
||||
ROWS.forEachIndexed { r, row ->
|
||||
PC_KEY_ROWS.forEachIndexed { r, row ->
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(3.dp), modifier = Modifier.fillMaxWidth().height(rowHeight)) {
|
||||
row.forEachIndexed { i, key ->
|
||||
val state = mods[key.code] ?: MOD_OFF
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.gestures.detectDragGestures
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.offset
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.DragIndicator
|
||||
import androidx.compose.material.icons.filled.Image
|
||||
import androidx.compose.material.icons.filled.Keyboard
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Slider
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_CIRCLE
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_DPAD
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_PILL
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_RECT
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_ROUND
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_STICK
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_TRIGGER
|
||||
import com.droiddeck.launcher.input.ScreenKeys
|
||||
import com.droiddeck.launcher.input.pcKeyLabel
|
||||
import com.droiddeck.launcher.input.screenKeyDirections
|
||||
import java.io.File
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private val PANEL_WIDTH = 600.dp
|
||||
private val CHIP_PADDING = androidx.compose.foundation.layout.PaddingValues(horizontal = 6.dp, vertical = 2.dp)
|
||||
|
||||
@Composable
|
||||
fun ScreenKeyEditor(
|
||||
codes: List<Int>,
|
||||
keyY: Float,
|
||||
size: Int,
|
||||
opacity: Float,
|
||||
shape: Int,
|
||||
toggle: Boolean,
|
||||
labels: Boolean,
|
||||
text: String,
|
||||
hasImage: Boolean,
|
||||
onResize: (Int) -> Unit,
|
||||
onOpacity: (Float) -> Unit,
|
||||
onShape: (Int) -> Unit,
|
||||
onToggle: (Boolean) -> Unit,
|
||||
onLabels: (Boolean) -> Unit,
|
||||
onText: (String) -> Unit,
|
||||
onImage: (String) -> Unit,
|
||||
onClearImage: () -> Unit,
|
||||
onBind: () -> Unit,
|
||||
onTextFocus: () -> Unit,
|
||||
onTextBlur: () -> Unit,
|
||||
onDirection: (Int) -> Unit,
|
||||
onRemove: () -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val p = LocalPalette.current
|
||||
val context = LocalContext.current
|
||||
var offset by remember(codes) { mutableStateOf(Offset.Zero) }
|
||||
val density = LocalDensity.current
|
||||
val screen = LocalConfiguration.current
|
||||
val panelWidthPx = with(density) { PANEL_WIDTH.toPx() }
|
||||
val screenWidthPx = screen.screenWidthDp * density.density
|
||||
val screenHeightPx = screen.screenHeightDp * density.density
|
||||
val atTop = keyY > 0.45f
|
||||
val button = shape <= SCREEN_KEY_RECT
|
||||
val directions = screenKeyDirections(shape)
|
||||
val pickImage = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri != null) importImage(context, uri, codes)?.let(onImage)
|
||||
}
|
||||
Box(
|
||||
Modifier.fillMaxSize().systemBarsPadding().padding(horizontal = 10.dp, vertical = 6.dp),
|
||||
contentAlignment = if (atTop) Alignment.TopCenter else Alignment.BottomCenter,
|
||||
) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = PANEL_WIDTH).fillMaxWidth()
|
||||
.offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) },
|
||||
shape = RoundedCornerShape(18.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column(Modifier.padding(start = 10.dp, end = 6.dp, top = 2.dp, bottom = 6.dp)) {
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
Icons.Default.DragIndicator, null,
|
||||
tint = p.onBackground.copy(alpha = 0.45f),
|
||||
modifier = Modifier.pointerInput(panelWidthPx, screenWidthPx, screenHeightPx) {
|
||||
detectDragGestures { change, drag ->
|
||||
change.consume()
|
||||
offset = Offset(
|
||||
(offset.x + drag.x).coerceIn(
|
||||
-(screenWidthPx - panelWidthPx) / 2f + 8f,
|
||||
(screenWidthPx - panelWidthPx) / 2f - 8f,
|
||||
),
|
||||
(offset.y + drag.y).coerceIn(
|
||||
-screenHeightPx * if (atTop) 0f else 0.75f,
|
||||
screenHeightPx * if (atTop) 0.75f else 0f,
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
Box(
|
||||
Modifier.weight(1f).height(36.dp)
|
||||
.background(p.line2.copy(alpha = 0.24f), RoundedCornerShape(9.dp))
|
||||
.padding(horizontal = 10.dp),
|
||||
contentAlignment = Alignment.CenterStart,
|
||||
) {
|
||||
BasicTextField(
|
||||
value = text,
|
||||
onValueChange = { onText(it.take(24)) },
|
||||
singleLine = true,
|
||||
textStyle = TextStyle(
|
||||
color = p.onBackground, fontSize = 13.sp,
|
||||
fontWeight = FontWeight.Medium,
|
||||
),
|
||||
cursorBrush = androidx.compose.ui.graphics.SolidColor(p.primary),
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
|
||||
modifier = Modifier.fillMaxWidth().onFocusChanged {
|
||||
if (it.isFocused) onTextFocus() else onTextBlur()
|
||||
},
|
||||
)
|
||||
if (text.isEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.screen_key_text),
|
||||
color = p.onBackground.copy(alpha = 0.4f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (button) {
|
||||
IconButton(onClick = onBind, modifier = Modifier.width(38.dp)) {
|
||||
Icon(
|
||||
Icons.Default.Keyboard, stringResource(R.string.screen_key_bind),
|
||||
tint = p.onBackground.copy(alpha = 0.8f),
|
||||
)
|
||||
}
|
||||
}
|
||||
IconButton(onClick = { pickImage.launch("image/*") }, modifier = Modifier.width(38.dp)) {
|
||||
Icon(
|
||||
Icons.Default.Image, stringResource(R.string.screen_key_image),
|
||||
tint = p.onBackground.copy(alpha = 0.8f),
|
||||
)
|
||||
}
|
||||
if (hasImage) IconButton(onClick = onClearImage, modifier = Modifier.width(34.dp)) {
|
||||
Icon(Icons.Default.Close, stringResource(R.string.screen_key_image_clear), tint = p.error)
|
||||
}
|
||||
IconButton(onClick = onRemove, modifier = Modifier.width(38.dp)) {
|
||||
Icon(Icons.Default.Delete, stringResource(R.string.common_delete), tint = p.error)
|
||||
}
|
||||
IconButton(onClick = onDismiss, modifier = Modifier.width(34.dp)) {
|
||||
Icon(Icons.Default.Close, null, tint = p.onBackground)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(vertical = 2.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
listOf<Pair<Int?, Int>>(
|
||||
(if (button) SCREEN_KEY_PILL else null) to R.string.screen_key_type_button,
|
||||
SCREEN_KEY_TRIGGER to R.string.screen_key_shape_trigger,
|
||||
SCREEN_KEY_DPAD to R.string.touch_dpad,
|
||||
SCREEN_KEY_STICK to R.string.deck_surface_stick,
|
||||
).forEach { (value, label) ->
|
||||
val selected = if (value == null) button else shape == value
|
||||
Chip(stringResource(label), selected, Modifier.weight(1f)) {
|
||||
onShape(if (value == null) (if (button) shape else SCREEN_KEY_PILL) else value)
|
||||
}
|
||||
}
|
||||
Box(Modifier.width(1.dp).height(20.dp).background(p.line2))
|
||||
listOf(
|
||||
SCREEN_KEY_PILL to R.string.screen_key_shape_pill,
|
||||
SCREEN_KEY_ROUND to R.string.screen_key_shape_round,
|
||||
SCREEN_KEY_CIRCLE to R.string.screen_key_shape_circle,
|
||||
SCREEN_KEY_RECT to R.string.screen_key_shape_rect,
|
||||
).forEach { (value, label) ->
|
||||
Chip(
|
||||
stringResource(label), button && shape == value, Modifier.weight(1f), enabled = button,
|
||||
) { onShape(value) }
|
||||
}
|
||||
Box(Modifier.width(1.dp).height(20.dp).background(p.line2))
|
||||
Text(
|
||||
stringResource(R.string.screen_key_toggle), Modifier.padding(start = 6.dp, end = 2.dp),
|
||||
style = MaterialTheme.typography.bodySmall, maxLines = 1,
|
||||
)
|
||||
Switch(checked = toggle, onCheckedChange = onToggle, modifier = Modifier.height(30.dp))
|
||||
}
|
||||
if (directions.isNotEmpty()) {
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(vertical = 2.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
directions.forEachIndexed { index, arrow ->
|
||||
val code = codes.getOrNull(index) ?: 0
|
||||
Chip(
|
||||
if (code == 0) arrow else "$arrow ${pcKeyLabel(context, code)}",
|
||||
code != 0, Modifier.weight(1f),
|
||||
) { onDirection(index) }
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.screen_key_text), Modifier.padding(start = 6.dp, end = 2.dp),
|
||||
style = MaterialTheme.typography.bodySmall, maxLines = 1,
|
||||
)
|
||||
Switch(checked = labels, onCheckedChange = onLabels, modifier = Modifier.height(30.dp))
|
||||
}
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(top = 2.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
KeySlider(
|
||||
label = stringResource(R.string.screen_key_size),
|
||||
value = "$size",
|
||||
current = size.toFloat(),
|
||||
min = ScreenKeys.MIN_SIZE.toFloat(),
|
||||
max = ScreenKeys.MAX_SIZE.toFloat(),
|
||||
modifier = Modifier.weight(1f),
|
||||
) { onResize(it.roundToInt()) }
|
||||
KeySlider(
|
||||
label = stringResource(R.string.screen_key_opacity),
|
||||
value = "${(opacity * 100).roundToInt()}%",
|
||||
current = opacity,
|
||||
min = 0.2f,
|
||||
max = 1f,
|
||||
modifier = Modifier.weight(1f),
|
||||
onChange = onOpacity,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun KeySlider(
|
||||
label: String,
|
||||
value: String,
|
||||
current: Float,
|
||||
min: Float,
|
||||
max: Float,
|
||||
modifier: Modifier,
|
||||
onChange: (Float) -> Unit,
|
||||
) {
|
||||
Row(modifier, verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(label, style = MaterialTheme.typography.bodySmall, maxLines = 1)
|
||||
Slider(current, onChange, Modifier.weight(1f).height(28.dp), valueRange = min..max)
|
||||
Text(
|
||||
value, Modifier.width(32.dp), style = MaterialTheme.typography.bodySmall,
|
||||
fontWeight = FontWeight.SemiBold, maxLines = 1,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Chip(
|
||||
label: String,
|
||||
selected: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
enabled: Boolean = true,
|
||||
onClick: () -> Unit,
|
||||
) {
|
||||
val p = LocalPalette.current
|
||||
Box(
|
||||
modifier
|
||||
.background(
|
||||
if (selected) p.primary.copy(alpha = 0.18f) else p.line2.copy(alpha = 0.28f),
|
||||
CircleShape,
|
||||
)
|
||||
.clickable(enabled = enabled, onClick = onClick)
|
||||
.padding(vertical = 6.dp),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
label, style = MaterialTheme.typography.labelSmall, maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
color = when {
|
||||
!enabled -> p.onBackground.copy(alpha = 0.28f)
|
||||
selected -> p.primary
|
||||
else -> p.onBackground.copy(alpha = 0.7f)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun importImage(context: Context, uri: Uri, codes: List<Int>): String? {
|
||||
val dir = File(context.filesDir, ScreenKeys.IMAGE_DIR).apply { mkdirs() }
|
||||
val name = "key_" + codes.joinToString("_") + "_" + System.currentTimeMillis() + ".png"
|
||||
val longest = (ScreenKeys.MAX_SIZE * context.resources.displayMetrics.density).toInt()
|
||||
return runCatching {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) }
|
||||
val source = maxOf(bounds.outWidth, bounds.outHeight)
|
||||
if (source <= 0) return@runCatching null
|
||||
var sample = 1
|
||||
while (source / (sample * 2) >= longest) sample *= 2
|
||||
val bitmap = context.contentResolver.openInputStream(uri)?.use {
|
||||
BitmapFactory.decodeStream(it, null, BitmapFactory.Options().apply { inSampleSize = sample })
|
||||
} ?: return@runCatching null
|
||||
val scale = longest.toFloat() / maxOf(bitmap.width, bitmap.height)
|
||||
val scaled = if (scale < 1f) {
|
||||
Bitmap.createScaledBitmap(
|
||||
bitmap,
|
||||
(bitmap.width * scale).toInt().coerceAtLeast(1),
|
||||
(bitmap.height * scale).toInt().coerceAtLeast(1),
|
||||
true,
|
||||
)
|
||||
} else {
|
||||
bitmap
|
||||
}
|
||||
try {
|
||||
val saved = File(dir, name).outputStream().use { scaled.compress(Bitmap.CompressFormat.PNG, 100, it) }
|
||||
if (saved) name else null
|
||||
} finally {
|
||||
if (scaled !== bitmap) bitmap.recycle()
|
||||
scaled.recycle()
|
||||
}
|
||||
}.getOrNull().also { if (it == null) File(dir, name).delete() }
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.PC_KEYS
|
||||
import com.droiddeck.launcher.input.PC_KEY_ROWS
|
||||
import com.droiddeck.launcher.input.PC_MODIFIERS
|
||||
import com.droiddeck.launcher.input.PcKey
|
||||
import com.droiddeck.launcher.input.pcKeyLabel
|
||||
|
||||
@Composable
|
||||
fun ScreenKeyPicker(
|
||||
onDismiss: () -> Unit,
|
||||
onAdd: (List<Int>) -> Unit,
|
||||
initial: List<Int> = emptyList(),
|
||||
sequence: Boolean = false,
|
||||
sequenceLimit: Int = 4,
|
||||
slots: List<String> = emptyList(),
|
||||
) {
|
||||
var picked by remember { mutableStateOf(initial) }
|
||||
val p = LocalPalette.current
|
||||
val toggle: (Int) -> Unit = { code ->
|
||||
picked = if (sequence) {
|
||||
if (code in picked) picked - code else (picked + code).take(sequenceLimit)
|
||||
} else if (code in PC_MODIFIERS) {
|
||||
if (code in picked) picked - code else picked + code
|
||||
} else {
|
||||
picked.filter { it in PC_MODIFIERS } + code
|
||||
}
|
||||
}
|
||||
val rows = remember {
|
||||
val functionRow = PC_KEY_ROWS[0]
|
||||
val nav = listOfNotNull(PC_KEYS[110], PC_KEYS[111], PC_KEYS[102], PC_KEYS[107], PC_KEYS[104], PC_KEYS[109]) +
|
||||
listOfNotNull(PC_KEYS[103], PC_KEYS[105], PC_KEYS[108], PC_KEYS[106])
|
||||
listOf(functionRow.take(13), nav) + PC_KEY_ROWS.drop(1)
|
||||
}
|
||||
val unit = remember { rows.maxOf { row -> row.fold(0f) { sum, key -> sum + key.weight } } }
|
||||
Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().padding(10.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = 820.dp).fillMaxWidth().fillMaxHeight(), shape = RoundedCornerShape(20.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column {
|
||||
Row(Modifier.fillMaxWidth().padding(start = 16.dp, end = 6.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(stringResource(R.string.screen_keys_add), Modifier.weight(1f), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
IconButton(onClick = onDismiss) { Icon(Icons.Default.Close, null) }
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth().heightIn(min = 26.dp).horizontalScroll(rememberScrollState()).padding(horizontal = 16.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
if (sequence && slots.size >= sequenceLimit) {
|
||||
slots.take(sequenceLimit).forEachIndexed { index, arrow ->
|
||||
val code = picked.getOrNull(index)
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
color = if (code == null) p.line2.copy(alpha = 0.2f) else p.primary.copy(alpha = 0.16f),
|
||||
modifier = if (code == null) Modifier else Modifier.clickable { picked = picked - code },
|
||||
) {
|
||||
Text(
|
||||
if (code == null) arrow else "$arrow ${pcKeyLabel(LocalContext.current, code)}",
|
||||
Modifier.padding(horizontal = 8.dp, vertical = 2.dp),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
picked.forEach { code ->
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp), color = p.primary.copy(alpha = 0.16f),
|
||||
modifier = Modifier.clickable { picked = picked - code },
|
||||
) {
|
||||
Text(pcKeyLabel(LocalContext.current, code), Modifier.padding(horizontal = 8.dp, vertical = 2.dp), style = MaterialTheme.typography.labelMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
LazyColumn(Modifier.weight(1f).fillMaxWidth(), contentPadding = PaddingValues(vertical = 6.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
items(rows.size) { index ->
|
||||
val row = rows[index]
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
) {
|
||||
row.forEach { key -> KeyCell(key, key.code in picked, Modifier.weight(key.weight)) { toggle(key.code) } }
|
||||
if (row.sumOf { it.weight.toDouble() } < unit) {
|
||||
Spacer(Modifier.weight(unit - row.fold(0f) { sum, key -> sum + key.weight }))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Button(
|
||||
onClick = { onAdd(picked) },
|
||||
enabled = picked.isNotEmpty(),
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
Text(stringResource(R.string.screen_keys_add))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun KeyCell(key: PcKey, selected: Boolean, modifier: Modifier, onClick: () -> Unit) {
|
||||
val p = LocalPalette.current
|
||||
val label = pcKeyLabel(LocalContext.current, key.code)
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
color = if (selected) p.primary.copy(alpha = 0.2f) else p.surface,
|
||||
border = BorderStroke(1.dp, if (selected) p.primary else p.line),
|
||||
modifier = modifier.height(36.dp).clickable(onClick = onClick),
|
||||
) {
|
||||
Box(Modifier.fillMaxSize().padding(horizontal = 2.dp), contentAlignment = Alignment.Center) {
|
||||
Text(
|
||||
label, style = MaterialTheme.typography.labelMedium, maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
fontSize = if (label.length > 4) 10.sp else 12.sp,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import com.droiddeck.launcher.R
|
||||
|
||||
@Composable
|
||||
fun ScreenLayoutPanel(
|
||||
layouts: List<String>,
|
||||
onSave: (String) -> Unit,
|
||||
onLoad: (String) -> Unit,
|
||||
onDelete: (String) -> Unit,
|
||||
onImport: (String) -> Unit,
|
||||
exportText: () -> String,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val p = LocalPalette.current
|
||||
val context = LocalContext.current
|
||||
var naming by remember { mutableStateOf(false) }
|
||||
var name by remember { mutableStateOf("") }
|
||||
|
||||
val importer = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
val text = runCatching {
|
||||
context.contentResolver.openInputStream(uri)?.use { it.readBytes().toString(Charsets.UTF_8) }
|
||||
}.getOrNull()
|
||||
if (text != null) onImport(text)
|
||||
}
|
||||
val exporter = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
runCatching { context.contentResolver.openOutputStream(uri)?.use { it.write(exportText().toByteArray()) } }
|
||||
}
|
||||
|
||||
Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column(Modifier.padding(start = 20.dp, end = 8.dp, top = 6.dp, bottom = 12.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
stringResource(R.string.screen_key_layout), Modifier.weight(1f),
|
||||
style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
IconButton(onClick = onDismiss) { Icon(Icons.Default.Close, null) }
|
||||
}
|
||||
Row(Modifier.fillMaxWidth().padding(end = 12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
TextButton(onClick = { naming = true }) { Text(stringResource(R.string.screen_key_layout_save)) }
|
||||
TextButton(onClick = { importer.launch(arrayOf("application/json", "text/plain", "*/*")) }) {
|
||||
Text(stringResource(R.string.boot_movie_import))
|
||||
}
|
||||
TextButton(onClick = { exporter.launch("droiddeck-layout.icp") }) {
|
||||
Text(stringResource(R.string.screen_key_layout_export))
|
||||
}
|
||||
}
|
||||
Column(Modifier.padding(end = 12.dp).heightIn(max = 320.dp).verticalScroll(rememberScrollState())) {
|
||||
layouts.forEach { layout ->
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
layout, Modifier.weight(1f).clickable { onLoad(layout) }.padding(vertical = 14.dp),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
)
|
||||
TextButton(onClick = { onDelete(layout) }) {
|
||||
Text(stringResource(R.string.common_delete), color = p.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (naming) {
|
||||
Dialog(onDismissRequest = { naming = false }, properties = DialogProperties(usePlatformDefaultWidth = false)) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
OutlinedTextField(
|
||||
name, { name = it.take(24) }, Modifier.fillMaxWidth(), singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
|
||||
)
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
TextButton(onClick = { naming = false }) { Text(stringResource(R.string.common_cancel)) }
|
||||
Button(onClick = { onSave(name.trim()); naming = false }, enabled = name.isNotBlank()) {
|
||||
Text(stringResource(R.string.screen_key_layout_save))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -261,6 +261,10 @@ class DrawerActions(
|
||||
val onLowLatencyTouch: (Boolean) -> Unit = {},
|
||||
val onTouch: (String) -> Unit,
|
||||
val onGestureSettings: () -> Unit = {},
|
||||
val onScreenKeys: (() -> Unit)? = null,
|
||||
val onScreenLayouts: (() -> Unit)? = null,
|
||||
val screenKeysShown: Boolean = true,
|
||||
val onScreenKeysShown: (Boolean) -> Unit = {},
|
||||
val onFexPreset: (String) -> Unit,
|
||||
val onUpscaler: (Int) -> Unit = {},
|
||||
val onUpscaleSharpness: (Int) -> Unit = {},
|
||||
@@ -555,6 +559,23 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
|
||||
ActionRow(stringResource(R.string.gesture_settings), stringResource(R.string.gesture_settings_hint),
|
||||
stringResource(R.string.ctrl_edit), a.onGestureSettings,
|
||||
modifier = focus.track(page, "gesture-settings"))
|
||||
if (a.onScreenKeys != null) {
|
||||
SettingsRow(stringResource(R.string.screen_keys), null) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
) {
|
||||
SecondaryButton(stringResource(R.string.ctrl_edit),
|
||||
modifier = focus.track(page, "screen-keys"), onClick = a.onScreenKeys)
|
||||
a.onScreenLayouts?.let { open ->
|
||||
SecondaryButton(stringResource(R.string.screen_key_layout),
|
||||
modifier = focus.track(page, "screen-key-layouts"), onClick = open)
|
||||
}
|
||||
ToggleSwitch(a.screenKeysShown, true,
|
||||
modifier = focus.track(page, "screen-keys-shown"), onChange = a.onScreenKeysShown)
|
||||
}
|
||||
}
|
||||
}
|
||||
ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null,
|
||||
if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never))
|
||||
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)),
|
||||
|
||||
Reference in new issue
Block a user