浏览代码

[Widget] Add List Wheel Viewport

/main
iizzaya 5 年前
当前提交
e49cc1d1
共有 7 个文件被更改,包括 1998 次插入3 次删除
  1. 412
      Runtime/painting/matrix_utils.cs
  2. 2
      Runtime/widgets/routes.cs
  3. 11
      Runtime/widgets/scroll_metrics.cs
  4. 751
      Runtime/rendering/list_wheel_viewport.cs
  5. 11
      Runtime/rendering/list_wheel_viewport.cs.meta
  6. 803
      Runtime/widgets/list_wheel_scroll_view.cs
  7. 11
      Runtime/widgets/list_wheel_scroll_view.cs.meta

412
Runtime/painting/matrix_utils.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using UnityEngine;
using Rect = Unity.UIWidgets.ui.Rect;
namespace Unity.UIWidgets.painting {
public static class MatrixUtils {

return result;
}
public static Vector3 perspectiveTransform(this Matrix4x4 m4, Vector3 arg) {
List<float> argStorage = new List<float> {arg[0], arg[1], arg[2]};
float x_ = (m4[0] * argStorage[0]) +
(m4[4] * argStorage[1]) +
(m4[8] * argStorage[2]) +
m4[12];
float y_ = (m4[1] * argStorage[0]) +
(m4[5] * argStorage[1]) +
(m4[9] * argStorage[2]) +
m4[13];
float z_ = (m4[2] * argStorage[0]) +
(m4[6] * argStorage[1]) +
(m4[10] * argStorage[2]) +
m4[14];
float w_ = 1.0f /
((m4[3] * argStorage[0]) +
(m4[7] * argStorage[1]) +
(m4[11] * argStorage[2]) +
m4[15]);
argStorage[0] = x_ * w_;
argStorage[1] = y_ * w_;
argStorage[2] = z_ * w_;
return arg;
}
public static Offset transformPoint(Matrix4x4 transform, Offset point) {
Vector3 position3 = new Vector3(point.dx, point.dy, 0.0f);
Vector3 transformed3 = transform.perspectiveTransform(position3);
return new Offset(transformed3.x, transformed3.y);
}
public static Rect transformRect(Matrix4x4 transform, Rect rect) {
Offset point1 = transformPoint(transform, rect.topLeft);
Offset point2 = transformPoint(transform, rect.topRight);
Offset point3 = transformPoint(transform, rect.bottomLeft);
Offset point4 = transformPoint(transform, rect.bottomRight);
return Rect.fromLTRB(
_min4(point1.dx, point2.dx, point3.dx, point4.dx),
_min4(point1.dy, point2.dy, point3.dy, point4.dy),
_max4(point1.dx, point2.dx, point3.dx, point4.dx),
_max4(point1.dy, point2.dy, point3.dy, point4.dy)
);
}
static float _min4(float a, float b, float c, float d) {
return Mathf.Min(a, Mathf.Min(b, Mathf.Min(c, d)));
}
static float _max4(float a, float b, float c, float d) {
return Mathf.Max(a, Mathf.Max(b, Mathf.Max(c, d)));
}
public static Matrix4x4 toMatrix4x4(this Matrix3 matrix3) {
var matrix = Matrix4x4.identity;

return matrix;
}
public static Matrix4x4 createCylindricalProjectionTransform(
float radius,
float angle,
float perspective = 0.001f,
Axis orientation = Axis.vertical
) {
D.assert(perspective >= 0 && perspective <= 1.0);
Matrix4x4 result = Matrix4x4.identity;
result[3, 2] = -perspective;
result[2, 3] = -radius;
result[3, 3] = perspective * radius + 1.0f;
result *= (
orientation == Axis.horizontal
? rotationY(angle)
: rotationX(angle)
) * translationValues(0.0f, 0.0f, radius);
return result;
}
public static Matrix4x4 rotationY(float angle) {
var matrix = Matrix4x4.zero;
matrix[15] = 1.0f;
float c = Mathf.Cos(angle);
float s = Mathf.Sin(angle);
matrix[0] = c;
matrix[1] = 0.0f;
matrix[2] = -s;
matrix[4] = 0.0f;
matrix[5] = 1.0f;
matrix[6] = 0.0f;
matrix[8] = s;
matrix[9] = 0.0f;
matrix[10] = c;
matrix[3] = 0.0f;
matrix[7] = 0.0f;
matrix[11] = 0.0f;
return matrix;
}
public static Matrix4x4 rotationX(float angle) {
var matrix = Matrix4x4.zero;
matrix[15] = 1.0f;
float c = Mathf.Cos(angle);
float s = Mathf.Sin(angle);
matrix[0] = 1.0f;
matrix[1] = 0.0f;
matrix[2] = 0.0f;
matrix[4] = 0.0f;
matrix[5] = c;
matrix[6] = s;
matrix[8] = 0.0f;
matrix[9] = -s;
matrix[10] = c;
matrix[3] = 0.0f;
matrix[7] = 0.0f;
matrix[11] = 0.0f;
return matrix;
}
public static Matrix4x4 translationValues(float x, float y, float z) {
var matrix = Matrix4x4.identity;
matrix[14] = z;
matrix[13] = y;
matrix[12] = x;
return matrix;
}
public static Matrix4x4 translate(this Matrix4x4 m, Vector4 x, float y = 0.0f, float z = 0.0f) {
float tw = x.w;
float tx = x.x;
float ty = x.y;
float tz = x.z;
float t1 = m[0] * tx + m[4] * ty + m[8] * tz + m[12] * tw;
float t2 = m[1] * tx + m[5] * ty + m[9] * tz + m[13] * tw;
float t3 = m[2] * tx + m[6] * ty + m[10] * tz + m[14] * tw;
float t4 = m[3] * tx + m[7] * ty + m[11] * tz + m[15] * tw;
m[12] = t1;
m[13] = t2;
m[14] = t3;
m[15] = t4;
return m;
}
public static Matrix4x4 translate(this Matrix4x4 m, Vector3 x, float y = 0.0f, float z = 0.0f) {
float tw = 1.0f;
float tx = x.x;
float ty = x.y;
float tz = x.z;
float t1 = m[0] * tx + m[4] * ty + m[8] * tz + m[12] * tw;
float t2 = m[1] * tx + m[5] * ty + m[9] * tz + m[13] * tw;
float t3 = m[2] * tx + m[6] * ty + m[10] * tz + m[14] * tw;
float t4 = m[3] * tx + m[7] * ty + m[11] * tz + m[15] * tw;
m[12] = t1;
m[13] = t2;
m[14] = t3;
m[15] = t4;
return m;
}
public static Matrix4x4 translate(this Matrix4x4 m, float x, float y = 0.0f, float z = 0.0f) {
float tw = 1.0f;
float tx = x;
float ty = y;
float tz = z;
float t1 = m[0] * tx + m[4] * ty + m[8] * tz + m[12] * tw;
float t2 = m[1] * tx + m[5] * ty + m[9] * tz + m[13] * tw;
float t3 = m[2] * tx + m[6] * ty + m[10] * tz + m[14] * tw;
float t4 = m[3] * tx + m[7] * ty + m[11] * tz + m[15] * tw;
m[12] = t1;
m[13] = t2;
m[14] = t3;
m[15] = t4;
return m;
}
public static void multiply(this Matrix3 m, Matrix3 arg) {
float m00 = m[0];
float m01 = m[3];
float m02 = m[6];
float m10 = m[1];
float m11 = m[4];
float m12 = m[7];
float m20 = m[2];
float m21 = m[5];
float m22 = m[8];
List<float> argStorage = new List<float> {
arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7], arg[8]
};
float n00 = argStorage[0];
float n01 = argStorage[3];
float n02 = argStorage[6];
float n10 = argStorage[1];
float n11 = argStorage[4];
float n12 = argStorage[7];
float n20 = argStorage[2];
float n21 = argStorage[5];
float n22 = argStorage[8];
m[0] = (m00 * n00) + (m01 * n10) + (m02 * n20);
m[3] = (m00 * n01) + (m01 * n11) + (m02 * n21);
m[6] = (m00 * n02) + (m01 * n12) + (m02 * n22);
m[1] = (m10 * n00) + (m11 * n10) + (m12 * n20);
m[4] = (m10 * n01) + (m11 * n11) + (m12 * n21);
m[7] = (m10 * n02) + (m11 * n12) + (m12 * n22);
m[2] = (m20 * n00) + (m21 * n10) + (m22 * n20);
m[5] = (m20 * n01) + (m21 * n11) + (m22 * n21);
m[8] = (m20 * n02) + (m21 * n12) + (m22 * n22);
}
public static void multiply(this Matrix4x4 m, Matrix4x4 arg) {
float m00 = m[0];
float m01 = m[4];
float m02 = m[8];
float m03 = m[12];
float m10 = m[1];
float m11 = m[5];
float m12 = m[9];
float m13 = m[13];
float m20 = m[2];
float m21 = m[6];
float m22 = m[10];
float m23 = m[14];
float m30 = m[3];
float m31 = m[7];
float m32 = m[11];
float m33 = m[15];
List<float> argStorage = new List<float> {
arg.m00, arg.m01, arg.m02, arg.m03,
arg.m10, arg.m11, arg.m12, arg.m13,
arg.m20, arg.m21, arg.m22, arg.m23,
arg.m30, arg.m31, arg.m32, arg.m33,
};
float n00 = argStorage[0];
float n01 = argStorage[4];
float n02 = argStorage[8];
float n03 = argStorage[12];
float n10 = argStorage[1];
float n11 = argStorage[5];
float n12 = argStorage[9];
float n13 = argStorage[13];
float n20 = argStorage[2];
float n21 = argStorage[6];
float n22 = argStorage[10];
float n23 = argStorage[14];
float n30 = argStorage[3];
float n31 = argStorage[7];
float n32 = argStorage[11];
float n33 = argStorage[15];
m[0] = (m00 * n00) + (m01 * n10) + (m02 * n20) + (m03 * n30);
m[4] = (m00 * n01) + (m01 * n11) + (m02 * n21) + (m03 * n31);
m[8] = (m00 * n02) + (m01 * n12) + (m02 * n22) + (m03 * n32);
m[12] = (m00 * n03) + (m01 * n13) + (m02 * n23) + (m03 * n33);
m[1] = (m10 * n00) + (m11 * n10) + (m12 * n20) + (m13 * n30);
m[5] = (m10 * n01) + (m11 * n11) + (m12 * n21) + (m13 * n31);
m[9] = (m10 * n02) + (m11 * n12) + (m12 * n22) + (m13 * n32);
m[13] = (m10 * n03) + (m11 * n13) + (m12 * n23) + (m13 * n33);
m[2] = (m20 * n00) + (m21 * n10) + (m22 * n20) + (m23 * n30);
m[6] = (m20 * n01) + (m21 * n11) + (m22 * n21) + (m23 * n31);
m[10] = (m20 * n02) + (m21 * n12) + (m22 * n22) + (m23 * n32);
m[14] = (m20 * n03) + (m21 * n13) + (m22 * n23) + (m23 * n33);
m[3] = (m30 * n00) + (m31 * n10) + (m32 * n20) + (m33 * n30);
m[7] = (m30 * n01) + (m31 * n11) + (m32 * n21) + (m33 * n31);
m[11] = (m30 * n02) + (m31 * n12) + (m32 * n22) + (m33 * n32);
m[15] = (m30 * n03) + (m31 * n13) + (m32 * n23) + (m33 * n33);
}
public static void scale(this Matrix4x4 m, object x, float? y = null, float? z = null) {
float sx = 0f;
float sy = 0f;
float sz = 0f;
float sw = x is Vector4 _xv4 ? _xv4.w : 1.0f;
if (x is Vector3 xv3) {
sx = xv3.x;
sy = xv3.y;
sz = xv3.z;
}
else if (x is Vector4 xv4) {
sx = xv4.x;
sy = xv4.y;
sz = xv4.z;
}
else if (x is float xf) {
sx = xf;
sy = y ?? xf;
sz = z ?? xf;
}
m[0] *= sx;
m[1] *= sx;
m[2] *= sx;
m[3] *= sx;
m[4] *= sy;
m[5] *= sy;
m[6] *= sy;
m[7] *= sy;
m[8] *= sz;
m[9] *= sz;
m[10] *= sz;
m[11] *= sz;
m[12] *= sw;
m[13] *= sw;
m[14] *= sw;
m[15] *= sw;
}
public static Rect inverseTransformRect(Matrix4x4 transform, Rect rect) {
D.assert(rect != null);
D.assert(transform.determinant != 0.0);
if (transform == Matrix4x4.identity)
return rect;
var copy = transform;
copy.invert();
transform = copy;
return transformRect(transform, rect);
}
public static float invert(this Matrix4x4 m) {
return m.copyInverse();
}
public static float copyInverse(this Matrix4x4 m) {
List<float> argStorage = new List<float> {
m.m00, m.m01, m.m02, m.m03,
m.m10, m.m11, m.m12, m.m13,
m.m20, m.m21, m.m22, m.m23,
m.m30, m.m31, m.m32, m.m33,
};
float a00 = argStorage[0];
float a01 = argStorage[1];
float a02 = argStorage[2];
float a03 = argStorage[3];
float a10 = argStorage[4];
float a11 = argStorage[5];
float a12 = argStorage[6];
float a13 = argStorage[7];
float a20 = argStorage[8];
float a21 = argStorage[9];
float a22 = argStorage[10];
float a23 = argStorage[11];
float a30 = argStorage[12];
float a31 = argStorage[13];
float a32 = argStorage[14];
float a33 = argStorage[15];
float b00 = a00 * a11 - a01 * a10;
float b01 = a00 * a12 - a02 * a10;
float b02 = a00 * a13 - a03 * a10;
float b03 = a01 * a12 - a02 * a11;
float b04 = a01 * a13 - a03 * a11;
float b05 = a02 * a13 - a03 * a12;
float b06 = a20 * a31 - a21 * a30;
float b07 = a20 * a32 - a22 * a30;
float b08 = a20 * a33 - a23 * a30;
float b09 = a21 * a32 - a22 * a31;
float b10 = a21 * a33 - a23 * a31;
float b11 = a22 * a33 - a23 * a32;
float det =
(b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06);
if (det == 0.0f) {
m[0] = argStorage[0];
m[1] = argStorage[1];
m[2] = argStorage[2];
m[3] = argStorage[3];
m[4] = argStorage[4];
m[5] = argStorage[5];
m[6] = argStorage[6];
m[7] = argStorage[7];
m[8] = argStorage[8];
m[9] = argStorage[9];
m[10] = argStorage[10];
m[11] = argStorage[11];
m[12] = argStorage[12];
m[13] = argStorage[13];
m[14] = argStorage[14];
m[15] = argStorage[15];
return 0.0f;
}
float invDet = 1.0f / det;
m[0] = (a11 * b11 - a12 * b10 + a13 * b09) * invDet;
m[1] = (-a01 * b11 + a02 * b10 - a03 * b09) * invDet;
m[2] = (a31 * b05 - a32 * b04 + a33 * b03) * invDet;
m[3] = (-a21 * b05 + a22 * b04 - a23 * b03) * invDet;
m[4] = (-a10 * b11 + a12 * b08 - a13 * b07) * invDet;
m[5] = (a00 * b11 - a02 * b08 + a03 * b07) * invDet;
m[6] = (-a30 * b05 + a32 * b02 - a33 * b01) * invDet;
m[7] = (a20 * b05 - a22 * b02 + a23 * b01) * invDet;
m[8] = (a10 * b10 - a11 * b08 + a13 * b06) * invDet;
m[9] = (-a00 * b10 + a01 * b08 - a03 * b06) * invDet;
m[10] = (a30 * b04 - a31 * b02 + a33 * b00) * invDet;
m[11] = (-a20 * b04 + a21 * b02 - a23 * b00) * invDet;
m[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet;
m[13] = (a00 * b09 - a01 * b07 + a02 * b06) * invDet;
m[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet;
m[15] = (a20 * b03 - a21 * b01 + a22 * b00) * invDet;
return det;
}
public static Matrix3 toMatrix3(this Matrix4x4 m) {
var m3 = Matrix3.I();
m3[0] = m[0];
m3[1] = m[1];
m3[2] = m[2];
m3[3] = m[3];
m3[4] = m[4];
m3[5] = m[5];
m3[6] = m[6];
m3[7] = m[7];
m3[8] = m[8];
return m3;
}
}
public class TransformProperty : DiagnosticsProperty<Matrix3> {

DiagnosticLevel level = DiagnosticLevel.info
) : base(name, value, showName: showName, defaultValue: defaultValue ?? Diagnostics.kNoDefaultValue,
level: level) {
}
level: level) { }
protected override string valueToString(TextTreeConfiguration parentConfiguration = null) {
if (parentConfiguration != null && !parentConfiguration.lineBreakProperties) {

2
Runtime/widgets/routes.cs


this._willPopCallbacks.Remove(callback);
}
protected internal bool hasScopedWillPopCallback {
public bool hasScopedWillPopCallback {
get { return this._willPopCallbacks.isNotEmpty(); }
}

11
Runtime/widgets/scroll_metrics.cs


);
}
if (it is IFixedExtentMetrics) {
return new FixedExtentMetrics(
minScrollExtent: minScrollExtent ?? it.minScrollExtent,
maxScrollExtent: maxScrollExtent ?? it.maxScrollExtent,
pixels: pixels ?? it.pixels,
viewportDimension: viewportDimension ?? it.viewportDimension,
axisDirection: axisDirection ?? it.axisDirection,
itemIndex: ((IFixedExtentMetrics) it).itemIndex
);
}
return new FixedScrollMetrics(
minScrollExtent: minScrollExtent ?? it.minScrollExtent,
maxScrollExtent: maxScrollExtent ?? it.maxScrollExtent,

751
Runtime/rendering/list_wheel_viewport.cs


using System;
using System.Collections.Generic;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using UnityEngine;
using Rect = Unity.UIWidgets.ui.Rect;
namespace Unity.UIWidgets.rendering {
delegate float ___ChildSizingFunction(RenderBox child);
public interface IListWheelChildManager {
int? childCount { get; }
bool childExistsAt(int index);
void createChild(int index, RenderBox after);
void removeChild(RenderBox child);
}
public class ListWheelParentData : ContainerBoxParentData<RenderBox> {
public int index;
}
public class RenderListWheelViewport : ContainerRenderObjectMixinRenderBox<RenderBox, ListWheelParentData>,
RenderAbstractViewport {
public RenderListWheelViewport(
IListWheelChildManager childManager,
ViewportOffset offset,
float itemExtent,
float diameterRatio = defaultDiameterRatio,
float perspective = defaultPerspective,
float offAxisFraction = 0.0f,
bool useMagnifier = false,
float magnification = 1.0f,
bool clipToSize = true,
bool renderChildrenOutsideViewport = false,
List<RenderBox> children = null
) {
D.assert(childManager != null);
D.assert(offset != null);
D.assert(diameterRatio > 0, () => diameterRatioZeroMessage);
D.assert(perspective > 0);
D.assert(perspective <= 0.01f, () => perspectiveTooHighMessage);
D.assert(magnification > 0);
D.assert(itemExtent > 0);
D.assert(
!renderChildrenOutsideViewport || !clipToSize,
() => clipToSizeAndRenderChildrenOutsideViewportConflict
);
this.childManager = childManager;
this._offset = offset;
this._diameterRatio = diameterRatio;
this._perspective = perspective;
this._offAxisFraction = offAxisFraction;
this._useMagnifier = useMagnifier;
this._magnification = magnification;
this._itemExtent = itemExtent;
this._clipToSize = clipToSize;
this._renderChildrenOutsideViewport = renderChildrenOutsideViewport;
this.addAll(children);
}
public const float defaultDiameterRatio = 2.0f;
public const float defaultPerspective = 0.003f;
public const string diameterRatioZeroMessage = "You can't set a diameterRatio " +
"of 0 or of a negative number. It would imply a cylinder of 0 in diameter " +
"in which case nothing will be drawn.";
public const string perspectiveTooHighMessage = "A perspective too high will " +
"be clipped in the z-axis and therefore not renderable. Value must be " +
"between 0 and 0.0f1.";
public const string clipToSizeAndRenderChildrenOutsideViewportConflict =
"Cannot renderChildrenOutsideViewport and clipToSize since children " +
"rendered outside will be clipped anyway.";
public readonly IListWheelChildManager childManager;
public ViewportOffset offset {
get { return this._offset; }
set {
D.assert(value != null);
if (value == this._offset) {
return;
}
if (this.attached) {
this._offset.removeListener(this._hasScrolled);
}
this._offset = value;
if (this.attached) {
this._offset.addListener(this._hasScrolled);
}
this.markNeedsLayout();
}
}
ViewportOffset _offset;
public float diameterRatio {
get { return this._diameterRatio; }
set {
D.assert(
value > 0,
() => diameterRatioZeroMessage
);
this._diameterRatio = value;
this.markNeedsPaint();
}
}
float _diameterRatio;
public float perspective {
get { return this._perspective; }
set {
D.assert(value > 0);
D.assert(
value <= 0.01f,
() => perspectiveTooHighMessage
);
if (value == this._perspective) {
return;
}
this._perspective = value;
this.markNeedsPaint();
}
}
float _perspective;
public float offAxisFraction {
get { return this._offAxisFraction; }
set {
if (value == this._offAxisFraction) {
return;
}
this._offAxisFraction = value;
this.markNeedsPaint();
}
}
float _offAxisFraction = 0.0f;
public bool useMagnifier {
get { return this._useMagnifier; }
set {
if (value == this._useMagnifier) {
return;
}
this._useMagnifier = value;
this.markNeedsPaint();
}
}
bool _useMagnifier = false;
public float magnification {
get { return this._magnification; }
set {
D.assert(value > 0);
if (value == this._magnification) {
return;
}
this._magnification = value;
this.markNeedsPaint();
}
}
float _magnification = 1.0f;
public float itemExtent {
get { return this._itemExtent; }
set {
D.assert(value > 0);
if (value == this._itemExtent) {
return;
}
this._itemExtent = value;
this.markNeedsLayout();
}
}
float _itemExtent;
public bool clipToSize {
get { return this._clipToSize; }
set {
D.assert(
!this.renderChildrenOutsideViewport || !this.clipToSize,
() => clipToSizeAndRenderChildrenOutsideViewportConflict
);
if (value == this._clipToSize) {
return;
}
this._clipToSize = value;
this.markNeedsPaint();
}
}
bool _clipToSize;
public bool renderChildrenOutsideViewport {
get { return this._renderChildrenOutsideViewport; }
set {
D.assert(
!this.renderChildrenOutsideViewport || !this.clipToSize,
() => clipToSizeAndRenderChildrenOutsideViewportConflict
);
if (value == this._renderChildrenOutsideViewport) {
return;
}
this._renderChildrenOutsideViewport = value;
this.markNeedsLayout();
}
}
bool _renderChildrenOutsideViewport;
void _hasScrolled() {
this.markNeedsLayout();
}
public override void setupParentData(RenderObject child) {
if (!(child.parentData is ListWheelParentData)) {
child.parentData = new ListWheelParentData();
}
}
public override void attach(object owner) {
base.attach(owner);
this._offset.addListener(this._hasScrolled);
}
public override void detach() {
this._offset.removeListener(this._hasScrolled);
base.detach();
}
public override bool isRepaintBoundary {
get { return true; }
}
float _viewportExtent {
get {
D.assert(this.hasSize);
return this.size.height;
}
}
float _minEstimatedScrollExtent {
get {
D.assert(this.hasSize);
if (this.childManager.childCount == null) {
return float.NegativeInfinity;
}
return 0.0f;
}
}
float _maxEstimatedScrollExtent {
get {
D.assert(this.hasSize);
if (this.childManager.childCount == null) {
return float.PositiveInfinity;
}
return Mathf.Max(0.0f, ((this.childManager.childCount ?? 0) - 1) * this._itemExtent);
}
}
float _topScrollMarginExtent {
get {
D.assert(this.hasSize);
return -this.size.height / 2.0f + this._itemExtent / 2.0f;
}
}
float _getUntransformedPaintingCoordinateY(float layoutCoordinateY) {
return layoutCoordinateY - this._topScrollMarginExtent - this.offset.pixels;
}
float _maxVisibleRadian {
get {
if (this._diameterRatio < 1.0f) {
return Mathf.PI / 2.0f;
}
return Mathf.Asin(1.0f / this._diameterRatio);
}
}
float _getIntrinsicCrossAxis(___ChildSizingFunction childSize) {
float extent = 0.0f;
RenderBox child = this.firstChild;
while (child != null) {
extent = Mathf.Max(extent, childSize(child));
child = this.childAfter(child);
}
return extent;
}
protected override float computeMinIntrinsicWidth(float height) {
return this._getIntrinsicCrossAxis(
(RenderBox child) => child.getMinIntrinsicWidth(height)
);
}
protected override float computeMaxIntrinsicWidth(float height) {
return this._getIntrinsicCrossAxis(
(RenderBox child) => child.getMaxIntrinsicWidth(height)
);
}
protected override float computeMinIntrinsicHeight(float width) {
if (this.childManager.childCount == null) {
return 0.0f;
}
return (this.childManager.childCount ?? 0) * this._itemExtent;
}
protected internal override float computeMaxIntrinsicHeight(float width) {
if (this.childManager.childCount == null) {
return 0.0f;
}
return (this.childManager.childCount ?? 0) * this._itemExtent;
}
protected override bool sizedByParent {
get { return true; }
}
protected override void performResize() {
this.size = this.constraints.biggest;
}
public int indexOf(RenderBox child) {
D.assert(child != null);
ListWheelParentData childParentData = (ListWheelParentData) child.parentData;
return childParentData.index;
}
public int scrollOffsetToIndex(float scrollOffset) {
return (scrollOffset / this.itemExtent).floor();
}
public float indexToScrollOffset(int index) {
return index * this.itemExtent;
}
void _createChild(int index,
RenderBox after = null
) {
this.invokeLayoutCallback<BoxConstraints>((BoxConstraints constraints) => {
D.assert(this.constraints == this.constraints);
this.childManager.createChild(index, after: after);
});
}
void _destroyChild(RenderBox child) {
this.invokeLayoutCallback<BoxConstraints>((BoxConstraints constraints) => {
D.assert(this.constraints == this.constraints);
this.childManager.removeChild(child);
});
}
void _layoutChild(RenderBox child, BoxConstraints constraints, int index) {
child.layout(constraints, parentUsesSize: true);
ListWheelParentData childParentData = (ListWheelParentData) child.parentData;
float crossPosition = this.size.width / 2.0f - child.size.width / 2.0f;
childParentData.offset = new Offset(crossPosition, this.indexToScrollOffset(index));
}
protected override void performLayout() {
BoxConstraints childConstraints = this.constraints.copyWith(
minHeight: this._itemExtent,
maxHeight: this._itemExtent,
minWidth: 0.0f
);
float visibleHeight = this.size.height;
if (this.renderChildrenOutsideViewport) {
visibleHeight *= 2;
}
float firstVisibleOffset = this.offset.pixels + this._itemExtent / 2 - visibleHeight / 2;
float lastVisibleOffset = firstVisibleOffset + visibleHeight;
int targetFirstIndex = this.scrollOffsetToIndex(firstVisibleOffset);
int targetLastIndex = this.scrollOffsetToIndex(lastVisibleOffset);
if (targetLastIndex * this._itemExtent == lastVisibleOffset) {
targetLastIndex--;
}
while (!this.childManager.childExistsAt(targetFirstIndex) && targetFirstIndex <= targetLastIndex) {
targetFirstIndex++;
}
while (!this.childManager.childExistsAt(targetLastIndex) && targetFirstIndex <= targetLastIndex) {
targetLastIndex--;
}
if (targetFirstIndex > targetLastIndex) {
while (this.firstChild != null) {
this._destroyChild(this.firstChild);
}
return;
}
if (this.childCount > 0 &&
(this.indexOf(this.firstChild) > targetLastIndex || this.indexOf(this.lastChild) < targetFirstIndex)) {
while (this.firstChild != null) {
this._destroyChild(this.firstChild);
}
}
if (this.childCount == 0) {
this._createChild(targetFirstIndex);
this._layoutChild(this.firstChild, childConstraints, targetFirstIndex);
}
int currentFirstIndex = this.indexOf(this.firstChild);
int currentLastIndex = this.indexOf(this.lastChild);
while (currentFirstIndex < targetFirstIndex) {
this._destroyChild(this.firstChild);
currentFirstIndex++;
}
while (currentLastIndex > targetLastIndex) {
this._destroyChild(this.lastChild);
currentLastIndex--;
}
RenderBox child = this.firstChild;
while (child != null) {
child.layout(childConstraints, parentUsesSize: true);
child = this.childAfter(child);
}
while (currentFirstIndex > targetFirstIndex) {
this._createChild(currentFirstIndex - 1);
this._layoutChild(this.firstChild, childConstraints, --currentFirstIndex);
}
while (currentLastIndex < targetLastIndex) {
this._createChild(currentLastIndex + 1, after: this.lastChild);
this._layoutChild(this.lastChild, childConstraints, ++currentLastIndex);
}
this.offset.applyViewportDimension(this._viewportExtent);
float minScrollExtent = this.childManager.childExistsAt(targetFirstIndex - 1)
? this._minEstimatedScrollExtent
: this.indexToScrollOffset(targetFirstIndex);
float maxScrollExtent = this.childManager.childExistsAt(targetLastIndex + 1)
? this._maxEstimatedScrollExtent
: this.indexToScrollOffset(targetLastIndex);
this.offset.applyContentDimensions(minScrollExtent, maxScrollExtent);
}
bool _shouldClipAtCurrentOffset() {
float highestUntransformedPaintY = this._getUntransformedPaintingCoordinateY(0.0f);
return highestUntransformedPaintY < 0.0f
|| this.size.height < highestUntransformedPaintY + this._maxEstimatedScrollExtent + this._itemExtent;
}
public override void paint(PaintingContext context, Offset offset) {
if (this.childCount > 0) {
if (this._clipToSize && this._shouldClipAtCurrentOffset()) {
context.pushClipRect(
this.needsCompositing,
offset,
Offset.zero & this.size, this._paintVisibleChildren
);
}
else {
this._paintVisibleChildren(context, offset);
}
}
}
void _paintVisibleChildren(PaintingContext context, Offset offset) {
RenderBox childToPaint = this.firstChild;
ListWheelParentData childParentData = (ListWheelParentData) childToPaint?.parentData;
while (childParentData != null) {
this._paintTransformedChild(childToPaint, context, offset, childParentData.offset);
childToPaint = this.childAfter(childToPaint);
childParentData = (ListWheelParentData) childToPaint?.parentData;
}
}
void _paintTransformedChild(RenderBox child, PaintingContext context, Offset offset, Offset layoutOffset) {
Offset untransformedPaintingCoordinates = offset + new Offset(
layoutOffset.dx,
this._getUntransformedPaintingCoordinateY(layoutOffset.dy)
);
float fractionalY = (untransformedPaintingCoordinates.dy + this._itemExtent / 2.0f) / this.size.height;
float angle = -(fractionalY - 0.5f) * 2.0f * this._maxVisibleRadian;
if (angle > Mathf.PI / 2.0f || angle < -Mathf.PI / 2.0f) {
return;
}
var radius = this.size.height * this._diameterRatio / 2.0f;
var deltaY = radius * Mathf.Sin(angle);
Matrix3 transform = Matrix3.I();
// Matrix4x4 transform2 = MatrixUtils.createCylindricalProjectionTransform(
// radius: this.size.height * this._diameterRatio / 2.0f,
// angle: angle,
// perspective: this._perspective
// );
// Offset offsetToCenter = new Offset(untransformedPaintingCoordinates.dx, -this._topScrollMarginExtent);
Offset offsetToCenter =
new Offset(untransformedPaintingCoordinates.dx, -deltaY - this._topScrollMarginExtent);
if (!this.useMagnifier) {
this._paintChildCylindrically(context, offset, child, transform, offsetToCenter);
}
else {
this._paintChildWithMagnifier(
context,
offset,
child,
transform,
offsetToCenter,
untransformedPaintingCoordinates
);
}
}
void _paintChildWithMagnifier(
PaintingContext context,
Offset offset,
RenderBox child,
// Matrix4x4 cylindricalTransform,
Matrix3 cylindricalTransform,
Offset offsetToCenter,
Offset untransformedPaintingCoordinates
) {
float magnifierTopLinePosition = this.size.height / 2 - this._itemExtent * this._magnification / 2;
float magnifierBottomLinePosition = this.size.height / 2 + this._itemExtent * this._magnification / 2;
bool isAfterMagnifierTopLine = untransformedPaintingCoordinates.dy
>= magnifierTopLinePosition - this._itemExtent * this._magnification;
bool isBeforeMagnifierBottomLine = untransformedPaintingCoordinates.dy
<= magnifierBottomLinePosition;
if (isAfterMagnifierTopLine && isBeforeMagnifierBottomLine) {
Rect centerRect = Rect.fromLTWH(
0.0f,
magnifierTopLinePosition, this.size.width, this._itemExtent * this._magnification);
Rect topHalfRect = Rect.fromLTWH(
0.0f,
0.0f, this.size.width,
magnifierTopLinePosition);
Rect bottomHalfRect = Rect.fromLTWH(
0.0f,
magnifierBottomLinePosition, this.size.width,
magnifierTopLinePosition);
context.pushClipRect(
false,
offset,
centerRect,
(PaintingContext context1, Offset offset1) => {
context1.pushTransform(
false,
offset1,
cylindricalTransform,
// this._centerOriginTransform(cylindricalTransform),
(PaintingContext context2, Offset offset2) => {
context2.paintChild(
child,
offset2 + untransformedPaintingCoordinates);
});
});
context.pushClipRect(
false,
offset,
untransformedPaintingCoordinates.dy <= magnifierTopLinePosition
? topHalfRect
: bottomHalfRect,
(PaintingContext context1, Offset offset1) => {
this._paintChildCylindrically(
context1,
offset1,
child,
cylindricalTransform,
offsetToCenter
);
}
);
}
else {
this._paintChildCylindrically(
context,
offset,
child,
cylindricalTransform,
offsetToCenter
);
}
}
void _paintChildCylindrically(
PaintingContext context,
Offset offset,
RenderBox child,
// Matrix4x4 cylindricalTransform,
Matrix3 cylindricalTransform,
Offset offsetToCenter
) {
context.pushTransform(
false,
offset,
cylindricalTransform,
// this._centerOriginTransform(cylindricalTransform),
(PaintingContext _context, Offset _offset) => { _context.paintChild(child, _offset + offsetToCenter); }
);
}
Matrix4x4 _magnifyTransform() {
Matrix4x4 magnify = Matrix4x4.identity;
magnify.translate(this.size.width * (-this._offAxisFraction + 0.5f), this.size.height / 2f);
magnify.scale(this._magnification, this._magnification, this._magnification);
magnify.translate(-this.size.width * (-this._offAxisFraction + 0.5f), -this.size.height / 2f);
return magnify;
}
Matrix3 _centerOriginTransform(Matrix3 originalMatrix) {
Matrix3 result = Matrix3.I();
Offset centerOriginTranslation = Alignment.center.alongSize(this.size);
result.setTranslate(centerOriginTranslation.dx * (-this._offAxisFraction * 2 + 1),
centerOriginTranslation.dy);
result.multiply(originalMatrix);
result.setTranslate(-centerOriginTranslation.dx * (-this._offAxisFraction * 2 + 1),
-centerOriginTranslation.dy);
return result;
}
Matrix4x4 _centerOriginTransform(Matrix4x4 originalMatrix) {
Matrix4x4 result = Matrix4x4.identity;
Offset centerOriginTranslation = Alignment.center.alongSize(this.size);
result.translate(centerOriginTranslation.dx * (-this._offAxisFraction * 2 + 1),
centerOriginTranslation.dy);
result.multiply(originalMatrix);
result.translate(-centerOriginTranslation.dx * (-this._offAxisFraction * 2 + 1),
-centerOriginTranslation.dy);
return result;
}
public void applyPaintTransform(RenderBox child, Matrix4x4 transform) {
ListWheelParentData parentData = (ListWheelParentData) child?.parentData;
transform.translate(0.0f, this._getUntransformedPaintingCoordinateY(parentData.offset.dy));
}
public override Rect describeApproximatePaintClip(RenderObject child) {
if (child != null && this._shouldClipAtCurrentOffset()) {
return Offset.zero & this.size;
}
return null;
}
protected override bool hitTestChildren(HitTestResult result, Offset position = null
) {
return false;
}
public RevealedOffset getOffsetToReveal(RenderObject target, float alignment,
Rect rect = null
) {
rect = rect ?? target.paintBounds;
RenderObject child = target;
while (child.parent != this) {
child = (RenderObject) child.parent;
}
ListWheelParentData parentData = (ListWheelParentData) child.parentData;
float targetOffset = parentData.offset.dy;
Matrix4x4 transform = target.getTransformTo(this).toMatrix4x4();
Rect bounds = MatrixUtils.transformRect(transform, rect);
Rect targetRect = bounds.translate(0.0f, (this.size.height - this.itemExtent) / 2);
return new RevealedOffset(offset: targetOffset, rect: targetRect);
}
public new RenderObject parent {
get { return (RenderObject) base.parent; }
}
public new void showOnScreen(
RenderObject descendant = null,
Rect rect = null,
TimeSpan? duration = null,
Curve curve = null
) {
duration = duration ?? TimeSpan.Zero;
curve = curve ?? Curves.ease;
if (descendant != null) {
RevealedOffset revealedOffset = this.getOffsetToReveal(descendant, 0.5f, rect: rect);
if (duration == TimeSpan.Zero) {
this.offset.jumpTo(revealedOffset.offset);
}
else {
this.offset.animateTo(revealedOffset.offset, duration: (TimeSpan) duration, curve: curve);
}
rect = revealedOffset.rect;
}
base.showOnScreen(
rect: rect,
duration: duration,
curve: curve
);
}
}
}

11
Runtime/rendering/list_wheel_viewport.cs.meta


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

803
Runtime/widgets/list_wheel_scroll_view.cs


using System;
using System.Collections.Generic;
using RSG;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.physics;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.scheduler;
using Unity.UIWidgets.ui;
using UnityEngine;
namespace Unity.UIWidgets.widgets {
public interface ListWheelChildDelegate {
Widget build(BuildContext context, int index);
int? estimatedChildCount { get; }
int trueIndexOf(int index);
bool shouldRebuild(ListWheelChildDelegate oldDelegate);
}
public class ListWheelChildListDelegate : ListWheelChildDelegate {
public ListWheelChildListDelegate(
List<Widget> children
) {
D.assert(children != null);
this.children = children;
}
public readonly List<Widget> children;
public int? estimatedChildCount {
get { return this.children.Count; }
}
public Widget build(BuildContext context, int index) {
if (index < 0 || index >= this.children.Count) {
return null;
}
return new Container(child: this.children[index]);
}
public int trueIndexOf(int index) {
return index;
}
public bool shouldRebuild(ListWheelChildDelegate oldDelegate) {
return this.children != ((ListWheelChildListDelegate) oldDelegate).children;
}
}
public class ListWheelChildLoopingListDelegate : ListWheelChildDelegate {
public ListWheelChildLoopingListDelegate(
List<Widget> children
) {
D.assert(children != null);
this.children = children;
}
public readonly List<Widget> children;
public int? estimatedChildCount {
get { return null; }
}
public int trueIndexOf(int index) {
while (index < 0) {
index += this.children.Count;
}
return index % this.children.Count;
}
public Widget build(BuildContext context, int index) {
if (this.children.isEmpty()) {
return null;
}
while (index < 0) {
index += this.children.Count;
}
return new Container(child: this.children[index % this.children.Count]);
}
public bool shouldRebuild(ListWheelChildDelegate oldDelegate) {
return this.children != ((ListWheelChildLoopingListDelegate) oldDelegate).children;
}
}
public class ListWheelChildBuilderDelegate : ListWheelChildDelegate {
public ListWheelChildBuilderDelegate(
IndexedWidgetBuilder builder,
int? childCount = null
) {
D.assert(builder != null);
this.builder = builder;
this.childCount = childCount;
}
public readonly IndexedWidgetBuilder builder;
public readonly int? childCount;
public int? estimatedChildCount {
get { return this.childCount; }
}
public Widget build(BuildContext context, int index) {
if (this.childCount == null) {
Widget child = this.builder(context, index);
return child == null ? null : new Container(child: child);
}
if (index < 0 || index >= this.childCount) {
return null;
}
return new Container(child: this.builder(context, index));
}
public int trueIndexOf(int index) {
return index;
}
public bool shouldRebuild(ListWheelChildDelegate oldDelegate) {
return this.builder != ((ListWheelChildBuilderDelegate) oldDelegate).builder ||
this.childCount != ((ListWheelChildBuilderDelegate) oldDelegate).childCount;
}
}
class ListWheelScrollViewUtils {
public static int _getItemFromOffset(
float offset,
float itemExtent,
float minScrollExtent,
float maxScrollExtent
) {
return (_clipOffsetToScrollableRange(offset, minScrollExtent, maxScrollExtent) / itemExtent).round();
}
public static float _clipOffsetToScrollableRange(
float offset,
float minScrollExtent,
float maxScrollExtent
) {
return Mathf.Min(Mathf.Max(offset, minScrollExtent), maxScrollExtent);
}
}
public class FixedExtentScrollController : ScrollController {
public FixedExtentScrollController(
int initialItem = 0
) {
this.initialItem = initialItem;
}
public readonly int initialItem;
public int selectedItem {
get {
D.assert(this.positions.isNotEmpty(),
() =>
"FixedExtentScrollController.selectedItem cannot be accessed before a scroll view is built with it."
);
D.assert(this.positions.Count == 1,
() =>
"The selectedItem property cannot be read when multiple scroll views are attached to the same FixedExtentScrollController."
);
_FixedExtentScrollPosition position = (_FixedExtentScrollPosition) this.position;
return position.itemIndex;
}
}
public IPromise animateToItem(
int itemIndex,
TimeSpan duration,
Curve curve
) {
if (!this.hasClients) {
return Promise.Resolved();
}
List<IPromise> futures = new List<IPromise>();
foreach (_FixedExtentScrollPosition position in this.positions) {
futures.Add(position.animateTo(
itemIndex * position.itemExtent,
duration: duration,
curve: curve
));
}
return Promise.All(futures);
}
public void jumpToItem(int itemIndex) {
foreach (_FixedExtentScrollPosition position in this.positions) {
position.jumpTo(itemIndex * position.itemExtent);
}
}
public override ScrollPosition createScrollPosition(ScrollPhysics physics, ScrollContext context,
ScrollPosition oldPosition) {
return new _FixedExtentScrollPosition(
physics: physics,
context: context,
initialItem: this.initialItem,
oldPosition: oldPosition
);
}
}
public interface IFixedExtentMetrics {
int itemIndex { set; get; }
FixedExtentMetrics copyWith(
float? minScrollExtent = null,
float? maxScrollExtent = null,
float? pixels = null,
float? viewportDimension = null,
AxisDirection? axisDirection = null,
int? itemIndex = null
);
}
public class FixedExtentMetrics : FixedScrollMetrics, IFixedExtentMetrics {
public FixedExtentMetrics(
int itemIndex,
float minScrollExtent = 0.0f,
float maxScrollExtent = 0.0f,
float pixels = 0.0f,
float viewportDimension = 0.0f,
AxisDirection axisDirection = AxisDirection.down
) : base(
minScrollExtent: minScrollExtent,
maxScrollExtent: maxScrollExtent,
pixels: pixels,
viewportDimension: viewportDimension,
axisDirection: axisDirection
) {
this.itemIndex = itemIndex;
}
public int itemIndex { get; set; }
public FixedExtentMetrics copyWith(
float? minScrollExtent = null,
float? maxScrollExtent = null,
float? pixels = null,
float? viewportDimension = null,
AxisDirection? axisDirection = null,
int? itemIndex = null
) {
return new FixedExtentMetrics(
minScrollExtent: minScrollExtent ?? this.minScrollExtent,
maxScrollExtent: maxScrollExtent ?? this.maxScrollExtent,
pixels: pixels ?? this.pixels,
viewportDimension: viewportDimension ?? this.viewportDimension,
axisDirection: axisDirection ?? this.axisDirection,
itemIndex: itemIndex ?? this.itemIndex
);
}
}
class _FixedExtentScrollPosition : ScrollPositionWithSingleContext, IFixedExtentMetrics {
public _FixedExtentScrollPosition(
ScrollPhysics physics,
ScrollContext context,
int initialItem,
bool keepScrollOffset = true,
ScrollPosition oldPosition = null,
string debugLabel = null
) : base(
physics: physics,
context: context,
initialPixels: _getItemExtentFromScrollContext(context) * initialItem,
keepScrollOffset: keepScrollOffset,
oldPosition: oldPosition,
debugLabel: debugLabel
) {
D.assert(
context is _FixedExtentScrollableState,
() => "FixedExtentScrollController can only be used with ListWheelScrollViews"
);
}
static float _getItemExtentFromScrollContext(ScrollContext context) {
_FixedExtentScrollableState scrollable = (_FixedExtentScrollableState) context;
return scrollable.itemExtent;
}
public float itemExtent {
get { return _getItemExtentFromScrollContext(this.context); }
}
public int itemIndex {
get {
return ListWheelScrollViewUtils._getItemFromOffset(
offset: this.pixels,
itemExtent: this.itemExtent,
minScrollExtent: this.minScrollExtent,
maxScrollExtent: this.maxScrollExtent
);
}
set { }
}
public FixedExtentMetrics copyWith(
float? minScrollExtent = null,
float? maxScrollExtent = null,
float? pixels = null,
float? viewportDimension = null,
AxisDirection? axisDirection = null,
int? itemIndex = null
) {
return new FixedExtentMetrics(
minScrollExtent: minScrollExtent ?? this.minScrollExtent,
maxScrollExtent: maxScrollExtent ?? this.maxScrollExtent,
pixels: pixels ?? this.pixels,
viewportDimension: viewportDimension ?? this.viewportDimension,
axisDirection: axisDirection ?? this.axisDirection,
itemIndex: itemIndex ?? this.itemIndex
);
}
}
class _FixedExtentScrollable : Scrollable {
public _FixedExtentScrollable(
float itemExtent,
ViewportBuilder viewportBuilder,
Key key = null,
AxisDirection axisDirection = AxisDirection.down,
ScrollController controller = null,
ScrollPhysics physics = null
) : base(
key: key,
axisDirection: axisDirection,
controller: controller,
physics: physics,
viewportBuilder: viewportBuilder
) {
this.itemExtent = itemExtent;
}
public readonly float itemExtent;
public override State createState() {
return new _FixedExtentScrollableState();
}
}
class _FixedExtentScrollableState : ScrollableState {
public float itemExtent {
get {
_FixedExtentScrollable actualWidget = (_FixedExtentScrollable) this.widget;
return actualWidget.itemExtent;
}
}
}
public class FixedExtentScrollPhysics : ScrollPhysics {
public FixedExtentScrollPhysics(
ScrollPhysics parent = null
) : base(parent: parent) { }
public override ScrollPhysics applyTo(ScrollPhysics ancestor) {
return new FixedExtentScrollPhysics(parent: this.buildParent(ancestor));
}
public override Simulation createBallisticSimulation(ScrollMetrics position, float velocity) {
D.assert(
position is _FixedExtentScrollPosition,
() => "FixedExtentScrollPhysics can only be used with Scrollables that uses " +
"the FixedExtentScrollController"
);
_FixedExtentScrollPosition metrics = (_FixedExtentScrollPosition) position;
if ((velocity <= 0.0f && metrics.pixels <= metrics.minScrollExtent) ||
(velocity >= 0.0f && metrics.pixels >= metrics.maxScrollExtent)) {
return base.createBallisticSimulation(metrics, velocity);
}
Simulation testFrictionSimulation =
base.createBallisticSimulation(metrics, velocity);
if (testFrictionSimulation != null
&& (testFrictionSimulation.x(float.PositiveInfinity) == metrics.minScrollExtent
|| testFrictionSimulation.x(float.PositiveInfinity) == metrics.maxScrollExtent)) {
return base.createBallisticSimulation(metrics, velocity);
}
int settlingItemIndex = ListWheelScrollViewUtils._getItemFromOffset(
offset: testFrictionSimulation?.x(float.PositiveInfinity) ?? metrics.pixels,
itemExtent: metrics.itemExtent,
minScrollExtent: metrics.minScrollExtent,
maxScrollExtent: metrics.maxScrollExtent
);
float settlingPixels = settlingItemIndex * metrics.itemExtent;
if (velocity.abs() < this.tolerance.velocity
&& (settlingPixels - metrics.pixels).abs() < this.tolerance.distance) {
return null;
}
if (settlingItemIndex == metrics.itemIndex) {
return new SpringSimulation(this.spring,
metrics.pixels,
settlingPixels,
velocity,
tolerance: this.tolerance
);
}
return FrictionSimulation.through(
metrics.pixels,
settlingPixels,
velocity, this.tolerance.velocity * velocity.sign()
);
}
}
public class ListWheelScrollView : StatefulWidget {
public ListWheelScrollView(
float itemExtent,
List<Widget> children = null,
Key key = null,
ScrollController controller = null,
ScrollPhysics physics = null,
float diameterRatio = RenderListWheelViewport.defaultDiameterRatio,
float perspective = RenderListWheelViewport.defaultPerspective,
float offAxisFraction = 0.0f,
bool useMagnifier = false,
float magnification = 1.0f,
ValueChanged<int> onSelectedItemChanged = null,
bool clipToSize = true,
bool renderChildrenOutsideViewport = false,
ListWheelChildDelegate childDelegate = null
) : base(key: key) {
D.assert(children != null || childDelegate != null);
D.assert(diameterRatio > 0.0, () => RenderListWheelViewport.diameterRatioZeroMessage);
D.assert(perspective > 0);
D.assert(perspective <= 0.01f, () => RenderListWheelViewport.perspectiveTooHighMessage);
D.assert(magnification > 0);
D.assert(itemExtent > 0);
D.assert(
!renderChildrenOutsideViewport || !clipToSize,
() => RenderListWheelViewport.clipToSizeAndRenderChildrenOutsideViewportConflict
);
this.childDelegate = childDelegate ?? new ListWheelChildListDelegate(children: children);
this.itemExtent = itemExtent;
this.controller = controller;
this.physics = physics;
this.diameterRatio = diameterRatio;
this.perspective = perspective;
this.offAxisFraction = offAxisFraction;
this.useMagnifier = useMagnifier;
this.magnification = magnification;
this.onSelectedItemChanged = onSelectedItemChanged;
this.clipToSize = clipToSize;
this.renderChildrenOutsideViewport = renderChildrenOutsideViewport;
}
public static ListWheelScrollView useDelegate(
float itemExtent,
List<Widget> children = null,
ListWheelChildDelegate childDelegate = null,
Key key = null,
ScrollController controller = null,
ScrollPhysics physics = null,
float diameterRatio = RenderListWheelViewport.defaultDiameterRatio,
float perspective = RenderListWheelViewport.defaultPerspective,
float offAxisFraction = 0.0f,
bool useMagnifier = false,
float magnification = 1.0f,
ValueChanged<int> onSelectedItemChanged = null,
bool clipToSize = true,
bool renderChildrenOutsideViewport = false
) {
return new ListWheelScrollView(
itemExtent: itemExtent,
children: children,
childDelegate: childDelegate,
key: key,
controller: controller,
physics: physics,
diameterRatio: diameterRatio,
perspective: perspective,
offAxisFraction: offAxisFraction,
useMagnifier: useMagnifier,
magnification: magnification,
onSelectedItemChanged: onSelectedItemChanged,
clipToSize: clipToSize,
renderChildrenOutsideViewport: renderChildrenOutsideViewport
);
}
public readonly ScrollController controller;
public readonly ScrollPhysics physics;
public readonly float diameterRatio;
public readonly float perspective;
public readonly float offAxisFraction;
public readonly bool useMagnifier;
public readonly float magnification;
public readonly float itemExtent;
public readonly ValueChanged<int> onSelectedItemChanged;
public readonly bool clipToSize;
public readonly bool renderChildrenOutsideViewport;
public readonly ListWheelChildDelegate childDelegate;
public override State createState() {
return new _ListWheelScrollViewState();
}
}
class _ListWheelScrollViewState : State<ListWheelScrollView> {
int _lastReportedItemIndex = 0;
ScrollController scrollController;
public override void initState() {
base.initState();
this.scrollController = this.widget.controller ?? new FixedExtentScrollController();
if (this.widget.controller is FixedExtentScrollController controller) {
this._lastReportedItemIndex = controller.initialItem;
}
}
public override void didUpdateWidget(StatefulWidget oldWidget) {
base.didUpdateWidget(oldWidget);
if (this.widget.controller != null && this.widget.controller != this.scrollController) {
ScrollController oldScrollController = this.scrollController;
SchedulerBinding.instance.addPostFrameCallback((_) => { oldScrollController.dispose(); });
this.scrollController = this.widget.controller;
}
}
public override Widget build(BuildContext context) {
return new NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) => {
if (notification.depth == 0
&& this.widget.onSelectedItemChanged != null
&& notification is ScrollUpdateNotification
&& notification.metrics is FixedExtentMetrics metrics) {
int currentItemIndex = metrics.itemIndex;
if (currentItemIndex != this._lastReportedItemIndex) {
this._lastReportedItemIndex = currentItemIndex;
int trueIndex = this.widget.childDelegate.trueIndexOf(currentItemIndex);
this.widget.onSelectedItemChanged(trueIndex);
}
}
return false;
},
child: new _FixedExtentScrollable(
controller: this.scrollController,
physics: this.widget.physics,
itemExtent: this.widget.itemExtent,
viewportBuilder: (BuildContext _context, ViewportOffset _offset) => {
return new ListWheelViewport(
diameterRatio: this.widget.diameterRatio,
perspective: this.widget.perspective,
offAxisFraction: this.widget.offAxisFraction,
useMagnifier: this.widget.useMagnifier,
magnification: this.widget.magnification,
itemExtent: this.widget.itemExtent,
clipToSize: this.widget.clipToSize,
renderChildrenOutsideViewport: this.widget.renderChildrenOutsideViewport,
offset: _offset,
childDelegate: this.widget.childDelegate
);
}
)
);
}
}
public class ListWheelElement : RenderObjectElement, IListWheelChildManager {
public ListWheelElement(ListWheelViewport widget) : base(widget) { }
public new ListWheelViewport widget {
get { return (ListWheelViewport) base.widget; }
}
public new RenderListWheelViewport renderObject {
get { return (RenderListWheelViewport) base.renderObject; }
}
readonly Dictionary<int, Widget> _childWidgets = new Dictionary<int, Widget>();
readonly SplayTree<int, Element> _childElements = new SplayTree<int, Element>();
public override void update(Widget newWidget) {
ListWheelViewport oldWidget = this.widget;
base.update(newWidget);
ListWheelChildDelegate newDelegate = ((ListWheelViewport) newWidget).childDelegate;
ListWheelChildDelegate oldDelegate = oldWidget.childDelegate;
if (newDelegate != oldDelegate &&
(newDelegate.GetType() != oldDelegate.GetType() || newDelegate.shouldRebuild(oldDelegate))) {
this.performRebuild();
}
}
public int? childCount {
get { return this.widget.childDelegate.estimatedChildCount; }
}
protected override void performRebuild() {
this._childWidgets.Clear();
base.performRebuild();
if (this._childElements.isEmpty()) {
return;
}
int firstIndex = this._childElements.First().Key;
int lastIndex = this._childElements.Last().Key;
for (int index = firstIndex; index <= lastIndex; ++index) {
Element newChild = this.updateChild(this._childElements[index], this.retrieveWidget(index), index);
if (newChild != null) {
this._childElements[index] = newChild;
}
else {
this._childElements.Remove(index);
}
}
}
Widget retrieveWidget(int index) {
return this._childWidgets.putIfAbsent(index,
() => { return this.widget.childDelegate.build(this, index); });
}
public bool childExistsAt(int index) {
return this.retrieveWidget(index) != null;
}
public void createChild(int index, RenderBox after) {
this.owner.buildScope(this, () => {
bool insertFirst = after == null;
D.assert(insertFirst || this._childElements[index - 1] != null);
// Debug.Log($"{index}: {this._childElements.getOrDefault(index)}");
Element newChild = this.updateChild(this._childElements.getOrDefault(index), this.retrieveWidget(index),
index);
// Debug.Log(newChild);
if (newChild != null) {
this._childElements[index] = newChild;
}
else {
this._childElements.Remove(index);
}
});
}
public void removeChild(RenderBox child) {
int index = this.renderObject.indexOf(child);
this.owner.buildScope(this, () => {
D.assert(this._childElements.ContainsKey(index));
Element result = this.updateChild(this._childElements[index], null, index);
D.assert(result == null);
this._childElements.Remove(index);
D.assert(!this._childElements.ContainsKey(index));
});
}
protected override Element updateChild(Element child, Widget newWidget, object newSlot) {
ListWheelParentData oldParentData = (ListWheelParentData) child?.renderObject?.parentData;
Element newChild = base.updateChild(child, newWidget, newSlot);
ListWheelParentData newParentData = (ListWheelParentData) newChild?.renderObject?.parentData;
if (newParentData != null) {
newParentData.index = (int) newSlot;
if (oldParentData != null) {
newParentData.offset = oldParentData.offset;
}
}
return newChild;
}
protected override void insertChildRenderObject(RenderObject child, object slot) {
RenderListWheelViewport renderObject = this.renderObject;
D.assert(renderObject.debugValidateChild(child));
renderObject.insert((RenderBox) child,
(RenderBox) this._childElements.getOrDefault((int) slot - 1)?.renderObject);
// Debug.Log($"insert: {this._childElements.getOrDefault((int) slot - 1)}");
D.assert(renderObject == this.renderObject);
}
protected override void moveChildRenderObject(RenderObject child, dynamic slot) {
const string moveChildRenderObjectErrorMessage =
"Currently we maintain the list in contiguous increasing order, so " +
"moving children around is not allowed.";
D.assert(false, () => moveChildRenderObjectErrorMessage);
}
protected override void removeChildRenderObject(RenderObject child) {
D.assert(child.parent == this.renderObject);
this.renderObject.remove((RenderBox) child);
}
public override void visitChildren(ElementVisitor visitor) {
foreach (var item in this._childElements) {
visitor(item.Value);
}
}
protected override void forgetChild(Element child) {
this._childElements.Remove((int) (child.slot));
}
}
public class ListWheelViewport : RenderObjectWidget {
public ListWheelViewport(
float itemExtent,
ViewportOffset offset,
ListWheelChildDelegate childDelegate,
Key key = null,
float diameterRatio = RenderListWheelViewport.defaultDiameterRatio,
float perspective = RenderListWheelViewport.defaultPerspective,
float offAxisFraction = 0.0f,
bool useMagnifier = false,
float magnification = 1.0f,
bool clipToSize = true,
bool renderChildrenOutsideViewport = false
) : base(key: key) {
D.assert(childDelegate != null);
D.assert(offset != null);
D.assert(diameterRatio > 0, () => RenderListWheelViewport.diameterRatioZeroMessage);
D.assert(perspective > 0);
D.assert(perspective <= 0.01, () => RenderListWheelViewport.perspectiveTooHighMessage);
D.assert(itemExtent > 0);
D.assert(
!renderChildrenOutsideViewport || !clipToSize,
() => RenderListWheelViewport.clipToSizeAndRenderChildrenOutsideViewportConflict
);
this.itemExtent = itemExtent;
this.offset = offset;
this.childDelegate = childDelegate;
this.diameterRatio = diameterRatio;
this.perspective = perspective;
this.offAxisFraction = offAxisFraction;
this.useMagnifier = useMagnifier;
this.magnification = magnification;
this.clipToSize = clipToSize;
this.renderChildrenOutsideViewport = renderChildrenOutsideViewport;
}
public readonly float diameterRatio;
public readonly float perspective;
public readonly float offAxisFraction;
public readonly bool useMagnifier;
public readonly float magnification;
public readonly float itemExtent;
public readonly bool clipToSize;
public readonly bool renderChildrenOutsideViewport;
public readonly ViewportOffset offset;
public readonly ListWheelChildDelegate childDelegate;
public override Element createElement() {
return new ListWheelElement(this);
}
public override RenderObject createRenderObject(BuildContext context) {
ListWheelElement childManager = (ListWheelElement) context;
return new RenderListWheelViewport(
childManager: childManager,
offset: this.offset,
diameterRatio: this.diameterRatio,
perspective: this.perspective,
offAxisFraction: this.offAxisFraction,
useMagnifier: this.useMagnifier,
magnification: this.magnification,
itemExtent: this.itemExtent,
clipToSize: this.clipToSize,
renderChildrenOutsideViewport: this.renderChildrenOutsideViewport
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
var viewport = (RenderListWheelViewport) renderObject;
viewport.offset = this.offset;
viewport.diameterRatio = this.diameterRatio;
viewport.perspective = this.perspective;
viewport.offAxisFraction = this.offAxisFraction;
viewport.useMagnifier = this.useMagnifier;
viewport.magnification = this.magnification;
viewport.itemExtent = this.itemExtent;
viewport.clipToSize = this.clipToSize;
viewport.renderChildrenOutsideViewport = this.renderChildrenOutsideViewport;
}
}
}

11
Runtime/widgets/list_wheel_scroll_view.cs.meta


fileFormatVersion: 2
guid: 5f33e2b8a13bf4308b91f226530921e3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存