fractional_offset.dart 6.93 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui show lerpDouble;

7 8
import 'package:flutter/foundation.dart';

9
import 'alignment.dart';
10 11
import 'basic_types.dart';

12
/// An offset that's expressed as a fraction of a [Size].
13
///
14 15 16 17 18 19
/// `FractionalOffset(1.0, 0.0)` represents the top right of the [Size].
///
/// `FractionalOffset(0.0, 1.0)` represents the bottom left of the [Size].
///
/// `FractionalOffset(0.5, 2.0)` represents a point half way across the [Size],
/// below the bottom of the rectangle by the height of the [Size].
20
///
21
/// The [FractionalOffset] class specifies offsets in terms of a distance from
22
/// the top left, regardless of the [TextDirection].
23
///
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/// ## Design discussion
///
/// [FractionalOffset] and [Alignment] are two different representations of the
/// same information: the location within a rectangle relative to the size of
/// the rectangle. The difference between the two classes is in the coordinate
/// system they use to represent the location.
///
/// [FractionalOffset] uses a coordinate system with an origin in the top-left
/// corner of the rectangle whereas [Alignment] uses a coordinate system with an
/// origin in the center of the rectangle.
///
/// Historically, [FractionalOffset] predates [Alignment]. When we attempted to
/// make a version of [FractionalOffset] that adapted to the [TextDirection], we
/// ran into difficulty because placing the origin in the top-left corner
/// introduced a left-to-right bias that was hard to remove.
///
/// By placing the origin in the center, [Alignment] and [AlignmentDirectional]
/// are able to use the same origin, which means we can use a linear function to
/// resolve an [AlignmentDirectional] into an [Alignment] in both
/// [TextDirection.rtl] and [TextDirection.ltr].
///
/// [Alignment] is better for most purposes than [FractionalOffset] and should
/// be used instead of [FractionalOffset]. We continue to implement
/// [FractionalOffset] to support code that predates [Alignment].
///
49 50
/// See also:
///
51 52
///  * [Alignment], which uses a coordinate system based on the center of the
///    rectangle instead of the top left corner of the rectangle.
53
@immutable
54
class FractionalOffset extends Alignment {
55 56 57
  /// Creates a fractional offset.
  ///
  /// The [dx] and [dy] arguments must not be null.
58
  const FractionalOffset(double dx, double dy)
59
    : super(dx * 2.0 - 1.0, dy * 2.0 - 1.0);
60

61 62 63 64
  /// Creates a fractional offset from a specific offset and size.
  ///
  /// The returned [FractionalOffset] describes the position of the
  /// [Offset] in the [Size], as a fraction of the [Size].
65
  factory FractionalOffset.fromOffsetAndSize(Offset offset, Size size) {
66
    return FractionalOffset(
67 68 69 70
      offset.dx / size.width,
      offset.dy / size.height,
    );
  }
71 72 73 74 75

  /// Creates a fractional offset from a specific offset and rectangle.
  ///
  /// The offset is assumed to be relative to the same origin as the rectangle.
  ///
76
  /// If the offset is relative to the top left of the rectangle, use [
77 78 79 80 81
  /// FractionalOffset.fromOffsetAndSize] instead, passing `rect.size`.
  ///
  /// The returned [FractionalOffset] describes the position of the
  /// [Offset] in the [Rect], as a fraction of the [Rect].
  factory FractionalOffset.fromOffsetAndRect(Offset offset, Rect rect) {
82
    return FractionalOffset.fromOffsetAndSize(
83 84 85 86 87
      offset - rect.topLeft,
      rect.size,
    );
  }

88 89
  /// The distance fraction in the horizontal direction.
  ///
90
  /// A value of 0.0 corresponds to the leftmost edge. A value of 1.0
91 92 93 94
  /// corresponds to the rightmost edge. Values are not limited to that range;
  /// negative values represent positions to the left of the left edge, and
  /// values greater than 1.0 represent positions to the right of the right
  /// edge.
95
  double get dx => (x + 1.0) / 2.0;
96

97 98
  /// The distance fraction in the vertical direction.
  ///
99 100
  /// A value of 0.0 corresponds to the topmost edge. A value of 1.0 corresponds
  /// to the bottommost edge. Values are not limited to that range; negative
101
  /// values represent positions above the top, and values greater than 1.0
102
  /// represent positions below the bottom.
103
  double get dy => (y + 1.0) / 2.0;
104

105
  /// The top left corner.
106
  static const FractionalOffset topLeft = FractionalOffset(0.0, 0.0);
107 108

