Overrides on hands in auto

This commit is contained in:
loucass003 committed 2026-08-05 14:02:43 +02:00
1 parent d18ad4f8e2
commit 0c2b8947dd
10 files changed
+173 -153

No files matched your search

@@ -48,7 +48,8 @@ data class BoneRoutingConfig(
/** Generate the routes from the connected trackers and the output priority. */
val automatic: Boolean = true,
/**
* Explicit routes, ignored while [automatic]. Do not read directly, use BoneRoutingManager.
* Explicit routes. Ignored while [automatic], apart from the overridable bones, which
* the user owns in either mode. Do not read directly, use BoneRoutingManager.
*/
val manualRoutes: Map<BodyPart, Set<RoutingOutput>>? = null,
)
@@ -54,10 +54,13 @@ class BoneRoutingBasicBehaviour(private val appContext: AppContextProvider) : Bo
.map { fineBodyParts -> Triple(config, outputStates, fineBodyParts) }
}
.onEach { (config, outputStates, fineBodyParts) ->
val candidates = determineCandidateBones(config, fineBodyParts)
val candidates = determineCandidateBones(fineBodyParts)
receiver.context.dispatch(
BoneRoutingActions.SetRoutes(
effectiveRoutes(computeAutomaticRoutes(candidates, outputStates), outputStates),
effectiveRoutes(
computeAutomaticRoutes(candidates, outputStates) + overrideRoutes(config),
outputStates,
),
),
)
}
@@ -42,6 +42,17 @@ fun requiredBones(output: RoutingOutput): Set<BodyPart> = when (output) {
RoutingOutput.VMC -> VMC_SUPPORTED_BONES
}
/**
* Bones automatic never routes on its own. Sending a hand to the driver takes over the
* real controllers, so it stays the user's call whatever the mode.
*/
val OVERRIDABLE_BONES: Set<BodyPart> = setOf(BodyPart.LEFT_HAND, BodyPart.RIGHT_HAND)
fun overridableBones(output: RoutingOutput): Set<BodyPart> =
OVERRIDABLE_BONES intersect acceptedBones(output) subtract requiredBones(output)
fun overrideRoutes(config: BoneRoutingConfig): Routes = config.manualRoutes.orEmpty().filterKeys { it in OVERRIDABLE_BONES }
fun isActive(states: OutputStates, output: RoutingOutput): Boolean = states[output] == RoutingOutputState.ACTIVE
fun platformSupports(appContext: AppContextProvider, output: RoutingOutput): Boolean = when (output) {
@@ -89,7 +100,8 @@ fun intendedRoutesFlow(appContext: AppContextProvider): Flow<Routes> = combine(
appContext.config.settings.context.state.map { it.data.boneRoutingConfig }.distinctUntilChanged(),
appContext.boneRouting.context.state.map { it.routes },
) { config, sent ->
effectiveRoutes(if (config.automatic) sent else config.manualRoutes.orEmpty(), ALL_ACTIVE)
val requested = if (config.automatic) sent else config.manualRoutes.orEmpty()
effectiveRoutes(requested + overrideRoutes(config), ALL_ACTIVE)
}.distinctUntilChanged()
/**
@@ -113,41 +125,27 @@ fun effectiveRoutes(routes: Routes, outputStates: OutputStates): Routes {
return result
}
/**
* The table a first switch to manual starts from: what automatic would route if every
* output this platform has were switched on. An output that is merely off still gets its
* bones, so switching it on later actually sends something.
*
* TODO: maybe this is bad, or annoying logic. we could default to just empty routes when switching to manual
*/
fun seedManualRoutes(appContext: AppContextProvider): Routes {
val config = appContext.config.settings.context.state.value.data.boneRoutingConfig
val trackers = appContext.server.context.state.value.trackers.values.map { it.context.state.value }
val allEnabled = RoutingOutput.entries.associateWith {
if (platformSupports(appContext, it)) RoutingOutputState.ACTIVE else RoutingOutputState.UNSUPPORTED
}
return computeAutomaticRoutes(determineCandidateBones(config, trackedBodyParts(trackers)), allEnabled)
}
fun applyRoutingChange(
config: BoneRoutingConfig,
automatic: Boolean,
routes: Routes,
seed: Routes,
): BoneRoutingConfig {
fun sanitized(source: Routes): Routes = source
// Switching modes only flips the switch: each mode keeps the picks left in it.
if (automatic != config.automatic) return config.copy(automatic = automatic)
val requested = routes
.mapValues { (bone, outputs) ->
outputs.filterTo(mutableSetOf()) { bone in acceptedBones(it) && bone !in requiredBones(it) }
}
.filterValues { it.isNotEmpty() }
return config.copy(
automatic = automatic,
manualRoutes = when {
automatic -> config.manualRoutes
config.automatic -> config.manualRoutes ?: sanitized(seed)
else -> sanitized(routes)
manualRoutes = if (automatic) {
// Automatic owns every other bone, so only the overrides come from the request.
config.manualRoutes.orEmpty().filterKeys { it !in OVERRIDABLE_BONES } +
requested.filterKeys { it in OVERRIDABLE_BONES }
} else {
requested
},
)
}
@@ -173,16 +171,9 @@ fun trackedBodyParts(trackers: Collection<TrackerState>): Set<BodyPart?> = track
.map { it.bodyPart }
.toSet()
fun determineCandidateBones(config: BoneRoutingConfig, fineBodyParts: Set<BodyPart?>): Set<BodyPart> {
val candidates = candidateToFineBodyParts
.filterValues { it.any { bp -> bp in fineBodyParts } }
.keys
// Hands aren't toggled automatically
val handBones = config.manualRoutes.orEmpty().keys.filter {
it == BodyPart.LEFT_HAND || it == BodyPart.RIGHT_HAND
}
return candidates + handBones
}
fun determineCandidateBones(fineBodyParts: Set<BodyPart?>): Set<BodyPart> = candidateToFineBodyParts
.filterValues { it.any { bp -> bp in fineBodyParts } }
.keys
/** Hands each bone to the best output that is on and can take it, so nothing is sent twice. */
fun computeAutomaticRoutes(candidateBones: Set<BodyPart>, outputStates: OutputStates): Routes {
@@ -9,8 +9,8 @@ import dev.slimevr.routing.applyRoutingChange
import dev.slimevr.routing.conflictingOutputs
import dev.slimevr.routing.intendedRoutesFlow
import dev.slimevr.routing.outputStatesFlow
import dev.slimevr.routing.overridableBones
import dev.slimevr.routing.requiredBones
import dev.slimevr.routing.seedManualRoutes
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
@@ -43,6 +43,7 @@ private fun buildResponse(
output = output,
accepts = acceptedBones(output).toList(),
requires = requiredBones(output).toList(),
overridable = overridableBones(output).toList(),
conflicts = conflictingOutputs(output).toList(),
state = outputStates[output],
)
@@ -79,7 +80,6 @@ class BoneRoutingBehaviour(
bone to outputs
}
.toMap()
val seed = seedManualRoutes(appContext)
settings.context.dispatch(
SettingsActions.Update {
@@ -88,7 +88,6 @@ class BoneRoutingBehaviour(
config = boneRoutingConfig,
automatic = req.automatic == true,
routes = requested,
seed = seed,
),
)
},
@@ -148,7 +148,6 @@ class TrackerErrorCheckBehaviour(private val server: VRServer) : TrackingCheckli
class SteamVRHandsCheckBehaviour(
private val server: VRServer,
private val settings: Settings,
private val boneRouting: BoneRoutingManager,
) : TrackingChecklistBehaviourType {
private val HAND_BONES = setOf(BodyPart.LEFT_HAND, BodyPart.RIGHT_HAND)
@@ -157,7 +156,6 @@ class SteamVRHandsCheckBehaviour(
trackers: List<TrackerState>,
routes: Routes,
driverConnected: Boolean,
automatic: Boolean,
): TrackingChecklistStep {
// The skeleton computes a hand bone from the arm chain, so routing one sends a hand
// tracker to SteamVR whether or not the user wears anything on that hand.
@@ -172,7 +170,7 @@ class SteamVRHandsCheckBehaviour(
return TrackingChecklistStep(
valid = !handsSentToDriver || (!hasControllers && hasHandTrackers),
enabled = driverConnected && !automatic,
enabled = driverConnected,
ignorable = true,
visibility = TrackingChecklistStepVisibility.WHEN_INVALID,
)
@@ -183,7 +181,6 @@ class SteamVRHandsCheckBehaviour(
trackerStatesFlow(server),
boneRouting.context.state.map { state -> state.routes },
server.context.state.map { state -> state.drivers.isNotEmpty() }.distinctUntilChanged(),
settings.context.state.map { state -> state.data.boneRoutingConfig.automatic }.distinctUntilChanged(),
::computeStep,
)
.distinctUntilChanged()
@@ -35,7 +35,7 @@ class TrackingChecklist(
add(FullResetCheckBehaviour(appContext.server, appContext.resetsManager))
add(MountingCalibrationCheckBehaviour(appContext.server, appContext.resetsManager, appContext.config.settings))
add(TrackerErrorCheckBehaviour(appContext.server))
add(SteamVRHandsCheckBehaviour(appContext.server, appContext.config.settings, appContext.boneRouting))
add(SteamVRHandsCheckBehaviour(appContext.server, appContext.boneRouting))
add(FeetMountingCalibrationCheckBehaviour(appContext.server, appContext.resetsManager, appContext.config.settings))
// TODO: STAY_ALIGNED_CONFIGURED