Files
pipelines/app/lib/ui/motion/quick_switch_effect.dart
T

161 lines
5.4 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'quick_switch_animation.dart';
/// A paint-only pose shared by the live workspace and the Settings preview.
/// Translation is a fraction of the card's current viewport, not screen pixels.
class QuickSwitchPose {
const QuickSwitchPose({
this.translation = Offset.zero,
this.scale = 1,
this.rotationX = 0,
this.rotationY = 0,
this.rotationZ = 0,
this.depth = 0,
this.opacity = 1,
this.shadow = 0,
});
final Offset translation;
final double scale, rotationX, rotationY, rotationZ, depth, opacity, shadow;
Matrix4 transform(Size size) {
// A flat card at constant depth projects to a uniform 2D scale. Keep it
// affine so backdrop filters do not enter the renderer's 3D path.
if (rotationX == 0 && rotationY == 0) {
final projection = 1 / (1 + .0012 * depth);
return Matrix4.identity()
..translateByDouble(
translation.dx * size.width * projection,
translation.dy * size.height * projection,
0,
1,
)
..rotateZ(rotationZ)
..scaleByDouble(scale * projection, scale * projection, 1, 1);
}
return Matrix4.identity()
..setEntry(3, 2, .0012)
..translateByDouble(
translation.dx * size.width,
translation.dy * size.height,
depth,
1,
)
..rotateX(rotationX)
..rotateY(rotationY)
..rotateZ(rotationZ)
..scaleByDouble(scale, scale, scale, 1);
}
}
bool quickSwitchOutgoingOnTop(QuickSwitchAnimation style, double progress) =>
style != QuickSwitchAnimation.tape && progress < .5;
/// Decks clear the front card sideways before changing its stacking order.
/// Its full-height viewport stays intact on the way out; only the return to
/// the small preview resizes it. No hinge rotation is used for a deck.
Rect quickSwitchRect(
QuickSwitchAnimation style,
double progress,
Rect from,
Rect to, {
required bool outgoing,
}) {
final t = progress.clamp(0.0, 1.0);
final deck =
style == QuickSwitchAnimation.cardStack ||
style == QuickSwitchAnimation.deckSlide;
// The next card is already full-sized behind the opaque front card.
// Sliding the front card aside reveals it instead of exposing empty space
// above a view still travelling up from the small preview.
if (deck && !outgoing) return to;
final travel = deck ? ((t - .45) / .55).clamp(0.0, 1.0) : t;
return Rect.lerp(from, to, travel)!;
}
QuickSwitchPose quickSwitchPose(
QuickSwitchAnimation style,
double progress, {
required bool outgoing,
}) {
final t = progress.clamp(0.0, 1.0);
final wave = t == 0 || t == 1 ? 0.0 : math.sin(math.pi * t);
final direction = outgoing ? -1.0 : 1.0;
switch (style) {
case QuickSwitchAnimation.tape:
case QuickSwitchAnimation.swap:
return const QuickSwitchPose();
case QuickSwitchAnimation.arcSwap:
// Vertical travel is the same as Swap. This half-sine bows both paths
// outwards, meeting their exact endpoints without a hinge or 3D filter.
return QuickSwitchPose(
translation: Offset(direction * .38 * wave, 0),
rotationZ: direction * .035 * wave,
shadow: .4 * wave,
);
case QuickSwitchAnimation.cardStack:
return QuickSwitchPose(
translation: Offset(
outgoing ? -1.15 * wave : .04 * wave,
outgoing ? -.06 * wave : 0,
),
scale: outgoing ? 1 + .025 * wave : 1 - .035 * wave,
rotationZ: outgoing ? -.045 * wave : 0,
depth: (outgoing && t < .5 ? -18 : 18) * wave,
shadow: outgoing ? wave : .4 * wave,
);
case QuickSwitchAnimation.deckSlide:
return QuickSwitchPose(
translation: Offset(outgoing ? 1.15 * wave : -.03 * wave, 0),
scale: 1 - (outgoing ? .025 : .04) * wave,
depth: 18 * wave,
shadow: .7 * wave,
);
case QuickSwitchAnimation.fan:
return QuickSwitchPose(
translation: Offset(direction * .38 * wave, -.08 * wave),
rotationZ: direction * (outgoing ? .24 : .12) * wave,
scale: 1 - .035 * wave,
shadow: wave,
);
case QuickSwitchAnimation.slide:
return QuickSwitchPose(
translation: Offset(direction * (outgoing ? .95 : .38) * wave, 0),
shadow: .3 * wave,
);
case QuickSwitchAnimation.verticalPush:
return QuickSwitchPose(
translation: Offset(0, direction * (outgoing ? .48 : .15) * wave),
);
case QuickSwitchAnimation.fade:
return QuickSwitchPose(
opacity: outgoing ? (2 * t - 1).abs() : 1 - .55 * wave,
);
case QuickSwitchAnimation.zoom:
return QuickSwitchPose(
scale: 1 + (outgoing ? -.22 : .08) * wave,
opacity: 1 - (outgoing ? .35 : .12) * wave,
shadow: .6 * wave,
);
case QuickSwitchAnimation.flip:
final angle = direction * math.pi * (outgoing ? .85 : .45) * wave;
return QuickSwitchPose(
rotationY: angle,
opacity: math.cos(angle).clamp(0.0, 1.0),
depth: 60 * wave,
shadow: wave,
);
case QuickSwitchAnimation.rolodex:
return QuickSwitchPose(
translation: Offset(
outgoing ? -.18 * wave : 0,
outgoing ? -.08 * wave : 0,
),
rotationY: direction * .22 * wave,
rotationZ: direction * .035 * wave,
depth: -(outgoing ? 90 : 40) * wave,
shadow: wave,
);
}
}