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

5 6
import 'package:flutter/foundation.dart';

7
import 'simulation.dart';
8

9 10
export 'simulation.dart' show Simulation;

Ian Hickson's avatar
Ian Hickson committed
11 12 13 14
/// A simulation that applies limits to another simulation.
///
/// The limits are only applied to the other simulation's outputs. For example,
/// if a maximum position was applied to a gravity simulation with the
15
/// particle's initial velocity being up, and the acceleration being down, and
Ian Hickson's avatar
Ian Hickson committed
16 17 18 19 20 21
/// the maximum position being between the initial position and the curve's
/// apogee, then the particle would return to its initial position in the same
/// amount of time as it would have if the maximum had not been applied; the
/// difference would just be that the position would be reported as pinned to
/// the maximum value for the times that it would otherwise have been reported
/// as higher.
22 23 24 25 26 27
///
/// Similarly, this means that the [x] value will change at a rate that does not
/// match the reported [dx] value while one or the other is being clamped.
///
/// The [isDone] logic is unaffected by the clamping; it reflects the logic of
/// the underlying simulation.
28
class ClampedSimulation extends Simulation {
Ian Hickson's avatar
Ian Hickson committed
29 30 31 32
  /// Creates a [ClampedSimulation] that clamps the given simulation.
  ///
  /// The named arguments specify the ranges for the clamping behavior, as
  /// applied to [x] and [dx].
33 34
  ClampedSimulation(
    this.simulation, {
35 36 37
    this.xMin = double.negativeInfinity,
    this.xMax = double.infinity,
    this.dxMin = double.negativeInfinity,
38
    this.dxMax = double.infinity,
39 40 41
  }) : assert(simulation != null),
       assert(xMax >= xMin),
       assert(dxMax >= dxMin);
42

Ian Hickson's avatar
Ian Hickson committed
43 44
  /// The simulation being clamped. Calls to [x], [dx], and [isDone] are
  /// forwarded to the simulation.
45
  final Simulation simulation;
Ian Hickson's avatar
Ian Hickson committed
46 47

  /// The minimum to apply to [x].
48
  final double xMin;
Ian Hickson's avatar
Ian Hickson committed
49 50

  /// The maximum to apply to [x].
51
  final double xMax;
Ian Hickson's avatar
Ian Hickson committed
52 53

  /// The minimum to apply to [dx].
54
  final double dxMin;
Ian Hickson's avatar
Ian Hickson committed
55 56

  /// The maximum to apply to [dx].
57 58
  final double dxMax;

59
  @override
60
  double x(double time) => clampDouble(simulation.x(time), xMin, xMax);
61 62

  @override
63
  double dx(double time) => clampDouble(simulation.dx(time), dxMin, dxMax);
64 65

  @override
66
  bool isDone(double time) => simulation.isDone(time);
67 68 69

  @override
  String toString() => '${objectRuntimeType(this, 'ClampedSimulation')}(simulation: $simulation, x: ${xMin.toStringAsFixed(1)}..${xMax.toStringAsFixed(1)}, dx: ${dxMin.toStringAsFixed(1)}..${dxMax.toStringAsFixed(1)})';
70
}