浏览代码

physics.

/main
kg 6 年前
当前提交
a0609a63
共有 39 个文件被更改,包括 1544 次插入17 次删除
  1. 4
      Assets/UIWidgets/foundation/basic_types.cs
  2. 65
      Assets/UIWidgets/foundation/change_notifier.cs
  3. 11
      Assets/UIWidgets/gestures/recognizer.cs
  4. 36
      Assets/UIWidgets/rendering/viewport_offset.cs
  5. 8
      Assets/UIWidgets/ui/geometry.cs
  6. 4
      Assets/UIWidgets/widgets/framework.cs
  7. 70
      Assets/UIWidgets/widgets/notification_listener.cs
  8. 3
      Assets/UIWidgets/widgets/notification_listener.cs.meta
  9. 75
      Assets/UIWidgets/widgets/scroll_matrics.cs
  10. 3
      Assets/UIWidgets/widgets/scroll_matrics.cs.meta
  11. 139
      Assets/UIWidgets/widgets/scroll_notification.cs
  12. 3
      Assets/UIWidgets/widgets/scroll_notification.cs.meta
  13. 53
      Assets/UIWidgets/widgets/scroll_notification.mixin.gen.cs
  14. 11
      Assets/UIWidgets/widgets/scroll_notification.mixin.gen.cs.meta
  15. 32
      Assets/UIWidgets/widgets/scroll_notification.mixin.njk
  16. 3
      Assets/UIWidgets/widgets/scroll_notification.mixin.njk.meta
  17. 352
      Assets/UIWidgets/widgets/scroll_physics.cs
  18. 3
      Assets/UIWidgets/widgets/scroll_physics.cs.meta
  19. 7
      Assets/UIWidgets/widgets/scroll_position.cs
  20. 3
      Assets/UIWidgets/widgets/scroll_position.cs.meta
  21. 169
      Assets/UIWidgets/widgets/scroll_simulation.cs
  22. 3
      Assets/UIWidgets/widgets/scroll_simulation.cs.meta
  23. 4
      Assets/UIWidgets/animation/curves.cs
  24. 44
      Assets/UIWidgets/physics/clamped_simulation.cs
  25. 3
      Assets/UIWidgets/physics/clamped_simulation.cs.meta
  26. 96
      Assets/UIWidgets/physics/friction_simulation.cs
  27. 3
      Assets/UIWidgets/physics/friction_simulation.cs.meta
  28. 36
      Assets/UIWidgets/physics/gravity_simulation.cs
  29. 3
      Assets/UIWidgets/physics/gravity_simulation.cs.meta
  30. 15
      Assets/UIWidgets/physics/simulation.cs
  31. 11
      Assets/UIWidgets/physics/simulation.cs.meta
  32. 238
      Assets/UIWidgets/physics/spring_simulation.cs
  33. 3
      Assets/UIWidgets/physics/spring_simulation.cs.meta
  34. 28
      Assets/UIWidgets/physics/tolerance.cs
  35. 3
      Assets/UIWidgets/physics/tolerance.cs.meta
  36. 14
      Assets/UIWidgets/physics/utils.cs
  37. 3
      Assets/UIWidgets/physics/utils.cs.meta

4
Assets/UIWidgets/foundation/basic_types.cs


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

