Commit 503218cd authored by Adam Barth's avatar Adam Barth

Merge pull request #762 from abarth/image_repeat

Implement ImageRepeat
parents 7eb387b1 9c00bc53
...@@ -573,6 +573,35 @@ enum ImageRepeat { ...@@ -573,6 +573,35 @@ enum ImageRepeat {
noRepeat noRepeat
} }
Iterable<Rect> _generateImageTileRects(Rect outputRect, Rect fundamentalRect, ImageRepeat repeat) sync* {
if (repeat == ImageRepeat.noRepeat) {
yield fundamentalRect;
return;
}
int startX = 0;
int startY = 0;
int stopX = 0;
int stopY = 0;
double strideX = fundamentalRect.width;
double strideY = fundamentalRect.height;
if (repeat == ImageRepeat.repeat || repeat == ImageRepeat.repeatX) {
startX = ((outputRect.left - fundamentalRect.left) / strideX).floor();
stopX = ((outputRect.right - fundamentalRect.right) / strideX).ceil();
}
if (repeat == ImageRepeat.repeat || repeat == ImageRepeat.repeatY) {
startY = ((outputRect.top - fundamentalRect.top) / strideY).floor();
stopY = ((outputRect.bottom - fundamentalRect.bottom) / strideY).ceil();
}
for (int i = startX; i <= stopX; ++i) {
for (int j = startY; j <= stopY; ++j)
yield fundamentalRect.shift(new Offset(i * strideX, j * strideY));
}
}
/// Paint an image into the given rectangle in the canvas /// Paint an image into the given rectangle in the canvas
void paintImage({ void paintImage({
Canvas canvas, Canvas canvas,
...@@ -580,7 +609,7 @@ void paintImage({ ...@@ -580,7 +609,7 @@ void paintImage({
ui.Image image, ui.Image image,
ColorFilter colorFilter, ColorFilter colorFilter,
ImageFit fit, ImageFit fit,
repeat: ImageRepeat.noRepeat, ImageRepeat repeat: ImageRepeat.noRepeat,
Rect centerSlice, Rect centerSlice,
double alignX, double alignX,
double alignY double alignY
...@@ -640,7 +669,6 @@ void paintImage({ ...@@ -640,7 +669,6 @@ void paintImage({
// as we apply a nine-patch stretch. // as we apply a nine-patch stretch.
assert(sourceSize == inputSize); assert(sourceSize == inputSize);
} }
// TODO(abarth): Implement |repeat|.
Paint paint = new Paint()..isAntiAlias = false; Paint paint = new Paint()..isAntiAlias = false;
if (colorFilter != null) if (colorFilter != null)
paint.colorFilter = colorFilter; paint.colorFilter = colorFilter;
...@@ -648,10 +676,14 @@ void paintImage({ ...@@ -648,10 +676,14 @@ void paintImage({
double dy = (outputSize.height - destinationSize.height) * (alignY ?? 0.5); double dy = (outputSize.height - destinationSize.height) * (alignY ?? 0.5);
Point destinationPosition = rect.topLeft + new Offset(dx, dy); Point destinationPosition = rect.topLeft + new Offset(dx, dy);
Rect destinationRect = destinationPosition & destinationSize; Rect destinationRect = destinationPosition & destinationSize;
if (centerSlice == null) if (centerSlice == null) {
canvas.drawImageRect(image, Point.origin & sourceSize, destinationRect, paint); Rect sourceRect = Point.origin & sourceSize;
else for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
canvas.drawImageNine(image, centerSlice, destinationRect, paint); canvas.drawImageRect(image, sourceRect, tileRect, paint);
} else {
for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
canvas.drawImageNine(image, centerSlice, tileRect, paint);
}
} }
/// A background image for a box. /// A background image for a box.
......
...@@ -25,7 +25,7 @@ class RenderImage extends RenderBox { ...@@ -25,7 +25,7 @@ class RenderImage extends RenderBox {
ColorFilter colorFilter, ColorFilter colorFilter,
ImageFit fit, ImageFit fit,
FractionalOffset alignment, FractionalOffset alignment,
repeat: ImageRepeat.noRepeat, ImageRepeat repeat: ImageRepeat.noRepeat,
Rect centerSlice Rect centerSlice
}) : _image = image, }) : _image = image,
_width = width, _width = width,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment