fix: float the pet over the messages and let it be dragged
The pet used to sit in the column above the composer, reserving a strip of its own: the message list ended there, which read as an opaque band cutting off the last row. It is now a layer over the message area, so the list keeps its full height and shows through around the sprite. Only the body of the sheet takes touches (contentBox, 4dp out); taps and drags on the transparent canvas still reach the messages underneath. Dragging the body moves the pet along the composer's top edge (or the activity cap's) with a small lift while held and the pack's drag reaction. The spot is stored as a fraction of the width in pet.x.v1, so a restart or a change in width puts it back in the same place. Taps now react on tap-up so a drag does not fire a left/right reaction. 0.4.1 (401).
This commit is contained in:
1 parent
d72af036a1
commit
d25167de58
8 files changed
+348
-58
No files matched your search
@@ -26,6 +26,7 @@ class PetLibrary extends ChangeNotifier {
|
||||
static const _activeKey = 'pet.active.v1';
|
||||
static const _enabledKey = 'pet.enabled.v1';
|
||||
static const _sizeKey = 'pet.size.v1';
|
||||
static const _positionKey = 'pet.x.v1';
|
||||
|
||||
/// The canvas width a pet is drawn at; the list offers these.
|
||||
static const sizes = <double>[40, 52, 64];
|
||||
@@ -34,6 +35,9 @@ class PetLibrary extends ChangeNotifier {
|
||||
bool enabled = false;
|
||||
double size = 52;
|
||||
|
||||
/// Where the pet stands along its edge, 0 = left end, 1 = right end.
|
||||
double position = 0.5;
|
||||
|
||||
String? _packDir;
|
||||
bool _loaded = false;
|
||||
|
||||
@@ -53,6 +57,7 @@ class PetLibrary extends ChangeNotifier {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
enabled = prefs.getBool(_enabledKey) ?? false;
|
||||
size = (prefs.getDouble(_sizeKey) ?? 52).clamp(32, 80);
|
||||
position = (prefs.getDouble(_positionKey) ?? 0.5).clamp(0.0, 1.0);
|
||||
final active = prefs.getString(_activeKey);
|
||||
if (active != null && active.isNotEmpty) {
|
||||
final root = await _petsRoot();
|
||||
@@ -93,6 +98,15 @@ class PetLibrary extends ChangeNotifier {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setPosition(double value) async {
|
||||
position = value.clamp(0.0, 1.0);
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setDouble(_positionKey, position);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// Unpacks and activates a pet package. [root] is for tests; the app uses
|
||||
/// the documents directory.
|
||||
Future<PetPack> importZip(List<int> bytes, {Directory? root}) async {
|
||||
|
||||
@@ -167,10 +167,18 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(child: _messageList()),
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(child: _messageList()),
|
||||
// Over the messages, not in the column: the list keeps its
|
||||
// full height and shows through around the pet.
|
||||
Positioned.fill(child: PetLayer(state: _petState())),
|
||||
],
|
||||
),
|
||||
),
|
||||
_permissions(),
|
||||
_forms(),
|
||||
PetSlot(state: _petState()),
|
||||
if (chat.running) _activityStrip(),
|
||||
_composer(widget.keyboardInset),
|
||||
],
|
||||
|
||||
@@ -221,19 +221,26 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Text(
|
||||
'我们今天要做什么?',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: scheme.onSurface,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Text(
|
||||
'我们今天要做什么?',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: scheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Positioned.fill(child: PetLayer(state: 'idle')),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
@@ -260,7 +267,6 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
|
||||
items: _attachments,
|
||||
onRemove: _removeAttachment,
|
||||
),
|
||||
const PetSlot(state: 'idle'),
|
||||
_composer(theme, widget.keyboardInset),
|
||||
],
|
||||
),
|
||||
|
||||
+165
-41
@@ -1,6 +1,7 @@
|
||||
// The input-box pet. A PetSprite plays the pack's sheet frames; PetSlot is
|
||||
// the strip it stands on, placed directly above the composer (or the activity
|
||||
// cap when a turn runs). Nothing renders until a pack is imported and enabled.
|
||||
// The input-box pet. A PetSprite plays the pack's sheet frames; PetLayer is
|
||||
// the floating layer it lives in, placed over the chat's message area so the
|
||||
// messages keep their full height and show through around the pet. Nothing
|
||||
// renders until a pack is imported and enabled.
|
||||
import 'dart:async';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
@@ -9,12 +10,27 @@ import 'package:flutter/material.dart';
|
||||
import '../pets/pet_library.dart';
|
||||
import '../pets/pet_pack.dart';
|
||||
|
||||
/// The strip a pet stands on. It reserves the space above the baseline so the
|
||||
/// sprite does not cover the last message; the feet rest on its bottom edge,
|
||||
/// which is the composer's (or the activity cap's) top.
|
||||
class PetSlot extends StatelessWidget {
|
||||
/// The pet over the chat. It reserves no layout space: the message list runs
|
||||
/// underneath, and only the sprite's body takes touches. The feet rest on the
|
||||
/// bottom edge of the layer, which is the composer's top (or the activity
|
||||
/// cap's while a turn runs). Dragging it sideways moves it along that edge.
|
||||
class PetLayer extends StatefulWidget {
|
||||
final String state;
|
||||
const PetSlot({super.key, required this.state});
|
||||
const PetLayer({super.key, required this.state});
|
||||
|
||||
@override
|
||||
State<PetLayer> createState() => _PetLayerState();
|
||||
}
|
||||
|
||||
class _PetLayerState extends State<PetLayer> {
|
||||
/// Where the feet are, as a fraction of the layer's width. Null: use the
|
||||
/// saved spot.
|
||||
double? _fraction;
|
||||
bool _dragging = false;
|
||||
double _lift = 0;
|
||||
|
||||
/// How far from either end the body keeps, logical px.
|
||||
static const _edge = 8.0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -24,30 +40,66 @@ class PetSlot extends StatelessWidget {
|
||||
final library = PetLibrary.instance;
|
||||
final pack = library.pack;
|
||||
if (!library.enabled || pack == null) return const SizedBox.shrink();
|
||||
final scale = library.size / pack.canvas.width;
|
||||
final height = (pack.anchor.dy * scale).clamp(20.0, 80.0);
|
||||
return SizedBox(
|
||||
height: height,
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: Transform.translate(
|
||||
// Put the anchor (the feet) on the bottom edge; the rest of the
|
||||
// canvas hangs above, inside the reserved strip.
|
||||
offset: Offset(
|
||||
(pack.canvas.width / 2 - pack.anchor.dx) * scale,
|
||||
// The composer's own top padding sits between this strip and
|
||||
// its visible capsule; step over it so the feet land on the
|
||||
// edge, not above it.
|
||||
(pack.canvas.height - pack.anchor.dy) * scale + 6,
|
||||
return LayoutBuilder(builder: (context, constraints) {
|
||||
final width = constraints.maxWidth;
|
||||
final size = library.size;
|
||||
final scale = size / pack.canvas.width;
|
||||
final height = size * pack.canvas.height / pack.canvas.width;
|
||||
|
||||
// Body extents left and right of the feet, so dragging keeps the
|
||||
// whole pet on screen.
|
||||
final leftExtent = (pack.anchor.dx - pack.contentBox.left) * scale;
|
||||
final rightExtent = (pack.contentBox.right - pack.anchor.dx) * scale;
|
||||
final lo = leftExtent + _edge;
|
||||
final hi = width - rightExtent - _edge;
|
||||
final minFoot = hi <= lo ? width / 2 : lo;
|
||||
final maxFoot = hi <= lo ? width / 2 : hi;
|
||||
|
||||
final fraction = (_fraction ?? library.position).clamp(0.0, 1.0);
|
||||
final foot = (fraction * width).clamp(minFoot, maxFoot);
|
||||
|
||||
// Stand on the composer's top edge. The sprite's own canvas goes a
|
||||
// little below the anchor, and the composer keeps a few px of top
|
||||
// padding above its capsule, so step past both; that overflow is
|
||||
// fine, the layer does not clip.
|
||||
final bottom =
|
||||
-((pack.canvas.height - pack.anchor.dy) * scale + 6) + _lift;
|
||||
|
||||
return Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
Positioned(
|
||||
left: foot - pack.anchor.dx * scale,
|
||||
bottom: bottom,
|
||||
width: size,
|
||||
height: height,
|
||||
child: PetSprite(
|
||||
pack: pack,
|
||||
state: widget.state,
|
||||
width: size,
|
||||
dragging: _dragging,
|
||||
onDragStart: () => setState(() {
|
||||
_dragging = true;
|
||||
_lift = -6;
|
||||
}),
|
||||
onDragUpdate: (dx) {
|
||||
final next = ((_fraction ?? library.position) * width + dx)
|
||||
.clamp(minFoot, maxFoot);
|
||||
setState(() => _fraction = next / width);
|
||||
},
|
||||
onDragEnd: () {
|
||||
final spot = _fraction;
|
||||
setState(() {
|
||||
_dragging = false;
|
||||
_lift = 0;
|
||||
});
|
||||
if (spot != null) library.setPosition(spot);
|
||||
},
|
||||
),
|
||||
),
|
||||
child: PetSprite(
|
||||
pack: pack,
|
||||
state: state,
|
||||
width: library.size,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -60,11 +112,23 @@ class PetSprite extends StatefulWidget {
|
||||
/// The pack canvas's on-screen width; the height follows the canvas ratio.
|
||||
final double width;
|
||||
|
||||
/// A drag is in progress: play the pack's drag reaction while held.
|
||||
final bool dragging;
|
||||
|
||||
/// Called when the body is dragged; the layer moves the pet.
|
||||
final VoidCallback? onDragStart;
|
||||
final ValueChanged<double>? onDragUpdate;
|
||||
final VoidCallback? onDragEnd;
|
||||
|
||||
const PetSprite({
|
||||
super.key,
|
||||
required this.pack,
|
||||
required this.state,
|
||||
required this.width,
|
||||
this.dragging = false,
|
||||
this.onDragStart,
|
||||
this.onDragUpdate,
|
||||
this.onDragEnd,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -83,6 +147,9 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
String? _oneShot;
|
||||
String? _pendingState;
|
||||
|
||||
/// The drag reaction's name while a drag holds it.
|
||||
String? _dragAnimation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -95,9 +162,28 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
if (oldWidget.pack != widget.pack) {
|
||||
_oneShot = null;
|
||||
_pendingState = null;
|
||||
_dragAnimation = null;
|
||||
_begin(widget.state);
|
||||
} else if (oldWidget.state != widget.state) {
|
||||
if (_oneShot != null) {
|
||||
return;
|
||||
}
|
||||
if (oldWidget.dragging != widget.dragging) {
|
||||
_oneShot = null;
|
||||
if (widget.dragging) {
|
||||
final drag = widget.pack.reactions['drag'];
|
||||
_dragAnimation =
|
||||
drag != null && widget.pack.animations.containsKey(drag) ? drag : null;
|
||||
final name = _dragAnimation ?? widget.pack.animationForState(widget.state);
|
||||
if (name != null) _beginNamed(name);
|
||||
} else {
|
||||
final next = _pendingState ?? widget.state;
|
||||
_pendingState = null;
|
||||
_dragAnimation = null;
|
||||
_begin(next);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (oldWidget.state != widget.state) {
|
||||
if (_oneShot != null || widget.dragging) {
|
||||
_pendingState = widget.state;
|
||||
} else {
|
||||
_begin(widget.state);
|
||||
@@ -111,10 +197,10 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _begin(String state) {
|
||||
final animation = widget.pack.animation(
|
||||
widget.pack.animationForState(state),
|
||||
);
|
||||
void _begin(String state) => _beginNamed(widget.pack.animationForState(state));
|
||||
|
||||
void _beginNamed(String? name) {
|
||||
final animation = widget.pack.animation(name);
|
||||
if (animation == null) {
|
||||
_timer?.cancel();
|
||||
_animation = null;
|
||||
@@ -164,6 +250,16 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
setState(() {});
|
||||
return;
|
||||
}
|
||||
// Held by a drag: keep the pose until it is let go, even when the pack's
|
||||
// drag sheet plays once.
|
||||
if (widget.dragging &&
|
||||
_dragAnimation != null &&
|
||||
animation.name == _dragAnimation &&
|
||||
!animation.loop) {
|
||||
setState(() => _frame = 0);
|
||||
_schedule();
|
||||
return;
|
||||
}
|
||||
if (animation.loop) {
|
||||
setState(() => _frame = 0);
|
||||
_schedule();
|
||||
@@ -173,7 +269,7 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void _react(TapDownDetails details) {
|
||||
void _react(TapUpDetails details) {
|
||||
final left = details.localPosition.dx < widget.width / 2;
|
||||
final name = widget.pack.reactions[left ? 'tapLeft' : 'tapRight'];
|
||||
final animation = widget.pack.animation(name);
|
||||
@@ -192,8 +288,15 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
final width = widget.width;
|
||||
final height = width * widget.pack.canvas.height / widget.pack.canvas.width;
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTapDown: _react,
|
||||
// Only the body takes touches; the rest of the layer lets the message
|
||||
// list scroll.
|
||||
behavior: HitTestBehavior.deferToChild,
|
||||
onTapUp: _react,
|
||||
onHorizontalDragStart: (_) => widget.onDragStart?.call(),
|
||||
onHorizontalDragUpdate: (details) =>
|
||||
widget.onDragUpdate?.call(details.delta.dx),
|
||||
onHorizontalDragEnd: (_) => widget.onDragEnd?.call(),
|
||||
onHorizontalDragCancel: () => widget.onDragEnd?.call(),
|
||||
child: CustomPaint(
|
||||
size: Size(width, height),
|
||||
painter: _PetPainter(
|
||||
@@ -201,6 +304,7 @@ class _PetSpriteState extends State<PetSprite> {
|
||||
animation: _animation,
|
||||
image: _image,
|
||||
frame: _frame,
|
||||
spriteSize: Size(width, height),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -213,13 +317,32 @@ class _PetPainter extends CustomPainter {
|
||||
final ui.Image? image;
|
||||
final int frame;
|
||||
|
||||
/// The on-screen size, for hit testing.
|
||||
final Size spriteSize;
|
||||
|
||||
const _PetPainter({
|
||||
required this.pack,
|
||||
required this.animation,
|
||||
required this.image,
|
||||
required this.frame,
|
||||
required this.spriteSize,
|
||||
});
|
||||
|
||||
/// The body only: taps and drags on the transparent canvas around it go to
|
||||
/// whatever is underneath.
|
||||
@override
|
||||
bool? hitTest(Offset position) {
|
||||
if (spriteSize.width <= 0) return false;
|
||||
final scale = spriteSize.width / pack.canvas.width;
|
||||
final body = Rect.fromLTRB(
|
||||
pack.contentBox.left * scale,
|
||||
pack.contentBox.top * scale,
|
||||
pack.contentBox.right * scale,
|
||||
pack.contentBox.bottom * scale,
|
||||
).inflate(4);
|
||||
return body.contains(position);
|
||||
}
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final animation = this.animation;
|
||||
@@ -263,5 +386,6 @@ class _PetPainter extends CustomPainter {
|
||||
oldDelegate.image != image ||
|
||||
oldDelegate.animation != animation ||
|
||||
oldDelegate.frame != frame ||
|
||||
oldDelegate.pack != pack;
|
||||
oldDelegate.pack != pack ||
|
||||
oldDelegate.spriteSize != spriteSize;
|
||||
}
|
||||
Reference in new issue
Block a user