291 lines
10 KiB
Dart
291 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
||
import '../models/target_style.dart';
|
||
import '../utils/target_scoring.dart';
|
||
|
||
/// A target design with hits on it, drawn in the shared 1200 × 1200 space and
|
||
/// scaled to the space it gets (always square). The designs are transparent
|
||
/// outside the target; a miss is drawn where it landed, on the screen
|
||
/// background. The small previews ([zoomToHits]) show the target only,
|
||
/// without the misses.
|
||
///
|
||
/// The default target is painted as vectors, so its rings and numbers stay
|
||
/// sharp at any size or zoom; the other designs are their v5 images.
|
||
class TargetFace extends StatelessWidget {
|
||
final TargetStyle style;
|
||
final List<Offset> hits; // in target space (0–1200)
|
||
final Color hitColor;
|
||
final Set<int> highlighted; // indexes of [hits] drawn with a ring
|
||
final Set<int> dimmed; // indexes drawn faded (e.g. other values while one is pressed)
|
||
final bool showScoring; // debug: the radii the scoring uses, over the design
|
||
final bool zoomToHits; // small previews: the target only, zoomed in on the shots
|
||
|
||
const TargetFace({
|
||
super.key,
|
||
required this.style,
|
||
this.hits = const [],
|
||
this.hitColor = const Color(0xFF22C7E0),
|
||
this.highlighted = const {},
|
||
this.dimmed = const {},
|
||
this.showScoring = false,
|
||
this.zoomToHits = false,
|
||
});
|
||
|
||
|
||
|
||
/// How far [zoomToHits] zooms: just enough, round the centre, to keep every
|
||
/// shot (and a little air) in view; never past 3× and never so far that the
|
||
/// black and ring 6 are cut, so there is always something to read them by.
|
||
static double zoomFor(List<Offset> hits) {
|
||
if (hits.isEmpty) return 1;
|
||
var r = 0.0;
|
||
for (final h in hits) {
|
||
final d = (TargetScoring.shown(h) - TargetScoring.center).distance;
|
||
if (d > r) r = d;
|
||
}
|
||
final view = (r + TargetScoring.bulletDiameter / 2 + 30).clamp(260.0, TargetScoring.size / 2);
|
||
return (TargetScoring.size / 2 / view).clamp(1.0, 3.0);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final asset = style.asset;
|
||
// Small previews: the target only, so misses are left out
|
||
final hits = zoomToHits ? [for (final h in this.hits) if (TargetScoring.scoreAt(h) > 0) h] : this.hits;
|
||
final zoom = zoomToHits ? zoomFor(hits) : 1.0;
|
||
final face = AspectRatio(
|
||
aspectRatio: 1,
|
||
child: Stack(
|
||
fit: StackFit.expand,
|
||
children: [
|
||
if (asset == null)
|
||
const RepaintBoundary(child: CustomPaint(painter: DefaultTargetPainter()))
|
||
else
|
||
Image.asset(asset, fit: BoxFit.contain, filterQuality: FilterQuality.medium, gaplessPlayback: true),
|
||
if (showScoring) const CustomPaint(painter: ScoringRingsPainter()),
|
||
if (hits.isNotEmpty)
|
||
CustomPaint(
|
||
painter: HitsPainter(hits: hits, color: hitColor, highlighted: highlighted, dimmed: dimmed),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
if (zoom == 1) return face;
|
||
return AspectRatio(
|
||
aspectRatio: 1,
|
||
child: LayoutBuilder(
|
||
builder: (context, box) => ClipRRect(
|
||
borderRadius: BorderRadius.circular(box.biggest.shortestSide * 0.1),
|
||
child: Transform.scale(scale: zoom, child: face),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// Maps the shared target space into a square of [size], centred; returns
|
||
/// the scale (screen px per target unit).
|
||
double toFaceSpace(Canvas canvas, Size size) {
|
||
final s = size.shortestSide / TargetScoring.size;
|
||
canvas.translate((size.width - size.shortestSide) / 2, (size.height - size.shortestSide) / 2);
|
||
canvas.scale(s);
|
||
return s;
|
||
}
|
||
|
||
/// Small preview of a design, for the pickers and the settings row.
|
||
class TargetThumb extends StatelessWidget {
|
||
final TargetStyle style;
|
||
final double size;
|
||
|
||
const TargetThumb(this.style, {super.key, required this.size});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final asset = style.asset;
|
||
return SizedBox.square(
|
||
dimension: size,
|
||
child: asset == null
|
||
? CustomPaint(painter: DefaultTargetPainter(numbers: size >= 60))
|
||
: Image.asset(
|
||
asset,
|
||
fit: BoxFit.contain,
|
||
cacheWidth: (size * MediaQuery.devicePixelRatioOf(context)).round().clamp(64, 600),
|
||
filterQuality: FilterQuality.medium,
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// The default target ("00 Privzeta", target-designs/v5) in vectors: white
|
||
/// paper ending on ring 1, black 7–10, numbers 1–8 on the four axes, no glow.
|
||
class DefaultTargetPainter extends CustomPainter {
|
||
final bool numbers;
|
||
|
||
const DefaultTargetPainter({this.numbers = true});
|
||
|
||
|
||
// Outer radius of score zone k (1–9); zone 10 is the centre dot
|
||
static double ring(int k) => k == 10 ? 15 : 15 + 55 * (10 - k).toDouble();
|
||
|
||
@override
|
||
void paint(Canvas canvas, Size size) {
|
||
canvas.save();
|
||
toFaceSpace(canvas, size);
|
||
const c = TargetScoring.center;
|
||
|
||
|
||
// Ring lines are filled discs painted from the outside in (line colour out
|
||
// to r + w/2, then the zone colour from r − w/2), not stroked circles:
|
||
// Impeller draws thin stroked circles unevenly, some rings thinner than
|
||
// the rest
|
||
final black = Paint()..color = Colors.black;
|
||
final white = Paint()..color = Colors.white;
|
||
const paperLine = 2.0;
|
||
const blackLine = 3.0;
|
||
// Paper with lines 1–6 (ring 1 is its edge)
|
||
for (int k = 1; k <= 6; k++) {
|
||
canvas.drawCircle(c, ring(k) + paperLine / 2, black);
|
||
canvas.drawCircle(c, ring(k) - paperLine / 2, white);
|
||
}
|
||
// The black (zones 7–10) with white lines 8 and 9
|
||
canvas.drawCircle(c, ring(7), black);
|
||
for (final k in [8, 9]) {
|
||
canvas.drawCircle(c, ring(k) + blackLine / 2, white);
|
||
canvas.drawCircle(c, ring(k) - blackLine / 2, black);
|
||
}
|
||
canvas.drawCircle(c, ring(10), white);
|
||
|
||
if (numbers) {
|
||
for (int k = 1; k <= 8; k++) {
|
||
final tp = TextPainter(
|
||
text: TextSpan(
|
||
text: '$k',
|
||
style: TextStyle(
|
||
fontFamily: 'Inter',
|
||
fontSize: 33,
|
||
fontWeight: FontWeight.w700,
|
||
height: 1,
|
||
color: k <= 6 ? Colors.black : Colors.white,
|
||
),
|
||
),
|
||
textDirection: TextDirection.ltr,
|
||
)..layout();
|
||
final mid = ring(k) - 27.5;
|
||
for (final d in const [Offset(-1, 0), Offset(1, 0), Offset(0, -1), Offset(0, 1)]) {
|
||
final at = c + d * mid;
|
||
// Centre the cap height (≈ 0.73 em of Inter) on the zone
|
||
tp.paint(canvas, Offset(at.dx - tp.width / 2, at.dy - tp.height / 2 + 33 * 0.03));
|
||
}
|
||
}
|
||
}
|
||
canvas.restore();
|
||
}
|
||
|
||
@override
|
||
bool shouldRepaint(DefaultTargetPainter old) => old.numbers != numbers;
|
||
}
|
||
|
||
/// Hits in target space: a filled dot of [TargetScoring.bulletDiameter] with a
|
||
/// dark rim, semi-transparent so overlapping hits stay visible. Highlighted
|
||
/// ones are drawn on top with a red border on their own edge.
|
||
class HitsPainter extends CustomPainter {
|
||
final List<Offset> hits;
|
||
final Color color;
|
||
final Set<int> highlighted;
|
||
final Set<int> dimmed;
|
||
|
||
static const Color selectedColor = Color(0xFFE5322D);
|
||
|
||
const HitsPainter({required this.hits, required this.color, this.highlighted = const {}, this.dimmed = const {}});
|
||
|
||
@override
|
||
void paint(Canvas canvas, Size size) {
|
||
canvas.save();
|
||
toFaceSpace(canvas, size);
|
||
const r = TargetScoring.bulletDiameter / 2;
|
||
final rim = Color.lerp(color, Colors.black, 0.6)!;
|
||
|
||
// Highlighted last so they sit on top
|
||
final order = [
|
||
for (int i = 0; i < hits.length; i++)
|
||
if (!highlighted.contains(i)) i,
|
||
for (int i = 0; i < hits.length; i++)
|
||
if (highlighted.contains(i)) i,
|
||
];
|
||
for (final i in order) {
|
||
final p = TargetScoring.shown(hits[i]); // a shot far off is kept in the square
|
||
final selected = highlighted.contains(i);
|
||
final faded = dimmed.contains(i);
|
||
// Semi-transparent, so overlapping hits and the rings under them show through
|
||
canvas.drawCircle(p + const Offset(0, 2), r,
|
||
Paint()..color = Colors.black.withValues(alpha: faded ? 0.03 : (selected ? 0.35 : 0.18))..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3));
|
||
// Faded ones barely show; highlighted ones are solid
|
||
canvas.drawCircle(p, r, Paint()..color = color.withValues(alpha: faded ? 0.18 : (selected ? 1.0 : 0.78)));
|
||
canvas.drawCircle(
|
||
p,
|
||
r - 0.75,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 1.5
|
||
..color = rim.withValues(alpha: faded ? 0.12 : 0.8));
|
||
// Selected: a thick red border on the hit's own edge, nothing around it
|
||
if (selected) {
|
||
canvas.drawCircle(
|
||
p,
|
||
r - 3,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 6
|
||
..color = selectedColor);
|
||
}
|
||
}
|
||
canvas.restore();
|
||
}
|
||
|
||
@override
|
||
bool shouldRepaint(HitsPainter old) =>
|
||
old.hits != hits || old.color != color || old.highlighted != highlighted || old.dimmed != dimmed;
|
||
}
|
||
|
||
/// Debug overlay: the outer edge of every score zone exactly as
|
||
/// [TargetScoring] uses it, and the centre cross. The red's outer side is the
|
||
/// edge: a hole whose edge touches it (or lies inside) scores that ring.
|
||
class ScoringRingsPainter extends CustomPainter {
|
||
const ScoringRingsPainter();
|
||
|
||
@override
|
||
void paint(Canvas canvas, Size size) {
|
||
canvas.save();
|
||
final s = toFaceSpace(canvas, size);
|
||
const c = TargetScoring.center;
|
||
final line = Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 1 / s // 1 screen pixel at any size
|
||
..color = const Color(0xFFFF1744);
|
||
// Drawn just inside each edge: the red's outer side is the exact scoring
|
||
// edge, so it covers the printed line and nothing outside it
|
||
for (final r in TargetScoring.rings) {
|
||
canvas.drawCircle(c, r - line.strokeWidth / 2, line);
|
||
}
|
||
canvas.drawLine(c - const Offset(30, 0), c + const Offset(30, 0), line);
|
||
canvas.drawLine(c - const Offset(0, 30), c + const Offset(0, 30), line);
|
||
// Ring values just inside each edge, on the diagonal
|
||
for (int i = 0; i < TargetScoring.rings.length; i++) {
|
||
final r = TargetScoring.rings[i];
|
||
if (r < 40) continue;
|
||
final tp = TextPainter(
|
||
text: TextSpan(
|
||
text: '${10 - i}',
|
||
style: const TextStyle(fontFamily: 'Inter', fontSize: 22, fontWeight: FontWeight.w800, color: Color(0xFFFF1744)),
|
||
),
|
||
textDirection: TextDirection.ltr,
|
||
)..layout();
|
||
final at = c + Offset(1, 1) / 1.41421 * (r - 16);
|
||
tp.paint(canvas, at - Offset(tp.width / 2, tp.height / 2));
|
||
}
|
||
canvas.restore();
|
||
}
|
||
|
||
@override
|
||
bool shouldRepaint(ScoringRingsPainter old) => false;
|
||
}
|