| 1 | import 'dart:math' as math; |
| 2 | import 'dart:ui' as ui; |
| 3 | |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:flutter/services.dart'; |
| 6 | import 'package:gba_core/gba_core.dart'; |
| 7 | |
| 8 | import 'emulator_session.dart'; |
| 9 | import 'game_pad.dart'; |
| 10 | |
| 11 | /// Plays a game: the picture, the on-screen controls, and keyboard or |
| 12 | /// game controller input. |
| 13 | class EmulatorScreen extends StatefulWidget |
| 14 | const EmulatorScreen({ |
| 15 | super.key, |
| 16 | required this.romPath, |
| 17 | required this.savePath, |
| 18 | this.biosPath, |
| 19 | }); |
| 20 | |
| 21 | final String romPath; |
| 22 | final String savePath; |
| 23 | final String? biosPath; |
| 24 | |
| 25 | @override |
| 26 | State<EmulatorScreen> createState |
| 27 | } |
| 28 | |
| 29 | class _EmulatorScreenState extends State |
| 30 | with WidgetsBindingObserver { |
| 31 | EmulatorSession? _session; |
| 32 | String? _startError; |
| 33 | bool _userPaused = false; |
| 34 | bool _leaving = false; |
| 35 | int _touchKeys = 0; |
| 36 | int _keyboardKeys = 0; |
| 37 | |
| 38 | @override |
| 39 | void initState() { |
| 40 | super.initState(); |
| 41 | WidgetsBinding.instance.addObserver( |
| 42 | SystemChrome.setEnabledSystemUIMode(SystemUiMode |
| 43 | _start(); |
| 44 | } |
| 45 | |
| 46 | Future<void> _start |
| 47 | try { |
| 48 | final session = await EmulatorSession |
| 49 | romPath: widget.romPath, |
| 50 | savePath: widget.savePath, |
| 51 | biosPath: widget.biosPath, |
| 52 | ); |
| 53 | if (!mounted) { |
| 54 | await session.stop(); |
| 55 | return; |
| 56 | } |
| 57 | session.error.addListener(_onSessionError); |
| 58 | setState(() => _session = session); |
| 59 | } on StateError catch (error) { |
| 60 | if (mounted) setState(() => _startError = |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void _onSessionError() { |
| 65 | final error = _session?.error |
| 66 | if (error != null && mounted) setState |
| 67 | } |
| 68 | |
| 69 | @override |
| 70 | void didChangeAppLifecycleState(AppLifecycleState |
| 71 | final session = _session; |
| 72 | if (session == null) return |
| 73 | if (state == AppLifecycleState.resumed) { |
| 74 | if (!_userPaused) session.resume() |
| 75 | } else { |
| 76 | // Also stores the save, in case the app is closed from here. |
| 77 | session.pause(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @override |
| 82 | void dispose() { |
| 83 | WidgetsBinding.instance.removeObserver( |
| 84 | SystemChrome.setEnabledSystemUIMode(SystemUiMode |
| 85 | final session = _session; |
| 86 | if (session != null && ! |
| 87 | super.dispose(); |
| 88 | } |
| 89 | |
| 90 | Future<void> _leave |
| 91 | if (_leaving) return; |
| 92 | _leaving = true; |
| 93 | final navigator = Navigator. |
| 94 | await _session?.stop(); |
| 95 | if (mounted) navigator.pop(); |
| 96 | } |
| 97 | |
| 98 | void _togglePause() { |
| 99 | final session = _session; |
| 100 | if (session == null) return |
| 101 | setState(() => _userPaused = ! |
| 102 | _userPaused ? session.pause() : session |
| 103 | } |
| 104 | |
| 105 | void _sendKeys() => _session |
| 106 | |
| 107 | KeyEventResult _onKey(FocusNode |
| 108 | final key = _keyMap[event.logicalKey]; |
| 109 | if (key == null) return |
| 110 | if (event is KeyDownEvent || |
| 111 | _keyboardKeys |= key; |
| 112 | } else if (event is KeyUpEvent |
| 113 | _keyboardKeys &= ~key; |
| 114 | } |
| 115 | _sendKeys(); |
| 116 | return KeyEventResult.handled; |
| 117 | } |
| 118 | |
| 119 | static final Map |
| 120 | LogicalKeyboardKey.arrowUp: GbaKey |
| 121 | LogicalKeyboardKey.arrowDown: GbaKey |
| 122 | LogicalKeyboardKey.arrowLeft: GbaKey |
| 123 | LogicalKeyboardKey.arrowRight: GbaKey |
| 124 | LogicalKeyboardKey.keyX: GbaKey |
| 125 | LogicalKeyboardKey.keyZ: GbaKey |
| 126 | LogicalKeyboardKey.keyA: GbaKey |
| 127 | LogicalKeyboardKey.keyS: GbaKey |
| 128 | LogicalKeyboardKey.enter: GbaKey |
| 129 | LogicalKeyboardKey.backspace: GbaKey |
| 130 | LogicalKeyboardKey.gameButtonA: GbaKey |
| 131 | LogicalKeyboardKey.gameButtonB: GbaKey |
| 132 | LogicalKeyboardKey.gameButtonLeft1: GbaKey |
| 133 | LogicalKeyboardKey.gameButtonRight1: GbaKey |
| 134 | LogicalKeyboardKey.gameButtonStart: GbaKey |
| 135 | LogicalKeyboardKey.gameButtonSelect: GbaKey |
| 136 | }; |
| 137 | |
| 138 | @override |
| 139 | Widget build(BuildContext |
| 140 | return PopScope( |
| 141 | canPop: false, |
| 142 | onPopInvokedWithResult: (didPop, _) { |
| 143 | if (!didPop) _leave(); |
| 144 | }, |
| 145 | child: Scaffold( |
| 146 | backgroundColor: Colors.black, |
| 147 | body: Focus( |
| 148 | autofocus: true, |
| 149 | onKeyEvent: _onKey, |
| 150 | child: _startError != null |
| 151 | ? _buildError(context) |
| 152 | : _buildGame(context), |
| 153 | ), |
| 154 | ), |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | Widget _buildError(BuildContext |
| 159 | return SafeArea( |
| 160 | child: Center( |
| 161 | child: Padding( |
| 162 | padding: const EdgeInsets. |
| 163 | child: Column( |
| 164 | mainAxisSize: MainAxisSize.min, |
| 165 | children: [ |
| 166 | const Icon(Icons |
| 167 | const SizedBox(height: |
| 168 | Text( |
| 169 | _startError!, |
| 170 | style: const TextStyle(color |
| 171 | textAlign: TextAlign.center, |
| 172 | maxLines: 12, |
| 173 | overflow: TextOverflow.ellipsis, |
| 174 | ), |
| 175 | const SizedBox(height: |
| 176 | FilledButton(onPressed: _leave, child: |
| 177 | ], |
| 178 | ), |
| 179 | ), |
| 180 | ), |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | Widget _buildGame(BuildContext |
| 185 | final padding = MediaQuery. |
| 186 | return LayoutBuilder( |
| 187 | builder: (context, constraints) { |
| 188 | final size = constraints.biggest; |
| 189 | final landscape = size.width > |
| 190 | final screen = _screenRect(size |
| 191 | // In landscape the close and pause buttons sit in the top corners |
| 192 | // of the side panels; the shoulder buttons go below them. |
| 193 | final layout = GamePadLayout. |
| 194 | size, |
| 195 | screen, |
| 196 | topReserved: padding.top + _TopBar |
| 197 | ); |
| 198 | final session = _session; |
| 199 | return Stack( |
| 200 | fit: StackFit.expand, |
| 201 | children: [ |
| 202 | if (session != null) |
| 203 | CustomPaint(painter: _FramePainter(session |
| 204 | else |
| 205 | Positioned.fromRect( |
| 206 | rect: screen, |
| 207 | child: const Center(child |
| 208 | ), |
| 209 | GamePad( |
| 210 | layout: layout, |
| 211 | onKeysChanged: (keys) { |
| 212 | _touchKeys = keys; |
| 213 | _sendKeys(); |
| 214 | }, |
| 215 | ), |
| 216 | Positioned( |
| 217 | top: padding.top, |
| 218 | left: 0, |
| 219 | right: 0, |
| 220 | child: _TopBar( |
| 221 | title: landscape ? null : session? |
| 222 | fps: session?.fps, |
| 223 | paused: _userPaused, |
| 224 | onClose: _leave, |
| 225 | onPause: _togglePause, |
| 226 | ), |
| 227 | ), |
| 228 | if (_userPaused) |
| 229 | Positioned.fromRect( |
| 230 | rect: screen, |
| 231 | child: const ColoredBox( |
| 232 | color: Color(0x99000000), |
| 233 | child: Center( |
| 234 | child: Text( |
| 235 | 'Paused', |
| 236 | style: TextStyle(color: Colors |
| 237 | ), |
| 238 | ), |
| 239 | ), |
| 240 | ), |
| 241 | ], |
| 242 | ); |
| 243 | }, |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | /// The game picture's rectangle: full width under the top bar in |
| 248 | /// portrait, centred between the control panels in landscape, always at |
| 249 | /// the GBA's 3:2 aspect ratio. |
| 250 | static Rect _screenRect |
| 251 | if (size.width > size.height) { |
| 252 | final height = math.min |
| 253 | size.height - padding.vertical - 16 |
| 254 | size.width * 0.56 / 1.5 |
| 255 | ); |
| 256 | final width = height * 1.5 |
| 257 | return Rect.fromCenter |
| 258 | center: Offset(size.width / |
| 259 | width: width, |
| 260 | height: height, |
| 261 | ); |
| 262 | } |
| 263 | final top = padding.top + |
| 264 | final width = size.width; |
| 265 | return Rect.fromLTWH |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /// Close on the left; frame rate and pause on the right. Without a |
| 270 | /// [title] (landscape) the middle is empty and lets touches through. |
| 271 | class _TopBar extends StatelessWidget |
| 272 | const _TopBar({ |
| 273 | required this.title, |
| 274 | required this.fps, |
| 275 | required this.paused, |
| 276 | required this.onClose, |
| 277 | required this.onPause, |
| 278 | }); |
| 279 | |
| 280 | static const double |
| 281 | |
| 282 | final String? title; |
| 283 | final ValueNotifier<double |
| 284 | final bool paused; |
| 285 | final VoidCallback onClose; |
| 286 | final VoidCallback onPause; |
| 287 | |
| 288 | @override |
| 289 | Widget build(BuildContext |
| 290 | const style = TextStyle(color |
| 291 | final fps = this. |
| 292 | final title = this. |
| 293 | return SizedBox( |
| 294 | height: height, |
| 295 | child: Row( |
| 296 | children: [ |
| 297 | IconButton( |
| 298 | tooltip: 'Close game', |
| 299 | icon: const Icon( |
| 300 | onPressed: onClose, |
| 301 | ), |
| 302 | if (title != null) |
| 303 | Expanded( |
| 304 | child: Text(title, style: |
| 305 | ) |
| 306 | else |
| 307 | const Spacer(), |
| 308 | if (fps != null && ! |
| 309 | ValueListenableBuilder<double>( |
| 310 | valueListenable: fps, |
| 311 | builder: (context, value, _) => |
| 312 | Text('${value. |
| 313 | ), |
| 314 | IconButton( |
| 315 | tooltip: paused ? 'Resume' : 'Pause' |
| 316 | icon: Icon( |
| 317 | paused ? Icons.play_arrow : |
| 318 | color: Colors.white70, |
| 319 | ), |
| 320 | onPressed: onPause, |
| 321 | ), |
| 322 | ], |
| 323 | ), |
| 324 | ); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// Draws the latest frame, scaled with hard pixel edges. |
| 329 | class _FramePainter extends CustomPainter |
| 330 | _FramePainter(this.frame, |
| 331 | |
| 332 | final ValueNotifier<ui.Image |
| 333 | final Rect screen; |
| 334 | |
| 335 | @override |
| 336 | void paint(Canvas |
| 337 | final image = frame.value; |
| 338 | if (image == null) return |
| 339 | canvas.drawImageRect( |
| 340 | image, |
| 341 | Rect.fromLTWH(0, |
| 342 | screen, |
| 343 | Paint()..filterQuality = FilterQuality |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | @override |
| 348 | bool shouldRepaint(_FramePainter |
| 349 | old.frame != frame || old.screen != |
| 350 | } |