Add modular story library and shared-progress translations

This commit is contained in:
Hadrian Burkhardt
2026-11-03 17:15:15 +01:00
parent 5b9a76fb53
commit d45cd0698a
30 changed files with 2694 additions and 495 deletions
+136 -128
View File
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import '../domain/puzzle.dart';
import 'paper.dart';
import 'translation.dart';
class Board extends StatelessWidget {
const Board({
@@ -25,149 +26,155 @@ class Board extends StatelessWidget {
final double maxWidth;
@override
Widget build(BuildContext context) => PageSwipeBlocker(
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,
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: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(width: u, height: u),
for (var col = 0; col < n; col++)
clue(
'${puzzle.cols[col]}',
complete(
List.generate(n, (i) => i * n + col),
puzzle.cols[col],
),
),
],
),
for (var row = 0; row < n; row++)
);
bool complete(Iterable<int> indices, int total) =>
indices.where((i) => cells[i] == 1).length == total;
return Align(
alignment: Alignment.center,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
clue(
'${puzzle.rows[row]}',
complete(
List.generate(n, (i) => row * n + i),
puzzle.rows[row],
),
),
SizedBox(width: u, height: u),
for (var col = 0; col < n; col++)
Builder(
builder: (context) {
final i = row * n + col,
value = cells[i],
fixed = puzzle.given[i],
inside = r?.contains(i, n) ?? false;
final state = value == 0
? 'empty'
: value == 1
? 'lit'
: 'crossed out';
return Semantics(
label:
clue(
'${puzzle.cols[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.rows[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],
inside = r?.contains(i, n) ?? false;
final state = value == 0
? 'empty'
: value == 1
? 'lit'
: 'crossed out';
return Semantics(
label: CopyScope.text(
context,
'Row ${row + 1}, column ${col + 1}, $state${fixed ? ', fixed clue' : ''}',
button: !fixed && !won,
enabled: !fixed && !won,
onTap: fixed || won ? null : () => onMark(i),
excludeSemantics: true,
child: GestureDetector(
key: ValueKey('cell-$i'),
behavior: HitTestBehavior.opaque,
),
button: !fixed && !won,
enabled: !fixed && !won,
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: .22)
: Colors.transparent,
borderRadius: BorderRadius.circular(
2,
),
border: Border.all(
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
? const Color(0xffaa5b2a)
: muted.withValues(
alpha: .48,
),
width: inside ? 1.4 : .65,
? amber.withValues(alpha: .22)
: Colors.transparent,
borderRadius:
BorderRadius.circular(2),
border: Border.all(
color: inside
? const Color(0xffaa5b2a)
: muted.withValues(
alpha: .48,
),
width: inside ? 1.4 : .65,
),
),
),
child: Stack(
children: [
if (value != 0)
Positioned.fill(
child: CustomPaint(
painter: PencilMark(
value: value,
variation: i,
child: Stack(
children: [
if (value != 0)
Positioned.fill(
child: CustomPaint(
painter: PencilMark(
value: value,
variation: i,
),
),
),
),
if (fixed)
Positioned(
right: 3,
bottom: 3,
child: Container(
width: 4,
height: 4,
decoration: BoxDecoration(
color: muted,
shape: BoxShape.circle,
if (fixed)
Positioned(
right: 3,
bottom: 3,
child: Container(
width: 4,
height: 4,
decoration: BoxDecoration(
color: muted,
shape: BoxShape.circle,
),
),
),
),
],
],
),
),
),
),
),
),
);
},
),
],
),
],
),
);
},
);
},
),
],
),
],
),
);
},
),
),
),
),
@@ -265,7 +272,7 @@ class PixelPicture extends StatelessWidget {
@override
Widget build(BuildContext context) => Semantics(
image: true,
label: 'Recovered pixel picture: $label',
label: CopyScope.text(context, 'Recovered pixel picture: $label'),
child: Center(
child: SizedBox(
width: height,
@@ -307,7 +314,7 @@ class PatternStamp extends StatelessWidget {
@override
Widget build(BuildContext context) => Semantics(
image: true,
label: 'Your restored seven by seven pattern: $label',
label: CopyScope.text(context, 'Your restored pattern: $label'),
child: SizedBox.square(
dimension: height,
child: CustomPaint(painter: _Pixels(picture)),
@@ -320,10 +327,11 @@ class _Pixels extends CustomPainter {
final List<int> picture;
@override
void paint(Canvas c, Size s) {
final p = Paint()..color = blue, u = s.width / 7;
for (var i = 0; i < 49; i++) {
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 % 7 * u, i ~/ 7 * u, u + .3, u + .3), p);
c.drawRect(Rect.fromLTWH(i % n * u, i ~/ n * u, u + .3, u + .3), p);
}
}
}
+3 -1
View File
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'translation.dart';
import 'board.dart';
import 'paper.dart';
@@ -90,7 +92,7 @@ class _DiscoveryState extends State<Discovery>
PatternStamp(widget.picture, label: widget.label, height: 56),
const SizedBox(width: 14),
Flexible(
child: Text(
child: CopyText(
'Your restored pattern\n${widget.label}',
style: hand(18, color: muted),
),
+162
View File
@@ -0,0 +1,162 @@
part of '../main.dart';
// The original story's folded note and repair inspection are optional book
// presentation behavior. Standard story packages use the reusable page renderer.
extension _MaraStory on _NotebookState {
Widget maraStory() {
final p = g.book.pages[g.storyIndex];
final heading = <Widget>[
folio('THE COTTAGE / ${g.storyIndex + 1} OF ${g.book.pages.length}'),
writing(p.title, size: 31),
];
final body = <Widget>[...heading];
Widget? wideBody;
switch (p.type) {
case StoryPageType.dedication:
body.addAll([
writing('A few things worth keeping', size: 20, color: muted),
divider(),
reading(p.text),
audio(),
]);
case StoryPageType.entry:
body.addAll([
if (p.illustrationAsset case final asset?)
Image.asset(
asset,
height: 155,
fit: BoxFit.contain,
semanticLabel: 'Watercolour of Gran’s stone cottage, blue door and warm window.',
),
writing(p.speaker, size: 17, color: muted),
reading(p.text),
if (p.continuation.isNotEmpty) reading(p.continuation),
if (g.book.tracks.containsKey(p.id)) audio(),
if (p.campaignIndex != 0)
action('Turn to the pattern →', () => turn(1)),
]);
case StoryPageType.puzzle:
final intro = writing(
g.campaignIndex < 2
? 'Restore a small light, or leave a bookmark and read on.'
: 'Compare the shared areas. Each cell counts in every area it belongs to.',
size: 21,
color: muted,
);
body.addAll([intro, puzzleBody()]);
wideBody = splitPuzzle(
side: [...heading, intro, areaPicker(), ...puzzleNotes()],
);
case StoryPageType.memory:
body.addAll([
discoveryArt(g.book.levels[p.campaignIndex!], height: 155),
writing(
g.recovered(p.campaignIndex!)
? 'A pattern restored · a memory kept'
: 'A memory to read · your puzzle can wait',
size: 17,
color: muted,
),
reading(p.text),
if (p.continuation.isNotEmpty) reading(p.continuation),
if (g.book.tracks.containsKey(p.id)) audio(),
action(
g.recovered(p.campaignIndex!)
? 'View the solved pattern'
: 'Return to this pattern',
() => g.showStory(
g.book.pageFor(p.campaignIndex!, StoryPageType.puzzle),
),
),
]);
case StoryPageType.note:
if (!prefs.flag('notebook_note_open')) {
body.addAll([
writing('A folded scrap, tucked between the leaves.'),
GestureDetector(
onTap: openLetter,
child: const Sketch('envelope', height: 190),
),
action('Unfold Gran’s note', openLetter),
writing(
'You don’t have to solve anything to discover this.',
size: 20,
color: muted,
),
]);
} else {
body.addAll([
writing(
'For the crossing',
size: 28,
color: const Color(0xff70503b),
),
reading(p.text),
audio(),
action('Fold the note again', () {
unawaited(narrator.stop());
unawaited(prefs.set('notebook_note_open', false));
refreshNotebook();
}),
]);
}
case StoryPageType.inspection:
body.addAll([
writing(
prefs.flag('notebook_handle_seen')
? 'A repair made to keep small fingers safe.'
: 'A repair note, tucked into the margin.',
size: 20,
color: muted,
),
reading(p.text),
audio(),
action('Read Gran’s margin note', inspect),
action('Follow the lantern into the next entry →', () => turn(1)),
]);
case StoryPageType.closing:
body.addAll([
reading(p.text),
if (g.book.tracks.containsKey(p.id)) audio(),
writing(
'Four memories in the cottage · ${List.generate(4, (i) => g.recovered(i)).where((v) => v).length} patterns restored',
size: 19,
color: muted,
),
action(
'Take the path to the harbour →',
crossToHarbour,
primary: true,
),
writing(
'Your unfinished patterns will wait here for you.',
size: 19,
color: muted,
),
]);
}
return Leaf(
key: ValueKey('story-${g.storyIndex}'),
onTurn: (d) => turn(d),
body: column(body),
wideBody: wideBody,
footer: navigation(),
);
}
void openLetter() {
unawaited(prefs.set('notebook_note_open', true));
_scheduleAutoplay();
refreshNotebook();
}
Future<void> inspect() async {
await prefs.set('notebook_handle_seen', true);
await message(
'A careful repair',
'The wire is hidden under a strip of cloth. In the margin, Gran has written: “The light matters. So do the hands carrying it.”',
close: 'Keep that thought',
);
if (mounted) refreshNotebook();
}
}
+6 -3
View File
@@ -1,6 +1,9 @@
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'translation.dart';
import 'package:flutter/gestures.dart';
const ink = Color(0xff36413c),
@@ -16,7 +19,7 @@ TextStyle hand(double size, {Color color = ink}) => TextStyle(
);
Widget writing(String value, {double size = 23, Color color = ink}) => Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: Text(value, style: hand(size, color: color)),
child: CopyText(value, style: hand(size, color: color)),
);
class InkButton extends StatelessWidget {
@@ -49,7 +52,7 @@ class InkButton extends StatelessWidget {
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 8),
shape: const RoundedRectangleBorder(),
),
child: Text(label, textAlign: TextAlign.center),
child: CopyText(label, textAlign: TextAlign.center),
),
),
);
@@ -241,7 +244,7 @@ class Cover extends StatelessWidget {
child: Center(
child: Transform.rotate(
angle: -.044,
child: Text(
child: CopyText(
'Mara’s\nisland notebook',
textAlign: TextAlign.center,
style: hand(28),
+74
View File
@@ -0,0 +1,74 @@
import 'package:flutter/material.dart';
import '../services/translation.dart';
Locale localeFromTag(String tag) {
final parts = tag.replaceAll('_', '-').split('-');
final script = parts.skip(1).where((p) => p.length == 4).firstOrNull;
final region = parts
.skip(1)
.where((p) => p.length == 2 || p.length == 3)
.firstOrNull;
return Locale.fromSubtags(
languageCode: parts.first.toLowerCase(),
scriptCode: script == null
? null
: '${script[0].toUpperCase()}${script.substring(1).toLowerCase()}',
countryCode: region?.toUpperCase(),
);
}
class CopyScope extends InheritedWidget {
CopyScope({
super.key,
required this.catalog,
required this.language,
this.overrides = const {},
required super.child,
}) : translator = catalog.translator(language, overrides: overrides);
final TranslationCatalog catalog;
final MessageTranslator translator;
final String language;
final Map<String, String> overrides;
static String text(BuildContext context, String source) {
final scope = context.dependOnInheritedWidgetOfExactType<CopyScope>();
return scope?.translator.text(source) ?? source;
}
@override
bool updateShouldNotify(CopyScope old) =>
old.language != language ||
old.catalog != catalog ||
old.overrides != overrides;
}
class CopyText extends StatelessWidget {
const CopyText(this.data, {super.key, this.style, this.textAlign});
final String data;
final TextStyle? style;
final TextAlign? textAlign;
@override
Widget build(BuildContext context) {
final language = Localizations.maybeLocaleOf(context)?.languageCode ?? 'en';
final latin = const [
'en',
'de',
'fr',
'es',
'it',
'pt',
'nl',
'da',
'sv',
'no',
'fi',
].contains(language);
return Text(
CopyScope.text(context, data),
style: latin
? style
: (style ?? const TextStyle()).copyWith(fontFamily: 'sans-serif'),
textAlign: textAlign,
);
}
}