arc.dart 8.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2016 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' show hashValues, lerpDouble;

import 'package:flutter/material.dart';
import 'package:meta/meta.dart';

// How close the begin and end points must be to an axis to be considered
// vertical or horizontal.
const double _kOnAxisDelta = 2.0;

15
/// A [Tween] that animates a [Point] along a circular arc.
16 17 18 19 20 21 22
///
/// The arc's radius is related to the bounding box that contains the [begin]
/// and [end] points. If the bounding box is taller than it is wide, then the
/// center of the circle will be horizontally aligned with the end point.
/// Otherwise the center of the circle will be aligned with the begin point.
/// The arc's sweep is always less than or equal to 90 degrees.
///
23 24 25
/// Unlike those of most Tweens, the [begin] and [end] members of a
/// [MaterialPointArcTween] are immutable.
///
26 27 28 29
/// See also:
///
/// * [MaterialRectArcTween]
class MaterialPointArcTween extends Tween<Point> {
30 31 32 33
  /// Creates a [Tween] for animating [Point]s along a circular arc.
  ///
  /// The [begin] and [end] points are required, cannot be null, and are
  /// immutable.
34 35 36 37
  MaterialPointArcTween({
    @required Point begin,
    @required Point end
  }) : super(begin: begin, end: end) {
38 39
    assert(begin != null);
    assert(end != null);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    // An explanation with a diagram can be found at https://goo.gl/vMSdRg
    final Offset delta = end - begin;
    final double deltaX = delta.dx.abs();
    final double deltaY = delta.dy.abs();
    final double distanceFromAtoB = delta.distance;
    final Point c = new Point(end.x, begin.y);

    double sweepAngle() => 2.0 * math.asin(distanceFromAtoB / (2.0 * _radius));

    if (deltaX > _kOnAxisDelta && deltaY > _kOnAxisDelta) {
      if (deltaX < deltaY) {
        _radius = distanceFromAtoB * distanceFromAtoB / (c - begin).distance / 2.0;
        _center = new Point(end.x + _radius * (begin.x - end.x).sign, end.y);
        if (begin.x < end.x) {
          _beginAngle = sweepAngle() * (begin.y - end.y).sign;
          _endAngle = 0.0;
        } else {
          _beginAngle = math.PI + sweepAngle() * (end.y - begin.y).sign;
          _endAngle = math.PI;
        }
      } else {
        _radius = distanceFromAtoB * distanceFromAtoB / (c - end).distance / 2.0;
        _center = new Point(begin.x, begin.y + (end.y - begin.y).sign * _radius);
        if (begin.y < end.y) {
          _beginAngle = -math.PI / 2.0;
          _endAngle = _beginAngle + sweepAngle() * (end.x - begin.x).sign;
        } else {
          _beginAngle = math.PI / 2.0;
          _endAngle = _beginAngle + sweepAngle() * (begin.x - end.x).sign;
        }
      }
    }
  }

  Point _center;
  double _radius;
  double _beginAngle;
  double _endAngle;

  /// The center of the circular arc, null if [begin] and [end] are horiztonally or
  /// vertically aligned.
  Point get center => _center;

  /// The radius of the circular arc, null if begin and end are horiztonally or
  /// vertically aligned.
  double get radius => _radius;

  /// The beginning of the arc's sweep in radians, measured from the positive X axis.
  /// Positive angles turn clockwise. Null if begin and end are horiztonally or
  /// vertically aligned.
  double get beginAngle => _beginAngle;

  /// The end of the arc's sweep in radians, measured from the positive X axis.
  /// Positive angles turn clockwise.
  double get endAngle => _beginAngle;

  /// Setting the arc's [begin] parameter is not supported. Construct a new arc instead.
  @override
  set begin(Point value) {
    assert(false); // not supported
  }

  /// Setting the arc's [end] parameter is not supported. Construct a new arc instead.
  @override
  set end(Point value) {
    assert(false); // not supported
  }

  @override
  Point lerp(double t) {
    if (t == 0.0)
      return begin;
    if (t == 1.0)
      return end;
    if (_beginAngle == null || _endAngle == null)
      return Point.lerp(begin, end, t);
    final double angle = lerpDouble(_beginAngle, _endAngle, t);
    final double x = math.cos(angle) * _radius;
    final double y = math.sin(angle) * _radius;
    return _center + new Offset(x, y);
  }

  @override
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other is! MaterialPointArcTween)
      return false;
    final MaterialPointArcTween typedOther = other;
    return begin == typedOther.begin
        && end == typedOther.end;
  }

  @override
  int get hashCode => hashValues(begin, end);

  @override
  String toString() {
    return '$runtimeType($begin \u2192 $end center=$center, radius=$radius, beginAngle=$beginAngle, endAngle=$endAngle)';
  }
}

