Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ object ControlProfileService {
private const val KEY_LIBRARY_PROFILE_ID = "libraryProfileId"
private const val KEY_GAME_OWNER_ID = "gameOwnerId"
private const val KEY_SECTION_SOURCES = "sectionSources"
private val STICK_TUNING_KEYS = arrayOf(
ControlsProfile.KEY_LEFT_STICK_DEADZONE,
ControlsProfile.KEY_RIGHT_STICK_DEADZONE,
ControlsProfile.KEY_LEFT_STICK_SENSITIVITY,
ControlsProfile.KEY_RIGHT_STICK_SENSITIVITY,
ControlsProfile.KEY_LEFT_STICK_DEADZONE_MODE,
ControlsProfile.KEY_RIGHT_STICK_DEADZONE_MODE,
ControlsProfile.KEY_LEFT_STICK_DIGITAL_MODE,
ControlsProfile.KEY_RIGHT_STICK_DIGITAL_MODE,
)

fun preview(context: Context, profile: ControlsProfile): ControlProfilePreview =
preview(readProfileJson(context, profile))
Expand Down Expand Up @@ -100,7 +110,7 @@ object ControlProfileService {
// arrays are included only when present.
return buildSet {
add(ControlProfileSection.ON_SCREEN)
if (json.has("controllers")) add(ControlProfileSection.PHYSICAL_CONTROLLER)
if (json.has("controllers") || hasStickTuning(json)) add(ControlProfileSection.PHYSICAL_CONTROLLER)
if (json.has("radialMenus")) add(ControlProfileSection.RADIAL_MENU)
if (json.has(KEY_GYRO_SETTINGS)) add(ControlProfileSection.GYRO)
if (json.has(KEY_TOUCHSCREEN_SETTINGS)) add(ControlProfileSection.TOUCHSCREEN)
Expand Down Expand Up @@ -605,6 +615,10 @@ object ControlProfileService {
}
if (ControlProfileSection.PHYSICAL_CONTROLLER in sections) {
destination.put("controllers", deepCopyArray(source.optJSONArray("controllers")))
STICK_TUNING_KEYS.forEach { key ->
if (source.has(key)) destination.put(key, source.opt(key))
else destination.remove(key)
}
}
if (ControlProfileSection.RADIAL_MENU in sections) {
destination.put("radialMenus", deepCopyArray(source.optJSONArray("radialMenus")))
Expand All @@ -622,7 +636,7 @@ object ControlProfileService {

private fun allStoredSections(json: JSONObject): Set<ControlProfileSection> = buildSet {
if (json.has("elements")) add(ControlProfileSection.ON_SCREEN)
if (json.has("controllers")) add(ControlProfileSection.PHYSICAL_CONTROLLER)
if (json.has("controllers") || hasStickTuning(json)) add(ControlProfileSection.PHYSICAL_CONTROLLER)
if (json.has("radialMenus")) add(ControlProfileSection.RADIAL_MENU)
if (json.has(KEY_GYRO_SETTINGS)) add(ControlProfileSection.GYRO)
if (json.has(KEY_TOUCHSCREEN_SETTINGS)) add(ControlProfileSection.TOUCHSCREEN)
Expand Down Expand Up @@ -696,14 +710,17 @@ object ControlProfileService {
val sections = sectionsOf(json)
if (sections.isEmpty()) throw IllegalArgumentException("Profile contains no settings")
requireArrayPayload(json, sections, ControlProfileSection.ON_SCREEN, "elements")
requireArrayPayload(json, sections, ControlProfileSection.PHYSICAL_CONTROLLER, "controllers")
requireArrayPayload(json, sections, ControlProfileSection.RADIAL_MENU, "radialMenus")
requireObjectPayload(json, sections, ControlProfileSection.GYRO, KEY_GYRO_SETTINGS)
requireObjectPayload(json, sections, ControlProfileSection.TOUCHSCREEN, KEY_TOUCHSCREEN_SETTINGS)
requireObjectPayload(json, sections, ControlProfileSection.SHOOTER, KEY_SHOOTER_SETTINGS)

if (ControlProfileSection.ON_SCREEN in sections) validateElements(json)
if (ControlProfileSection.PHYSICAL_CONTROLLER in sections) validateControllers(json)
if (ControlProfileSection.PHYSICAL_CONTROLLER in sections) {
if (json.has("controllers")) validateControllers(json)
else if (!hasStickTuning(json)) throw IllegalArgumentException("Profile has invalid physical controller settings")
validateStickTuning(json)
}
if (ControlProfileSection.RADIAL_MENU in sections) validateRadialMenus(json)
if (ControlProfileSection.GYRO in sections) {
validatePrimitiveSettings(requireNotNull(json.optJSONObject(KEY_GYRO_SETTINGS)), "gyro")
Expand Down Expand Up @@ -788,6 +805,48 @@ object ControlProfileService {
}
}

private fun validateStickTuning(json: JSONObject) {
optionalFiniteNumber(
json,
ControlsProfile.KEY_LEFT_STICK_DEADZONE,
ControlsProfile.MIN_STICK_DEADZONE.toDouble(),
ControlsProfile.MAX_STICK_DEADZONE.toDouble(),
)
optionalFiniteNumber(
json,
ControlsProfile.KEY_RIGHT_STICK_DEADZONE,
ControlsProfile.MIN_STICK_DEADZONE.toDouble(),
ControlsProfile.MAX_STICK_DEADZONE.toDouble(),
)
optionalFiniteNumber(
json,
ControlsProfile.KEY_LEFT_STICK_SENSITIVITY,
ControlsProfile.MIN_STICK_SENSITIVITY.toDouble(),
ControlsProfile.MAX_STICK_SENSITIVITY.toDouble(),
)
optionalFiniteNumber(
json,
ControlsProfile.KEY_RIGHT_STICK_SENSITIVITY,
ControlsProfile.MIN_STICK_SENSITIVITY.toDouble(),
ControlsProfile.MAX_STICK_SENSITIVITY.toDouble(),
)

val deadzoneModes = ControlsProfile.StickDeadzoneMode.values().mapTo(mutableSetOf()) { it.jsonName }
val digitalModes = ControlsProfile.StickDigitalMode.values().mapTo(mutableSetOf()) { it.jsonName }
validateOptionalEnum(json, ControlsProfile.KEY_LEFT_STICK_DEADZONE_MODE, deadzoneModes)
validateOptionalEnum(json, ControlsProfile.KEY_RIGHT_STICK_DEADZONE_MODE, deadzoneModes)
validateOptionalEnum(json, ControlsProfile.KEY_LEFT_STICK_DIGITAL_MODE, digitalModes)
validateOptionalEnum(json, ControlsProfile.KEY_RIGHT_STICK_DIGITAL_MODE, digitalModes)
}

private fun validateOptionalEnum(json: JSONObject, key: String, allowed: Set<String>) {
json.optStringOrNull(key)?.let { value ->
if (value !in allowed) throw IllegalArgumentException("Profile has an unsupported $key value: $value")
}
}

private fun hasStickTuning(json: JSONObject): Boolean = STICK_TUNING_KEYS.any(json::has)

private fun validateRadialMenus(json: JSONObject) {
val menus = requireNotNull(json.optJSONArray("radialMenus"))
if (menus.length() > 16) throw IllegalArgumentException("Profile contains too many radial menus")
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/app/gamenative/ui/component/QuickMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ object QuickMenuAction {
const val GYRO = 11
const val CONTROL_PROFILES = 12
const val CONTROL_PROFILE_APPLIED = 13
const val PHYSICAL_CONTROLLER_SETTINGS = 14
}

private object QuickMenuTab {
Expand Down Expand Up @@ -1121,21 +1122,27 @@ fun QuickMenu(
},
focusRequester = if (index == 0) controllerItemFocusRequester else null,
onFocused = { lastControllerFocusRequester = it },
secondaryIcon = if (item.id == QuickMenuAction.TOUCHSCREEN_MODE && touchscreenEnabled)
secondaryIcon = if (item.id == QuickMenuAction.EDIT_PHYSICAL_CONTROLLER)
Icons.Default.Settings
else if (item.id == QuickMenuAction.TOUCHSCREEN_MODE && touchscreenEnabled)
Icons.Default.Settings
else if (item.id == QuickMenuAction.SHOOTER_MODE && shooterEnabled)
Icons.Default.Settings
else if (item.id == QuickMenuAction.GYRO && gyroEnabled)
Icons.Default.Settings
else null,
secondaryContentDescriptionResId = if (item.id == QuickMenuAction.TOUCHSCREEN_MODE && touchscreenEnabled)
secondaryContentDescriptionResId = if (item.id == QuickMenuAction.EDIT_PHYSICAL_CONTROLLER)
R.string.physical_controller_settings_title
else if (item.id == QuickMenuAction.TOUCHSCREEN_MODE && touchscreenEnabled)
R.string.gesture_settings_title
else if (item.id == QuickMenuAction.SHOOTER_MODE && shooterEnabled)
R.string.shooter_mode_settings_title
else if (item.id == QuickMenuAction.GYRO && gyroEnabled)
R.string.gyro_settings_title
else null,
onSecondaryClick = if (item.id == QuickMenuAction.TOUCHSCREEN_MODE && touchscreenEnabled)
onSecondaryClick = if (item.id == QuickMenuAction.EDIT_PHYSICAL_CONTROLLER)
({ onItemSelected(QuickMenuAction.PHYSICAL_CONTROLLER_SETTINGS) })
else if (item.id == QuickMenuAction.TOUCHSCREEN_MODE && touchscreenEnabled)
onTouchGestureSettingsClick
else if (item.id == QuickMenuAction.SHOOTER_MODE && shooterEnabled)
onShooterModeSettingsClick
Expand Down
Loading
Loading