Files
openchamber-android-native/lib/widgets/pet_sprite.dart
T

267 lines
7.3 KiB
Dart
Raw Normal View History

// 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.
import 'dart:async';
import 'dart:ui' as ui;
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 {
final String state;
const PetSlot({super.key, required this.state});
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: PetLibrary.instance,
builder: (context, _) {
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,
),
child: PetSprite(
pack: pack,
state: state,
width: library.size,
),
),
),
);
},
);
}
}
class PetSprite extends StatefulWidget {
final PetPack pack;
final String state;
/// The pack canvas's on-screen width; the height follows the canvas ratio.
final double width;
const PetSprite({
super.key,
required this.pack,
required this.state,
required this.width,
});
@override
State<PetSprite> createState() => _PetSpriteState();
}
class _PetSpriteState extends State<PetSprite> {
PetAnimation? _animation;
ui.Image? _image;
String? _sheet;
int _frame = 0;
Timer? _timer;
int _loadToken = 0;
/// A reaction being played; the state resumes when it ends.
String? _oneShot;
String? _pendingState;
@override
void initState() {
super.initState();
_begin(widget.state);
}
@override
void didUpdateWidget(PetSprite oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.pack != widget.pack) {
_oneShot = null;
_pendingState = null;
_begin(widget.state);
} else if (oldWidget.state != widget.state) {
if (_oneShot != null) {
_pendingState = widget.state;
} else {
_begin(widget.state);
}
}
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
void _begin(String state) {
final animation = widget.pack.animation(
widget.pack.animationForState(state),
);
if (animation == null) {
_timer?.cancel();
_animation = null;
return;
}
_animation = animation;
_frame = 0;
_showSheet(animation);
_schedule();
}
/// Loads a new sheet; a frame from the old one must not be cropped with the
/// new animation's box, so the image clears while it loads.
void _showSheet(PetAnimation animation) {
if (animation.sheet == _sheet) return;
_sheet = animation.sheet;
_image = null;
final token = ++_loadToken;
PetLibrary.instance.imageFor(animation).then((image) {
if (!mounted || token != _loadToken) return;
setState(() => _image = image);
});
}
void _schedule() {
_timer?.cancel();
final animation = _animation;
if (animation == null || animation.frames.isEmpty) return;
final frame = animation.frames[_frame.clamp(0, animation.frames.length - 1)];
_timer = Timer(Duration(milliseconds: frame.ms), _tick);
}
void _tick() {
final animation = _animation;
if (!mounted || animation == null) return;
if (_frame + 1 < animation.frames.length) {
setState(() => _frame++);
_schedule();
return;
}
final oneShot = _oneShot;
if (oneShot != null) {
_oneShot = null;
final next = _pendingState ?? widget.state;
_pendingState = null;
_begin(next);
setState(() {});
return;
}
if (animation.loop) {
setState(() => _frame = 0);
_schedule();
return;
}
_begin(widget.state);
setState(() {});
}
void _react(TapDownDetails details) {
final left = details.localPosition.dx < widget.width / 2;
final name = widget.pack.reactions[left ? 'tapLeft' : 'tapRight'];
final animation = widget.pack.animation(name);
if (animation == null) return;
_oneShot = name;
_pendingState = widget.state;
_animation = animation;
_frame = 0;
_showSheet(animation);
_schedule();
setState(() {});
}
@override
Widget build(BuildContext context) {
final width = widget.width;
final height = width * widget.pack.canvas.height / widget.pack.canvas.width;
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTapDown: _react,
child: CustomPaint(
size: Size(width, height),
painter: _PetPainter(
pack: widget.pack,
animation: _animation,
image: _image,
frame: _frame,
),
),
);
}
}
class _PetPainter extends CustomPainter {
final PetPack pack;
final PetAnimation? animation;
final ui.Image? image;
final int frame;
const _PetPainter({
required this.pack,
required this.animation,
required this.image,
required this.frame,
});
@override
void paint(Canvas canvas, Size size) {
final animation = this.animation;
final image = this.image;
if (image == null || animation == null || animation.frames.isEmpty) return;
final cols = animation.cols;
final maxCols = image.width ~/ animation.frameW;
final maxRows = image.height ~/ animation.frameH;
if (maxCols <= 0 || maxRows <= 0) return;
final entry =
animation.frames[frame.clamp(0, animation.frames.length - 1)];
final col = (entry.index % cols).clamp(0, maxCols - 1);
final row = (entry.index ~/ cols).clamp(0, maxRows - 1);
final src = Rect.fromLTWH(
col * animation.frameW.toDouble(),
row * animation.frameH.toDouble(),
animation.frameW.toDouble(),
animation.frameH.toDouble(),
);
final scale = size.width / pack.canvas.width;
final dst = Rect.fromLTWH(
animation.box.left * scale,
animation.box.top * scale,
animation.box.width * scale,
animation.box.height * scale,
);
canvas.drawImageRect(
image,
src,
dst,
Paint()
..filterQuality =
pack.pixelArt ? FilterQuality.none : FilterQuality.medium,
);
}
@override
bool shouldRepaint(_PetPainter oldDelegate) =>
oldDelegate.image != image ||
oldDelegate.animation != animation ||
oldDelegate.frame != frame ||
oldDelegate.pack != pack;
}