Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 113 additions & 9 deletions packages/flame/lib/src/nine_tile_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import 'package:meta/meta.dart';
/// the 4 sides only in the direction in which they are located and expanding
/// the center in both directions.
/// That allows you to have non distorted borders.
///
/// When [Sprite.src] does not cover the whole [Sprite.image] (for example, a
/// sprite inside a spritesheet), each tile is drawn with its own
/// [Canvas.drawImageRect] call.
///
/// If the [Paint] used has a
/// [Paint.filterQuality] other than [FilterQuality.none], the sampling near a
/// tile's edges can read pixels from the neighboring tiles of the sheet, so
/// the seams between tiles bleed with those neighbors. This differs from
/// [Canvas.drawImageNine], which treats the whole [Sprite.image] as the source.
class NineTileBox {
static final _whitePaint = BasicPalette.white.paint();

Expand Down Expand Up @@ -155,21 +165,115 @@ class NineTileBox {

/// Renders this nine box with the dimensions provided by [dst].
void drawRect(Canvas c, [Rect? dst, Paint? overridePaint]) {
c.drawImageNine(
sprite.image,
center,
dst ?? _dst,
overridePaint ?? _whitePaint,
);
_draw(c, dst ?? _dst, overridePaint ?? _whitePaint);
}

/// Renders this nine box as a rectangle at [position] with size [size].
void draw(Canvas c, Vector2 position, Vector2 size, [Paint? overridePaint]) {
c.drawImageNine(
sprite.image,
center,
_draw(
c,
Rect.fromLTWH(position.x, position.y, size.x, size.y),
overridePaint ?? _whitePaint,
);
}

void _draw(Canvas c, Rect dst, Paint paint) {
if (dst.isEmpty) {
return;
}

final src = sprite.src;
final doesSpriteCoverFullImage =
src.left == 0 &&
src.top == 0 &&
src.right == sprite.image.width &&
src.bottom == sprite.image.height;

if (doesSpriteCoverFullImage) {
c.drawImageNine(sprite.image, center, dst, paint);
} else {
_drawTiles(c, dst, paint);
}
}

/// Draws the nine tiles of [sprite] stretched to fill [dst].
///
/// [Canvas.drawImageNine] is not used because it treats the whole
/// [Sprite.image] as the source, ignoring [Sprite.src].
void _drawTiles(Canvas c, Rect dst, Paint paint) {
final srcLeft = sprite.src.left;
final srcTop = sprite.src.top;
final srcRight = sprite.src.right;
final srcBottom = sprite.src.bottom;

final srcXs = [
srcLeft,
center.left + srcLeft,
center.right + srcLeft,
srcRight,
];

final srcYs = [
srcTop,
center.top + srcTop,
center.bottom + srcTop,
srcBottom,
];

final dstXs = _calculateDstPositionsInAxis(
dst.left,
dst.width,
center.left,
sprite.src.width - center.right,
);

final dstYs = _calculateDstPositionsInAxis(
dst.top,
dst.height,
center.top,
sprite.src.height - center.bottom,
);

for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
c.drawImageRect(
sprite.image,
Rect.fromLTRB(
srcXs[col],
srcYs[row],
srcXs[col + 1],
srcYs[row + 1],
),
Rect.fromLTRB(
dstXs[col],
dstYs[row],
dstXs[col + 1],
dstYs[row + 1],
),
paint,
);
}
}
}

/// Returns the four positions of one axis in destination space.
///
/// When [leading] and [trailing] fixed borders do not fit in [length], both
/// are scaled down so they meet, leaving no room for the center.
static List<double> _calculateDstPositionsInAxis(
double start,
double length,
double leading,
double trailing,
) {
final sum = leading + trailing;
final scale = sum > length ? length / sum : 1.0;

return [
start,
start + (leading * scale),
start + length - (trailing * scale),
start + length,
];
}
}
30 changes: 28 additions & 2 deletions packages/flame/lib/src/widgets/nine_tile_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:ui';

import 'package:flame/cache.dart';
import 'package:flame/flame.dart';
import 'package:flame/src/extensions/vector2.dart';
import 'package:flame/src/nine_tile_box.dart' as non_widget;
import 'package:flame/src/sprite.dart';
import 'package:flame/src/widgets/base_future_builder.dart';
Expand All @@ -15,12 +16,14 @@ class _Painter({
required final Image image,
required final double tileSize,
required final double destTileSize,
final Vector2? srcPosition,
final Vector2? srcSize,
}) extends CustomPainter {
late final non_widget.NineTileBox _nineTileBox;

this
: _nineTileBox = non_widget.NineTileBox(
Sprite(image),
Sprite(image, srcPosition: srcPosition, srcSize: srcSize),
Comment thread
NagaChiang marked this conversation as resolved.
Resolved
tileSize: tileSize.toInt(),
destTileSize: destTileSize.toInt(),
);
Expand All @@ -31,7 +34,13 @@ class _Painter({
}

@override
bool shouldRepaint(_) => false;
bool shouldRepaint(covariant _Painter oldDelegate) {
return image != oldDelegate.image ||
tileSize != oldDelegate.tileSize ||
destTileSize != oldDelegate.destTileSize ||
srcPosition != oldDelegate.srcPosition ||
srcSize != oldDelegate.srcSize;
}
}

/// A [StatelessWidget] that renders NineTileBox
Expand All @@ -43,6 +52,13 @@ class NineTileBoxWidget extends StatefulWidget {

/// The size of the tile that will be used to render on the canvas
final double destTileSize;

/// The position of the sprite region within the image
final Vector2? srcPosition;

/// The size of the sprite region within the image
final Vector2? srcSize;

final double? width;
final double? height;

Expand All @@ -60,6 +76,8 @@ class NineTileBoxWidget extends StatefulWidget {
required Image image,
required this.tileSize,
required this.destTileSize,
this.srcPosition,
this.srcSize,
this.width,
this.height,
this.child,
Expand All @@ -83,6 +101,8 @@ class NineTileBoxWidget extends StatefulWidget {
required String path,
required this.tileSize,
required this.destTileSize,
this.srcPosition,
this.srcSize,
Images? images,
this.width,
this.height,
Expand Down Expand Up @@ -131,6 +151,8 @@ class _NineTileBoxWidgetState() extends State<NineTileBoxWidget> {
image: image,
tileSize: widget.tileSize,
destTileSize: widget.destTileSize,
srcPosition: widget.srcPosition,
srcSize: widget.srcSize,
width: widget.width,
height: widget.height,
padding: widget.padding,
Expand All @@ -148,6 +170,8 @@ class const InternalNineTileBox({
required final Image image,
required final double tileSize,
required final double destTileSize,
final Vector2? srcPosition,
final Vector2? srcSize,
final Widget? child,
final double? width,
final double? height,
Expand All @@ -164,6 +188,8 @@ class const InternalNineTileBox({
image: image,
tileSize: tileSize,
destTileSize: destTileSize,
srcPosition: srcPosition,
srcSize: srcSize,
),
child: Container(
padding: padding,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/flame/test/_goldens/nine_tile_box_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/flame/test/_goldens/nine_tile_box_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file added packages/flame/test/_resources/nine-tile-sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading