浏览代码

more animations.

/main
kg 6 年前
当前提交
5fc95ffa
共有 9 个文件被更改,包括 518 次插入40 次删除
  1. 3
      Assets/UIWidgets/animation/animation.cs
  2. 8
      Assets/UIWidgets/animation/animation_controller.cs
  3. 331
      Assets/UIWidgets/animation/animations.cs
  4. 14
      Assets/UIWidgets/animation/listener_helpers.mixin.gen.cs
  5. 22
      Assets/UIWidgets/animation/listener_helpers.mixin.njk
  6. 89
      Assets/UIWidgets/animation/tween.cs
  7. 6
      Assets/UIWidgets/foundation/basic_types.cs
  8. 58
      Assets/UIWidgets/ui/geometry.cs
  9. 27
      Assets/UIWidgets/ui/painting/painting.cs

3
Assets/UIWidgets/animation/animation.cs


namespace UIWidgets.animation {
public enum AnimationStatus {
dismissed,
completed,
}

8
Assets/UIWidgets/animation/animation_controller.cs


}
public class AnimationController :
AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationEagerListenerMixinAnimationDouble {
AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationEagerListenerMixinAnimation<double> {
public AnimationController(
double? value = null,
TimeSpan? duration = null,

public override double x(double timeInSeconds) {
D.assert(timeInSeconds >= 0.0);
double t = (timeInSeconds / this._periodInSeconds) % 1.0;
return this.lerpDouble(this.min, this.max, t);
return MathUtils.lerpDouble(this.min, this.max, t);
}
public override double dx(double timeInSeconds) {

public override bool isDone(double timeInSeconds) {
return false;
}
private double lerpDouble(double a, double b, double t) {
return a + (b - a) * t;
}
}
}

331
Assets/UIWidgets/animation/animations.cs


using System;
using UIWidgets.foundation;
using UIWidgets.ui;

}
}
public abstract class AnimationDouble : Animation<double> {
}
AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationLazyListenerMixinAnimationDouble {
AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationLazyListenerMixinAnimation<double> {
public ProxyAnimation(Animation<double> animation = null) {
this._parent = animation;
if (this._parent == null) {

}
public class ReverseAnimation : AnimationLocalStatusListenersMixinAnimationLazyListenerMixinAnimationDouble {
public class ReverseAnimation : AnimationLocalStatusListenersMixinAnimationLazyListenerMixinAnimation<double> {
public ReverseAnimation(Animation<double> parent) {
D.assert(parent != null);
this._parent = parent;

}
}
public class CurvedAnimation : AnimationWithParentMixin<double, double> {
public CurvedAnimation(
Animation<double> parent = null,
Curve curve = null,
Curve reverseCurve = null
) {
D.assert(parent != null);
D.assert(curve != null);
this._parent = parent;
this.curve = curve;
this.reverseCurve = reverseCurve;
this._updateCurveDirection(parent.status);
parent.addStatusListener(this._updateCurveDirection);
}
public override Animation<double> parent {
get { return this._parent; }
}
readonly Animation<double> _parent;
public Curve curve;
public Curve reverseCurve;
AnimationStatus? _curveDirection;
void _updateCurveDirection(AnimationStatus status) {
switch (status) {
case AnimationStatus.dismissed:
case AnimationStatus.completed:
this._curveDirection = null;
break;
case AnimationStatus.forward:
this._curveDirection = this._curveDirection ?? AnimationStatus.forward;
break;
case AnimationStatus.reverse:
this._curveDirection = this._curveDirection ?? AnimationStatus.reverse;
break;
}
}
bool _useForwardCurve {
get {
return this.reverseCurve == null ||
(this._curveDirection ?? this.parent.status) != AnimationStatus.reverse;
}
}
public override double value {
get {
Curve activeCurve = this._useForwardCurve ? this.curve : this.reverseCurve;
double t = this.parent.value;
if (activeCurve == null) {
return t;
}
if (t == 0.0 || t == 1.0) {
D.assert(() => {
double transformedValue = activeCurve.transform(t);
double roundedTransformedValue = transformedValue.round();
if (roundedTransformedValue != t) {
throw new UIWidgetsError(
string.Format(
"Invalid curve endpoint at {0}.\n" +
"Curves must map 0.0 to near zero and 1.0 to near one but " +
"{1} mapped {0} to {2}, which " +
"is near {3}.",
t, activeCurve.GetType(), transformedValue, roundedTransformedValue)
);
}
return true;
});
return t;
}
return activeCurve.transform(t);
}
}
public override string ToString() {
if (this.reverseCurve == null) {
return this.parent + "\u27A9" + this.curve;
}
if (this._useForwardCurve) {
return this.parent + "\u27A9" + this.curve + "\u2092\u2099/" + this.reverseCurve;
}
return this.parent + "\u27A9" + this.curve + "/" + this.reverseCurve + "\u2092\u2099";
}
}
enum _TrainHoppingMode {
minimize,
maximize
}
public class TrainHoppingAnimation :
AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationEagerListenerMixinAnimation<double> {
public TrainHoppingAnimation(
Animation<double> _currentTrain = null,
Animation<double> _nextTrain = null,
VoidCallback onSwitchedTrain = null) {
D.assert(_currentTrain != null);
this._currentTrain = _currentTrain;
this._nextTrain = _nextTrain;
this.onSwitchedTrain = onSwitchedTrain;
if (this._nextTrain != null) {
if (this._currentTrain.value > this._nextTrain.value) {
this._mode = _TrainHoppingMode.maximize;
} else {
this._mode = _TrainHoppingMode.minimize;
if (this._currentTrain.value == this._nextTrain.value) {
this._currentTrain = this._nextTrain;
this._nextTrain = null;
}
}
}
this._currentTrain.addStatusListener(this._statusChangeHandler);
this._currentTrain.addListener(this._valueChangeHandler);
if (this._nextTrain != null) {
this._nextTrain.addListener(this._valueChangeHandler);
}
}
public Animation<double> currentTrain {
get { return this._currentTrain; }
}
Animation<double> _currentTrain;
Animation<double> _nextTrain;
_TrainHoppingMode _mode;
public VoidCallback onSwitchedTrain;
AnimationStatus? _lastStatus;
void _statusChangeHandler(AnimationStatus status) {
D.assert(this._currentTrain != null);
if (status != this._lastStatus) {
this.notifyListeners();
this._lastStatus = status;
}
D.assert(this._lastStatus != null);
}
public override AnimationStatus status {
get { return this._currentTrain.status; }
}
double? _lastValue;
void _valueChangeHandler() {
D.assert(this._currentTrain != null);
bool hop = false;
if (this._nextTrain != null) {
switch (this._mode) {
case _TrainHoppingMode.minimize:
hop = this._nextTrain.value <= this._currentTrain.value;
break;
case _TrainHoppingMode.maximize:
hop = this._nextTrain.value >= this._currentTrain.value;
break;
}
if (hop) {
this._currentTrain.removeStatusListener(this._statusChangeHandler);
this._currentTrain.removeListener(this._valueChangeHandler);
this._currentTrain = this._nextTrain;
this._nextTrain = null;
this._currentTrain.addStatusListener(this._statusChangeHandler);
this._statusChangeHandler(this._currentTrain.status);
}
}
double newValue = this.value;
if (newValue != this._lastValue) {
this.notifyListeners();
this._lastValue = newValue;
}
D.assert(this._lastValue != null);
if (hop && this.onSwitchedTrain != null) {
this.onSwitchedTrain();
}
}
public override double value {
get { return this._currentTrain.value; }
}
public override void dispose() {
D.assert(this._currentTrain != null);
this._currentTrain.removeStatusListener(this._statusChangeHandler);
this._currentTrain.removeListener(this._valueChangeHandler);
this._currentTrain = null;
if (this._nextTrain != null) {
this._nextTrain.removeListener(this._valueChangeHandler);
this._nextTrain = null;
}
base.dispose();
}
public override string ToString() {
if (this._nextTrain != null) {
return string.Format("{0}\u27A9{1}(next: {2})", this.currentTrain, this.GetType(), this._nextTrain);
}
return string.Format("{0}\u27A9{1}(no next)", this.currentTrain, this.GetType());
}
}
public abstract class CompoundAnimation<T> :
AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationLazyListenerMixinAnimation<T> {
public CompoundAnimation(
Animation<T> first = null,
Animation<T> next = null
) {
D.assert(first != null);
D.assert(next != null);
this.first = first;
this.next = next;
}
public readonly Animation<T> first;
public readonly Animation<T> next;
protected override void didStartListening() {
this.first.addListener(this._maybeNotifyListeners);
this.first.addStatusListener(this._maybeNotifyStatusListeners);
this.next.addListener(this._maybeNotifyListeners);
this.next.addStatusListener(this._maybeNotifyStatusListeners);
}
protected override void didStopListening() {
this.first.removeListener(this._maybeNotifyListeners);
this.first.removeStatusListener(this._maybeNotifyStatusListeners);
this.next.removeListener(this._maybeNotifyListeners);
this.next.removeStatusListener(this._maybeNotifyStatusListeners);
}
public override AnimationStatus status {
get {
if (this.next.status == AnimationStatus.forward || this.next.status == AnimationStatus.reverse)
return this.next.status;
return this.first.status;
}
}
public override string ToString() {
return string.Format("{0}({1}, {2})", this.GetType(), this.first, this.next);
}
AnimationStatus _lastStatus;
void _maybeNotifyStatusListeners(AnimationStatus _) {
if (this.status != this._lastStatus) {
this._lastStatus = this.status;
this.notifyStatusListeners(this.status);
}
}
T _lastValue;
void _maybeNotifyListeners() {
if (object.Equals(this.value, this._lastValue)) {
this._lastValue = this.value;
this.notifyListeners();
}
}
}
public class AnimationMean : CompoundAnimation<double> {
public AnimationMean(
Animation<double> left = null,
Animation<double> right = null
) : base(first: left, next: right) {
}
public override double value {
get { return (this.first.value + this.next.value) / 2.0; }
}
}
public class AnimationMax : CompoundAnimation<double> {
public AnimationMax(
Animation<double> left = null,
Animation<double> right = null
) : base(first: left, next: right) {
}
public override double value {
get { return Math.Max(this.first.value, this.next.value); }
}
}
public class AnimationMin : CompoundAnimation<double> {
public AnimationMin(
Animation<double> left = null,
Animation<double> right = null
) : base(first: left, next: right) {
}
public override double value {
get { return Math.Min(this.first.value, this.next.value); }
}
}
public static readonly Animation<double> kAlwaysCompleteAnimation = new _AlwaysCompleteAnimation();
public static readonly Animation<double> kAlwaysDismissedAnimation = new _AlwaysDismissedAnimation();

14
Assets/UIWidgets/animation/listener_helpers.mixin.gen.cs


namespace UIWidgets.animation {
public abstract class AnimationLazyListenerMixinAnimationDouble : AnimationDouble {
public abstract class AnimationLazyListenerMixinAnimation<T> : Animation<T> {
int _listenerCounter = 0;
protected void didRegisterListener() {

public abstract class AnimationEagerListenerMixinAnimationDouble : AnimationDouble {
public abstract class AnimationEagerListenerMixinAnimation<T> : Animation<T> {
protected void didRegisterListener() {
}

public abstract class AnimationLocalListenersMixinAnimationLazyListenerMixinAnimationDouble : AnimationLazyListenerMixinAnimationDouble {
public abstract class AnimationLocalListenersMixinAnimationLazyListenerMixinAnimation<T> : AnimationLazyListenerMixinAnimation<T> {
readonly ObserverList<VoidCallback> _listeners = new ObserverList<VoidCallback>();
public override void addListener(VoidCallback listener) {

public abstract class AnimationLocalListenersMixinAnimationEagerListenerMixinAnimationDouble : AnimationEagerListenerMixinAnimationDouble {
public abstract class AnimationLocalListenersMixinAnimationEagerListenerMixinAnimation<T> : AnimationEagerListenerMixinAnimation<T> {
readonly ObserverList<VoidCallback> _listeners = new ObserverList<VoidCallback>();
public override void addListener(VoidCallback listener) {

public abstract class AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationLazyListenerMixinAnimationDouble : AnimationLocalListenersMixinAnimationLazyListenerMixinAnimationDouble {
public abstract class AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationLazyListenerMixinAnimation<T> : AnimationLocalListenersMixinAnimationLazyListenerMixinAnimation<T> {
readonly ObserverList<AnimationStatusListener> _statusListeners = new ObserverList<AnimationStatusListener>();
public override void addStatusListener(AnimationStatusListener listener) {

public abstract class AnimationLocalStatusListenersMixinAnimationLazyListenerMixinAnimationDouble : AnimationLazyListenerMixinAnimationDouble {
public abstract class AnimationLocalStatusListenersMixinAnimationLazyListenerMixinAnimation<T> : AnimationLazyListenerMixinAnimation<T> {
readonly ObserverList<AnimationStatusListener> _statusListeners = new ObserverList<AnimationStatusListener>();
public override void addStatusListener(AnimationStatusListener listener) {

public abstract class AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationEagerListenerMixinAnimationDouble : AnimationLocalListenersMixinAnimationEagerListenerMixinAnimationDouble {
public abstract class AnimationLocalStatusListenersMixinAnimationLocalListenersMixinAnimationEagerListenerMixinAnimation<T> : AnimationLocalListenersMixinAnimationEagerListenerMixinAnimation<T> {
readonly ObserverList<AnimationStatusListener> _statusListeners = new ObserverList<AnimationStatusListener>();
public override void addStatusListener(AnimationStatusListener listener) {

22
Assets/UIWidgets/animation/listener_helpers.mixin.njk


namespace UIWidgets.animation {
{% macro AnimationLazyListenerMixin(with) %}
public abstract class AnimationLazyListenerMixin{{with}} : {{with}} {
public abstract class AnimationLazyListenerMixin{{with | safe}} : {{with | safe}} {
int _listenerCounter = 0;
protected void didRegisterListener() {

}
{% endmacro %}
{{ AnimationLazyListenerMixin('AnimationDouble') }}
{{ AnimationLazyListenerMixin('Animation<T>') }}
public abstract class AnimationEagerListenerMixin{{with}} : {{with}} {
public abstract class AnimationEagerListenerMixin{{with | safe}} : {{with | safe}} {
protected void didRegisterListener() {
}

}
{% endmacro %}
{{ AnimationEagerListenerMixin('AnimationDouble') }}
{{ AnimationEagerListenerMixin('Animation<T>') }}
public abstract class AnimationLocalListenersMixin{{with}} : {{with}} {
public abstract class AnimationLocalListenersMixin{{with | safe}} : {{with | safe}} {
readonly ObserverList<VoidCallback> _listeners = new ObserverList<VoidCallback>();
public override void addListener(VoidCallback listener) {

}
{% endmacro %}
{{ AnimationLocalListenersMixin('AnimationLazyListenerMixinAnimationDouble') }}
{{ AnimationLocalListenersMixin('AnimationLazyListenerMixinAnimation<T>') }}
{{ AnimationLocalListenersMixin('AnimationEagerListenerMixinAnimationDouble') }}
{{ AnimationLocalListenersMixin('AnimationEagerListenerMixinAnimation<T>') }}
public abstract class AnimationLocalStatusListenersMixin{{with}} : {{with}} {
public abstract class AnimationLocalStatusListenersMixin{{with | safe}} : {{with | safe}} {
readonly ObserverList<AnimationStatusListener> _statusListeners = new ObserverList<AnimationStatusListener>();
public override void addStatusListener(AnimationStatusListener listener) {

}
{% endmacro %}
{{ AnimationLocalStatusListenersMixin('AnimationLocalListenersMixinAnimationLazyListenerMixinAnimationDouble') }}
{{ AnimationLocalStatusListenersMixin('AnimationLocalListenersMixinAnimationLazyListenerMixinAnimation<T>') }}
{{ AnimationLocalStatusListenersMixin('AnimationLazyListenerMixinAnimationDouble') }}
{{ AnimationLocalStatusListenersMixin('AnimationLazyListenerMixinAnimation<T>') }}
{{ AnimationLocalStatusListenersMixin('AnimationLocalListenersMixinAnimationEagerListenerMixinAnimationDouble') }}
{{ AnimationLocalStatusListenersMixin('AnimationLocalListenersMixinAnimationEagerListenerMixinAnimation<T>') }}
}

89
Assets/UIWidgets/animation/tween.cs


using System;
using System.Collections.Generic;
using UIWidgets.foundation;
using UIWidgets.ui;
namespace UIWidgets.animation {
public abstract class Animatable<T> {

public static bool operator !=(Tween<T> left, Tween<T> right) {
return !object.Equals(left, right);
}
}
public class ReverseTween<T> : Tween<T> {
public ReverseTween(Tween<T> parent) : base(begin: parent.end, end: parent.begin) {
}
public readonly Tween<T> parent;
public override T lerp(double t) {
return this.parent.lerp(1.0 - t);
}
}
public class ColorTween : Tween<Color> {
public ColorTween(Color begin = null, Color end = null) : base(begin: begin, end: end) {
}
public override Color lerp(double t) {
return Color.lerp(this.begin, this.end, t);
}
}
public class SizeTween : Tween<Size> {
public SizeTween(Size begin = null, Size end = null) : base(begin: begin, end: end) {
}
public override Size lerp(double t) {
return Size.lerp(this.begin, this.end, t);
}
}
public class RectTween : Tween<Rect> {
public RectTween(Rect begin = null, Rect end = null) : base(begin: begin, end: end) {
}
public override Rect lerp(double t) {
return Rect.lerp(this.begin, this.end, t);
}
}
public class IntTween : Tween<int> {
public IntTween(int begin, int end) : base(begin: begin, end: end) {
}
public override int lerp(double t) {
return (this.begin + (this.end - this.begin) * t).round();
}
}
public class DoubleTween : Tween<double> {
public DoubleTween(int begin, int end) : base(begin: begin, end: end) {
}
public override double lerp(double t) {
return this.begin + (this.end - this.begin) * t;
}
}
public class StepTween : Tween<int> {
public StepTween(int begin, int end) : base(begin: begin, end: end) {
}
public override int lerp(double t) {
return (this.begin + (this.end - this.begin) * t).floor();
}
}
public class CurveTween : Animatable<double> {
public CurveTween(Curve curve = null) {
D.assert(curve != null);
this.curve = curve;
}
public readonly Curve curve;
public override double evaluate(Animation<double> animation) {
double t = animation.value;
if (t == 0.0 || t == 1.0) {
D.assert(this.curve.transform(t).round() == t);
return t;
}
return this.curve.transform(t);
}
public override string ToString() {
return string.Format("{0}(curve: {1})", this.GetType(), this.curve);
}
}
}

6
Assets/UIWidgets/foundation/basic_types.cs


public static bool isNotEmpty<TKey, TValue>(this IDictionary<TKey, TValue> it) {
return it.Count != 0;
}
public static bool isEmpty(this string it) {
return string.IsNullOrEmpty(it);
}

}
public static bool isFinite(this double it) {
return !double.IsInfinity(it);
}
}
}

58
Assets/UIWidgets/ui/geometry.cs


return value;
}
}
public static bool isFinite(this double it) {
return !double.IsInfinity(it);
}
public static double lerpDouble(double a, double b, double t) {
return a + (b - a) * t;
}
public static int round(this double value) {
return (int) Math.Round(value);
}
public static int floor(this double value) {
return (int) Math.Floor(value);
}
}

return offset.dx >= 0.0 && offset.dx < this.width && offset.dy >= 0.0 && offset.dy < this.height;
}
public static Size lerp(Size a, Size b, double t) {
if (a == null && b == null) {
return null;
}
if (a == null) {
return b * t;
}
if (b == null) {
return a * (1.0 - t);
}
return new Size(MathUtils.lerpDouble(a.width, b.width, t), MathUtils.lerpDouble(a.height, b.height, t));
}
public bool Equals(Size other) {
return this._dx.Equals(other._dx) && this._dy.Equals(other._dy);
}

public bool contains(Rect rect) {
return this.contains(rect.topLeft) && this.contains(rect.bottomRight);
}
public static Rect lerp(Rect a, Rect b, double t) {
if (a == null && b == null) {
return null;
}
if (a == null) {
return Rect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
}
if (b == null) {
double k = 1.0 - t;
return Rect.fromLTRB(a.left * k, a.top * k, a.right * k, a.bottom * k);
}
return Rect.fromLTRB(
MathUtils.lerpDouble(a.left, b.left, t),
MathUtils.lerpDouble(a.top, b.top, t),
MathUtils.lerpDouble(a.right, b.right, t),
MathUtils.lerpDouble(a.bottom, b.bottom, t)
);
}
public bool Equals(Rect other) {

27
Assets/UIWidgets/ui/painting/painting.cs


using UnityEngine;
namespace UIWidgets.ui {
public static class PaintingUtils {
internal static Color _scaleAlpha(this Color a, double factor) {
return a.withAlpha((a.alpha * factor).round().clamp(0, 255));
}
}
public class Color : IEquatable<Color> {
public Color(long value) {
this.value = value & 0xFFFFFFFF;

public Color withBlue(int b) {
return Color.fromARGB(this.alpha, this.red, this.green, b);
}
public static Color lerp(Color a, Color b, double t) {
if (a == null && b == null) {
return null;
}
if (a == null) {
return b._scaleAlpha(t);
}
if (b == null) {
return a._scaleAlpha(1.0 - t);
}
return Color.fromARGB(
((int) MathUtils.lerpDouble(a.alpha, b.alpha, t)).clamp(0, 255),
((int) MathUtils.lerpDouble(a.red, b.red, t)).clamp(0, 255),
((int) MathUtils.lerpDouble(a.green, b.green, t)).clamp(0, 255),
((int) MathUtils.lerpDouble(a.blue, b.blue, t)).clamp(0, 255)
);
}
public bool Equals(Color other) {

正在加载...
取消
保存