65
Assets/UIWidgets/foundation/change_notifier.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.ui;
using UnityEngine;

}
public static class ListenableUtils {
public static Listenable merge(List<Listenable> listenables) {
public static Listenable merge(this List<Listenable> listenables) {
return new _MergingListenable(listenables);
}
}

}
public class ChangeNotifier : Listenable {
public ObserverList<VoidCallback> _listeners = new ObserverList<VoidCallback>();
public class ChangeNotifier : Listenable, IDisposable {
ObserverList<VoidCallback> _listeners = new ObserverList<VoidCallback>();
bool _debugAssertNotDisposed() {
D.assert(() => {
if (this._listeners == null) {
throw new UIWidgetsError(
string.Format("A {0} was used after being disposed.\n" +
"Once you have called dispose() on a {0}, it can no longer be used.",
this.GetType()));
}
return true;
});
public bool hasListeners {
get { return this._listeners.Count > 0; }
return true;
}
protected bool hasListeners {
get {
D.assert(this._debugAssertNotDisposed());
return this._listeners.isNotEmpty();
}
D.assert(this._debugAssertNotDisposed());
D.assert(this._debugAssertNotDisposed());
public void Dispose() {
this.dispose();
}
D.assert(this._debugAssertNotDisposed());
public void notifyListeners() {
protected void notifyListeners() {
D.assert(this._debugAssertNotDisposed());
if (this._listeners != null) {
var localListeners = new List<VoidCallback>(this._listeners);
foreach (VoidCallback listener in localListeners) {

}
}
catch (Exception ex) {
Debug.LogError("error while dispatching notifications: " + ex);
UIWidgetsError.reportError(new UIWidgetsErrorDetails(
exception: ex,
library: "foundation library",
context: "while dispatching notifications for " + this.GetType(),
informationCollector: information => {
information.AppendLine("The " + this.GetType() + " sending notification was:");
information.Append(" " + this);
}
));
}
}
}

public class _MergingListenable : ChangeNotifier {
public _MergingListenable(List<Listenable> _children) {
class _MergingListenable : ChangeNotifier {
internal _MergingListenable(List<Listenable> _children) {
this._children = _children;
foreach (Listenable child in _children) {

}
}
public readonly List<Listenable> _children;
readonly List<Listenable> _children;
public override void dispose() {
foreach (Listenable child in this._children) {

base.dispose();
}
public override string ToString() {
return "Listenable.merge([" + string.Join(", ", this._children.Select(c => c.ToString()).ToArray()) + "])";
}
}
public class ValueNotifier<T> : ChangeNotifier, ValueListenable<T> {

}
}
public T _value;
T _value;
public override string ToString() {
return Diagnostics.describeIdentity(this) + "(" + this._value + ")";
}
}
}

11
Assets/UIWidgets/gestures/recognizer.cs


result = callback();
}
catch (Exception ex) {
Debug.LogError("Error while handling a gesture [" + name + "]: " + ex);
UIWidgetsError.reportError(new UIWidgetsErrorDetails(
exception: ex,
library: "gesture",
context: "while handling a gesture",
informationCollector: information => {
information.AppendLine("Handler: " + name);
information.AppendLine("Recognizer:");
information.AppendLine(" " + this);
}
));
}
return result;

36
Assets/UIWidgets/rendering/viewport_offset.cs


using System;
using System.Collections.Generic;
using RSG;
using UIWidgets.animation;
using UIWidgets.foundation;
namespace UIWidgets.rendering {

return ScrollDirection.forward;
}
throw new Exception("unknown direction");
D.assert(false);
return default(ScrollDirection);
}
}

public abstract void correctBy(double correction);
public abstract void jumpTo(double pixels);
public abstract IPromise<object> animateTo(double to, TimeSpan duration, Curve curve);
public abstract bool allowImplicitScrolling { get; }
public override string ToString() {
var description = new List<string>();
this.debugFillDescription(description);
return Diagnostics.describeIdentity(this) + "(" + string.Join(", ", description.ToArray()) + ")";
}
protected virtual void debugFillDescription(List<String> description) {
description.Add("offset: " + this.pixels.ToString("F1"));
}
public class _FixedViewportOffset : ViewportOffset {
public _FixedViewportOffset(double _pixels) {
class _FixedViewportOffset : ViewportOffset {
internal _FixedViewportOffset(double _pixels) {
public new static _FixedViewportOffset zero() {
internal new static _FixedViewportOffset zero() {
public double _pixels;
double _pixels;
public override double pixels {
get { return this._pixels; }

public override void jumpTo(double pixels) {
}
public override IPromise<object> animateTo(double to, TimeSpan duration, Curve curve) {
return Promise<object>.Resolved(null);
}
}
public override bool allowImplicitScrolling {
get { return false; }
}
}
}

8
Assets/UIWidgets/ui/geometry.cs


return value;
}
public static double abs(this double value) {
return Math.Abs(value);
}
public static int sign(this double value) {
return Math.Sign(value);
}
}
public abstract class OffsetBase : IEquatable<OffsetBase> {

4
Assets/UIWidgets/widgets/framework.cs


}
}
public void visitAncestorElements(Func<Element, bool> visitor) {
}
public virtual void visitChildren(ElementVisitor visitor) {
}

public interface BuildContext {
Widget widget { get; }
void visitAncestorElements(Func<Element, bool> visitor);
}
public class BuildOwner {

70
Assets/UIWidgets/widgets/notification_listener.cs


using System.Collections.Generic;
using UIWidgets.foundation;
namespace UIWidgets.widgets {
public delegate bool NotificationListenerCallback<T>(T notification) where T : Notification;
public abstract class Notification {
protected virtual bool visitAncestor(Element element) {
if (element is StatelessElement) {
StatelessWidget widget = (StatelessWidget) element.widget;
var listener = widget as _NotificationListener;
if (listener != null) {
if (listener._dispatch(this, element)) {
return false;
}
}
}
return true;
}
public void dispatch(BuildContext target) {
D.assert(target != null);
target.visitAncestorElements(this.visitAncestor);
}
public override string ToString() {
var description = new List<string>();
this.debugFillDescription(description);
return string.Format("{0}({1})", this.GetType(), string.Join(", ", description.ToArray()));
}
protected virtual void debugFillDescription(List<string> description) {
}
}
interface _NotificationListener {
bool _dispatch(Notification notification, Element element);
}
public class NotificationListener<T> : StatelessWidget, _NotificationListener where T : Notification {
public NotificationListener(
string key = null,
Widget child = null,
NotificationListenerCallback<T> onNotification = null) : base(key) {
this.child = child;
this.onNotification = onNotification;
}
public readonly Widget child;
public readonly NotificationListenerCallback<T> onNotification;
bool _NotificationListener._dispatch(Notification notification, Element element) {
if (this.onNotification != null && notification is T) {
bool result = this.onNotification((T) notification);
return result;
}
return false;
}
public override Widget build(BuildContext context) {
return this.child;
}
}
public class LayoutChangedNotification : Notification {
}
}

3
Assets/UIWidgets/widgets/notification_listener.cs.meta


fileFormatVersion: 2
guid: 647bb259966f4e4e8196f7c9c4ef56dc
timeCreated: 1536582324

75
Assets/UIWidgets/widgets/scroll_matrics.cs


using System;
using UIWidgets.painting;
namespace UIWidgets.widgets {
public interface ScrollMetrics {
double minScrollExtent { get; }
double maxScrollExtent { get; }
double pixels { get; }
double viewportDimension { get; }
AxisDirection axisDirection { get; }
}
public static class ScrollMetricsUtils {
public static Axis axis(this ScrollMetrics it) {
return AxisUtils.axisDirectionToAxis(it.axisDirection);
}
public static bool outOfRange(this ScrollMetrics it) {
return it.pixels < it.minScrollExtent || it.pixels > it.maxScrollExtent;
}
public static bool atEdge(this ScrollMetrics it) {
return it.pixels == it.minScrollExtent || it.pixels == it.maxScrollExtent;
}
public static double extentBefore(this ScrollMetrics it) {
return Math.Max(it.pixels - it.minScrollExtent, 0.0);
}
public static double extentInside(this ScrollMetrics it) {
return Math.Min(it.pixels, it.maxScrollExtent) -
Math.Max(it.pixels, it.minScrollExtent) +
Math.Min(it.viewportDimension, it.maxScrollExtent - it.minScrollExtent);
}
public static double extentAfter(this ScrollMetrics it) {
return Math.Max(it.maxScrollExtent - it.pixels, 0.0);
}
}
public class FixedScrollMetrics : ScrollMetrics {
public FixedScrollMetrics(
double minScrollExtent = 0.0,
double maxScrollExtent = 0.0,
double pixels = 0.0,
double viewportDimension = 0.0,
AxisDirection axisDirection = AxisDirection.down
) {
this.minScrollExtent = minScrollExtent;
this.maxScrollExtent = maxScrollExtent;
this.pixels = pixels;
this.viewportDimension = viewportDimension;
this.axisDirection = axisDirection;
}
public double minScrollExtent { get; private set; }
public double maxScrollExtent { get; private set; }
public double pixels { get; private set; }
public double viewportDimension { get; private set; }
public AxisDirection axisDirection { get; private set; }
public override string ToString() {
return string.Format("{0}({1:F1})..[{2:F1}]..{3:F1})",
this.GetType(), this.extentBefore(), this.extentInside(), this.extentAfter());
}
}
}

3
Assets/UIWidgets/widgets/scroll_matrics.cs.meta


fileFormatVersion: 2
guid: 25e09958cead4597adacd8c3dbf7b12f
timeCreated: 1536561228

139
Assets/UIWidgets/widgets/scroll_notification.cs


using System.Collections.Generic;
using UIWidgets.gestures;
using UIWidgets.rendering;
namespace UIWidgets.widgets {
public abstract class ScrollNotification : ViewportNotificationMixinLayoutChangedNotification {
protected ScrollNotification(
ScrollMetrics metrics = null,
BuildContext context = null
) {
this.metrics = metrics;
this.context = context;
}
public readonly ScrollMetrics metrics;
public readonly BuildContext context;
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(this.metrics.ToString());
}
public static bool defaultScrollNotificationPredicate(ScrollNotification notification) {
return notification.depth == 0;
}
}
public class ScrollStartNotification : ScrollNotification {
public ScrollStartNotification(
ScrollMetrics metrics = null,
BuildContext context = null,
DragStartDetails dragDetails = null
) : base(metrics: metrics, context: context) {
this.dragDetails = dragDetails;
}
public readonly DragStartDetails dragDetails;
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
if (this.dragDetails != null)
description.Add(this.dragDetails.ToString());
}
}
public class ScrollUpdateNotification : ScrollNotification {
public ScrollUpdateNotification(
ScrollMetrics metrics = null,
BuildContext context = null,
DragUpdateDetails dragDetails = null,
double scrollDelta = 0
) : base(metrics: metrics, context: context) {
this.dragDetails = dragDetails;
this.scrollDelta = scrollDelta;
}
public readonly DragUpdateDetails dragDetails;
public readonly double scrollDelta;
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(string.Format("scrollDelta: {0}", this.scrollDelta));
if (this.dragDetails != null) {
description.Add(this.dragDetails.ToString());
}
}
}
public class OverscrollNotification : ScrollNotification {
public OverscrollNotification(
ScrollMetrics metrics = null,
BuildContext context = null,
DragUpdateDetails dragDetails = null,
double overscroll = 0,
double velocity = 0
) : base(metrics: metrics, context: context) {
this.dragDetails = dragDetails;
this.overscroll = overscroll;
this.velocity = velocity;
}
public readonly DragUpdateDetails dragDetails;
public readonly double overscroll;
public readonly double velocity;
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(string.Format("overscroll: {0:F1}", this.overscroll));
description.Add(string.Format("velocity: {0:F1}", this.velocity));
if (this.dragDetails != null) {
description.Add(this.dragDetails.ToString());
}
}
}
public class ScrollEndNotification : ScrollNotification {
public ScrollEndNotification(
ScrollMetrics metrics = null,
BuildContext context = null,
DragEndDetails dragDetails = null,
double overscroll = 0,
double velocity = 0
) : base(metrics: metrics, context: context) {
this.dragDetails = dragDetails;
}
public readonly DragEndDetails dragDetails;
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
if (this.dragDetails != null) {
description.Add(this.dragDetails.ToString());
}
}
}
public class UserScrollNotification : ScrollNotification {
public UserScrollNotification(
ScrollMetrics metrics = null,
BuildContext context = null,
ScrollDirection direction = ScrollDirection.idle
) : base(metrics: metrics, context: context) {
this.direction = direction;
}
public readonly ScrollDirection direction;
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(string.Format("direction: {0}", this.direction));
}
}
public delegate bool ScrollNotificationPredicate(ScrollNotification notification);
}

3
Assets/UIWidgets/widgets/scroll_notification.cs.meta


fileFormatVersion: 2
guid: 76d5830e8d5948e8a312ac1699cbfcb1
timeCreated: 1536583483

53
Assets/UIWidgets/widgets/scroll_notification.mixin.gen.cs


using System.Collections.Generic;
using UIWidgets.rendering;
namespace UIWidgets.widgets {
public abstract class ViewportNotificationMixinNotification : Notification {
public int depth {
get { return _depth; }
}
int _depth = 0;
protected override bool visitAncestor(Element element) {
if (element is RenderObjectElement && element.renderObject is RenderAbstractViewport) {
this._depth += 1;
}
return base.visitAncestor(element);
}
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(string.Format("depth: {0} ({1})",
this._depth, this._depth == 0 ? "local" : "remote"));
}
}
public abstract class ViewportNotificationMixinLayoutChangedNotification : LayoutChangedNotification {
public int depth {
get { return _depth; }
}
int _depth = 0;
protected override bool visitAncestor(Element element) {
if (element is RenderObjectElement && element.renderObject is RenderAbstractViewport) {
this._depth += 1;
}
return base.visitAncestor(element);
}
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(string.Format("depth: {0} ({1})",
this._depth, this._depth == 0 ? "local" : "remote"));
}
}
}

11
Assets/UIWidgets/widgets/scroll_notification.mixin.gen.cs.meta


fileFormatVersion: 2
guid: 76fcf9dfb96bf49579e29af4f80c2d52
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

32
Assets/UIWidgets/widgets/scroll_notification.mixin.njk


