Files
logic-puzzle/lib/ui/paper.dart
T
Hadrian Burkhardt 0718e3f252 Open Lumen on a responsive bookshelf and land pending story work
- Start the app on a shelf of every installed book instead of the last
  book's home. Cards show the cover, translated metadata, available
  languages, restored patterns and either "Continue reading" or a way in.
- Keep each book's saved route: the shelf is never written to
  flutter_screen, so choosing a book resumes its own leaf.
- Lay the shelf out responsively: dense single-column cards on phones,
  a two-column grid from 720 logical pixels, a centred 860 px column on
  tablets and a chevron that exists in the bundled handwriting font.
- Keep Settings reachable from the shelf and let the home page's story
  library button open the shelf instead of a dialog.
- Add tool/shelf_screenshots_test.dart, which renders the shelf at phone
  and tablet metrics, checks that every card and Settings stay reachable
  without overflow, and writes artifacts/library/shelf-*.png.
- Commit the pending Midnight Invitation package (campaign, notebook,
  narration and artwork), cast drama and ambience tooling, region
  selection, phone/tablet integration coverage and related docs.

flutter analyze is clean and all 74 widget/domain tests pass.
2026-11-05 14:29:50 +01:00

351 lines
11 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'translation.dart';
import 'package:flutter/gestures.dart';
const ink = Color(0xff36413c),
blue = Color(0xff274956),
muted = Color(0xff796b55),
cream = Color(0xfff3e7cc),
amber = Color(0xffe0b568);
TextStyle hand(double size, {Color color = ink}) => TextStyle(
fontFamily: 'MaraHand',
fontSize: size,
height: 1.30,
color: color,
);
Widget writing(String value, {double size = 23, Color color = ink}) => Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: CopyText(value, style: hand(size, color: color)),
);
class InkButton extends StatelessWidget {
const InkButton(
this.label,
this.onPressed, {
super.key,
this.size = 23,
this.primary = false,
});
final String label;
final VoidCallback? onPressed;
final double size;
final bool primary;
@override
Widget build(BuildContext context) => PageSwipeBlocker(
child: DecoratedBox(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: blue.withValues(alpha: primary ? .5 : .18)),
),
),
child: TextButton(
onPressed: onPressed,
style: TextButton.styleFrom(
foregroundColor: blue,
disabledForegroundColor: muted.withValues(alpha: .4),
textStyle: hand(size),
minimumSize: const Size(48, 48),
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 8),
shape: const RoundedRectangleBorder(),
),
child: CopyText(label, textAlign: TextAlign.center),
),
),
);
}
class Paper extends StatefulWidget {
const Paper({super.key, required this.child, this.onTurn});
final Widget child;
final ValueChanged<int>? onTurn;
@override
State<Paper> createState() => _PaperState();
}
/// Artwork follows the selected story on reading, contents and puzzle pages.
class PaperSkin extends InheritedWidget {
const PaperSkin({super.key, required this.asset, required super.child});
final String? asset;
static String? assetOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<PaperSkin>()?.asset;
@override
bool updateShouldNotify(PaperSkin oldWidget) => asset != oldWidget.asset;
}
class _PaperState extends State<Paper> {
Offset _drag = Offset.zero;
Offset _origin = Offset.zero;
@override
Widget build(BuildContext context) => ColoredBox(
color: PaperSkin.assetOf(context) == null
? const Color(0xff314345)
: const Color(0xff241e27),
child: SafeArea(
child: LayoutBuilder(
builder: (context, c) {
final landscape = c.maxWidth > c.maxHeight;
final scale = landscape
? c.maxWidth / 1024
: math.min(c.maxWidth / 1024, c.maxHeight / 1536);
final w = 1024 * scale,
h = 1536 * scale,
left = (c.maxWidth - w) / 2,
top = (c.maxHeight - h) / 2;
final narrow = c.maxWidth < 480;
final content = landscape
? Rect.fromLTRB(
c.maxWidth * .10,
c.maxHeight * .06,
c.maxWidth * .83,
c.maxHeight * .94,
)
: Rect.fromLTRB(
left + w * (narrow ? .08 : .11),
top + h * .12,
left + w * (narrow ? .91 : .83),
top + h * .91,
);
return Listener(
onPointerCancel: (_) {
_drag = Offset.zero;
},
child: GestureDetector(
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onHorizontalDragStart: widget.onTurn == null
? null
: (details) {
_origin = details.globalPosition;
_drag = Offset.zero;
},
onHorizontalDragUpdate: widget.onTurn == null
? null
: (details) {
_drag = details.globalPosition - _origin;
},
onHorizontalDragCancel: widget.onTurn == null
? null
: () {
_drag = Offset.zero;
},
onHorizontalDragEnd: widget.onTurn == null
? null
: (_) {
final drag = _drag;
_drag = Offset.zero;
if (drag.dx.abs() >= 48 &&
drag.dx.abs() > drag.dy.abs() * 1.3) {
widget.onTurn?.call(drag.dx < 0 ? 1 : -1);
}
},
child: Stack(
clipBehavior: Clip.hardEdge,
children: [
Positioned(
left: left,
top: top,
width: w,
height: h,
child: Image.asset(
PaperSkin.assetOf(context) ??
'assets/artwork/mara-single-page-v2.png',
fit: BoxFit.fill,
excludeFromSemantics: true,
),
),
Positioned.fromRect(rect: content, child: widget.child),
],
),
),
);
},
),
),
);
}
/// A descendant horizontal recognizer wins over the page recognizer on drags.
/// Taps still reach the controls, and vertical drags can reach a ScrollView.
class PageSwipeBlocker extends StatelessWidget {
const PageSwipeBlocker({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) => GestureDetector(
behavior: HitTestBehavior.opaque,
onHorizontalDragStart: (_) {},
onHorizontalDragUpdate: (_) {},
onHorizontalDragEnd: (_) {},
child: child,
);
}
class Leaf extends StatelessWidget {
const Leaf({
super.key,
required this.body,
this.wideBody,
this.fittedBody,
this.footer,
this.onTurn,
});
final Widget body;
/// A bounded portrait layout, used to keep the complete puzzle in view.
final Widget? fittedBody;
/// Replaces [body] on a landscape page. The shorter page suits side-by-side
/// columns better than one long scroll, so it should arrange its own panes.
final Widget? wideBody;
final Widget? footer;
final ValueChanged<int>? onTurn;
@override
Widget build(BuildContext context) {
final wide =
wideBody != null &&
MediaQuery.orientationOf(context) == Orientation.landscape;
return Paper(
onTurn: onTurn,
child: Column(
children: [
Expanded(
child: wide
? wideBody!
: fittedBody ??
SingleChildScrollView(
// Each newly opened leaf starts at the top, including
// leaves revisited with Previous or a bookmark.
key: const ValueKey('leaf-scroll'),
padding: const EdgeInsets.fromLTRB(4, 8, 4, 8),
child: body,
),
),
?footer,
],
),
);
}
}
class Cover extends StatelessWidget {
const Cover({super.key, required this.onTap});
final VoidCallback onTap;
@override
Widget build(BuildContext context) => Semantics(
button: true,
label: 'Open Mara’s cloth-bound island notebook',
child: GestureDetector(
onTap: onTap,
child: SizedBox(
height: 230,
child: Center(
child: AspectRatio(
aspectRatio: 1.5,
child: LayoutBuilder(
builder: (context, c) => Stack(
children: [
Positioned.fill(
child: Image.asset(
'assets/artwork/home-notebook-cover-v2.png',
fit: BoxFit.fill,
excludeFromSemantics: true,
),
),
Positioned(
left: c.maxWidth * .27,
top: c.maxHeight * .32,
width: c.maxWidth * .49,
height: c.maxHeight * .34,
child: Center(
child: Transform.rotate(
angle: -.044,
child: CopyText(
'Mara’s\nisland notebook',
textAlign: TextAlign.center,
style: hand(28),
),
),
),
),
],
),
),
),
),
),
),
);
}
class Sketch extends StatelessWidget {
const Sketch(this.kind, {super.key, this.height = 140});
final String kind;
final double height;
@override
Widget build(BuildContext context) => Semantics(
image: true,
label: kind == 'match'
? 'A pencil sketch of a match and a small amber flame'
: 'A folded note sealed with red wax',
child: SizedBox(
height: height,
width: double.infinity,
child: CustomPaint(painter: _SketchPainter(kind)),
),
);
}
class _SketchPainter extends CustomPainter {
_SketchPainter(this.kind);
final String kind;
@override
void paint(Canvas c, Size s) {
final p = Paint()..isAntiAlias = true;
final x = s.width * .51, h = s.height;
if (kind == 'match') {
p
..color = const Color(0xff997445)
..strokeWidth = h * .045;
c.drawLine(Offset(x - 25, h * .88), Offset(x + 12, h * .36), p);
p.color = const Color(0xff654539);
c.drawOval(Rect.fromLTRB(x + 2, h * .27, x + 26, h * .41), p);
p.color = amber;
final f = Path()
..moveTo(x + 12, h * .33)
..cubicTo(x - 24, h * .17, x + 30, h * .19, x + 17, h * .05)
..cubicTo(x + 56, h * .20, x + 32, h * .33, x + 12, h * .33);
c.drawPath(f, p);
} else {
final r = Rect.fromLTRB(s.width * .10, h * .15, s.width * .90, h * .86);
p.color = const Color(0xffead9b2);
c.drawRect(r, p);
p
..color = const Color(0xffb8a17c)
..style = PaintingStyle.stroke
..strokeWidth = 1.5;
c.drawRect(r, p);
c.drawPath(
Path()
..moveTo(r.left, r.top)
..lineTo(s.width * .5, h * .59)
..lineTo(r.right, r.top),
p,
);
c.drawLine(r.bottomLeft, Offset(s.width * .4, h * .5), p);
c.drawLine(r.bottomRight, Offset(s.width * .6, h * .5), p);
p
..style = PaintingStyle.fill
..color = const Color(0xff9e5f49);
c.drawCircle(Offset(s.width * .5, h * .57), h * .075, p);
}
}
@override
bool shouldRepaint(_SketchPainter old) => old.kind != kind;
}