259 lines
9.4 KiB
Dart
259 lines
9.4 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 default target is painted as vectors, so its rings and numbers stay
|
||
/// sharp at any size or zoom; the other designs are their v3 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
|
||
|
||
const TargetFace({
|
||
super.key,
|
||
required this.style,
|
||
this.hits = const [],
|
||
this.hitColor = const Color(0xFF22C7E0),
|
||
this.highlighted = const {},
|
||
this.dimmed = const {},
|
||
this.showScoring = false,
|
||
});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final asset = style.asset;
|
||
return AspectRatio(
|
||
aspectRatio: 1,
|
||
child: Stack(
|
||
fit: StackFit.expand,
|
||
children: [
|
||
if (asset == null)
|
||
const RepaintBoundary(child: CustomPaint(painter: DefaultTargetPainter(glow: true)))
|
||
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),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 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(glow: true, 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 · brez sijaja") in vectors: white paper
|
||
/// ending on ring 1, black 7–10, numbers 1–8 on the four axes. [glow] adds a
|
||
/// small glow (white rim, short light-blue halo).
|
||
class DefaultTargetPainter extends CustomPainter {
|
||
final bool glow;
|
||
final bool numbers;
|
||
|
||
const DefaultTargetPainter({this.glow = false, this.numbers = true});
|
||
|
||
static const double _paper = 510;
|
||
static const Color _glowOuter = Color(0xFF9FD8FF);
|
||
|
||
// 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) {
|
||
final s = size.shortestSide / TargetScoring.size;
|
||
canvas.save();
|
||
canvas.translate((size.width - size.shortestSide) / 2, (size.height - size.shortestSide) / 2);
|
||
canvas.scale(s);
|
||
const c = TargetScoring.center;
|
||
|
||
if (glow) {
|
||
// figcraft drop-shadow "radius" is the blur; Flutter wants sigma ≈ blur / 2
|
||
void blurDisc(double r, Color color, double blur) => canvas.drawCircle(
|
||
c, r, Paint()..color = color..maskFilter = MaskFilter.blur(BlurStyle.normal, blur / 2));
|
||
// A small glow: a short light-blue halo and a white rim
|
||
blurDisc(_paper + 2, _glowOuter.withValues(alpha: 0.55), 40);
|
||
blurDisc(_paper + 1, Colors.white.withValues(alpha: 0.7), 10);
|
||
}
|
||
|
||
// Paper, then the black (zones 7–10)
|
||
canvas.drawCircle(c, _paper, Paint()..color = Colors.white);
|
||
canvas.drawCircle(c, ring(7), Paint()..color = Colors.black);
|
||
|
||
final line = Paint()
|
||
..style = PaintingStyle.stroke
|
||
..isAntiAlias = true;
|
||
for (int k = 1; k <= 6; k++) {
|
||
canvas.drawCircle(c, ring(k), line..color = Colors.black..strokeWidth = 3);
|
||
}
|
||
for (final k in [8, 9]) {
|
||
canvas.drawCircle(c, ring(k), line..color = Colors.white..strokeWidth = 5);
|
||
}
|
||
canvas.drawCircle(c, ring(10), Paint()..color = Colors.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.glow != glow || 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) {
|
||
final s = size.shortestSide / TargetScoring.size;
|
||
canvas.save();
|
||
canvas.translate((size.width - size.shortestSide) / 2, (size.height - size.shortestSide) / 2);
|
||
canvas.scale(s);
|
||
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 = hits[i];
|
||
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.05 : 0.18)..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3));
|
||
canvas.drawCircle(p, r, Paint()..color = color.withValues(alpha: faded ? 0.25 : (selected ? 0.9 : 0.78)));
|
||
canvas.drawCircle(
|
||
p,
|
||
r - 1.5,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 3
|
||
..color = rim.withValues(alpha: faded ? 0.2 : 0.8));
|
||
// Selected: a 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) {
|
||
final s = size.shortestSide / TargetScoring.size;
|
||
canvas.save();
|
||
canvas.translate((size.width - size.shortestSide) / 2, (size.height - size.shortestSide) / 2);
|
||
canvas.scale(s);
|
||
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;
|
||
}
|