| 1 | import 'dart:math' as math; |
| 2 | |
| 3 | import 'package:flutter/material.dart'; |
| 4 | import 'package:flutter/services.dart'; |
| 5 | import 'package:gba_core/gba_core.dart'; |
| 6 | |
| 7 | /// A round or rectangular on-screen button. |
| 8 | final class PadButton { |
| 9 | const PadButton(this |
| 10 | |
| 11 | /// The [GbaKey] bit it presses. |
| 12 | final int key; |
| 13 | final String label; |
| 14 | final Rect rect; |
| 15 | final bool round; |
| 16 | |
| 17 | bool hit(Offset |
| 18 | if (!round) return rect. |
| 19 | // A generous touch target: 25% beyond the drawn circle. |
| 20 | final radius = rect.shortestSide / |
| 21 | return (point - rect.center). |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// Where the on-screen controls go around the game picture, and which |
| 26 | /// buttons a touch at a given point presses. |
| 27 | final class GamePadLayout { |
| 28 | GamePadLayout._(this.dpad |
| 29 | |
| 30 | /// Lays the controls out in [size], around the game picture at [screen] |
| 31 | /// below it in portrait, on either side of it in landscape, where they |
| 32 | /// start below [topReserved] (the row holding the close and pause |
| 33 | /// buttons). |
| 34 | factory GamePadLayout.compute( |
| 35 | Size size, |
| 36 | Rect screen, { |
| 37 | double topReserved = 0, |
| 38 | }) { |
| 39 | final landscape = size.width > |
| 40 | return landscape |
| 41 | ? _landscape(size, screen, |
| 42 | : _portrait(size, screen); |
| 43 | } |
| 44 | |
| 45 | /// The D-pad's square. |
| 46 | final Rect dpad; |
| 47 | |
| 48 | /// A, B, L, R, Start and Select. |
| 49 | final List<PadButton |
| 50 | |
| 51 | static GamePadLayout _portrait |
| 52 | final top = screen.bottom + |
| 53 | final area = Rect. |
| 54 | final w = area.width; |
| 55 | final h = area.height; |
| 56 | final shoulderHeight = math.min |
| 57 | final shoulderWidth = w * 0.3 |
| 58 | final dpadSize = math.min |
| 59 | final buttonRadius = math.min |
| 60 | final smallWidth = w * 0.18 |
| 61 | final smallHeight = math.min |
| 62 | final smallY = area.bottom - |
| 63 | return GamePadLayout._( |
| 64 | Rect.fromCenter( |
| 65 | center: Offset(w * 0.25 |
| 66 | width: dpadSize, |
| 67 | height: dpadSize, |
| 68 | ), |
| 69 | [ |
| 70 | PadButton( |
| 71 | GbaKey.l, |
| 72 | 'L', |
| 73 | Rect.fromLTWH(12, |
| 74 | round: false, |
| 75 | ), |
| 76 | PadButton( |
| 77 | GbaKey.r, |
| 78 | 'R', |
| 79 | Rect.fromLTWH( |
| 80 | w - 12 - shoulderWidth, |
| 81 | top + 8, |
| 82 | shoulderWidth, |
| 83 | shoulderHeight, |
| 84 | ), |
| 85 | round: false, |
| 86 | ), |
| 87 | PadButton( |
| 88 | GbaKey.a, |
| 89 | 'A', |
| 90 | Rect.fromCircle( |
| 91 | center: Offset(w * 0.85 |
| 92 | radius: buttonRadius, |
| 93 | ), |
| 94 | ), |
| 95 | PadButton( |
| 96 | GbaKey.b, |
| 97 | 'B', |
| 98 | Rect.fromCircle( |
| 99 | center: Offset(w * 0.66 |
| 100 | radius: buttonRadius, |
| 101 | ), |
| 102 | ), |
| 103 | PadButton( |
| 104 | GbaKey.select, |
| 105 | 'SELECT', |
| 106 | Rect.fromCenter( |
| 107 | center: Offset(w * 0.39 |
| 108 | width: smallWidth, |
| 109 | height: smallHeight, |
| 110 | ), |
| 111 | round: false, |
| 112 | ), |
| 113 | PadButton( |
| 114 | GbaKey.start, |
| 115 | 'START', |
| 116 | Rect.fromCenter( |
| 117 | center: Offset(w * 0.61 |
| 118 | width: smallWidth, |
| 119 | height: smallHeight, |
| 120 | ), |
| 121 | round: false, |
| 122 | ), |
| 123 | ], |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | static GamePadLayout _landscape |
| 128 | final left = Rect. |
| 129 | final right = Rect. |
| 130 | final h = size.height; |
| 131 | final panel = left.width; |
| 132 | final shoulderTop = topReserved + 8 |
| 133 | final shoulderHeight = math.min |
| 134 | final dpadSize = math.min |
| 135 | final buttonRadius = math.min |
| 136 | final smallHeight = math.min |
| 137 | return GamePadLayout._( |
| 138 | Rect.fromCenter( |
| 139 | center: Offset( |
| 140 | left.center.dx, |
| 141 | math.max(h * 0.52, |
| 142 | ), |
| 143 | width: dpadSize, |
| 144 | height: dpadSize, |
| 145 | ), |
| 146 | [ |
| 147 | PadButton( |
| 148 | GbaKey.l, |
| 149 | 'L', |
| 150 | Rect.fromLTWH( |
| 151 | left.left + 12, |
| 152 | shoulderTop, |
| 153 | left.width - 24, |
| 154 | shoulderHeight, |
| 155 | ), |
| 156 | round: false, |
| 157 | ), |
| 158 | PadButton( |
| 159 | GbaKey.r, |
| 160 | 'R', |
| 161 | Rect.fromLTWH( |
| 162 | right.left + 12, |
| 163 | shoulderTop, |
| 164 | right.width - 24, |
| 165 | shoulderHeight, |
| 166 | ), |
| 167 | round: false, |
| 168 | ), |
| 169 | PadButton( |
| 170 | GbaKey.a, |
| 171 | 'A', |
| 172 | Rect.fromCircle( |
| 173 | center: Offset(right.left + |
| 174 | radius: buttonRadius, |
| 175 | ), |
| 176 | ), |
| 177 | PadButton( |
| 178 | GbaKey.b, |
| 179 | 'B', |
| 180 | Rect.fromCircle( |
| 181 | center: Offset(right.left + |
| 182 | radius: buttonRadius, |
| 183 | ), |
| 184 | ), |
| 185 | PadButton( |
| 186 | GbaKey.select, |
| 187 | 'SELECT', |
| 188 | Rect.fromCenter( |
| 189 | center: Offset(left.center. |
| 190 | width: left.width * 0.6, |
| 191 | height: smallHeight, |
| 192 | ), |
| 193 | round: false, |
| 194 | ), |
| 195 | PadButton( |
| 196 | GbaKey.start, |
| 197 | 'START', |
| 198 | Rect.fromCenter( |
| 199 | center: Offset(right.center. |
| 200 | width: right.width * 0.6, |
| 201 | height: smallHeight, |
| 202 | ), |
| 203 | round: false, |
| 204 | ), |
| 205 | ], |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | /// The buttons a touch at [point] presses. |
| 210 | int keysAt(Offset |
| 211 | var keys = _dpadKeys(point) |
| 212 | for (final button in buttons) { |
| 213 | if (button.hit(point)) keys |= |
| 214 | } |
| 215 | return keys; |
| 216 | } |
| 217 | |
| 218 | /// Eight directions, by angle from the D-pad's centre; the middle is a |
| 219 | /// small dead zone. The touch area reaches a little beyond the drawing. |
| 220 | int _dpadKeys(Offset |
| 221 | final offset = point - dpad. |
| 222 | final radius = dpad.width / |
| 223 | final distance = offset.distance; |
| 224 | if (distance < radius * 0.18 || |
| 225 | // 0 = right, counting clockwise (screen y points down) in eighths. |
| 226 | final sector = |
| 227 | ((math.atan2(offset.dy, offset |
| 228 | return const [ |
| 229 | GbaKey.right, |
| 230 | GbaKey.right | GbaKey |
| 231 | GbaKey.down, |
| 232 | GbaKey.down | GbaKey |
| 233 | GbaKey.left, |
| 234 | GbaKey.left | GbaKey |
| 235 | GbaKey.up, |
| 236 | GbaKey.up | GbaKey |
| 237 | ][sector]; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// The on-screen controls. Each finger is tracked separately, so buttons |
| 242 | /// can be held together and a thumb can slide from one to another. |
| 243 | class GamePad extends StatefulWidget |
| 244 | const GamePad({super |
| 245 | |
| 246 | final GamePadLayout layout; |
| 247 | |
| 248 | /// Called with the mask of held [GbaKey]s whenever it changes. |
| 249 | final ValueChanged<int |
| 250 | |
| 251 | @override |
| 252 | State<GamePad> createState |
| 253 | } |
| 254 | |
| 255 | class _GamePadState extends State |
| 256 | final Map<int |
| 257 | int _keys = 0; |
| 258 | |
| 259 | void _update() { |
| 260 | var keys = 0; |
| 261 | for (final point in _pointers. |
| 262 | keys |= widget.layout.keysAt |
| 263 | } |
| 264 | if (keys == _keys) return; |
| 265 | if ((keys & ~_keys) != 0 |
| 266 | setState(() => _keys = keys); |
| 267 | widget.onKeysChanged(keys); |
| 268 | } |
| 269 | |
| 270 | @override |
| 271 | Widget build(BuildContext |
| 272 | return Listener( |
| 273 | behavior: HitTestBehavior.opaque, |
| 274 | onPointerDown: (event) { |
| 275 | _pointers[event.pointer] = event.localPosition; |
| 276 | _update(); |
| 277 | }, |
| 278 | onPointerMove: (event) { |
| 279 | _pointers[event.pointer] = event.localPosition; |
| 280 | _update(); |
| 281 | }, |
| 282 | onPointerUp: (event) { |
| 283 | _pointers.remove(event.pointer); |
| 284 | _update(); |
| 285 | }, |
| 286 | onPointerCancel: (event) { |
| 287 | _pointers.remove(event.pointer); |
| 288 | _update(); |
| 289 | }, |
| 290 | child: CustomPaint( |
| 291 | painter: _GamePadPainter(widget.layout, |
| 292 | size: Size.infinite, |
| 293 | ), |
| 294 | ); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | class _GamePadPainter extends CustomPainter |
| 299 | _GamePadPainter(this.layout, |
| 300 | |
| 301 | final GamePadLayout layout; |
| 302 | final int keys; |
| 303 | |
| 304 | static const Color |
| 305 | static const Color |
| 306 | static const Color |
| 307 | |
| 308 | @override |
| 309 | void paint(Canvas |
| 310 | _paintDpad(canvas); |
| 311 | for (final button in layout. |
| 312 | final paint = Paint() |
| 313 | ..color = (keys & button.key) != |
| 314 | if (button.round) { |
| 315 | canvas.drawCircle( |
| 316 | button.rect.center, |
| 317 | button.rect.shortestSide / 2, |
| 318 | paint, |
| 319 | ); |
| 320 | } else { |
| 321 | canvas.drawRRect( |
| 322 | RRect.fromRectAndRadius( |
| 323 | button.rect, |
| 324 | Radius.circular(button.rect |
| 325 | ), |
| 326 | paint, |
| 327 | ); |
| 328 | } |
| 329 | _label( |
| 330 | canvas, |
| 331 | button.label, |
| 332 | button.rect.center, |
| 333 | button.round ? button.rect.height * |
| 334 | ); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void _paintDpad(Canvas |
| 339 | final rect = layout.dpad; |
| 340 | final arm = rect.width / |
| 341 | final center = rect.center; |
| 342 | void part(int |
| 343 | canvas.drawRRect( |
| 344 | RRect.fromRectAndRadius(r, |
| 345 | Paint()..color = (keys & |
| 346 | ); |
| 347 | } |
| 348 | |
| 349 | part(GbaKey.up, |
| 350 | part( |
| 351 | GbaKey.down, |
| 352 | Rect.fromLTWH(center.dx |
| 353 | ); |
| 354 | part(GbaKey.left, |
| 355 | part( |
| 356 | GbaKey.right, |
| 357 | Rect.fromLTWH(rect.right |
| 358 | ); |
| 359 | canvas.drawRect( |
| 360 | Rect.fromCenter(center: center |
| 361 | Paint()..color = _idle, |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | void _label(Canvas |
| 366 | final painter = TextPainter( |
| 367 | text: TextSpan( |
| 368 | text: text, |
| 369 | style: TextStyle( |
| 370 | color: _text, |
| 371 | fontSize: fontSize, |
| 372 | fontWeight: FontWeight.w600, |
| 373 | ), |
| 374 | ), |
| 375 | textDirection: TextDirection.ltr, |
| 376 | )..layout(); |
| 377 | painter.paint( |
| 378 | canvas, |
| 379 | center - Offset(painter.width / |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | @override |
| 384 | bool shouldRepaint(_GamePadPainter |
| 385 | old.keys != keys || old.layout != |
| 386 | } |