  /// The center point along the top edge.
109
  static const FractionalOffset topCenter = FractionalOffset(0.5, 0.0);
110 111

  /// The top right corner.
112
  static const FractionalOffset topRight = FractionalOffset(1.0, 0.0);
113

114
  /// The center point along the left edge.
115
  static const FractionalOffset centerLeft = FractionalOffset(0.0, 0.5);
116 117

  /// The center point, both horizontally and vertically.
118
  static const FractionalOffset center = FractionalOffset(0.5, 0.5);
119 120

  /// The center point along the right edge.
121
  static const FractionalOffset centerRight = FractionalOffset(1.0, 0.5);
122

123
  /// The bottom left corner.
124
  static const FractionalOffset bottomLeft = FractionalOffset(0.0, 1.0);
125 126

  /// The center point along the bottom edge.
127
  static const FractionalOffset bottomCenter = FractionalOffset(0.5, 1.0);
128 129

  /// The bottom right corner.
130
  static const FractionalOffset bottomRight = FractionalOffset(1.0, 1.0);
131

132 133
  @override
  Alignment operator -(Alignment other) {
134
    if (other is! FractionalOffset) {
135
      return super - other;
136
    }
137
    return FractionalOffset(dx - other.dx, dy - other.dy);
138 139 140 141
  }

  @override
  Alignment operator +(Alignment other) {
142
    if (other is! FractionalOffset) {
143
      return super + other;
144
    }
145
    return FractionalOffset(dx + other.dx, dy + other.dy);
146 147 148 149
  }

  @override
  FractionalOffset operator -() {
150
    return FractionalOffset(-dx, -dy);
151 152 153 154
  }

  @override
  FractionalOffset operator *(double other) {
155
    return FractionalOffset(dx * other, dy * other);
156 157 158 159
  }

  @override
  FractionalOffset operator /(double other) {
160
    return FractionalOffset(dx / other, dy / other);
161 162 163 164
  }

  @override
  FractionalOffset operator ~/(double other) {
165
    return FractionalOffset((dx ~/ other).toDouble(), (dy ~/ other).toDouble());
166 167 168 169
  }

  @override
  FractionalOffset operator %(double other) {
170
    return FractionalOffset(dx % other, dy % other);
171 172
  }

173 174 175
  /// Linearly interpolate between two [FractionalOffset]s.
  ///
  /// If either is null, this function interpolates from [FractionalOffset.center].
176
  ///
177
  /// {@macro dart.ui.shadow.lerp}
178
  static FractionalOffset? lerp(FractionalOffset? a, FractionalOffset? b, double t) {
179 180
    if (identical(a, b)) {
      return a;
181 182
    }
    if (a == null) {
183
      return FractionalOffset(ui.lerpDouble(0.5, b!.dx, t)!, ui.lerpDouble(0.5, b.dy, t)!);
184 185
    }
    if (b == null) {
186
      return FractionalOffset(ui.lerpDouble(a.dx, 0.5, t)!, ui.lerpDouble(a.dy, 0.5, t)!);
187
    }
188
    return FractionalOffset(ui.lerpDouble(a.dx, b.dx, t)!, ui.lerpDouble(a.dy, b.dy, t)!);
189
  }
190 191 192 193 194 195

  @override
  String toString() {
    return 'FractionalOffset(${dx.toStringAsFixed(1)}, '
                            '${dy.toStringAsFixed(1)})';
  }
196
}