From a09eafe3d2a40ee84efc2500f2cf0029fde024d7 Mon Sep 17 00:00:00 2001 From: Jonas Bark Date: Thu, 18 Dec 2025 14:51:49 +0100 Subject: [PATCH] improve trainer controller view --- lib/i10n/intl_de.arb | 2 +- lib/i10n/intl_fr.arb | 2 +- lib/pages/button_simulator.dart | 154 ++++++++++++++++++++++---------- lib/pages/trainer.dart | 2 +- lib/utils/keymap/buttons.dart | 4 +- 5 files changed, 113 insertions(+), 51 deletions(-) diff --git a/lib/i10n/intl_de.arb b/lib/i10n/intl_de.arb index de472ea0..2cd77d8e 100644 --- a/lib/i10n/intl_de.arb +++ b/lib/i10n/intl_de.arb @@ -371,7 +371,7 @@ "showSupportedControllers": "Unterstützte Controller anzeigen", "showTroubleshootingGuide": "Anleitung zur Fehlerbehebung anzeigen", "signal": "Signal", - "simulateButtons": "Tasten simulieren", + "simulateButtons": "Trainersteuerung", "simulateKeyboardShortcut": "Tastenkombination simulieren", "simulateMediaKey": "Medientaste simulieren", "simulateTouch": "Berührung simulieren", diff --git a/lib/i10n/intl_fr.arb b/lib/i10n/intl_fr.arb index 542481f6..52a612ba 100644 --- a/lib/i10n/intl_fr.arb +++ b/lib/i10n/intl_fr.arb @@ -371,7 +371,7 @@ "showSupportedControllers": "Afficher les manettes compatibles", "showTroubleshootingGuide": "Afficher le guide de dépannage", "signal": "Signal", - "simulateButtons": "Simuler des boutons", + "simulateButtons": "Commandes de l'entraîneur", "simulateKeyboardShortcut": "Simuler un raccourci clavier", "simulateMediaKey": "Simuler la touche média", "simulateTouch": "Simuler le toucher", diff --git a/lib/pages/button_simulator.dart b/lib/pages/button_simulator.dart index 14e6a53c..c31cf253 100644 --- a/lib/pages/button_simulator.dart +++ b/lib/pages/button_simulator.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:bike_control/bluetooth/devices/mywhoosh/link.dart'; import 'package:bike_control/bluetooth/devices/openbikecontrol/obc_ble_emulator.dart'; import 'package:bike_control/bluetooth/devices/openbikecontrol/obc_mdns_emulator.dart'; @@ -79,6 +81,8 @@ class _ButtonSimulatorState extends State { static const Duration _keyPressDuration = Duration(milliseconds: 100); + InGameAction? _pressedAction; + @override void initState() { super.initState(); @@ -98,7 +102,7 @@ class _ButtonSimulatorState extends State { // If no saved hotkeys, initialize with defaults if (savedHotkeys.isEmpty) { - final connectedTrainers = core.logic.connectedTrainerConnections; + final connectedTrainers = core.logic.enabledTrainerConnections; final allActions = []; for (final connection in connectedTrainers) { @@ -138,6 +142,9 @@ class _ButtonSimulatorState extends State { if (action == null) return KeyEventResult.ignored; + _pressedAction = action; + setState(() {}); + // Find the connection that supports this action final connectedTrainers = core.logic.connectedTrainerConnections; final connection = connectedTrainers.firstOrNullWhere((c) => c.supportedActions.contains(action)); @@ -149,11 +156,17 @@ class _ButtonSimulatorState extends State { _keyPressDuration, () { if (mounted) { + _pressedAction = null; + setState(() {}); _sendKey(context, down: false, action: action, connection: connection); } }, ); return KeyEventResult.handled; + } else { + _pressedAction = null; + setState(() {}); + buildToast(context, title: 'No connected trainer.'); } return KeyEventResult.ignored; @@ -163,6 +176,8 @@ class _ButtonSimulatorState extends State { Widget build(BuildContext context) { final connectedTrainers = core.logic.enabledTrainerConnections; + final isMobile = MediaQuery.sizeOf(context).width < 600; + return Focus( focusNode: _focusNode, autofocus: true, @@ -220,60 +235,100 @@ class _ButtonSimulatorState extends State { supportedActions.contains(InGameAction.shiftDown)) 'Shifting': [InGameAction.shiftUp, InGameAction.shiftDown], 'Other': supportedActions - .where((action) => action != InGameAction.shiftUp && action != InGameAction.shiftDown) + .where( + (action) => + action != InGameAction.shiftUp && + action != InGameAction.shiftDown && + action != InGameAction.steerLeft && + action != InGameAction.steerRight, + ) .toList(), + if (supportedActions.contains(InGameAction.steerLeft) && + supportedActions.contains(InGameAction.steerRight)) + 'Steering': [InGameAction.steerLeft, InGameAction.steerRight], }; return [ GradientText(connection.title).bold.large, - Column( - crossAxisAlignment: CrossAxisAlignment.start, - spacing: 12, - children: [ - for (final group in actionGroups.entries) ...[ - Text(group.key).bold, - Wrap( - spacing: 12, - runSpacing: 12, - children: group.value.map( - (action) { - final hotkey = _hotkeys[action]; - return Builder( - builder: (context) { - return PrimaryButton( - size: ButtonSize(1.6), - leading: hotkey != null - ? KeyWidget( - label: hotkey.toUpperCase(), - ) - : null, - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, + ConstrainedBox( + constraints: BoxConstraints(maxWidth: 800), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + spacing: 12, + children: [ + for (final group in actionGroups.entries) ...[ + Text(group.key.toUpperCase()).bold.muted, + GridView.count( + shrinkWrap: true, + physics: NeverScrollableScrollPhysics(), + crossAxisSpacing: 8, + mainAxisSpacing: 8, + crossAxisCount: min(group.value.length, 3), + childAspectRatio: isMobile ? 1.4 : 2.4, + children: group.value.map( + (action) { + final hotkey = _hotkeys[action]; + return Builder( + builder: (context) { + return Stack( + alignment: Alignment.topRight, + fit: StackFit.expand, children: [ - Text(action.title), - if (action.alternativeTitle != null) - Text( - action.alternativeTitle!, - style: TextStyle(fontSize: 12, color: Colors.gray), + Button( + style: _pressedAction == action + ? ButtonStyle.outline() + : group.key == 'Other' + ? ButtonStyle.outline() + : ButtonStyle.primary(), + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + if (action.icon != null) ...[ + Icon(action.icon), + SizedBox(height: 8), + ], + Text( + action.title, + textAlign: TextAlign.center, + style: TextStyle(height: 1), + maxLines: 2, + ).bold, + if (action.alternativeTitle != null) + Text( + action.alternativeTitle!.toUpperCase(), + style: TextStyle(fontSize: 10, color: Colors.gray), + ), + ], + ), + onPressed: () {}, + onTapDown: (c) async { + _sendKey(context, down: true, action: action, connection: connection); + }, + onTapUp: (c) async { + _sendKey(context, down: false, action: action, connection: connection); + }, + ), + + if (hotkey != null) + Positioned( + top: -4, + right: -4, + child: KeyWidget( + label: hotkey.toUpperCase(), + ), ), ], - ), - onPressed: () {}, - onTapDown: (c) async { - _sendKey(context, down: true, action: action, connection: connection); - }, - onTapUp: (c) async { - _sendKey(context, down: false, action: action, connection: connection); - }, - ); - }, - ); - }, - ).toList(), - ), - SizedBox(height: 12), + ); + }, + ); + }, + ).toList(), + ), + SizedBox(height: 12), + ], ], - ], + ), ), ]; }, @@ -335,6 +390,13 @@ class _ButtonSimulatorState extends State { required InGameAction action, required TrainerConnection connection, }) async { + if (!connection.isConnected.value) { + if (down) { + buildToast(context, title: 'No connected trainer.'); + } + + return; + } if (action.possibleValues != null) { if (down) return; showDropdown( diff --git a/lib/pages/trainer.dart b/lib/pages/trainer.dart index ca7a993b..a1c82486 100644 --- a/lib/pages/trainer.dart +++ b/lib/pages/trainer.dart @@ -92,7 +92,7 @@ class _TrainerPageState extends State with WidgetsBindingObserver { final showWhooshLinkAsOther = (core.logic.showObpBluetoothEmulator || core.logic.showObpMdnsEmulator) && core.logic.showMyWhooshLink; - final isMobile = MediaQuery.sizeOf(context).width < 600; + final isMobile = MediaQuery.sizeOf(context).width < 800; return Scrollbar( controller: _scrollController, child: SingleChildScrollView( diff --git a/lib/utils/keymap/buttons.dart b/lib/utils/keymap/buttons.dart index 7f44c8a7..fa3741ec 100644 --- a/lib/utils/keymap/buttons.dart +++ b/lib/utils/keymap/buttons.dart @@ -16,8 +16,8 @@ enum InGameAction { steerRight('Steer Right', alternativeTitle: 'Right', icon: RadixIcons.doubleArrowRight), // mywhoosh - cameraAngle('Change Camera Angle', possibleValues: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], icon: BootstrapIcons.camera), - emote('Emote', possibleValues: [1, 2, 3, 4, 5, 6], icon: BootstrapIcons.personRaisedHand), + 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),