Files
logic-puzzle/lib/ui/paper.dart
T

422 lines
13 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 '../services/browser_assets.dart';
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',
fontFamilyFallback: const [
'LumenText',
'LumenSymbols',
'LumenSymbols2',
'LumenMath',
],
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;
}
/// Flutter's default mouse drag threshold is one pixel. That turns ordinary
/// click jitter into a swipe and cancels a child button's/cell's tap recognizer.
/// Use the normal tap tolerance for mouse drags too, retaining deliberate swipes.
class _PageHorizontalDrag extends HorizontalDragGestureRecognizer {
_PageHorizontalDrag({super.debugOwner});
@override
bool hasSufficientGlobalDistanceToAccept(
PointerDeviceKind pointerDeviceKind,
double? deviceTouchSlop,
) => pointerDeviceKind == PointerDeviceKind.mouse
? globalDistanceMoved.abs() > kTouchSlop
: super.hasSufficientGlobalDistanceToAccept(
pointerDeviceKind,
deviceTouchSlop,
);
}
class _PageDragDetector extends StatelessWidget {
const _PageDragDetector({
required this.child,
this.onHorizontalDragStart,
this.onHorizontalDragUpdate,
this.onHorizontalDragEnd,
this.onHorizontalDragCancel,
this.dragStartBehavior = DragStartBehavior.start,
});
final Widget child;
final GestureDragStartCallback? onHorizontalDragStart;
final GestureDragUpdateCallback? onHorizontalDragUpdate;
final GestureDragEndCallback? onHorizontalDragEnd;
final GestureDragCancelCallback? onHorizontalDragCancel;
final DragStartBehavior dragStartBehavior;
@override
Widget build(BuildContext context) => RawGestureDetector(
behavior: HitTestBehavior.opaque,
gestures: {
if (onHorizontalDragStart != null ||
onHorizontalDragUpdate != null ||
onHorizontalDragEnd != null ||
onHorizontalDragCancel != null)
_PageHorizontalDrag:
GestureRecognizerFactoryWithHandlers<_PageHorizontalDrag>(
() => _PageHorizontalDrag(debugOwner: this),
(recognizer) => recognizer
..gestureSettings = MediaQuery.maybeGestureSettingsOf(context)
..dragStartBehavior = dragStartBehavior
..onStart = onHorizontalDragStart
..onUpdate = onHorizontalDragUpdate
..onEnd = onHorizontalDragEnd
..onCancel = onHorizontalDragCancel,
),
},
child: child,
);
}
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: _PageDragDetector(
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(
// Crop the source image while painting, rather than
// caching a tall off-screen paper layer on wide displays.
left: landscape ? 0 : left,
top: landscape ? 0 : top,
width: landscape ? c.maxWidth : w,
height: landscape ? c.maxHeight : h,
child: RepaintBoundary(
child: Image.asset(
artworkAsset(
PaperSkin.assetOf(context) ??
'assets/artwork/mara-single-page-v2.png',
),
fit: landscape ? BoxFit.cover : 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) => _PageDragDetector(
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(
artworkAsset('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;
}