From 998a066a827e94f3fa1da346796b11f3354e0f0d Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 13 Oct 2015 12:04:38 -0700 Subject: [PATCH 1/4] Add a centerSlice parameter to images This lets you draw nine-patch images. --- examples/widgets/nine_patch.dart | 14 +++ .../flutter/lib/src/painting/box_painter.dart | 119 ++++++++++++------ packages/flutter/lib/src/rendering/image.dart | 51 +++++--- packages/flutter/lib/src/widgets/basic.dart | 37 ++++-- packages/flutter_sprites/lib/label.dart | 2 +- 5 files changed, 153 insertions(+), 70 deletions(-) create mode 100644 examples/widgets/nine_patch.dart diff --git a/examples/widgets/nine_patch.dart b/examples/widgets/nine_patch.dart new file mode 100644 index 0000000000..f0faf2cd60 --- /dev/null +++ b/examples/widgets/nine_patch.dart @@ -0,0 +1,14 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/painting.dart'; +import 'package:flutter/material.dart'; + +void main() { + runApp(new NetworkImage( + src: "http://38.media.tumblr.com/avatar_497c78dc767d_128.png", + fit: ImageFit.contain, + centerSlice: new Rect.fromLTRB(40.0, 40.0, 88.0, 88.0) + )); +} diff --git a/packages/flutter/lib/src/painting/box_painter.dart b/packages/flutter/lib/src/painting/box_painter.dart index e018615599..117c71823b 100644 --- a/packages/flutter/lib/src/painting/box_painter.dart +++ b/packages/flutter/lib/src/painting/box_painter.dart @@ -10,43 +10,47 @@ import 'package:flutter/services.dart'; import 'shadows.dart'; -/// An immutable set of offsets in each of the four cardinal directions +/// An immutable set of offsets in each of the four cardinal directions. /// /// Typically used for an offset from each of the four sides of a box. For /// example, the padding inside a box can be represented using this class. class EdgeDims { - /// Constructs an EdgeDims from offsets from the top, right, bottom and left + /// Constructs an EdgeDims from offsets from the top, right, bottom and left. const EdgeDims.TRBL(this.top, this.right, this.bottom, this.left); - /// Constructs an EdgeDims where all the offsets are value + /// Constructs an EdgeDims where all the offsets are value. const EdgeDims.all(double value) : top = value, right = value, bottom = value, left = value; - /// Constructs an EdgeDims with only the given values non-zero + /// Constructs an EdgeDims with only the given values non-zero. const EdgeDims.only({ this.top: 0.0, this.right: 0.0, this.bottom: 0.0, this.left: 0.0 }); - /// Constructs an EdgeDims with symmetrical vertical and horizontal offsets + /// Constructs an EdgeDims with symmetrical vertical and horizontal offsets. const EdgeDims.symmetric({ double vertical: 0.0, double horizontal: 0.0 }) : top = vertical, left = horizontal, bottom = vertical, right = horizontal; - /// The offset from the top + /// The offset from the top. final double top; - /// The offset from the right + /// The offset from the right. final double right; - /// The offset from the bottom + /// The offset from the bottom. final double bottom; - /// The offset from the left + /// The offset from the left. final double left; + /// Whether every dimension is non-negative. bool get isNonNegative => top >= 0.0 && right >= 0.0 && bottom >= 0.0 && left >= 0.0; + /// The size that this edge dims would occupy with an empty interior. + Size get collapsedSize => new Size(left + right, top + bottom); + EdgeDims operator-(EdgeDims other) { return new EdgeDims.TRBL( top - other.top, @@ -101,7 +105,7 @@ class EdgeDims { ); } - /// Linearly interpolate between two EdgeDims + /// Linearly interpolate between two EdgeDims. /// /// If either is null, this function interpolates from [EdgeDims.zero]. static EdgeDims lerp(EdgeDims a, EdgeDims b, double t) { @@ -119,7 +123,7 @@ class EdgeDims { ); } - /// An EdgeDims with zero offsets in each direction + /// An EdgeDims with zero offsets in each direction. static const EdgeDims zero = const EdgeDims.TRBL(0.0, 0.0, 0.0, 0.0); bool operator ==(dynamic other) { @@ -416,88 +420,121 @@ void paintImage({ Rect rect, ui.Image image, ui.ColorFilter colorFilter, - fit: ImageFit.scaleDown, + ImageFit fit, repeat: ImageRepeat.noRepeat, + Rect centerSlice, double positionX: 0.5, double positionY: 0.5 }) { - Size bounds = rect.size; - Size imageSize = new Size(image.width.toDouble(), image.height.toDouble()); + Size outputSize = rect.size; + Size inputSize = new Size(image.width.toDouble(), image.height.toDouble()); + Offset sliceBorder; + if (centerSlice != null) { + sliceBorder = new Offset( + centerSlice.left + inputSize.width - centerSlice.right, + centerSlice.top + inputSize.height - centerSlice.bottom + ); + outputSize -= sliceBorder; + inputSize -= sliceBorder; + } Size sourceSize; Size destinationSize; - switch(fit) { + fit ??= centerSlice == null ? ImageFit.scaleDown : ImageFit.fill; + assert(centerSlice == null || (fit != ImageFit.none && fit != ImageFit.cover)); + switch (fit) { case ImageFit.fill: - sourceSize = imageSize; - destinationSize = bounds; + sourceSize = inputSize; + destinationSize = outputSize; break; case ImageFit.contain: - sourceSize = imageSize; - if (bounds.width / bounds.height > sourceSize.width / sourceSize.height) - destinationSize = new Size(sourceSize.width * bounds.height / sourceSize.height, bounds.height); + sourceSize = inputSize; + if (outputSize.width / outputSize.height > sourceSize.width / sourceSize.height) + destinationSize = new Size(sourceSize.width * outputSize.height / sourceSize.height, outputSize.height); else - destinationSize = new Size(bounds.width, sourceSize.height * bounds.width / sourceSize.width); + destinationSize = new Size(outputSize.width, sourceSize.height * outputSize.width / sourceSize.width); break; case ImageFit.cover: - if (bounds.width / bounds.height > imageSize.width / imageSize.height) - sourceSize = new Size(imageSize.width, imageSize.width * bounds.height / bounds.width); + if (outputSize.width / outputSize.height > inputSize.width / inputSize.height) + sourceSize = new Size(inputSize.width, inputSize.width * outputSize.height / outputSize.width); else - sourceSize = new Size(imageSize.height * bounds.width / bounds.height, imageSize.height); - destinationSize = bounds; + sourceSize = new Size(inputSize.height * outputSize.width / outputSize.height, inputSize.height); + destinationSize = outputSize; break; case ImageFit.none: - sourceSize = new Size(math.min(imageSize.width, bounds.width), - math.min(imageSize.height, bounds.height)); + sourceSize = new Size(math.min(inputSize.width, outputSize.width), + math.min(inputSize.height, outputSize.height)); destinationSize = sourceSize; break; case ImageFit.scaleDown: - sourceSize = imageSize; - destinationSize = bounds; + sourceSize = inputSize; + destinationSize = outputSize; if (sourceSize.height > destinationSize.height) destinationSize = new Size(sourceSize.width * destinationSize.height / sourceSize.height, sourceSize.height); if (sourceSize.width > destinationSize.width) destinationSize = new Size(destinationSize.width, sourceSize.height * destinationSize.width / sourceSize.width); break; } + if (centerSlice != null) { + outputSize += sliceBorder; + destinationSize += sliceBorder; + // We don't have the ability to draw a subset of the image at the same time + // as we apply a nine-patch stretch. + assert(sourceSize == inputSize); + } // TODO(abarth): Implement |repeat|. Paint paint = new Paint()..isAntiAlias = false; if (colorFilter != null) paint.colorFilter = colorFilter; - double dx = (bounds.width - destinationSize.width) * positionX; - double dy = (bounds.height - destinationSize.height) * positionY; + double dx = (outputSize.width - destinationSize.width) * positionX; + double dy = (outputSize.height - destinationSize.height) * positionY; Point destinationPosition = rect.topLeft + new Offset(dx, dy); - canvas.drawImageRect(image, Point.origin & sourceSize, destinationPosition & destinationSize, paint); + Rect destinationRect = destinationPosition & destinationSize; + if (centerSlice == null) + canvas.drawImageRect(image, Point.origin & sourceSize, destinationRect, paint); + else + canvas.drawImageNine(image, centerSlice, destinationRect, paint); } typedef void BackgroundImageChangeListener(); -/// A background image for a box +/// A background image for a box. class BackgroundImage { - /// How the background image should be inscribed into the box + /// How the background image should be inscribed into the box. final ImageFit fit; - /// How to paint any portions of the box not covered by the background image + /// How to paint any portions of the box not covered by the background image. final ImageRepeat repeat; - /// A color filter to apply to the background image before painting it + /// The center slice for a nine-patch image. + /// + /// The region of the image inside the center slice will be stretched both + /// horizontally and vertically to fit the image into its destination. The + /// region of the image above and below the center slice will be stretched + /// only horizontally and the region of the image to the left and right of + /// the center slice will be stretched only vertically. + final Rect centerSlice; + + /// A color filter to apply to the background image before painting it. final ui.ColorFilter colorFilter; BackgroundImage({ ImageResource image, - this.fit: ImageFit.scaleDown, + this.fit, this.repeat: ImageRepeat.noRepeat, + this.centerSlice, this.colorFilter }) : _imageResource = image; - ui.Image _image; - /// The image to be painted into the background + /// The image to be painted into the background. ui.Image get image => _image; + ui.Image _image; ImageResource _imageResource; final List _listeners = new List(); - /// Call listener when the background images changes (e.g., arrives from the network) + /// Call listener when the background images changes (e.g., arrives from the network). void addChangeListener(BackgroundImageChangeListener listener) { // We add the listener to the _imageResource first so that the first change // listener doesn't get callback synchronously if the image resource is @@ -507,7 +544,7 @@ class BackgroundImage { _listeners.add(listener); } - /// No longer call listener when the background image changes + /// No longer call listener when the background image changes. void removeChangeListener(BackgroundImageChangeListener listener) { _listeners.remove(listener); // We need to remove ourselves as listeners from the _imageResource so that diff --git a/packages/flutter/lib/src/rendering/image.dart b/packages/flutter/lib/src/rendering/image.dart index ce6800c807..3863cc0f22 100644 --- a/packages/flutter/lib/src/rendering/image.dart +++ b/packages/flutter/lib/src/rendering/image.dart @@ -9,7 +9,7 @@ import 'package:flutter/painting.dart'; import 'box.dart'; import 'object.dart'; -/// An image in the render tree +/// An image in the render tree. /// /// The render image attempts to find a size for itself that fits in the given /// constraints and preserves the image's intrinisc aspect ratio. @@ -19,18 +19,20 @@ class RenderImage extends RenderBox { double width, double height, ui.ColorFilter colorFilter, - fit: ImageFit.scaleDown, - repeat: ImageRepeat.noRepeat + ImageFit fit, + repeat: ImageRepeat.noRepeat, + Rect centerSlice }) : _image = image, _width = width, _height = height, _colorFilter = colorFilter, _fit = fit, - _repeat = repeat; + _repeat = repeat, + _centerSlice = centerSlice; - ui.Image _image; - /// The image to display + /// The image to display. ui.Image get image => _image; + ui.Image _image; void set image (ui.Image value) { if (value == _image) return; @@ -40,9 +42,9 @@ class RenderImage extends RenderBox { markNeedsLayout(); } - double _width; - /// If non-null, requires the image to have this width + /// If non-null, requires the image to have this width. double get width => _width; + double _width; void set width (double value) { if (value == _width) return; @@ -50,9 +52,9 @@ class RenderImage extends RenderBox { markNeedsLayout(); } - double _height; - /// If non-null, requires the image to have this height + /// If non-null, requires the image to have this height. double get height => _height; + double _height; void set height (double value) { if (value == _height) return; @@ -60,9 +62,9 @@ class RenderImage extends RenderBox { markNeedsLayout(); } - ui.ColorFilter _colorFilter; /// If non-null, apply this color filter to the image before painint. ui.ColorFilter get colorFilter => _colorFilter; + ui.ColorFilter _colorFilter; void set colorFilter (ui.ColorFilter value) { if (value == _colorFilter) return; @@ -70,9 +72,9 @@ class RenderImage extends RenderBox { markNeedsPaint(); } - ImageFit _fit; - /// How to inscribe the image into the place allocated during layout + /// How to inscribe the image into the place allocated during layout. ImageFit get fit => _fit; + ImageFit _fit; void set fit (ImageFit value) { if (value == _fit) return; @@ -80,9 +82,9 @@ class RenderImage extends RenderBox { markNeedsPaint(); } - ImageRepeat _repeat; - /// Not yet implemented + /// Not yet implemented. ImageRepeat get repeat => _repeat; + ImageRepeat _repeat; void set repeat (ImageRepeat value) { if (value == _repeat) return; @@ -90,7 +92,23 @@ class RenderImage extends RenderBox { markNeedsPaint(); } - /// Find a size for the render image within the given constraints + /// The center slice for a nine-patch image. + /// + /// The region of the image inside the center slice will be stretched both + /// horizontally and vertically to fit the image into its destination. The + /// region of the image above and below the center slice will be stretched + /// only horizontally and the region of the image to the left and right of + /// the center slice will be stretched only vertically. + Rect get centerSlice => _centerSlice; + Rect _centerSlice; + void set centerSlice (Rect value) { + if (value == _centerSlice) + return; + _centerSlice = value; + markNeedsPaint(); + } + + /// Find a size for the render image within the given constraints. /// /// - The dimensions of the RenderImage must fit within the constraints. /// - The aspect ratio of the RenderImage matches the instrinsic aspect @@ -170,6 +188,7 @@ class RenderImage extends RenderBox { image: _image, colorFilter: _colorFilter, fit: _fit, + centerSlice: _centerSlice, repeat: _repeat ); } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index f4f3d1c4c0..202980efd0 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -809,8 +809,9 @@ class Image extends LeafRenderObjectWidget { this.width, this.height, this.colorFilter, - this.fit: ImageFit.scaleDown, - this.repeat: ImageRepeat.noRepeat + this.fit, + this.repeat: ImageRepeat.noRepeat, + this.centerSlice }) : super(key: key); final ui.Image image; @@ -819,6 +820,7 @@ class Image extends LeafRenderObjectWidget { final ui.ColorFilter colorFilter; final ImageFit fit; final ImageRepeat repeat; + final Rect centerSlice; RenderImage createRenderObject() => new RenderImage( image: image, @@ -826,7 +828,8 @@ class Image extends LeafRenderObjectWidget { height: height, colorFilter: colorFilter, fit: fit, - repeat: repeat); + repeat: repeat, + centerSlice: centerSlice); void updateRenderObject(RenderImage renderObject, Image oldWidget) { renderObject.image = image; @@ -835,6 +838,7 @@ class Image extends LeafRenderObjectWidget { renderObject.colorFilter = colorFilter; renderObject.fit = fit; renderObject.repeat = repeat; + renderObject.centerSlice = centerSlice; } } @@ -845,8 +849,9 @@ class ImageListener extends StatefulComponent { this.width, this.height, this.colorFilter, - this.fit: ImageFit.scaleDown, - this.repeat: ImageRepeat.noRepeat + this.fit, + this.repeat: ImageRepeat.noRepeat, + this.centerSlice }) : super(key: key) { assert(image != null); } @@ -857,6 +862,7 @@ class ImageListener extends StatefulComponent { final ui.ColorFilter colorFilter; final ImageFit fit; final ImageRepeat repeat; + final Rect centerSlice; _ImageListenerState createState() => new _ImageListenerState(); } @@ -894,7 +900,8 @@ class _ImageListenerState extends State { height: config.height, colorFilter: config.colorFilter, fit: config.fit, - repeat: config.repeat + repeat: config.repeat, + centerSlice: config.centerSlice ); } } @@ -906,8 +913,9 @@ class NetworkImage extends StatelessComponent { this.width, this.height, this.colorFilter, - this.fit: ImageFit.scaleDown, - this.repeat: ImageRepeat.noRepeat + this.fit, + this.repeat: ImageRepeat.noRepeat, + this.centerSlice }) : super(key: key); final String src; @@ -916,6 +924,7 @@ class NetworkImage extends StatelessComponent { final ui.ColorFilter colorFilter; final ImageFit fit; final ImageRepeat repeat; + final Rect centerSlice; Widget build(BuildContext context) { return new ImageListener( @@ -924,7 +933,8 @@ class NetworkImage extends StatelessComponent { height: height, colorFilter: colorFilter, fit: fit, - repeat: repeat + repeat: repeat, + centerSlice: centerSlice ); } } @@ -937,8 +947,9 @@ class AssetImage extends StatelessComponent { this.width, this.height, this.colorFilter, - this.fit: ImageFit.scaleDown, - this.repeat: ImageRepeat.noRepeat + this.fit, + this.repeat: ImageRepeat.noRepeat, + this.centerSlice }) : super(key: key); final String name; @@ -948,6 +959,7 @@ class AssetImage extends StatelessComponent { final ui.ColorFilter colorFilter; final ImageFit fit; final ImageRepeat repeat; + final Rect centerSlice; Widget build(BuildContext context) { return new ImageListener( @@ -956,7 +968,8 @@ class AssetImage extends StatelessComponent { height: height, colorFilter: colorFilter, fit: fit, - repeat: repeat + repeat: repeat, + centerSlice: centerSlice ); } } diff --git a/packages/flutter_sprites/lib/label.dart b/packages/flutter_sprites/lib/label.dart index be515e8598..b428c1042c 100644 --- a/packages/flutter_sprites/lib/label.dart +++ b/packages/flutter_sprites/lib/label.dart @@ -1,7 +1,7 @@ part of skysprites; /// Labels are used to display a string of text in a the node tree. To align -/// the label, the textAlign property of teh [TextStyle] can be set. +/// the label, the textAlign property of the [TextStyle] can be set. class Label extends Node { /// Creates a new Label with the provided [_text] and [_textStyle]. Label(this._text, [this._textStyle]) { From e3d7cba48241677ebe766463c9728aaddc7fc5c1 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 13 Oct 2015 14:15:27 -0700 Subject: [PATCH 2/4] Updates flutter sprites pubspec --- packages/flutter_sprites/pubspec.yaml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/flutter_sprites/pubspec.yaml b/packages/flutter_sprites/pubspec.yaml index d9ec7d32d4..4e1035b28e 100644 --- a/packages/flutter_sprites/pubspec.yaml +++ b/packages/flutter_sprites/pubspec.yaml @@ -1,12 +1,9 @@ -name: skysprites +name: flutter_sprites description: A sprite toolkit built on top of Flutter -version: 0.0.1 +version: 0.0.2 author: Flutter Authors homepage: http://flutter.io dependencies: flutter: ">=0.0.3 <0.1.0" sky_tools: ">=0.0.18 <0.1.0" - box2d: any -dependency_overrides: - flutter: - path: ../sky/packages/sky + box2d: ">=0.2.0 <0.3.0" From 1cf1cf9c64a37f904a41f4132e5fb7e98d5806ea Mon Sep 17 00:00:00 2001 From: Hixie Date: Fri, 9 Oct 2015 10:15:56 -0700 Subject: [PATCH 3/4] Port first sector demo to fn3 Also, fix warnings in rendering/sector_layout.dart Also, fix hit testing in rendering/sector_layout.dart Also, add WidgetToRenderBoxAdapter Also, make the rendering library debugging tools more resilient to dumping stuff before layout is complete. --- examples/raw/pubspec.yaml | 2 +- examples/rendering/lib/sector_layout.dart | 527 +++++++++++++++++ examples/rendering/pubspec.yaml | 2 +- examples/rendering/sector_layout.dart | 534 +----------------- examples/widgets/pubspec.yaml | 5 +- .../widgets/{sector.dart.old => sector.dart} | 58 +- packages/flutter/lib/src/rendering/box.dart | 2 +- packages/flutter/lib/src/widgets/basic.dart | 16 + 8 files changed, 584 insertions(+), 562 deletions(-) create mode 100644 examples/rendering/lib/sector_layout.dart rename examples/widgets/{sector.dart.old => sector.dart} (76%) diff --git a/examples/raw/pubspec.yaml b/examples/raw/pubspec.yaml index 02ca8c18af..0443125d5f 100644 --- a/examples/raw/pubspec.yaml +++ b/examples/raw/pubspec.yaml @@ -1,4 +1,4 @@ -name: raw +name: sky_raw_examples dependencies: flutter: ">=0.0.3 <0.1.0" sky_tools: any diff --git a/examples/rendering/lib/sector_layout.dart b/examples/rendering/lib/sector_layout.dart new file mode 100644 index 0000000000..8944c5f672 --- /dev/null +++ b/examples/rendering/lib/sector_layout.dart @@ -0,0 +1,527 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:math' as math; +import 'dart:ui' as ui; + +import 'package:flutter/rendering.dart'; + +const double kTwoPi = 2 * math.PI; + +class SectorConstraints extends Constraints { + const SectorConstraints({ + this.minDeltaRadius: 0.0, + this.maxDeltaRadius: double.INFINITY, + this.minDeltaTheta: 0.0, + this.maxDeltaTheta: kTwoPi + }); + + const SectorConstraints.tight({ double deltaRadius: 0.0, double deltaTheta: 0.0 }) + : minDeltaRadius = deltaRadius, + maxDeltaRadius = deltaRadius, + minDeltaTheta = deltaTheta, + maxDeltaTheta = deltaTheta; + + final double minDeltaRadius; + final double maxDeltaRadius; + final double minDeltaTheta; + final double maxDeltaTheta; + + double constrainDeltaRadius(double deltaRadius) { + return clamp(min: minDeltaRadius, max: maxDeltaRadius, value: deltaRadius); + } + + double constrainDeltaTheta(double deltaTheta) { + return clamp(min: minDeltaTheta, max: maxDeltaTheta, value: deltaTheta); + } + + bool get isTight => minDeltaTheta >= maxDeltaTheta && minDeltaTheta >= maxDeltaTheta; +} + +class SectorDimensions { + const SectorDimensions({ this.deltaRadius: 0.0, this.deltaTheta: 0.0 }); + + factory SectorDimensions.withConstraints( + SectorConstraints constraints, + { double deltaRadius: 0.0, double deltaTheta: 0.0 } + ) { + return new SectorDimensions( + deltaRadius: constraints.constrainDeltaRadius(deltaRadius), + deltaTheta: constraints.constrainDeltaTheta(deltaTheta) + ); + } + + final double deltaRadius; + final double deltaTheta; +} + +class SectorParentData extends ParentData { + double radius = 0.0; + double theta = 0.0; +} + +abstract class RenderSector extends RenderObject { + + void setupParentData(RenderObject child) { + if (child.parentData is! SectorParentData) + child.parentData = new SectorParentData(); + } + + // RenderSectors always use SectorParentData subclasses, as they need to be + // able to read their position information for painting and hit testing. + SectorParentData get parentData => super.parentData; + + SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { + return new SectorDimensions.withConstraints(constraints); + } + + SectorConstraints get constraints => super.constraints; + bool debugDoesMeetConstraints() { + assert(constraints != null); + assert(deltaRadius != null); + assert(deltaRadius < double.INFINITY); + assert(deltaTheta != null); + assert(deltaTheta < double.INFINITY); + return constraints.minDeltaRadius <= deltaRadius && + deltaRadius <= math.max(constraints.minDeltaRadius, constraints.maxDeltaRadius) && + constraints.minDeltaTheta <= deltaTheta && + deltaTheta <= math.max(constraints.minDeltaTheta, constraints.maxDeltaTheta); + } + void performResize() { + // default behaviour for subclasses that have sizedByParent = true + deltaRadius = constraints.constrainDeltaRadius(0.0); + deltaTheta = constraints.constrainDeltaTheta(0.0); + } + void performLayout() { + // descendants have to either override performLayout() to set both + // the dimensions and lay out children, or, set sizedByParent to + // true so that performResize()'s logic above does its thing. + assert(sizedByParent); + } + + Rect get paintBounds => new Rect.fromLTWH(0.0, 0.0, 2.0 * deltaRadius, 2.0 * deltaRadius); + + bool hitTest(HitTestResult result, { double radius, double theta }) { + if (radius < parentData.radius || radius >= parentData.radius + deltaRadius || + theta < parentData.theta || theta >= parentData.theta + deltaTheta) + return false; + hitTestChildren(result, radius: radius, theta: theta); + result.add(new HitTestEntry(this)); + return true; + } + void hitTestChildren(HitTestResult result, { double radius, double theta }) { } + + double deltaRadius; + double deltaTheta; +} + +abstract class RenderDecoratedSector extends RenderSector { + + RenderDecoratedSector(BoxDecoration decoration) : _decoration = decoration; + + BoxDecoration _decoration; + BoxDecoration get decoration => _decoration; + void set decoration (BoxDecoration value) { + if (value == _decoration) + return; + _decoration = value; + markNeedsPaint(); + } + + // offset must point to the center of the circle + void paint(PaintingContext context, Offset offset) { + assert(deltaRadius != null); + assert(deltaTheta != null); + assert(parentData is SectorParentData); + + if (_decoration == null) + return; + + if (_decoration.backgroundColor != null) { + final PaintingCanvas canvas = context.canvas; + Paint paint = new Paint()..color = _decoration.backgroundColor; + Path path = new Path(); + double outerRadius = (parentData.radius + deltaRadius); + Rect outerBounds = new Rect.fromLTRB(offset.dx-outerRadius, offset.dy-outerRadius, offset.dx+outerRadius, offset.dy+outerRadius); + path.arcTo(outerBounds, parentData.theta, deltaTheta, true); + double innerRadius = parentData.radius; + Rect innerBounds = new Rect.fromLTRB(offset.dx-innerRadius, offset.dy-innerRadius, offset.dx+innerRadius, offset.dy+innerRadius); + path.arcTo(innerBounds, parentData.theta + deltaTheta, -deltaTheta, false); + path.close(); + canvas.drawPath(path, paint); + } + } + +} + +class SectorChildListParentData extends SectorParentData with ContainerParentDataMixin { } + +class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRenderObjectMixin { + RenderSectorWithChildren(BoxDecoration decoration) : super(decoration); + + void hitTestChildren(HitTestResult result, { double radius, double theta }) { + RenderSector child = lastChild; + while (child != null) { + if (child.hitTest(result, radius: radius, theta: theta)) + return; + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.previousSibling; + } + } + + void visitChildren(RenderObjectVisitor visitor) { + RenderSector child = lastChild; + while (child != null) { + visitor(child); + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.previousSibling; + } + } +} + +class RenderSectorRing extends RenderSectorWithChildren { + // lays out RenderSector children in a ring + + RenderSectorRing({ + BoxDecoration decoration, + double deltaRadius: double.INFINITY, + double padding: 0.0 + }) : _padding = padding, _desiredDeltaRadius = deltaRadius, super(decoration); + + double _desiredDeltaRadius; + double get desiredDeltaRadius => _desiredDeltaRadius; + void set desiredDeltaRadius(double value) { + assert(value != null); + if (_desiredDeltaRadius != value) { + _desiredDeltaRadius = value; + markNeedsLayout(); + } + } + + double _padding; + double get padding => _padding; + void set padding(double value) { + // TODO(ianh): avoid code duplication + assert(value != null); + if (_padding != value) { + _padding = value; + markNeedsLayout(); + } + } + + void setupParentData(RenderObject child) { + // TODO(ianh): avoid code duplication + if (child.parentData is! SectorChildListParentData) + child.parentData = new SectorChildListParentData(); + } + + SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { + double outerDeltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); + double innerDeltaRadius = outerDeltaRadius - padding * 2.0; + double childRadius = radius + padding; + double paddingTheta = math.atan(padding / (radius + outerDeltaRadius)); + double innerTheta = paddingTheta; // increments with each child + double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta); + RenderSector child = firstChild; + while (child != null) { + SectorConstraints innerConstraints = new SectorConstraints( + maxDeltaRadius: innerDeltaRadius, + maxDeltaTheta: remainingDeltaTheta + ); + SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius); + innerTheta += childDimensions.deltaTheta; + remainingDeltaTheta -= childDimensions.deltaTheta; + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.nextSibling; + if (child != null) { + innerTheta += paddingTheta; + remainingDeltaTheta -= paddingTheta; + } + } + return new SectorDimensions.withConstraints(constraints, + deltaRadius: outerDeltaRadius, + deltaTheta: innerTheta); + } + + void performLayout() { + assert(this.parentData is SectorParentData); + deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); + assert(deltaRadius < double.INFINITY); + double innerDeltaRadius = deltaRadius - padding * 2.0; + double childRadius = this.parentData.radius + padding; + double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRadius)); + double innerTheta = paddingTheta; // increments with each child + double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta); + RenderSector child = firstChild; + while (child != null) { + SectorConstraints innerConstraints = new SectorConstraints( + maxDeltaRadius: innerDeltaRadius, + maxDeltaTheta: remainingDeltaTheta + ); + assert(child.parentData is SectorParentData); + child.parentData.theta = innerTheta; + child.parentData.radius = childRadius; + child.layout(innerConstraints, parentUsesSize: true); + innerTheta += child.deltaTheta; + remainingDeltaTheta -= child.deltaTheta; + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.nextSibling; + if (child != null) { + innerTheta += paddingTheta; + remainingDeltaTheta -= paddingTheta; + } + } + deltaTheta = innerTheta; + } + + // offset must point to the center of our circle + // each sector then knows how to paint itself at its location + void paint(PaintingContext context, Offset offset) { + // TODO(ianh): avoid code duplication + super.paint(context, offset); + RenderSector child = firstChild; + while (child != null) { + context.paintChild(child, offset.toPoint()); + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.nextSibling; + } + } + +} + +class RenderSectorSlice extends RenderSectorWithChildren { + // lays out RenderSector children in a stack + + RenderSectorSlice({ + BoxDecoration decoration, + double deltaTheta: kTwoPi, + double padding: 0.0 + }) : _padding = padding, _desiredDeltaTheta = deltaTheta, super(decoration); + + double _desiredDeltaTheta; + double get desiredDeltaTheta => _desiredDeltaTheta; + void set desiredDeltaTheta(double value) { + assert(value != null); + if (_desiredDeltaTheta != value) { + _desiredDeltaTheta = value; + markNeedsLayout(); + } + } + + double _padding; + double get padding => _padding; + void set padding(double value) { + // TODO(ianh): avoid code duplication + assert(value != null); + if (_padding != value) { + _padding = value; + markNeedsLayout(); + } + } + + void setupParentData(RenderObject child) { + // TODO(ianh): avoid code duplication + if (child.parentData is! SectorChildListParentData) + child.parentData = new SectorChildListParentData(); + } + + SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { + assert(this.parentData is SectorParentData); + double paddingTheta = math.atan(padding / this.parentData.radius); + double outerDeltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); + double innerDeltaTheta = outerDeltaTheta - paddingTheta * 2.0; + double childRadius = this.parentData.radius + padding; + double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0); + RenderSector child = firstChild; + while (child != null) { + SectorConstraints innerConstraints = new SectorConstraints( + maxDeltaRadius: remainingDeltaRadius, + maxDeltaTheta: innerDeltaTheta + ); + SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius); + childRadius += childDimensions.deltaRadius; + remainingDeltaRadius -= childDimensions.deltaRadius; + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.nextSibling; + childRadius += padding; + remainingDeltaRadius -= padding; + } + return new SectorDimensions.withConstraints(constraints, + deltaRadius: childRadius - this.parentData.radius, + deltaTheta: outerDeltaTheta); + } + + void performLayout() { + assert(this.parentData is SectorParentData); + deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); + assert(deltaTheta <= kTwoPi); + double paddingTheta = math.atan(padding / this.parentData.radius); + double innerTheta = this.parentData.theta + paddingTheta; + double innerDeltaTheta = deltaTheta - paddingTheta * 2.0; + double childRadius = this.parentData.radius + padding; + double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0); + RenderSector child = firstChild; + while (child != null) { + SectorConstraints innerConstraints = new SectorConstraints( + maxDeltaRadius: remainingDeltaRadius, + maxDeltaTheta: innerDeltaTheta + ); + child.parentData.theta = innerTheta; + child.parentData.radius = childRadius; + child.layout(innerConstraints, parentUsesSize: true); + childRadius += child.deltaRadius; + remainingDeltaRadius -= child.deltaRadius; + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.nextSibling; + childRadius += padding; + remainingDeltaRadius -= padding; + } + deltaRadius = childRadius - this.parentData.radius; + } + + // offset must point to the center of our circle + // each sector then knows how to paint itself at its location + void paint(PaintingContext context, Offset offset) { + // TODO(ianh): avoid code duplication + super.paint(context, offset); + RenderSector child = firstChild; + while (child != null) { + assert(child.parentData is SectorChildListParentData); + context.paintChild(child, offset.toPoint()); + final SectorChildListParentData childParentData = child.parentData; + child = childParentData.nextSibling; + } + } + +} + +class RenderBoxToRenderSectorAdapter extends RenderBox with RenderObjectWithChildMixin { + + RenderBoxToRenderSectorAdapter({ double innerRadius: 0.0, RenderSector child }) : + _innerRadius = innerRadius { + this.child = child; + } + + double _innerRadius; + double get innerRadius => _innerRadius; + void set innerRadius(double value) { + _innerRadius = value; + markNeedsLayout(); + } + + void setupParentData(RenderObject child) { + if (child.parentData is! SectorParentData) + child.parentData = new SectorParentData(); + } + + double getMinIntrinsicWidth(BoxConstraints constraints) { + if (child == null) + return super.getMinIntrinsicWidth(constraints); + return getIntrinsicDimensions(constraints).width; + } + + double getMaxIntrinsicWidth(BoxConstraints constraints) { + if (child == null) + return super.getMaxIntrinsicWidth(constraints); + return getIntrinsicDimensions(constraints).width; + } + + double getMinIntrinsicHeight(BoxConstraints constraints) { + if (child == null) + return super.getMinIntrinsicHeight(constraints); + return getIntrinsicDimensions(constraints).height; + } + + double getMaxIntrinsicHeight(BoxConstraints constraints) { + if (child == null) + return super.getMaxIntrinsicHeight(constraints); + return getIntrinsicDimensions(constraints).height; + } + + Size getIntrinsicDimensions(BoxConstraints constraints) { + assert(child is RenderSector); + assert(child.parentData is SectorParentData); + assert(constraints.maxWidth < double.INFINITY || constraints.maxHeight < double.INFINITY); + double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius; + SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius); + double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0; + return constraints.constrain(new Size(dimension, dimension)); + } + + void performLayout() { + if (child == null) { + size = constraints.constrain(Size.zero); + } else { + assert(child is RenderSector); + assert(constraints.maxWidth < double.INFINITY || constraints.maxHeight < double.INFINITY); + double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius; + assert(child.parentData is SectorParentData); + child.parentData.radius = innerRadius; + child.parentData.theta = 0.0; + child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), parentUsesSize: true); + double dimension = (innerRadius + child.deltaRadius) * 2.0; + size = constraints.constrain(new Size(dimension, dimension)); + } + } + + void paint(PaintingContext context, Offset offset) { + super.paint(context, offset); + if (child != null) { + Rect bounds = offset & size; + // we move the offset to the center of the circle for the RenderSectors + context.paintChild(child, bounds.center); + } + } + + bool hitTest(HitTestResult result, { Point position }) { + if (child == null) + return false; + double x = position.x; + double y = position.y; + // translate to our origin + x -= size.width/2.0; + y -= size.height/2.0; + // convert to radius/theta + double radius = math.sqrt(x*x+y*y); + double theta = (math.atan2(x, -y) - math.PI/2.0) % kTwoPi; + if (radius < innerRadius) + return false; + if (radius >= innerRadius + child.deltaRadius) + return false; + if (theta > child.deltaTheta) + return false; + child.hitTest(result, radius: radius, theta: theta); + result.add(new BoxHitTestEntry(this, position)); + return true; + } + +} + +class RenderSolidColor extends RenderDecoratedSector { + RenderSolidColor(Color backgroundColor, { + this.desiredDeltaRadius: double.INFINITY, + this.desiredDeltaTheta: kTwoPi + }) : this.backgroundColor = backgroundColor, + super(new BoxDecoration(backgroundColor: backgroundColor)); + + double desiredDeltaRadius; + double desiredDeltaTheta; + final Color backgroundColor; + + SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { + return new SectorDimensions.withConstraints(constraints, deltaTheta: desiredDeltaTheta); + } + + void performLayout() { + deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); + deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); + } + + void handleEvent(ui.Event event, HitTestEntry entry) { + if (event.type == 'pointerdown') + decoration = new BoxDecoration(backgroundColor: const Color(0xFFFF0000)); + else if (event.type == 'pointerup') + decoration = new BoxDecoration(backgroundColor: backgroundColor); + } +} diff --git a/examples/rendering/pubspec.yaml b/examples/rendering/pubspec.yaml index 0eb70a38f8..5158fb4835 100644 --- a/examples/rendering/pubspec.yaml +++ b/examples/rendering/pubspec.yaml @@ -1,4 +1,4 @@ -name: rendering +name: flutter_rendering_examples dependencies: flutter: ">=0.0.3 <0.1.0" sky_tools: any diff --git a/examples/rendering/sector_layout.dart b/examples/rendering/sector_layout.dart index 912127ad0a..ac3fc4699e 100644 --- a/examples/rendering/sector_layout.dart +++ b/examples/rendering/sector_layout.dart @@ -2,539 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:math' as math; -import 'dart:ui' as ui; - +import 'dart:async'; import 'package:flutter/rendering.dart'; - -const double kTwoPi = 2 * math.PI; - -class SectorConstraints extends Constraints { - const SectorConstraints({ - this.minDeltaRadius: 0.0, - this.maxDeltaRadius: double.INFINITY, - this.minDeltaTheta: 0.0, - this.maxDeltaTheta: kTwoPi - }); - - const SectorConstraints.tight({ double deltaRadius: 0.0, double deltaTheta: 0.0 }) - : minDeltaRadius = deltaRadius, - maxDeltaRadius = deltaRadius, - minDeltaTheta = deltaTheta, - maxDeltaTheta = deltaTheta; - - final double minDeltaRadius; - final double maxDeltaRadius; - final double minDeltaTheta; - final double maxDeltaTheta; - - double constrainDeltaRadius(double deltaRadius) { - return clamp(min: minDeltaRadius, max: maxDeltaRadius, value: deltaRadius); - } - - double constrainDeltaTheta(double deltaTheta) { - return clamp(min: minDeltaTheta, max: maxDeltaTheta, value: deltaTheta); - } - - bool get isTight => minDeltaTheta >= maxDeltaTheta && minDeltaTheta >= maxDeltaTheta; -} - -class SectorDimensions { - const SectorDimensions({ this.deltaRadius: 0.0, this.deltaTheta: 0.0 }); - - factory SectorDimensions.withConstraints( - SectorConstraints constraints, - { double deltaRadius: 0.0, double deltaTheta: 0.0 } - ) { - return new SectorDimensions( - deltaRadius: constraints.constrainDeltaRadius(deltaRadius), - deltaTheta: constraints.constrainDeltaTheta(deltaTheta) - ); - } - - final double deltaRadius; - final double deltaTheta; -} - -class SectorParentData extends ParentData { - double radius = 0.0; - double theta = 0.0; -} - -abstract class RenderSector extends RenderObject { - - void setupParentData(RenderObject child) { - if (child.parentData is! SectorParentData) - child.parentData = new SectorParentData(); - } - - SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { - return new SectorDimensions.withConstraints(constraints); - } - - SectorConstraints get constraints => super.constraints; - bool debugDoesMeetConstraints() { - assert(constraints != null); - assert(deltaRadius != null); - assert(deltaRadius < double.INFINITY); - assert(deltaTheta != null); - assert(deltaTheta < double.INFINITY); - return constraints.minDeltaRadius <= deltaRadius && - deltaRadius <= math.max(constraints.minDeltaRadius, constraints.maxDeltaRadius) && - constraints.minDeltaTheta <= deltaTheta && - deltaTheta <= math.max(constraints.minDeltaTheta, constraints.maxDeltaTheta); - } - void performResize() { - // default behaviour for subclasses that have sizedByParent = true - deltaRadius = constraints.constrainDeltaRadius(0.0); - deltaTheta = constraints.constrainDeltaTheta(0.0); - } - void performLayout() { - // descendants have to either override performLayout() to set both - // the dimensions and lay out children, or, set sizedByParent to - // true so that performResize()'s logic above does its thing. - assert(sizedByParent); - } - - Rect get paintBounds => new Rect.fromLTWH(0.0, 0.0, 2.0 * deltaRadius, 2.0 * deltaRadius); - - bool hitTest(HitTestResult result, { double radius, double theta }) { - assert(parentData is SectorParentData); - if (radius < parentData.radius || radius >= parentData.radius + deltaRadius || - theta < parentData.theta || theta >= parentData.theta + deltaTheta) - return false; - hitTestChildren(result, radius: radius, theta: theta); - result.add(new HitTestEntry(this)); - return true; - } - void hitTestChildren(HitTestResult result, { double radius, double theta }) { } - - double deltaRadius; - double deltaTheta; -} - -abstract class RenderDecoratedSector extends RenderSector { - - RenderDecoratedSector(BoxDecoration decoration) : _decoration = decoration; - - BoxDecoration _decoration; - BoxDecoration get decoration => _decoration; - void set decoration (BoxDecoration value) { - if (value == _decoration) - return; - _decoration = value; - markNeedsPaint(); - } - - // offset must point to the center of the circle - void paint(PaintingContext context, Offset offset) { - assert(deltaRadius != null); - assert(deltaTheta != null); - assert(parentData is SectorParentData); - - if (_decoration == null) - return; - - if (_decoration.backgroundColor != null) { - final PaintingCanvas canvas = context.canvas; - Paint paint = new Paint()..color = _decoration.backgroundColor; - Path path = new Path(); - double outerRadius = (parentData.radius + deltaRadius); - Rect outerBounds = new Rect.fromLTRB(offset.dx-outerRadius, offset.dy-outerRadius, offset.dx+outerRadius, offset.dy+outerRadius); - path.arcTo(outerBounds, parentData.theta, deltaTheta, true); - double innerRadius = parentData.radius; - Rect innerBounds = new Rect.fromLTRB(offset.dx-innerRadius, offset.dy-innerRadius, offset.dx+innerRadius, offset.dy+innerRadius); - path.arcTo(innerBounds, parentData.theta + deltaTheta, -deltaTheta, false); - path.close(); - canvas.drawPath(path, paint); - } - } - -} - -class SectorChildListParentData extends SectorParentData with ContainerParentDataMixin { } - -class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRenderObjectMixin { - RenderSectorWithChildren(BoxDecoration decoration) : super(decoration); - - void hitTestChildren(HitTestResult result, { double radius, double theta }) { - RenderSector child = lastChild; - while (child != null) { - assert(child.parentData is SectorChildListParentData); - if (child.hitTest(result, radius: radius, theta: theta)) - return; - child = child.parentData.previousSibling; - } - } - - void visitChildren(RenderObjectVisitor visitor) { - RenderSector child = lastChild; - while (child != null) { - visitor(child); - child = child.parentData.previousSibling; - } - } -} - -class RenderSectorRing extends RenderSectorWithChildren { - // lays out RenderSector children in a ring - - RenderSectorRing({ - BoxDecoration decoration, - double deltaRadius: double.INFINITY, - double padding: 0.0 - }) : super(decoration), _padding = padding, _desiredDeltaRadius = deltaRadius; - - double _desiredDeltaRadius; - double get desiredDeltaRadius => _desiredDeltaRadius; - void set desiredDeltaRadius(double value) { - assert(value != null); - if (_desiredDeltaRadius != value) { - _desiredDeltaRadius = value; - markNeedsLayout(); - } - } - - double _padding; - double get padding => _padding; - void set padding(double value) { - // TODO(ianh): avoid code duplication - assert(value != null); - if (_padding != value) { - _padding = value; - markNeedsLayout(); - } - } - - void setupParentData(RenderObject child) { - // TODO(ianh): avoid code duplication - if (child.parentData is! SectorChildListParentData) - child.parentData = new SectorChildListParentData(); - } - - SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { - double outerDeltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); - double innerDeltaRadius = outerDeltaRadius - padding * 2.0; - double childRadius = radius + padding; - double paddingTheta = math.atan(padding / (radius + outerDeltaRadius)); - double innerTheta = paddingTheta; // increments with each child - double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta); - RenderSector child = firstChild; - while (child != null) { - SectorConstraints innerConstraints = new SectorConstraints( - maxDeltaRadius: innerDeltaRadius, - maxDeltaTheta: remainingDeltaTheta - ); - SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius); - innerTheta += childDimensions.deltaTheta; - remainingDeltaTheta -= childDimensions.deltaTheta; - assert(child.parentData is SectorChildListParentData); - child = child.parentData.nextSibling; - if (child != null) { - innerTheta += paddingTheta; - remainingDeltaTheta -= paddingTheta; - } - } - return new SectorDimensions.withConstraints(constraints, - deltaRadius: outerDeltaRadius, - deltaTheta: innerTheta); - } - - void performLayout() { - assert(this.parentData is SectorParentData); - deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); - assert(deltaRadius < double.INFINITY); - double innerDeltaRadius = deltaRadius - padding * 2.0; - double childRadius = this.parentData.radius + padding; - double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRadius)); - double innerTheta = paddingTheta; // increments with each child - double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta); - RenderSector child = firstChild; - while (child != null) { - SectorConstraints innerConstraints = new SectorConstraints( - maxDeltaRadius: innerDeltaRadius, - maxDeltaTheta: remainingDeltaTheta - ); - assert(child.parentData is SectorParentData); - child.parentData.theta = innerTheta; - child.parentData.radius = childRadius; - child.layout(innerConstraints, parentUsesSize: true); - innerTheta += child.deltaTheta; - remainingDeltaTheta -= child.deltaTheta; - assert(child.parentData is SectorChildListParentData); - child = child.parentData.nextSibling; - if (child != null) { - innerTheta += paddingTheta; - remainingDeltaTheta -= paddingTheta; - } - } - deltaTheta = innerTheta; - } - - // offset must point to the center of our circle - // each sector then knows how to paint itself at its location - void paint(PaintingContext context, Offset offset) { - // TODO(ianh): avoid code duplication - super.paint(context, offset); - RenderSector child = firstChild; - while (child != null) { - assert(child.parentData is SectorChildListParentData); - context.paintChild(child, offset.toPoint()); - child = child.parentData.nextSibling; - } - } - -} - -class RenderSectorSlice extends RenderSectorWithChildren { - // lays out RenderSector children in a stack - - RenderSectorSlice({ - BoxDecoration decoration, - double deltaTheta: kTwoPi, - double padding: 0.0 - }) : super(decoration), _padding = padding, _desiredDeltaTheta = deltaTheta; - - double _desiredDeltaTheta; - double get desiredDeltaTheta => _desiredDeltaTheta; - void set desiredDeltaTheta(double value) { - assert(value != null); - if (_desiredDeltaTheta != value) { - _desiredDeltaTheta = value; - markNeedsLayout(); - } - } - - double _padding; - double get padding => _padding; - void set padding(double value) { - // TODO(ianh): avoid code duplication - assert(value != null); - if (_padding != value) { - _padding = value; - markNeedsLayout(); - } - } - - void setupParentData(RenderObject child) { - // TODO(ianh): avoid code duplication - if (child.parentData is! SectorChildListParentData) - child.parentData = new SectorChildListParentData(); - } - - SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { - assert(this.parentData is SectorParentData); - double paddingTheta = math.atan(padding / this.parentData.radius); - double outerDeltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); - double innerDeltaTheta = outerDeltaTheta - paddingTheta * 2.0; - double childRadius = this.parentData.radius + padding; - double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0); - RenderSector child = firstChild; - while (child != null) { - SectorConstraints innerConstraints = new SectorConstraints( - maxDeltaRadius: remainingDeltaRadius, - maxDeltaTheta: innerDeltaTheta - ); - SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius); - childRadius += childDimensions.deltaRadius; - remainingDeltaRadius -= childDimensions.deltaRadius; - assert(child.parentData is SectorChildListParentData); - child = child.parentData.nextSibling; - childRadius += padding; - remainingDeltaRadius -= padding; - } - return new SectorDimensions.withConstraints(constraints, - deltaRadius: childRadius - this.parentData.radius, - deltaTheta: outerDeltaTheta); - } - - void performLayout() { - assert(this.parentData is SectorParentData); - deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); - assert(deltaTheta <= kTwoPi); - double paddingTheta = math.atan(padding / this.parentData.radius); - double innerTheta = this.parentData.theta + paddingTheta; - double innerDeltaTheta = deltaTheta - paddingTheta * 2.0; - double childRadius = this.parentData.radius + padding; - double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0); - RenderSector child = firstChild; - while (child != null) { - SectorConstraints innerConstraints = new SectorConstraints( - maxDeltaRadius: remainingDeltaRadius, - maxDeltaTheta: innerDeltaTheta - ); - child.parentData.theta = innerTheta; - child.parentData.radius = childRadius; - child.layout(innerConstraints, parentUsesSize: true); - childRadius += child.deltaRadius; - remainingDeltaRadius -= child.deltaRadius; - assert(child.parentData is SectorChildListParentData); - child = child.parentData.nextSibling; - childRadius += padding; - remainingDeltaRadius -= padding; - } - deltaRadius = childRadius - this.parentData.radius; - } - - // offset must point to the center of our circle - // each sector then knows how to paint itself at its location - void paint(PaintingContext context, Offset offset) { - // TODO(ianh): avoid code duplication - super.paint(context, offset); - RenderSector child = firstChild; - while (child != null) { - assert(child.parentData is SectorChildListParentData); - context.paintChild(child, offset.toPoint()); - child = child.parentData.nextSibling; - } - } - -} - -class RenderBoxToRenderSectorAdapter extends RenderBox { - - RenderBoxToRenderSectorAdapter({ double innerRadius: 0.0, RenderSector child }) : - _innerRadius = innerRadius { - _child = child; - adoptChild(_child); - } - - double _innerRadius; - double get innerRadius => _innerRadius; - void set innerRadius(double value) { - _innerRadius = value; - markNeedsLayout(); - } - - RenderSector _child; - RenderSector get child => _child; - void set child(RenderSector value) { - if (_child != null) - dropChild(_child); - _child = value; - adoptChild(_child); - markNeedsLayout(); - } - - void setupParentData(RenderObject child) { - if (child.parentData is! SectorParentData) - child.parentData = new SectorParentData(); - } - - void visitChildren(RenderObjectVisitor visitor) { - visitor(_child); - } - - double getMinIntrinsicWidth(BoxConstraints constraints) { - if (child == null) - return super.getMinIntrinsicWidth(constraints); - return getIntrinsicDimensions(constraints).width; - } - - double getMaxIntrinsicWidth(BoxConstraints constraints) { - if (child == null) - return super.getMaxIntrinsicWidth(constraints); - return getIntrinsicDimensions(constraints).width; - } - - double getMinIntrinsicHeight(BoxConstraints constraints) { - if (child == null) - return super.getMinIntrinsicHeight(constraints); - return getIntrinsicDimensions(constraints).height; - } - - double getMaxIntrinsicHeight(BoxConstraints constraints) { - if (child == null) - return super.getMaxIntrinsicHeight(constraints); - return getIntrinsicDimensions(constraints).height; - } - - Size getIntrinsicDimensions(BoxConstraints constraints) { - assert(child is RenderSector); - assert(child.parentData is SectorParentData); - assert(constraints.maxWidth < double.INFINITY || constraints.maxHeight < double.INFINITY); - double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius; - SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius); - double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0; - return constraints.constrain(new Size(dimension, dimension)); - } - - void performLayout() { - if (child == null) { - size = constraints.constrain(Size.zero); - } else { - assert(child is RenderSector); - assert(constraints.maxWidth < double.INFINITY || constraints.maxHeight < double.INFINITY); - double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius; - assert(child.parentData is SectorParentData); - child.parentData.radius = innerRadius; - child.parentData.theta = 0.0; - child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), parentUsesSize: true); - double dimension = (innerRadius + child.deltaRadius) * 2.0; - size = constraints.constrain(new Size(dimension, dimension)); - } - } - - void paint(PaintingContext context, Offset offset) { - super.paint(context, offset); - if (child != null) { - Rect bounds = offset & size; - // we move the offset to the center of the circle for the RenderSectors - context.paintChild(child, bounds.center); - } - } - - bool hitTest(HitTestResult result, { Point position }) { - double x = position.x; - double y = position.y; - if (child == null) - return false; - // translate to our origin - x -= size.width/2.0; - y -= size.height/2.0; - // convert to radius/theta - double radius = math.sqrt(x*x+y*y); - double theta = (math.atan2(x, -y) - math.PI/2.0) % kTwoPi; - if (radius < innerRadius) - return false; - if (radius >= innerRadius + child.deltaRadius) - return false; - if (theta > child.deltaTheta) - return false; - child.hitTest(result, radius: radius, theta: theta); - result.add(new BoxHitTestEntry(this, position)); - return true; - } - -} - -class RenderSolidColor extends RenderDecoratedSector { - RenderSolidColor(Color backgroundColor, { - this.desiredDeltaRadius: double.INFINITY, - this.desiredDeltaTheta: kTwoPi - }) : this.backgroundColor = backgroundColor, - super(new BoxDecoration(backgroundColor: backgroundColor)); - - double desiredDeltaRadius; - double desiredDeltaTheta; - final Color backgroundColor; - - SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { - return new SectorDimensions.withConstraints(constraints, deltaTheta: desiredDeltaTheta); - } - - void performLayout() { - deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); - deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); - } - - void handleEvent(ui.Event event, HitTestEntry entry) { - if (event.type == 'pointerdown') - decoration = new BoxDecoration(backgroundColor: const Color(0xFFFF0000)); - else if (event.type == 'pointerup') - decoration = new BoxDecoration(backgroundColor: backgroundColor); - } -} +import 'lib/sector_layout.dart'; RenderBox buildSectorExample() { RenderSectorRing rootCircle = new RenderSectorRing(padding: 20.0); diff --git a/examples/widgets/pubspec.yaml b/examples/widgets/pubspec.yaml index 1a483612b6..8cc7e8c150 100644 --- a/examples/widgets/pubspec.yaml +++ b/examples/widgets/pubspec.yaml @@ -1,9 +1,12 @@ -name: widgets +name: sky_widgets_examples dependencies: flutter: ">=0.0.3 <0.1.0" sky_tools: any + flutter_rendering_examples: any dependency_overrides: material_design_icons: path: ../../sky/packages/material_design_icons flutter: path: ../../sky/packages/sky + flutter_rendering_examples: + path: ../rendering diff --git a/examples/widgets/sector.dart.old b/examples/widgets/sector.dart similarity index 76% rename from examples/widgets/sector.dart.old rename to examples/widgets/sector.dart index 0b7812118e..38bf742021 100644 --- a/examples/widgets/sector.dart.old +++ b/examples/widgets/sector.dart @@ -7,7 +7,7 @@ import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; -import '../rendering/sector_layout.dart'; +import 'package:flutter_rendering_examples/sector_layout.dart'; RenderBox initCircle() { return new RenderBoxToRenderSectorAdapter( @@ -16,10 +16,14 @@ RenderBox initCircle() { ); } -class SectorApp extends MaterialApp { +class SectorApp extends StatefulComponent { + SectorAppState createState() => new SectorAppState(); +} - RenderBoxToRenderSectorAdapter sectors = initCircle(); - math.Random rand = new math.Random(1); +class SectorAppState extends State { + + final RenderBoxToRenderSectorAdapter sectors = initCircle(); + final math.Random rand = new math.Random(1); void addSector() { double deltaTheta; @@ -52,27 +56,27 @@ class SectorApp extends MaterialApp { RenderBoxToRenderSectorAdapter sectorAddIcon = initSector(const Color(0xFF00DD00)); RenderBoxToRenderSectorAdapter sectorRemoveIcon = initSector(const Color(0xFFDD0000)); - bool enabledAdd = true; - bool enabledRemove = false; + bool _enabledAdd = true; + bool _enabledRemove = false; void updateEnabledState() { setState(() { var ring = (sectors.child as RenderSectorRing); SectorDimensions currentSize = ring.getIntrinsicDimensions(const SectorConstraints(), ring.deltaRadius); - enabledAdd = currentSize.deltaTheta < kTwoPi; - enabledRemove = ring.firstChild != null; + _enabledAdd = currentSize.deltaTheta < kTwoPi; + _enabledRemove = ring.firstChild != null; }); } Widget buildBody() { return new Material( - child: new Column([ + child: new Column([ new Container( padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0), - child: new Row([ + child: new Row([ new RaisedButton( - enabled: enabledAdd, + enabled: _enabledAdd, child: new IntrinsicWidth( - child: new Row([ + child: new Row([ new Container( padding: new EdgeDims.all(4.0), margin: new EdgeDims.only(right: 10.0), @@ -84,9 +88,9 @@ class SectorApp extends MaterialApp { onPressed: addSector ), new RaisedButton( - enabled: enabledRemove, + enabled: _enabledRemove, child: new IntrinsicWidth( - child: new Row([ + child: new Row([ new Container( padding: new EdgeDims.all(4.0), margin: new EdgeDims.only(right: 10.0), @@ -117,18 +121,20 @@ class SectorApp extends MaterialApp { ); } - Widget build() { - return new Theme( - data: new ThemeData.light(), - child: new Title( - title: 'Sector Layout', - child: new Scaffold( - toolBar: new ToolBar( - center: new Text('Sector Layout in a Widget Tree') - ), - body: buildBody() - ) - ) + Widget build(BuildContext context) { + return new MaterialApp( + theme: new ThemeData.light(), + title: 'Sector Layout', + routes: { + '/': (RouteArguments args) { + return new Scaffold( + toolBar: new ToolBar( + center: new Text('Sector Layout in a Widget Tree') + ), + body: buildBody() + ); + } + } ); } } diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index cfc9cd2cd5..25bb5d8829 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -633,7 +633,7 @@ abstract class RenderBox extends RenderObject { } } - String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}size: $size\n'; + String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}size: ${ hasSize ? size : "MISSING" }\n'; } /// A mixin that provides useful default behaviors for boxes with children diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index f4f3d1c4c0..346a4181a6 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -961,6 +961,22 @@ class AssetImage extends StatelessComponent { } } +class WidgetToRenderBoxAdapter extends LeafRenderObjectWidget { + WidgetToRenderBoxAdapter(RenderBox renderBox) + : renderBox = renderBox, + // WidgetToRenderBoxAdapter objects are keyed to their render box. This + // prevents the widget being used in the widget hierarchy in two different + // places, which would cause the RenderBox to get inserted in multiple + // places in the RenderObject tree. + super(key: new GlobalObjectKey(renderBox)) { + assert(renderBox != null); + } + + final RenderBox renderBox; + + RenderBox createRenderObject() => renderBox; +} + // EVENT HANDLING From 42469d2cb9570b1da6a0ec8ba6b8561fa9e9a421 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 13 Oct 2015 14:26:58 -0700 Subject: [PATCH 4/4] Fix game's pubspec.yaml --- examples/game/pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/game/pubspec.yaml b/examples/game/pubspec.yaml index 346cb34c2e..1787c99852 100644 --- a/examples/game/pubspec.yaml +++ b/examples/game/pubspec.yaml @@ -2,12 +2,12 @@ name: asteroids dependencies: flutter: ">=0.0.3 <0.1.0" sky_tools: any - skysprites: any + flutter_sprites: any box2d: any dependency_overrides: material_design_icons: path: ../../sky/packages/material_design_icons flutter: path: ../../sky/packages/sky - skysprites: + flutter_sprites: path: ../../skysprites