enum _CornerId {
  topLeft,
  topRight,
  bottomLeft,
  bottomRight
}

class _Diagonal {
  const _Diagonal(this.beginId, this.endId);
  final _CornerId beginId;
  final _CornerId endId;
}

const List<_Diagonal> _allDiagonals = const <_Diagonal>[
  const _Diagonal(_CornerId.topLeft, _CornerId.bottomRight),
  const _Diagonal(_CornerId.bottomRight, _CornerId.topLeft),
  const _Diagonal(_CornerId.topRight, _CornerId.bottomLeft),
  const _Diagonal(_CornerId.bottomLeft, _CornerId.topRight),
];

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
typedef dynamic _KeyFunc<T>(T input);

// Select the element for which the key function returns the maximum value.
dynamic/*=T*/ _maxBy/*<T>*/(Iterable<dynamic/*=T*/> input, _KeyFunc/*<T>*/ keyFunc) {
  dynamic/*=T*/ maxValue;
  dynamic maxKey;
  for (dynamic/*=T*/ value in input) {
    dynamic key = keyFunc(value);
    if (maxKey == null || key > maxKey) {
      maxValue = value;
      maxKey = key;
    }
  }
  return maxValue;
}

178
/// A [Tween] that animates a [Rect] from [begin] to [end].
179 180 181 182
///
/// The rectangle corners whose diagonal is closest to the overall direction of
/// the animation follow arcs defined with [MaterialPointArcTween].
///
183 184 185
/// Unlike those of most Tweens, the [begin] and [end] members of a
/// [MaterialPointArcTween] are immutable.
///
186 187
/// See also:
///
188 189
/// * [MaterialPointArcTween]. the analogue for [Point] interporation.
/// * [RectTween], which does a linear rectangle interpolation.
190
class MaterialRectArcTween extends RectTween {
191 192 193 194
  /// Creates a [Tween] for animating [Rect]s along a circular arc.
  ///
  /// The [begin] and [end] points are required, cannot be null, and are
  /// immutable.
195 196 197 198
  MaterialRectArcTween({
    @required Rect begin,
    @required Rect end
  }) : super(begin: begin, end: end) {
199 200
    assert(begin != null);
    assert(end != null);
201
    final Offset centersVector = end.center - begin.center;
202
    _diagonal = _maxBy/*<_Diagonal>*/(_allDiagonals, (_Diagonal d) => _diagonalSupport(centersVector, d));
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    _beginArc = new MaterialPointArcTween(
      begin: _cornerFor(begin, _diagonal.beginId),
      end: _cornerFor(end, _diagonal.beginId)
    );
    _endArc = new MaterialPointArcTween(
      begin: _cornerFor(begin, _diagonal.endId),
      end: _cornerFor(end, _diagonal.endId)
    );
  }

  _Diagonal _diagonal;
  MaterialPointArcTween _beginArc;
  MaterialPointArcTween _endArc;

  Point _cornerFor(Rect rect, _CornerId id) {
    switch (id) {
      case _CornerId.topLeft: return rect.topLeft;
      case _CornerId.topRight: return rect.topRight;
      case _CornerId.bottomLeft: return rect.bottomLeft;
      case _CornerId.bottomRight: return rect.bottomRight;
    }
    return Point.origin;
  }

  double _diagonalSupport(Offset centersVector, _Diagonal diagonal) {
    final Offset delta = _cornerFor(begin, diagonal.endId) - _cornerFor(begin, diagonal.beginId);
    final double length = delta.distance;
    return centersVector.dx * delta.dx / length + centersVector.dy * delta.dy / length;
  }

  /// The path of the corresponding [begin], [end] rectangle corners that lead
  /// the animation.
  MaterialPointArcTween get beginArc => _beginArc;

  /// The path of the corresponding [begin], [end] rectangle corners that trail
  /// the animation.
  MaterialPointArcTween get endArc => _endArc;

  /// Setting the arc's [begin] parameter is not supported. Construct a new arc instead.
  @override
  set begin(Rect value) {
    assert(false); // not supported
  }

  /// Setting the arc's [end] parameter is not supported. Construct a new arc instead.
  @override
  set end(Rect value) {
    assert(false); // not supported
  }

  @override
  Rect lerp(double t) {
    if (t == 0.0)
      return begin;
    if (t == 1.0)
      return end;
    return new Rect.fromPoints(_beginArc.lerp(t), _endArc.lerp(t));
  }

  @override
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other is! MaterialRectArcTween)
      return false;
    final MaterialRectArcTween typedOther = other;
    return begin == typedOther.begin
        && end == typedOther.end;
  }

  @override
  int get hashCode => hashValues(begin, end);

  @override
  String toString() {
    return '$runtimeType($begin \u2192 $end beginArc=$beginArc, endArc=$endArc)';
  }
}