Files
2026-11-14 10:10:36 +01:00

577 lines
25 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../domain/puzzle.dart';
import 'paper.dart';
import 'translation.dart';
const regionInk = Color(0xff84421f);
class RegionButton extends StatelessWidget {
const RegionButton({
super.key,
required this.label,
required this.onPressed,
required this.selected,
});
final String label;
final VoidCallback onPressed;
final bool selected;
@override
Widget build(BuildContext context) => PageSwipeBlocker(
child: Semantics(
selected: selected,
child: TextButton(
onPressed: onPressed,
style: TextButton.styleFrom(
foregroundColor: selected ? cream : regionInk,
backgroundColor: selected ? regionInk : cream.withValues(alpha: .5),
textStyle: hand(21).copyWith(
fontWeight: selected ? FontWeight.bold : FontWeight.normal,
),
minimumSize: const Size(48, 48),
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
side: BorderSide(color: regionInk, width: selected ? 2.4 : 1),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
),
child: CopyText(label),
),
),
);
}
class Board extends StatelessWidget {
const Board({
super.key,
required this.puzzle,
required this.cells,
required this.onMark,
this.selectedArea = 0,
this.won = false,
this.maxWidth = 350,
});
final Puzzle puzzle;
final List<int> cells;
final ValueChanged<int> onMark;
final int selectedArea;
final bool won;
/// Wide leaves give the grid more room; it still stays a square.
final double maxWidth;
@override
Widget build(BuildContext context) => PageSwipeBlocker(
child: Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxWidth),
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, c) {
final n = puzzle.size,
u = c.maxWidth / (n + 1.3),
r = puzzle.regions.isEmpty
? null
: puzzle.regions[selectedArea];
Widget clue(String text, bool complete) => SizedBox(
width: u,
height: u,
child: Center(
child: Text(
text,
style: hand(
(u * .42).clamp(16, 23),
color: complete ? blue : muted,
),
),
),
);
bool complete(Iterable<int> indices, int total) =>
indices.where((i) => cells[i] == 1).length == total;
return Align(
alignment: Alignment.center,
child: SizedBox(
width: (n + 1) * u,
height: (n + 1) * u,
child: Stack(
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(width: u, height: u),
for (var col = 0; col < n; col++)
clue(
puzzle.hiddenColumn == col ||
puzzle.omittedColumn == col
? '?'
: '${puzzle.cols[col]}',
puzzle.hiddenColumn != col &&
puzzle.omittedColumn != col &&
complete(
List.generate(n, (i) => i * n + col),
puzzle.cols[col],
),
),
],
),
for (var row = 0; row < n; row++)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
clue(
puzzle.hiddenRow == row ||
puzzle.omittedRow == row
? '?'
: '${puzzle.rows[row]}',
puzzle.hiddenRow != row &&
puzzle.omittedRow != row &&
complete(
List.generate(n, (i) => row * n + i),
puzzle.rows[row],
),
),
for (var col = 0; col < n; col++)
Builder(
builder: (context) {
final i = row * n + col,
value = cells[i],
fixed = puzzle.given[i],
lantern = puzzle.lanternPairs.any(
(pair) => pair.contains(i),
),
mirrored = puzzle.mirrorPairs.any(
(pair) => pair.contains(i),
),
footprint = puzzle.footprintTrails
.any(
(trail) => trail.contains(i),
),
inside = r?.contains(i, n) ?? false;
final state = value == 0
? 'empty'
: value == 1
? 'lit'
: 'crossed out';
return Semantics(
selected: inside,
label: CopyScope.text(
context,
'Row ${row + 1}, column ${col + 1}, $state${fixed ? ', fixed clue' : ''}${lantern ? ', lantern pair: exactly one is lit' : ''}${mirrored ? ', mirrored pair: both cells match' : ''}${footprint ? ', footprint trail: two adjacent cells are lit' : ''}',
),
button: !fixed && !won,
enabled: !fixed && !won,
onTap: fixed || won
? null
: () => onMark(i),
excludeSemantics: true,
child: GestureDetector(
key: ValueKey('cell-$i'),
behavior: HitTestBehavior.opaque,
onTap: fixed || won
? null
: () => onMark(i),
child: SizedBox(
width: u,
height: u,
child: Padding(
padding: const EdgeInsets.all(
2,
),
child: DecoratedBox(
decoration: BoxDecoration(
color: inside
? amber.withValues(
alpha: .38,
)
: Colors.transparent,
borderRadius:
BorderRadius.circular(
2,
),
border: Border.all(
color: inside
? regionInk
: muted.withValues(
alpha: .48,
),
width: inside ? 1.4 : .65,
),
),
child: Stack(
children: [
if (value != 0)
Positioned.fill(
child: RepaintBoundary(
child: CustomPaint(
painter:
PencilMark(
value:
value,
variation:
i,
),
),
),
),
if (lantern)
Positioned(
left: 1,
top: 1,
child: Container(
key: ValueKey(
'lantern-$i',
),
decoration:
const BoxDecoration(
color: cream,
shape: BoxShape
.circle,
),
padding:
const EdgeInsets.all(
1,
),
child: Icon(
Icons
.lightbulb_outline,
size: u * .31,
color: regionInk,
),
),
),
if (puzzle
.hiddenTotalCells
.contains(i))
Positioned(
left: 1,
top: 1,
child: Container(
key: ValueKey(
'diamond-$i',
),
color: cream,
child: Icon(
Icons
.diamond_outlined,
size: u * .31,
color: regionInk,
),
),
),
if (mirrored)
Positioned(
left: 1,
top: 1,
child: Container(
key: ValueKey(
'mirror-$i',
),
decoration:
const BoxDecoration(
color: cream,
shape: BoxShape
.circle,
),
padding:
const EdgeInsets.all(
1,
),
child: Icon(
Icons
.auto_awesome,
size: u * .31,
color: regionInk,
),
),
),
if (footprint)
Positioned(
left: 1,
top: 1,
child: Container(
key: ValueKey(
'footprint-$i',
),
decoration:
const BoxDecoration(
color: cream,
shape: BoxShape
.circle,
),
padding:
const EdgeInsets.all(
1,
),
child: SizedBox.square(
dimension:
u * .31,
child: CustomPaint(
painter:
FootprintMark(),
),
),
),
),
if (fixed)
Positioned(
right: 3,
bottom: 3,
child: Container(
width: 4,
height: 4,
decoration:
BoxDecoration(
color: muted,
shape: BoxShape
.circle,
),
),
),
],
),
),
),
),
),
);
},
),
],
),
],
),
if (r != null)
Positioned(
left: u * (1 + r.left),
top: u * (1 + r.top),
width: u * r.width,
height: u * r.height,
child: IgnorePointer(
child: DecoratedBox(
key: const ValueKey('selected-region-outline'),
decoration: BoxDecoration(
border: Border.all(
color: regionInk,
width: 3.2,
),
borderRadius: BorderRadius.circular(3),
),
),
),
),
],
),
),
);
},
),
),
),
),
),
);
}
/// A tiny shoe print, used only as a marker inside a puzzle cell.
class FootprintMark extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = regionInk;
canvas.save();
canvas.translate(size.width * .5, size.height * .5);
canvas.rotate(-.35);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(
-size.width * .25,
-size.height * .46,
size.width * .5,
size.height * .55,
),
Radius.circular(size.width * .22),
),
paint,
);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(
-size.width * .2,
size.height * .16,
size.width * .4,
size.height * .27,
),
Radius.circular(size.width * .1),
),
paint,
);
canvas.restore();
}
@override
bool shouldRepaint(covariant FootprintMark oldDelegate) => false;
}
/// Deterministic pencil strokes stay put when the player revisits a cell.
/// Both marks use paths, so their appearance never depends on a fallback font.
class PencilMark extends CustomPainter {
const PencilMark({required this.value, required this.variation});
final int value, variation;
@override
void paint(Canvas canvas, Size size) {
final unit = math.min(size.width, size.height);
final wobble = ((variation % 5) - 2) * .006;
final paint = Paint()
..isAntiAlias = true
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round;
canvas.save();
canvas.scale(size.width, size.height);
if (value == 1) {
paint
..color = blue.withValues(alpha: .78)
..strokeWidth = 1.65 / unit;
// Closely spaced, slightly wandering strokes shade the cell by hand.
canvas.clipRect(const Rect.fromLTWH(.13, .13, .74, .74));
for (var line = -12; line <= 20; line++) {
final x = line * .065;
canvas.drawPath(
Path()
..moveTo(x, .90)
..quadraticBezierTo(x + .34 + wobble, .52, x + .68, .09),
paint,
);
}
paint
..color = blue.withValues(alpha: .34)
..strokeWidth = .8 / unit;
for (var line = -6; line <= 12; line++) {
final x = line * .13;
canvas.drawPath(
Path()
..moveTo(x, .12)
..quadraticBezierTo(x + .3, .50 + wobble, x + .59, .89),
paint,
);
}
} else {
paint
..color = muted
..strokeWidth = 1.8 / unit;
canvas.drawPath(
Path()
..moveTo(.26, .24 + wobble)
..cubicTo(.43, .38, .58, .64, .76, .77),
paint,
);
canvas.drawPath(
Path()
..moveTo(.75, .22)
..cubicTo(.61, .42 + wobble, .40, .59, .24, .78),
paint,
);
paint
..color = muted.withValues(alpha: .30)
..strokeWidth = .65 / unit;
canvas.drawPath(
Path()
..moveTo(.28, .25)
..quadraticBezierTo(.54, .56, .74, .79),
paint,
);
}
canvas.restore();
}
@override
bool shouldRepaint(PencilMark old) =>
value != old.value || variation != old.variation;
}
class PixelPicture extends StatelessWidget {
const PixelPicture(
this.picture, {
super.key,
required this.label,
this.height = 230,
});
final List<int> picture;
final String label;
final double height;
@override
Widget build(BuildContext context) => Semantics(
image: true,
label: CopyScope.text(context, 'Recovered pixel picture: $label'),
child: Center(
child: SizedBox(
width: height,
height: height,
child: Padding(
padding: const EdgeInsets.all(24),
child: DecoratedBox(
decoration: BoxDecoration(
color: const Color(0xfffaf1d8),
boxShadow: [
BoxShadow(
color: ink.withValues(alpha: .15),
offset: const Offset(3, 4),
),
],
),
child: Padding(
padding: const EdgeInsets.all(16),
child: CustomPaint(painter: _Pixels(picture)),
),
),
),
),
),
);
}
class PatternStamp extends StatelessWidget {
const PatternStamp(
this.picture, {
super.key,
required this.label,
required this.height,
});
final List<int> picture;
final String label;
final double height;
@override
Widget build(BuildContext context) => Semantics(
image: true,
label: CopyScope.text(context, 'Your restored pattern: $label'),
child: SizedBox.square(
dimension: height,
child: CustomPaint(painter: _Pixels(picture)),
),
);
}
class _Pixels extends CustomPainter {
_Pixels(this.picture);
final List<int> picture;
@override
void paint(Canvas c, Size s) {
final n = math.sqrt(picture.length).round();
final p = Paint()..color = blue, u = s.width / n;
for (var i = 0; i < picture.length; i++) {
if (picture[i] == 1) {
c.drawRect(Rect.fromLTWH(i % n * u, i ~/ n * u, u + .3, u + .3), p);
}
}
}
@override
bool shouldRepaint(_Pixels old) => old.picture != picture;
}