Support TexturePacker image atlases

This commit is contained in:
gem
2026-06-09 12:49:01 +08:00
parent e2a584d4dc
commit 38f6e0c0c9
16 changed files with 343 additions and 26 deletions

View File

@@ -57,9 +57,7 @@ List<({Rect source, Rect destination})> runtimeNineSliceRects({
final destLeft = left.clamp(0.0, destination.width).toDouble();
final destTop = top.clamp(0.0, destination.height).toDouble();
final destRight = right.clamp(0.0, destination.width - destLeft).toDouble();
final destBottom = bottom
.clamp(0.0, destination.height - destTop)
.toDouble();
final destBottom = bottom.clamp(0.0, destination.height - destTop).toDouble();
final sourceXs = [
source.left,
@@ -529,7 +527,7 @@ class RuntimeComponent extends PositionComponent
_node.type == RuntimeNodeType.button)) {
final imagePaint = Paint()
..color = composeRuntimeColorAlpha(Colors.white, renderAlpha);
final source = _imageSourceRect(image);
final source = _imageSourceRect(image, _currentImageFrame(_node));
if (_usesNineSlice(source, rect)) {
_drawNineSliceImage(canvas, image, source, rect, imagePaint);
} else {
@@ -546,7 +544,13 @@ class RuntimeComponent extends PositionComponent
);
}
Rect _imageSourceRect(ui.Image image) {
Rect _imageSourceRect(ui.Image image, String? frameName) {
final frame = _loadedAsset == null
? null
: _resources.textureFrame(_loadedAsset!, frameName);
if (frame != null) {
return frame.rect;
}
return runtimeImageSourceRect(
imageWidth: image.width.toDouble(),
imageHeight: image.height.toDouble(),
@@ -666,6 +670,19 @@ class RuntimeComponent extends PositionComponent
return node.asset;
}
String? _currentImageFrame(RuntimeNode node) {
if (node.type != RuntimeNodeType.button) {
return node.frame;
}
if (!node.interactive && node.disabledFrame != null) {
return node.disabledFrame;
}
if (_pressed && node.pressedFrame != null) {
return node.pressedFrame;
}
return node.frame;
}
void _releaseRetainedImage(String asset, int generation, ui.Image? image) {
if (image == null) {
return;