using System.Collections.Generic;
using UIWidgets.rendering;
namespace UIWidgets.widgets {
{% macro ViewportNotificationMixin(with) %}
public abstract class ViewportNotificationMixin{{with}} : {{with}} {
public int depth {
get { return _depth; }
}
int _depth = 0;
protected override bool visitAncestor(Element element) {
if (element is RenderObjectElement && element.renderObject is RenderAbstractViewport) {
this._depth += 1;
}
return base.visitAncestor(element);
}
protected override void debugFillDescription(List<string> description) {
base.debugFillDescription(description);
description.Add(string.Format("depth: {0} ({1})",
this._depth, this._depth == 0 ? "local" : "remote"));
}
}
{% endmacro %}
{{ ViewportNotificationMixin('Notification') }}
{{ ViewportNotificationMixin('LayoutChangedNotification') }}
}

3
Assets/UIWidgets/widgets/scroll_notification.mixin.njk.meta


fileFormatVersion: 2
guid: 50050e438a664e30957959a7a1da310c
timeCreated: 1536583983

352
Assets/UIWidgets/widgets/scroll_physics.cs


using System;
using UIWidgets.foundation;
using UIWidgets.gestures;
using UIWidgets.physics;
using UIWidgets.ui;
namespace UIWidgets.widgets {
public class ScrollPhysics {
public ScrollPhysics(ScrollPhysics parent) {
this.parent = parent;
}
public readonly ScrollPhysics parent;
protected ScrollPhysics buildParent(ScrollPhysics ancestor) {
if (this.parent == null) {
return ancestor;
}
return this.parent.applyTo(ancestor) ?? ancestor;
}
public virtual ScrollPhysics applyTo(ScrollPhysics ancestor) {
return new ScrollPhysics(parent: this.buildParent(ancestor));
}
public virtual double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
if (this.parent == null) {
return offset;
}
return this.parent.applyPhysicsToUserOffset(position, offset);
}
public virtual bool shouldAcceptUserOffset(ScrollMetrics position) {
if (this.parent == null) {
return position.pixels != 0.0 || position.minScrollExtent != position.maxScrollExtent;
}
return this.parent.shouldAcceptUserOffset(position);
}
public virtual double applyBoundaryConditions(ScrollMetrics position, double value) {
if (this.parent == null) {
return 0.0;
}
return this.parent.applyBoundaryConditions(position, value);
}
public virtual Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
if (parent == null) {
return null;
}
return this.parent.createBallisticSimulation(position, velocity);
}
static readonly SpringDescription _kDefaultSpring = SpringDescription.withDampingRatio(
mass: 0.5,
stiffness: 100.0,
ratio: 1.1
);
public virtual SpringDescription spring {
get {
if (this.parent == null) {
return ScrollPhysics._kDefaultSpring;
}
return this.parent.spring ?? _kDefaultSpring;
}
}
// todo: Handle the case of the device pixel ratio changing. use 1 as devicePixelRatio for now.
static readonly Tolerance _kDefaultTolerance = new Tolerance(
velocity: 1.0 / (0.050 * 1),
distance: 1.0 / 1
);
public virtual Tolerance tolerance {
get {
if (this.parent == null) {
return _kDefaultTolerance;
}
return this.parent.tolerance ?? _kDefaultTolerance;
}
}
public virtual double minFlingDistance {
get {
if (this.parent == null) {
return Constants.kTouchSlop;
}
return this.parent.minFlingDistance;
}
}
public virtual double carriedMomentum(double existingVelocity) {
if (this.parent == null) {
return 0.0;
}
return this.parent.carriedMomentum(existingVelocity);
}
public virtual double minFlingVelocity {
get {
if (this.parent == null) {
return Constants.kMinFlingVelocity;
}
return this.parent.minFlingVelocity;
}
}
public virtual double maxFlingVelocity {
get {
if (this.parent == null) {
return Constants.kMaxFlingVelocity;
}
return this.parent.maxFlingVelocity;
}
}
public virtual double dragStartDistanceMotionThreshold {
get {
if (this.parent == null) {
return 0.0;
}
return this.parent.dragStartDistanceMotionThreshold;
}
}
public virtual bool allowImplicitScrolling {
get { return true; }
}
public override string ToString() {
if (this.parent == null) {
return string.Format("{0}", this.GetType());
}
return string.Format("{0} -> {1}", this.GetType(), this.parent);
}
}
public class BouncingScrollPhysics : ScrollPhysics {
public BouncingScrollPhysics(ScrollPhysics parent = null) : base(parent: parent) {
}
public override ScrollPhysics applyTo(ScrollPhysics ancestor) {
return new BouncingScrollPhysics(parent: this.buildParent(ancestor));
}
public double frictionFactor(double overscrollFraction) {
return 0.52 * Math.Pow(1 - overscrollFraction, 2);
}
public override double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
D.assert(position.minScrollExtent <= position.maxScrollExtent);
if (!position.outOfRange()) {
return offset;
}
double overscrollPastStart = Math.Max(position.minScrollExtent - position.pixels, 0.0);
double overscrollPastEnd = Math.Max(position.pixels - position.maxScrollExtent, 0.0);
double overscrollPast = Math.Max(overscrollPastStart, overscrollPastEnd);
bool easing = (overscrollPastStart > 0.0 && offset < 0.0) || (overscrollPastEnd > 0.0 && offset > 0.0);
double friction = easing
? this.frictionFactor((overscrollPast - offset.abs()) / position.viewportDimension)
: this.frictionFactor(overscrollPast / position.viewportDimension);
double direction = offset.sign();
return direction * _applyFriction(overscrollPast, offset.abs(), friction);
}
static double _applyFriction(double extentOutside, double absDelta, double gamma) {
D.assert(absDelta > 0);
double total = 0.0;
if (extentOutside > 0) {
double deltaToLimit = extentOutside / gamma;
if (absDelta < deltaToLimit) {
return absDelta * gamma;
}
total += extentOutside;
absDelta -= deltaToLimit;
}
return total + absDelta;
}
public override double applyBoundaryConditions(ScrollMetrics position, double value) {
return 0.0;
}
public override Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
Tolerance tolerance = this.tolerance;
if (velocity.abs() >= tolerance.velocity || position.outOfRange()) {
return new BouncingScrollSimulation(
spring: spring,
position: position.pixels,
velocity: velocity * 0.91,
leadingExtent: position.minScrollExtent,
trailingExtent: position.maxScrollExtent,
tolerance: tolerance
);
}
return null;
}
public override double minFlingVelocity {
get { return Constants.kMinFlingVelocity * 2.0; }
}
public override double carriedMomentum(double existingVelocity) {
return existingVelocity.sign() * Math.Min(0.000816 * Math.Pow(existingVelocity.abs(), 1.967), 40000.0);
}
public override double dragStartDistanceMotionThreshold {
get { return 3.5; }
}
}
public class ClampingScrollPhysics : ScrollPhysics {
public ClampingScrollPhysics(ScrollPhysics parent = null) : base(parent: parent) {
}
public override ScrollPhysics applyTo(ScrollPhysics ancestor) {
return new ClampingScrollPhysics(parent: this.buildParent(ancestor));
}
public override double applyBoundaryConditions(ScrollMetrics position, double value) {
D.assert(() => {
if (value == position.pixels) {
throw new UIWidgetsError(
string.Format(
"{0}.applyBoundaryConditions() was called redundantly.\n" +
"The proposed new position, {1}, is exactly equal to the current position of the " +
"given {2}, {3}.\n" +
"The applyBoundaryConditions method should only be called when the value is " +
"going to actually change the pixels, otherwise it is redundant.\n" +
"The physics object in question was:\n" +
" {4}\n" +
"The position object in question was:\n" +
" {5}\n",
this.GetType(), value, position.GetType(), position.pixels, this, position));
}
return true;
});
if (value < position.pixels && position.pixels <= position.minScrollExtent) {
return value - position.pixels;
}
if (position.maxScrollExtent <= position.pixels && position.pixels < value) {
return value - position.pixels;
}
if (value < position.minScrollExtent && position.minScrollExtent < position.pixels) {
return value - position.minScrollExtent;
}
if (position.pixels < position.maxScrollExtent && position.maxScrollExtent < value) {
return value - position.maxScrollExtent;
}
return 0.0;
}
public override Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
Tolerance tolerance = this.tolerance;
if (position.outOfRange()) {
double? end = null;
if (position.pixels > position.maxScrollExtent) {
end = position.maxScrollExtent;
}
if (position.pixels < position.minScrollExtent) {
end = position.minScrollExtent;
}
D.assert(end != null);
return new ScrollSpringSimulation(
this.spring,
position.pixels,
position.maxScrollExtent,
Math.Min(0.0, velocity),
tolerance: tolerance
);
}
if (velocity.abs() < tolerance.velocity) {
return null;
}
if (velocity > 0.0 && position.pixels >= position.maxScrollExtent) {
return null;
}
if (velocity < 0.0 && position.pixels <= position.minScrollExtent) {
return null;
}
return new ClampingScrollSimulation(
position: position.pixels,
velocity: velocity,
tolerance: tolerance
);
}
}
public class AlwaysScrollableScrollPhysics : ScrollPhysics {
public AlwaysScrollableScrollPhysics(ScrollPhysics parent = null) : base(parent: parent) {
}
public override ScrollPhysics applyTo(ScrollPhysics ancestor) {
return new AlwaysScrollableScrollPhysics(parent: this.buildParent(ancestor));
}
public override bool shouldAcceptUserOffset(ScrollMetrics position) {
return true;
}
}
public class NeverScrollableScrollPhysics : ScrollPhysics {
public NeverScrollableScrollPhysics(ScrollPhysics parent = null) : base(parent: parent) {
}
public override ScrollPhysics applyTo(ScrollPhysics ancestor) {
return new NeverScrollableScrollPhysics(parent: this.buildParent(ancestor));
}
public override bool shouldAcceptUserOffset(ScrollMetrics position) {
return false;
}
public override bool allowImplicitScrolling {
get { return false; }
}
}
}

