"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter/lib/src/painting/fractional_offset.dart" (13 Nov 2020, 7204 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 
    6 import 'dart:ui' as ui show lerpDouble;
    7 
    8 import 'package:flutter/foundation.dart';
    9 
   10 import 'alignment.dart';
   11 import 'basic_types.dart';
   12 
   13 /// An offset that's expressed as a fraction of a [Size].
   14 ///
   15 /// `FractionalOffset(1.0, 0.0)` represents the top right of the [Size].
   16 ///
   17 /// `FractionalOffset(0.0, 1.0)` represents the bottom left of the [Size].
   18 ///
   19 /// `FractionalOffset(0.5, 2.0)` represents a point half way across the [Size],
   20 /// below the bottom of the rectangle by the height of the [Size].
   21 ///
   22 /// The [FractionalOffset] class specifies offsets in terms of a distance from
   23 /// the top left, regardless of the [TextDirection].
   24 ///
   25 /// ## Design discussion
   26 ///
   27 /// [FractionalOffset] and [Alignment] are two different representations of the
   28 /// same information: the location within a rectangle relative to the size of
   29 /// the rectangle. The difference between the two classes is in the coordinate
   30 /// system they use to represent the location.
   31 ///
   32 /// [FractionalOffset] uses a coordinate system with an origin in the top-left
   33 /// corner of the rectangle whereas [Alignment] uses a coordinate system with an
   34 /// origin in the center of the rectangle.
   35 ///
   36 /// Historically, [FractionalOffset] predates [Alignment]. When we attempted to
   37 /// make a version of [FractionalOffset] that adapted to the [TextDirection], we
   38 /// ran into difficulty because placing the origin in the top-left corner
   39 /// introduced a left-to-right bias that was hard to remove.
   40 ///
   41 /// By placing the origin in the center, [Alignment] and [AlignmentDirectional]
   42 /// are able to use the same origin, which means we can use a linear function to
   43 /// resolve an [AlignmentDirectional] into an [Alignment] in both
   44 /// [TextDirection.rtl] and [TextDirection.ltr].
   45 ///
   46 /// [Alignment] is better for most purposes than [FractionalOffset] and should
   47 /// be used instead of [FractionalOffset]. We continue to implement
   48 /// [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
   53 ///    rectangle instead of the top left corner of the rectangle.
   54 @immutable
   55 class FractionalOffset extends Alignment {
   56   /// Creates a fractional offset.
   57   ///
   58   /// The [dx] and [dy] arguments must not be null.
   59   const FractionalOffset(double dx, double dy)
   60     : assert(dx != null),
   61       assert(dy != null),
   62       super(dx * 2.0 - 1.0, dy * 2.0 - 1.0);
   63 
   64   /// Creates a fractional offset from a specific offset and size.
   65   ///
   66   /// The returned [FractionalOffset] describes the position of the
   67   /// [Offset] in the [Size], as a fraction of the [Size].
   68   factory FractionalOffset.fromOffsetAndSize(Offset offset, Size size) {
   69     assert(size != null);
   70     assert(offset != null);
   71     return FractionalOffset(
   72       offset.dx / size.width,
   73       offset.dy / size.height,
   74     );
   75   }
   76 
   77   /// Creates a fractional offset from a specific offset and rectangle.
   78   ///
   79   /// The offset is assumed to be relative to the same origin as the rectangle.
   80   ///
   81   /// If the offset is relative to the top left of the rectangle, use [new
   82   /// FractionalOffset.fromOffsetAndSize] instead, passing `rect.size`.
   83   ///
   84   /// The returned [FractionalOffset] describes the position of the
   85   /// [Offset] in the [Rect], as a fraction of the [Rect].
   86   factory FractionalOffset.fromOffsetAndRect(Offset offset, Rect rect) {
   87     return FractionalOffset.fromOffsetAndSize(
   88       offset - rect.topLeft,
   89       rect.size,
   90     );
   91   }
   92 
   93   /// The distance fraction in the horizontal direction.
   94   ///
   95   /// A value of 0.0 corresponds to the leftmost edge. A value of 1.0
   96   /// corresponds to the rightmost edge. Values are not limited to that range;
   97   /// negative values represent positions to the left of the left edge, and
   98   /// values greater than 1.0 represent positions to the right of the right
   99   /// edge.
  100   double get dx => (x + 1.0) / 2.0;
  101 
  102   /// The distance fraction in the vertical direction.
  103   ///
  104   /// A value of 0.0 corresponds to the topmost edge. A value of 1.0 corresponds
  105   /// to the bottommost edge. Values are not limited to that range; negative
  106   /// values represent positions above the top, and values greater than 1.0
  107   /// represent positions below the bottom.
  108   double get dy => (y + 1.0) / 2.0;
  109 
  110   /// The top left corner.
  111   static const FractionalOffset topLeft = FractionalOffset(0.0, 0.0);
  112 
  113   /// The center point along the top edge.
  114   static const FractionalOffset topCenter = FractionalOffset(0.5, 0.0);
  115 
  116   /// The top right corner.
  117   static const FractionalOffset topRight = FractionalOffset(1.0, 0.0);
  118 
  119   /// The center point along the left edge.
  120   static const FractionalOffset centerLeft = FractionalOffset(0.0, 0.5);
  121 
  122   /// The center point, both horizontally and vertically.
  123   static const FractionalOffset center = FractionalOffset(0.5, 0.5);
  124 
  125   /// The center point along the right edge.
  126   static const FractionalOffset centerRight = FractionalOffset(1.0, 0.5);
  127 
  128   /// The bottom left corner.
  129   static const FractionalOffset bottomLeft = FractionalOffset(0.0, 1.0);
  130 
  131   /// The center point along the bottom edge.
  132   static const FractionalOffset bottomCenter = FractionalOffset(0.5, 1.0);
  133 
  134   /// The bottom right corner.
  135   static const FractionalOffset bottomRight = FractionalOffset(1.0, 1.0);
  136 
  137   @override
  138   Alignment operator -(Alignment other) {
  139     if (other is! FractionalOffset)
  140       return super - other;
  141     return FractionalOffset(dx - other.dx, dy - other.dy);
  142   }
  143 
  144   @override
  145   Alignment operator +(Alignment other) {
  146     if (other is! FractionalOffset)
  147       return super + other;
  148     return FractionalOffset(dx + other.dx, dy + other.dy);
  149   }
  150 
  151   @override
  152   FractionalOffset operator -() {
  153     return FractionalOffset(-dx, -dy);
  154   }
  155 
  156   @override
  157   FractionalOffset operator *(double other) {
  158     return FractionalOffset(dx * other, dy * other);
  159   }
  160 
  161   @override
  162   FractionalOffset operator /(double other) {
  163     return FractionalOffset(dx / other, dy / other);
  164   }
  165 
  166   @override
  167   FractionalOffset operator ~/(double other) {
  168     return FractionalOffset((dx ~/ other).toDouble(), (dy ~/ other).toDouble());
  169   }
  170 
  171   @override
  172   FractionalOffset operator %(double other) {
  173     return FractionalOffset(dx % other, dy % other);
  174   }
  175 
  176   /// Linearly interpolate between two [FractionalOffset]s.
  177   ///
  178   /// If either is null, this function interpolates from [FractionalOffset.center].
  179   ///
  180   /// {@macro dart.ui.shadow.lerp}
  181   static FractionalOffset? lerp(FractionalOffset? a, FractionalOffset? b, double t) {
  182     assert(t != null);
  183     if (a == null && b == null)
  184       return null;
  185     if (a == null)
  186       return FractionalOffset(ui.lerpDouble(0.5, b!.dx, t)!, ui.lerpDouble(0.5, b.dy, t)!);
  187     if (b == null)
  188       return FractionalOffset(ui.lerpDouble(a.dx, 0.5, t)!, ui.lerpDouble(a.dy, 0.5, t)!);
  189     return FractionalOffset(ui.lerpDouble(a.dx, b.dx, t)!, ui.lerpDouble(a.dy, b.dy, t)!);
  190   }
  191 
  192   @override
  193   String toString() {
  194     return 'FractionalOffset(${dx.toStringAsFixed(1)}, '
  195                             '${dy.toStringAsFixed(1)})';
  196   }
  197 }