Files
swiftcontrol/lib/utils/keymap/buttons.dart
2026-07-07 12:05:05 +02:00

292 lines
12 KiB
Dart

import 'package:bike_control/bluetooth/devices/cycplus/cycplus_bc2.dart';
import 'package:bike_control/bluetooth/devices/elite/elite_square.dart';
import 'package:bike_control/bluetooth/devices/elite/elite_sterzo.dart';
import 'package:bike_control/bluetooth/devices/gyroscope/gyroscope_steering.dart';
import 'package:bike_control/bluetooth/devices/openbikecontrol/protocol_parser.dart';
import 'package:bike_control/bluetooth/devices/wahoo/wahoo_kickr_bike_shift.dart';
import 'package:bike_control/bluetooth/devices/wheeltop/wheeltop_eds.dart';
import 'package:bike_control/bluetooth/devices/zwift/constants.dart';
import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/widgets/keymap_explanation.dart';
import 'package:dartx/dartx.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
enum InGameAction {
shiftUp('Shift Up', icon: BootstrapIcons.patchPlus),
shiftDown('Shift Down', icon: BootstrapIcons.patchMinus),
uturn('U-Turn', alternativeTitle: 'Down', icon: BootstrapIcons.arrowDownUp),
tuck('Tuck', icon: BootstrapIcons.speedometer),
steerLeft('Steer Left', alternativeTitle: 'Left', icon: RadixIcons.doubleArrowLeft, isLongPress: true),
steerRight('Steer Right', alternativeTitle: 'Right', icon: RadixIcons.doubleArrowRight, isLongPress: true),
// mywhoosh
cameraAngle('Change Camera Angle', possibleValues: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], icon: BootstrapIcons.cameraReels),
emote('Emote', possibleValues: [1, 2, 3, 4, 5, 6], icon: BootstrapIcons.emojiSmile),
toggleUi('Toggle UI', icon: RadixIcons.iconSwitch),
navigateLeft('Navigate Left', icon: BootstrapIcons.signTurnLeft),
navigateRight('Navigate Right', icon: BootstrapIcons.signTurnRight),
increaseResistance('Increase Resistance', icon: LucideIcons.chartNoAxesColumnIncreasing),
decreaseResistance('Decrease Resistance', icon: LucideIcons.chartNoAxesColumnDecreasing),
// zwift
openActionBar('Open Action Bar', alternativeTitle: 'Up', icon: BootstrapIcons.menuApp, isLongPress: true),
usePowerUp('Use Power-Up', icon: Icons.flash_on_outlined, isLongPress: true),
select('Select', icon: LucideIcons.mousePointerClick),
back('Back', icon: BootstrapIcons.arrowLeft),
rideOnBomb('Ride On Bomb', icon: LucideIcons.bomb, isLongPress: true),
// rouvy
kudos('Kudos', icon: BootstrapIcons.handThumbsUp),
pause('Pause/Resume', icon: BootstrapIcons.pause, isLongPress: true),
// headwind
headwindSpeed('Headwind Speed', possibleValues: [0, 25, 50, 75, 100], icon: Icons.air, isOutsideTrainerApp: true),
headwindSpeedInc('Headwind Speed Increase', icon: Icons.air, isOutsideTrainerApp: true),
headwindSpeedDec('Headwind Speed Decrease', icon: Icons.air, isOutsideTrainerApp: true),
headwindSpeedCyclicInc('Headwind Speed Cyclic Increase', icon: Icons.air, isOutsideTrainerApp: true),
headwindSpeedCyclicDec('Headwind Speed Cyclic Decrease', icon: Icons.air, isOutsideTrainerApp: true),
headwindHeartRateMode('Headwind HR Mode', icon: Icons.favorite, isOutsideTrainerApp: true),
// Incline devices (KICKR Climb, Elite Rizer)
inclineIncrease('Incline Up', icon: LucideIcons.trendingUp, isOutsideTrainerApp: true),
inclineDecrease('Incline Down', icon: LucideIcons.trendingDown, isOutsideTrainerApp: true),
inclineZero('Incline Flatten (0%)', icon: LucideIcons.minus, isOutsideTrainerApp: true),
inclineAutoMode('Incline Auto (Follow Grade)', icon: LucideIcons.mountain, isOutsideTrainerApp: true),
// openbikecontrol
up('Up', icon: RadixIcons.arrowUp),
down('Down', icon: RadixIcons.arrowDown),
home('Home', icon: RadixIcons.home),
menu('Menu', icon: RadixIcons.dropdownMenu),
gearSet('Gear Set', icon: LucideIcons.gauge),
pushToTalk('Push to Talk', icon: LucideIcons.mic),
skipInterval('Skip Interval', icon: LucideIcons.skipForward),
previousInterval('Previous Interval', icon: LucideIcons.skipBack),
lap('Lap', icon: LucideIcons.timer),
resume('Resume', icon: LucideIcons.play),
changeMode('Change Mode', icon: LucideIcons.repeat),
takeBreak('Take a Break', icon: LucideIcons.coffee),
joinRider('Join Rider', icon: LucideIcons.userPlus),
changeRoute('Change Route', icon: LucideIcons.signpost),
mapToggle('Map Toggle', icon: LucideIcons.map),
spectateRider('Spectate Rider', icon: LucideIcons.eye),
// trainer control
trainerSwitchMode('Trainer: Switch ERG/SIM', icon: LucideIcons.repeat, isOutsideTrainerApp: true),
trainerIntensityUp('Trainer: Intensity Up', icon: LucideIcons.trendingUp, isOutsideTrainerApp: true),
trainerIntensityDown('Trainer: Intensity Down', icon: LucideIcons.trendingDown, isOutsideTrainerApp: true),
workoutPauseResume('Workout: Pause/Resume', icon: LucideIcons.pause, isOutsideTrainerApp: true),
frontShift('Front Shift (Chainring)', icon: LucideIcons.arrowLeftRight, isOutsideTrainerApp: true),
// Wahoo ELEMNT — D-Fly channel buttons emitted via the Di2Definition.
dFlyChannel1('D-Fly Channel 1', icon: LucideIcons.circleDot),
dFlyChannel2('D-Fly Channel 2', icon: LucideIcons.circleDot),
dFlyChannel3('D-Fly Channel 3', icon: LucideIcons.circleDot),
dFlyChannel4('D-Fly Channel 4', icon: LucideIcons.circleDot),
// device / system
calibratePhoneSteering('Calibrate Steering', icon: BootstrapIcons.wrenchAdjustable, isOutsideTrainerApp: true),
screenRecording('Record Screen', icon: LucideIcons.video, isOutsideTrainerApp: true);
final String englishTitle;
final bool isLongPress;
final bool isOutsideTrainerApp;
final IconData? icon;
final String? alternativeTitle;
final List<int>? possibleValues;
const InGameAction(
this.englishTitle, {
this.possibleValues,
this.isOutsideTrainerApp = false,
this.alternativeTitle,
this.icon,
this.isLongPress = false,
});
/// Localized label shown in the UI (falls back to [englishTitle]).
String get title {
final l = AppLocalizations.current;
return switch (this) {
InGameAction.shiftUp => l.actionShiftUp,
InGameAction.shiftDown => l.actionShiftDown,
InGameAction.uturn => l.actionUturn,
InGameAction.tuck => l.actionTuck,
InGameAction.steerLeft => l.actionSteerLeft,
InGameAction.steerRight => l.actionSteerRight,
InGameAction.cameraAngle => l.actionCameraAngle,
InGameAction.emote => l.actionEmote,
InGameAction.toggleUi => l.actionToggleUi,
InGameAction.navigateLeft => l.actionNavigateLeft,
InGameAction.navigateRight => l.actionNavigateRight,
InGameAction.increaseResistance => l.actionIncreaseResistance,
InGameAction.decreaseResistance => l.actionDecreaseResistance,
InGameAction.openActionBar => l.actionOpenActionBar,
InGameAction.usePowerUp => l.actionUsePowerUp,
InGameAction.select => l.actionSelect,
InGameAction.back => l.actionBack,
InGameAction.rideOnBomb => l.actionRideOnBomb,
InGameAction.kudos => l.actionKudos,
InGameAction.pause => l.actionPause,
InGameAction.headwindSpeed => l.actionHeadwindSpeed,
InGameAction.headwindSpeedInc => l.actionHeadwindSpeedInc,
InGameAction.headwindSpeedDec => l.actionHeadwindSpeedDec,
InGameAction.headwindSpeedCyclicInc => l.actionHeadwindSpeedCyclicInc,
InGameAction.headwindSpeedCyclicDec => l.actionHeadwindSpeedCyclicDec,
InGameAction.headwindHeartRateMode => l.actionHeadwindHeartRateMode,
InGameAction.inclineIncrease => l.actionInclineIncrease,
InGameAction.inclineDecrease => l.actionInclineDecrease,
InGameAction.inclineZero => l.actionInclineZero,
InGameAction.inclineAutoMode => l.actionInclineAutoMode,
InGameAction.up => l.actionUp,
InGameAction.down => l.actionDown,
InGameAction.home => l.actionHome,
InGameAction.menu => l.actionMenu,
InGameAction.gearSet => l.actionGearSet,
InGameAction.pushToTalk => l.actionPushToTalk,
InGameAction.skipInterval => l.actionSkipInterval,
InGameAction.previousInterval => l.actionPreviousInterval,
InGameAction.lap => l.actionLap,
InGameAction.resume => l.actionResume,
InGameAction.changeMode => l.actionChangeMode,
InGameAction.takeBreak => l.actionTakeBreak,
InGameAction.joinRider => l.actionJoinRider,
InGameAction.changeRoute => l.actionChangeRoute,
InGameAction.mapToggle => l.actionMapToggle,
InGameAction.spectateRider => l.actionSpectateRider,
InGameAction.trainerSwitchMode => l.actionTrainerSwitchMode,
InGameAction.trainerIntensityUp => l.actionTrainerIntensityUp,
InGameAction.trainerIntensityDown => l.actionTrainerIntensityDown,
InGameAction.workoutPauseResume => l.actionWorkoutPauseResume,
InGameAction.frontShift => l.actionFrontShift,
InGameAction.dFlyChannel1 => l.actionDFlyChannel1,
InGameAction.dFlyChannel2 => l.actionDFlyChannel2,
InGameAction.dFlyChannel3 => l.actionDFlyChannel3,
InGameAction.dFlyChannel4 => l.actionDFlyChannel4,
InGameAction.calibratePhoneSteering => l.actionCalibratePhoneSteering,
InGameAction.screenRecording => l.actionScreenRecording,
};
}
@override
String toString() => englishTitle;
}
const trainerActions = [
InGameAction.shiftUp,
InGameAction.shiftDown,
InGameAction.trainerSwitchMode,
InGameAction.trainerIntensityUp,
InGameAction.trainerIntensityDown,
InGameAction.workoutPauseResume,
InGameAction.frontShift,
];
const trainerOnlyActions = [
InGameAction.trainerSwitchMode,
InGameAction.trainerIntensityUp,
InGameAction.trainerIntensityDown,
InGameAction.workoutPauseResume,
];
class ControllerButton {
static const int _deviceIdSuffixLength = 4;
static const _unset = Object();
final String name;
final int? identifier;
final InGameAction? action;
final Color? color;
final IconData? icon;
final String? sourceDeviceId;
const ControllerButton(
this.name, {
this.color,
this.icon,
this.identifier,
this.action,
this.sourceDeviceId,
});
ControllerButton copyWith({
String? name,
int? identifier,
InGameAction? action,
Color? color,
IconData? icon,
Object? sourceDeviceId = _unset,
}) {
final newSourceDeviceId = sourceDeviceId == _unset ? this.sourceDeviceId : sourceDeviceId as String?;
return ControllerButton(
name ?? this.name,
color: color ?? this.color,
icon: icon ?? this.icon,
identifier: identifier ?? this.identifier,
action: action ?? this.action,
sourceDeviceId: newSourceDeviceId,
);
}
String get displayName {
if (sourceDeviceId == null) {
return name.splitByUpperCase();
}
final shortenedId = sourceDeviceId!.length <= _deviceIdSuffixLength
? sourceDeviceId!
: sourceDeviceId!.substring(sourceDeviceId!.length - _deviceIdSuffixLength);
return '${name.splitByUpperCase()} (${shortenedId.toUpperCase()})';
}
/// Uppercase initials derived from the camelCase [name].
/// Example: `sideButtonLeft` → `SBL`, `navigationUp` → `NU`, `a` → `A`.
/// Uses the raw [name] (not [displayName]) so the multi-device `(HASH)`
/// suffix never leaks into the label.
String get initials {
final words = name.splitByUpperCase().split(' ');
return words
.where((w) => w.isNotEmpty)
.map(
(w) => w
.replaceAll('Up', '↑')
.replaceAll('Right', '→')
.replaceAll('Down', '↓')
.replaceAll('Left', '←')[0]
.toUpperCase(),
)
.join();
}
@override
String toString() {
return name;
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ControllerButton &&
runtimeType == other.runtimeType &&
name == other.name &&
identifier == other.identifier &&
action == other.action &&
color == other.color &&
icon == other.icon &&
sourceDeviceId == other.sourceDeviceId;
@override
int get hashCode => Object.hash(name, action, identifier, color, icon, sourceDeviceId);
static List<ControllerButton> get values => [
...SterzoButtons.values,
...GyroscopeSteeringButtons.values,
...ZwiftButtons.values,
...EliteSquareButtons.values,
...WahooKickrShiftButtons.values,
...CycplusBc2Buttons.values,
...WheeltopEdsButtons.values,
...OpenBikeProtocolParser.BUTTON_NAMES.values,
].distinct().toList();
}