3
Assets/UIWidgets/widgets/scroll_physics.cs.meta


fileFormatVersion: 2
guid: f58c175eda22408f95d902db1c4eec03
timeCreated: 1536563659

7
Assets/UIWidgets/widgets/scroll_position.cs


using UIWidgets.rendering;
namespace UIWidgets.widgets {
// public abstract class ScrollPosition : ViewportOffset, ScrollMetrics {
//
// }
}

3
Assets/UIWidgets/widgets/scroll_position.cs.meta


fileFormatVersion: 2
guid: d1ce64af14b4434b8850b299aae20240
timeCreated: 1536563588

169
Assets/UIWidgets/widgets/scroll_simulation.cs


using System;
using UIWidgets.foundation;
using UIWidgets.physics;
using UIWidgets.ui;
namespace UIWidgets.widgets {
public class BouncingScrollSimulation : Simulation {
public BouncingScrollSimulation(
double position,
double velocity,
double leadingExtent,
double trailingExtent,
SpringDescription spring,
Tolerance tolerance = null
) : base(tolerance: tolerance) {
D.assert(leadingExtent <= trailingExtent);
D.assert(spring != null);
this.leadingExtent = leadingExtent;
this.trailingExtent = trailingExtent;
this.spring = spring;
if (position < leadingExtent) {
this._springSimulation = this._underscrollSimulation(position, velocity);
this._springTime = double.NegativeInfinity;
} else if (position > trailingExtent) {
this._springSimulation = this._overscrollSimulation(position, velocity);
this._springTime = double.NegativeInfinity;
} else {
this._frictionSimulation = new FrictionSimulation(0.135, position, velocity);
double finalX = this._frictionSimulation.finalX;
if (velocity > 0.0 && finalX > trailingExtent) {
this._springTime = this._frictionSimulation.timeAtX(trailingExtent);
this._springSimulation = this._overscrollSimulation(
trailingExtent,
Math.Min(this._frictionSimulation.dx(this._springTime), maxSpringTransferVelocity)
);
D.assert(this._springTime.isFinite());
} else if (velocity < 0.0 && finalX < leadingExtent) {
this._springTime = this._frictionSimulation.timeAtX(leadingExtent);
this._springSimulation = this._underscrollSimulation(
leadingExtent,
Math.Min(this._frictionSimulation.dx(this._springTime), maxSpringTransferVelocity)
);
D.assert(this._springTime.isFinite());
} else {
this._springTime = double.PositiveInfinity;
}
}
}
const double maxSpringTransferVelocity = 5000.0;
public readonly double leadingExtent;
public readonly double trailingExtent;
public readonly SpringDescription spring;
readonly FrictionSimulation _frictionSimulation;
readonly Simulation _springSimulation;
readonly double _springTime;
double _timeOffset = 0.0;
Simulation _underscrollSimulation(double x, double dx) {
return new ScrollSpringSimulation(this.spring, x, this.leadingExtent, dx);
}
Simulation _overscrollSimulation(double x, double dx) {
return new ScrollSpringSimulation(this.spring, x, this.trailingExtent, dx);
}
Simulation _simulation(double time) {
Simulation simulation;
if (time > this._springTime) {
this._timeOffset = this._springTime.isFinite() ? this._springTime : 0.0;
simulation = this._springSimulation;
} else {
this._timeOffset = 0.0;
simulation = this._frictionSimulation;
}
simulation.tolerance = this.tolerance;
return simulation;
}
public override double x(double time) {
return this._simulation(time).x(time - this._timeOffset);
}
public override double dx(double time) {
return this._simulation(time).dx(time - this._timeOffset);
}
public override bool isDone(double time) {
return this._simulation(time).isDone(time - this._timeOffset);
}
public override string ToString() {
return string.Format("{0}(leadingExtent: {1}, trailingExtent: {2})",
this.GetType(), this.leadingExtent, this.trailingExtent);
}
}
public class ClampingScrollSimulation : Simulation {
public ClampingScrollSimulation(
double position,
double velocity,
double friction = 0.015,
Tolerance tolerance = null
) : base(tolerance: tolerance) {
D.assert(_flingVelocityPenetration(0.0) == _initialVelocityPenetration);
this.position = position;
this.velocity = velocity;
this.friction = friction;
this._duration = this._flingDuration(velocity);
this._distance = (velocity * this._duration / _initialVelocityPenetration).abs();
}
public readonly double position;
public readonly double velocity;
public readonly double friction;
readonly double _duration;
readonly double _distance;
static readonly double _kDecelerationRate = Math.Log(0.78) / Math.Log(0.9);
static double _decelerationForFriction(double friction) {
return friction * 61774.04968;
}
double _flingDuration(double velocity) {
double scaledFriction = this.friction * _decelerationForFriction(0.84);
double deceleration = Math.Log(0.35 * velocity.abs() / scaledFriction);
return Math.Exp(deceleration / (_kDecelerationRate - 1.0));
}
const double _initialVelocityPenetration = 3.065;
static double _flingDistancePenetration(double t) {
return (1.2 * t * t * t) - (3.27 * t * t) + (_initialVelocityPenetration * t);
}
static double _flingVelocityPenetration(double t) {
return (3.6 * t * t) - (6.54 * t) + _initialVelocityPenetration;
}
public override double x(double time) {
double t = (time / this._duration).clamp(0.0, 1.0);
return this.position + this._distance * _flingDistancePenetration(t) * this.velocity.sign();
}
public override double dx(double time) {
double t = (time / this._duration).clamp(0.0, 1.0);
return this._distance * _flingVelocityPenetration(t) * this.velocity.sign() / this._duration;
}
public override bool isDone(double time) {
return time >= this._duration;
}
}
}

3
Assets/UIWidgets/widgets/scroll_simulation.cs.meta


fileFormatVersion: 2
guid: c2459448f9d744ec8e3240a0e1a88754
timeCreated: 1536573658

4
Assets/UIWidgets/animation/curves.cs


namespace UIWidgets.animation {
public abstract class Curve {
}
}

44
Assets/UIWidgets/physics/clamped_simulation.cs


using UIWidgets.foundation;
using UIWidgets.ui;
namespace UIWidgets.physics {
public class ClampedSimulation : Simulation {
public ClampedSimulation(Simulation simulation,
double xMin = double.NegativeInfinity,
double xMax = double.PositiveInfinity,
double dxMin = double.NegativeInfinity,
double dxMax = double.PositiveInfinity
) {
D.assert(simulation != null);
D.assert(xMax >= xMin);
D.assert(dxMax >= dxMin);
this.simulation = simulation;
this.xMin = xMin;
this.dxMin = dxMin;
this.dxMax = dxMax;
}
public readonly Simulation simulation;
public readonly double xMin;
public readonly double xMax;
public readonly double dxMin;
public readonly double dxMax;
public override double x(double time) {
return this.simulation.x(time).clamp(this.xMin, this.xMax);
}
public override double dx(double time) {
return this.simulation.dx(time).clamp(this.dxMin, this.dxMax);
}
public override bool isDone(double time) {
return this.simulation.isDone(time);
}
}
}

3
Assets/UIWidgets/physics/clamped_simulation.cs.meta


fileFormatVersion: 2
guid: e1b026c0820446e69cc5ec78354b9418
timeCreated: 1536564505

96
Assets/UIWidgets/physics/friction_simulation.cs


using System;
using UIWidgets.foundation;
using UIWidgets.ui;
namespace UIWidgets.physics {
public class FrictionSimulation : Simulation {
public FrictionSimulation(
double drag, double position, double velocity,
Tolerance tolerance = null
) : base(tolerance: tolerance) {
this._drag = drag;
this._dragLog = Math.Log(drag);
this._x = position;
this._v = velocity;
}
public static FrictionSimulation through(double startPosition, double endPosition, double startVelocity,
double endVelocity) {
D.assert(startVelocity == 0.0 || endVelocity == 0.0 || startVelocity.sign() == endVelocity.sign());
D.assert(startVelocity.abs() >= endVelocity.abs());
D.assert((endPosition - startPosition).sign() == startVelocity.sign());
return new FrictionSimulation(
FrictionSimulation._dragFor(startPosition, endPosition, startVelocity, endVelocity),
startPosition,
startVelocity,
tolerance: new Tolerance(velocity: endVelocity.abs())
);
}
readonly double _drag;
readonly double _dragLog;
readonly double _x;
readonly double _v;
static double _dragFor(double startPosition, double endPosition, double startVelocity, double endVelocity) {
return Math.Pow(Math.E, (startVelocity - endVelocity) / (startPosition - endPosition));
}
public override double x(double time) {
return this._x + this._v * Math.Pow(this._drag, time) / this._dragLog - this._v / this._dragLog;
}
public override double dx(double time) {
return this._v * Math.Pow(this._drag, time);
}
public double finalX {
get { return this._x - this._v / this._dragLog; }
}
public double timeAtX(double x) {
if (x == this._x) {
return 0.0;
}
if (this._v == 0.0 || (this._v > 0 ? (x < this._x || x > this.finalX) : (x > this._x || x < this.finalX))) {
return double.PositiveInfinity;
}
return Math.Log(this._dragLog * (x - this._x) / this._v + 1.0) / this._dragLog;
}
public override bool isDone(double time) {
return this.dx(time).abs() < this.tolerance.velocity;
}
}
public class BoundedFrictionSimulation : FrictionSimulation {
BoundedFrictionSimulation(
double drag,
double position,
double velocity,
double _minX,
double _maxX
) : base(drag, position, velocity) {
D.assert(position.clamp(_minX, _maxX) == position);
this._minX = _minX;
this._maxX = _maxX;
}
readonly double _minX;
readonly double _maxX;
public override double x(double time) {
return base.x(time).clamp(this._minX, this._maxX);
}
public override bool isDone(double time) {
return base.isDone(time) ||
(this.x(time) - this._minX).abs() < this.tolerance.distance ||
(this.x(time) - this._maxX).abs() < this.tolerance.distance;
}
}
}

3
Assets/UIWidgets/physics/friction_simulation.cs.meta


fileFormatVersion: 2
guid: 1d5938e637654fddb90f71c0d24bb3c2
timeCreated: 1536564777

36
Assets/UIWidgets/physics/gravity_simulation.cs


using UIWidgets.foundation;
using UIWidgets.ui;
namespace UIWidgets.physics {
public class GravitySimulation : Simulation {
public GravitySimulation(
double acceleration,
double distance,
double endDistance,
double velocity
) {
D.assert(endDistance >= 0);
this._a = acceleration;
this._x = distance;
this._v = velocity;
this._end = endDistance;
}
readonly double _x;
readonly double _v;
readonly double _a;
readonly double _end;
public override double x(double time) {
return this._x + this._v * time + 0.5 * this._a * time * time;
}
public override double dx(double time) {
return this._v + time * this._a;
}
public override bool isDone(double time) {
return this.x(time).abs() >= this._end;
}
}
}

3
Assets/UIWidgets/physics/gravity_simulation.cs.meta


fileFormatVersion: 2
guid: f56308b79b45489b9cac8421a70c6795
timeCreated: 1536566201

15
Assets/UIWidgets/physics/simulation.cs


namespace UIWidgets.physics {
public abstract class Simulation {
protected Simulation(Tolerance tolerance = null) {
this.tolerance = tolerance ?? Tolerance.defaultTolerance;
}
public abstract double x(double time);
public abstract double dx(double time);
public abstract bool isDone(double time);
public Tolerance tolerance;
}
}

11
Assets/UIWidgets/physics/simulation.cs.meta


fileFormatVersion: 2
guid: 2856e502c3bc04fd6b143e53f228f788
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

238
Assets/UIWidgets/physics/spring_simulation.cs


using System;
using UIWidgets.foundation;
namespace UIWidgets.physics {
public class SpringDescription {
public SpringDescription(
double mass,
double stiffness,
double damping
) {
this.mass = mass;
this.stiffness = stiffness;
this.damping = damping;
}
public static SpringDescription withDampingRatio(
double mass,
double stiffness,
double ratio = 1.0
) {
var damping = ratio * 2.0 * Math.Sqrt(mass * stiffness);
return new SpringDescription(mass, stiffness, damping);
}
public readonly double mass;
public readonly double stiffness;
public readonly double damping;
public override string ToString() {
return string.Format("{0}(mass {1:F1}, stiffness: {2:F1}, damping: {3:F1})",
this.GetType(), this.mass, this.stiffness, this.damping);
}
}
public enum SpringType {
criticallyDamped,
underDamped,
overDamped,
}
public class SpringSimulation : Simulation {
public SpringSimulation(
SpringDescription spring,
double start,
double end,
double velocity,
Tolerance tolerance = null
) : base(tolerance: tolerance) {
this._endPosition = end;
this._solution = _SpringSolution.create(spring, start - end, velocity);
}
protected readonly double _endPosition;
readonly _SpringSolution _solution;
public SpringType type {
get { return this._solution.type; }
}
public override double x(double time) {
return this._endPosition + this._solution.x(time);
}
public override double dx(double time) {
return this._solution.dx(time);
}
public override bool isDone(double time) {
return PhysicsUtils.nearZero(this._solution.x(time), this.tolerance.distance) &&
PhysicsUtils.nearZero(this._solution.dx(time), this.tolerance.velocity);
}
public override string ToString() {
return string.Format("{0}(end: {1}, {2}", this.GetType(), this._endPosition, this.type);
}
}
public class ScrollSpringSimulation : SpringSimulation {
public ScrollSpringSimulation(
SpringDescription spring,
double start,
double end,
double velocity,
Tolerance tolerance = null
) : base(spring, start, end, velocity, tolerance: tolerance) {
}
public double x(double time) {
return this.isDone(time) ? this._endPosition : base.x(time);
}
}
abstract class _SpringSolution {
public static _SpringSolution create(
SpringDescription spring,
double initialPosition,
double initialVelocity
) {
D.assert(spring != null);
double cmk = spring.damping * spring.damping - 4 * spring.mass * spring.stiffness;
if (cmk == 0.0) {
return _CriticalSolution.create(spring, initialPosition, initialVelocity);
}
if (cmk > 0.0) {
return _OverdampedSolution.create(spring, initialPosition, initialVelocity);
}
return _UnderdampedSolution.create(spring, initialPosition, initialVelocity);
}
public abstract double x(double time);
public abstract double dx(double time);
public abstract SpringType type { get; }
}
class _CriticalSolution : _SpringSolution {
internal static _CriticalSolution create(
SpringDescription spring,
double distance,
double velocity
) {
double r = -spring.damping / (2.0 * spring.mass);
double c1 = distance;
double c2 = velocity / (r * distance);
return new _CriticalSolution(r, c1, c2);
}
private _CriticalSolution(
double r, double c1, double c2
) {
this._r = r;
this._c1 = c1;
this._c2 = c2;
}
readonly double _r, _c1, _c2;
public override double x(double time) {
return (this._c1 + this._c2 * time) * Math.Pow(Math.E, this._r * time);
}
public override double dx(double time) {
double power = Math.Pow(Math.E, this._r * time);
return this._r * (this._c1 + this._c2 * time) * power + this._c2 * power;
}
public override SpringType type {
get { return SpringType.criticallyDamped; }
}
}
class _OverdampedSolution : _SpringSolution {
internal static _OverdampedSolution create(
SpringDescription spring,
double distance,
double velocity
) {
double cmk = spring.damping * spring.damping - 4 * spring.mass * spring.stiffness;
double r1 = (-spring.damping - Math.Sqrt(cmk)) / (2.0 * spring.mass);
double r2 = (-spring.damping + Math.Sqrt(cmk)) / (2.0 * spring.mass);
double c2 = (velocity - r1 * distance) / (r2 - r1);
double c1 = distance - c2;
return new _OverdampedSolution(r1, r2, c1, c2);
}
private _OverdampedSolution(
double r1, double r2, double c1, double c2
) {
this._r1 = r1;
this._r2 = r2;
this._c1 = c1;
this._c2 = c2;
}
readonly double _r1, _r2, _c1, _c2;
public override double x(double time) {
return this._c1 * Math.Pow(Math.E, this._r1 * time) +
this._c2 * Math.Pow(Math.E, this._r2 * time);
}
public override double dx(double time) {
return this._c1 * this._r1 * Math.Pow(Math.E, this._r1 * time) +
this._c2 * this._r2 * Math.Pow(Math.E, this._r2 * time);
}
public override SpringType type {
get { return SpringType.overDamped; }
}
}
class _UnderdampedSolution : _SpringSolution {
internal static _UnderdampedSolution create(
SpringDescription spring,
double distance,
double velocity
) {
double w = Math.Sqrt(4.0 * spring.mass * spring.stiffness -
spring.damping * spring.damping) / (2.0 * spring.mass);
double r = -(spring.damping / 2.0 * spring.mass);
double c1 = distance;
double c2 = (velocity - r * distance) / w;
return new _UnderdampedSolution(w, r, c1, c2);
}
private _UnderdampedSolution(
double w, double r, double c1, double c2
) {
this._w = w;
this._r = r;
this._c1 = c1;
this._c2 = c2;
}
readonly double _w, _r, _c1, _c2;
public override double x(double time) {
return Math.Pow(Math.E, this._r * time) *
(this._c1 * Math.Cos(this._w * time) + this._c2 * Math.Sin(this._w * time));
}
public override double dx(double time) {
double power = Math.Pow(Math.E, this._r * time);
double cosine = Math.Cos(this._w * time);
double sine = Math.Sin(this._w * time);
return power * (this._c2 * this._w * cosine - this._c1 * this._w * sine) +
this._r * power * (this._c2 * sine + this._c1 * cosine);
}
public override SpringType type {
get { return SpringType.underDamped; }
}
}
}

3
Assets/UIWidgets/physics/spring_simulation.cs.meta


fileFormatVersion: 2
guid: 8ed83c6d32a548d08ba79900e173dd53
timeCreated: 1536566442

28
Assets/UIWidgets/physics/tolerance.cs


namespace UIWidgets.physics {
public class Tolerance {
public Tolerance(
double distance = _epsilonDefault,
double time = _epsilonDefault,
double velocity = _epsilonDefault
) {
this.distance = distance;
this.time = time;
this.velocity = velocity;
}
const double _epsilonDefault = 1e-3;
public static readonly Tolerance defaultTolerance = new Tolerance();
public readonly double distance;
public readonly double time;
public readonly double velocity;
public override string ToString() {
return string.Format("Tolerance(distance: ±{0}, time: ±{1}, velocity: ±{2})",
this.distance, this.time, this.velocity);
}
}
}

3
Assets/UIWidgets/physics/tolerance.cs.meta


fileFormatVersion: 2
guid: 24153b761064430a8482c0388fd41746
timeCreated: 1536564124

14
Assets/UIWidgets/physics/utils.cs


using UIWidgets.foundation;
namespace UIWidgets.physics {
public class PhysicsUtils {
public static bool nearEqual(double a, double b, double epsilon) {
D.assert(epsilon >= 0.0);
return (a > (b - epsilon)) && (a < (b + epsilon)) || a == b;
}
public static bool nearZero(double a, double epsilon) {
return nearEqual(a, 0.0, epsilon);
}
}
}

3
Assets/UIWidgets/physics/utils.cs.meta


fileFormatVersion: 2
guid: e65d36fdcaac4e92a77c930023bfd62f
timeCreated: 1536570487
正在加载...
取消
保存