浏览代码

fix : remove all matrix4x4 utils, move some useful matrix3 utils to matrix3 and leave only application-specific functions in MatrixUtils; replace property determinant with function invertable()

/main
xingwei.zhu 6 年前
当前提交
6d0089a0
共有 12 个文件被更改,包括 67 次插入185 次删除
  1. 147
      Runtime/painting/matrix_utils.cs
  2. 8
      Runtime/rendering/box.cs
  3. 2
      Runtime/rendering/layer.cs
  4. 6
      Runtime/rendering/object.cs
  5. 16
      Runtime/rendering/proxy_box.cs
  6. 6
      Runtime/rendering/sliver.cs
  7. 2
      Runtime/rendering/view.cs
  8. 8
      Runtime/rendering/viewport.cs
  9. 41
      Runtime/ui/matrix.cs
  10. 6
      Runtime/widgets/basic.cs
  11. 2
      Runtime/widgets/container.cs
  12. 8
      Runtime/widgets/widget_inspector.cs

147
Runtime/painting/matrix_utils.cs


namespace Unity.UIWidgets.painting {
public static class MatrixUtils {
public static Matrix3 makeRotate(float degree)
{
var m = Matrix3.I();
m.setRotate(degree);
return m;
}
public static Matrix3 makeTrans(Vector2 vector)
{
var m = Matrix3.I();
m.setTranslate(vector.x, vector.y);
return m;
}
public static Matrix3 makeTrans(float dx, float dy)
{
var m = Matrix3.I();
m.setTranslate(dx, dy);
return m;
}
public static Matrix3 Value(this Matrix3 matrix3)
{
var m = Matrix3.I();
m.copyFrom(matrix3);
return m;
}
public static Matrix3 inverse(this Matrix3 matrix3)
{
var m = Matrix3.I();
var invertable = matrix3.invert(m);
D.assert(invertable);
return m;
}
public static Rect inverseTransformRect(this Matrix3 matrix3, Rect rect)
{
D.assert(rect != null);
D.assert(matrix3.determinant != 0.0);
if (matrix3.isIdentity()) {
return rect;
}
var inverse = matrix3.inverse();
return inverse.transformRect(rect);
}
public static Offset transformPoint(this Matrix3 matrix3, Offset point)
{
return matrix3.mapXY((float)point.dx, (float)point.dy);
}
public static Rect transformRect(this Matrix3 matrix3, Rect rect)
{
return matrix3.mapRect(rect);
}
if (matrix3.isTranslate()) return matrix3.mapXY(0, 0);
return null;
}
public static Offset transformPoint(this Matrix4x4 transform, Offset point) {
var position3 = new Vector3((float) point.dx, (float) point.dy, 0);
var transformed3 = transform.MultiplyPoint(position3);
return new Offset(transformed3.x, transformed3.y);
}
public static Rect transformRect(this Matrix4x4 transform, Offset[] points, out bool isRect) {
D.assert(points != null && points.Length == 4, "expected 4 points");
var topLeft = transform.transformPoint(points[0]);
var topRight = transform.transformPoint(points[1]);
var bottomLeft = transform.transformPoint(points[2]);
var bottomRight = transform.transformPoint(points[3]);
isRect = topLeft.dy == topRight.dy
&& topRight.dx == bottomRight.dx
&& bottomRight.dy == bottomLeft.dy
&& bottomLeft.dx == topLeft.dx;
var left = Math.Min(Math.Min(Math.Min(topLeft.dx, topRight.dx), bottomLeft.dx), bottomRight.dx);
var right = Math.Max(Math.Max(Math.Max(topLeft.dx, topRight.dx), bottomLeft.dx), bottomRight.dx);
var top = Math.Min(Math.Min(Math.Min(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
var bottom = Math.Max(Math.Max(Math.Max(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
return Rect.fromLTRB(left, top, right, bottom);
}
public static Rect transformRect(this Matrix4x4 transform, Rect rect, out bool isRect) {
return transform.transformRect(
new[] {rect.topLeft, rect.topRight, rect.bottomLeft, rect.bottomRight},
out isRect);
}
public static Rect transformRect(this Matrix4x4 transform, Offset[] points) {
bool isRect;
return transform.transformRect(points, out isRect);
}
public static Rect transformRect(this Matrix4x4 transform, Rect rect) {
bool isRect;
return transform.transformRect(rect, out isRect);
}
public static Rect inverseTransformRect(this Matrix4x4 transform, Rect rect) {
D.assert(rect != null);
D.assert(transform.determinant != 0.0);
if (transform.isIdentity) {
return rect;
}
var inverse = transform.inverse;
return inverse.transformRect(rect);
}
public static Offset getAsTranslation(this Matrix4x4 transform)
{
if (transform.m00 == 1.0 &&
transform.m10 == 0.0 &&
transform.m20 == 0.0 &&
transform.m30 == 0.0 &&
transform.m01 == 0.0 &&
transform.m11 == 1.0 &&
transform.m21 == 0.0 &&
transform.m31 == 0.0 &&
transform.m02 == 0.0 &&
transform.m12 == 0.0 &&
transform.m22 == 1.0 &&
transform.m32 == 0.0 &&
transform.m23 == 0.0 &&
transform.m33 == 1.0) {
return new Offset(transform.m03, transform.m13);
}
return null;
}
public static bool isPerspective(this Matrix4x4 transform) {
return transform[3, 0] != 0 || transform[3, 1] != 0 || transform[3, 2] != 0 || transform[3, 3] != 1;
return matrix3.isTranslate() ? new Offset(matrix3[Matrix3.kMTransX], matrix3[Matrix3.kMTransY]) : null;
}
}
}

8
Runtime/rendering/box.cs


var childParentData = (BoxParentData) child.parentData;
var offset = childParentData.offset;
transform = MatrixUtils.makeTrans(offset.toVector()) * transform;
transform = Matrix3.makeTrans(offset.toVector()) * transform;
if (transform.determinant == 0) {
if (!transform.invertable()) {
return transform.transformPoint(point);
return transform.mapPoint(point);
return this.getTransformTo(ancestor).transformPoint(point);
return this.getTransformTo(ancestor).mapPoint(point);
}
public override Rect paintBounds {

2
Runtime/rendering/layer.cs


var totalOffset = this.offset + layerOffset;
if (totalOffset != Offset.zero) {
this._lastEffectiveTransform =
MatrixUtils.makeTrans(totalOffset.toVector()) * this._lastEffectiveTransform;
Matrix3.makeTrans(totalOffset.toVector()) * this._lastEffectiveTransform;
}
builder.pushTransform(this._lastEffectiveTransform);

6
Runtime/rendering/object.cs


public void pushTransform(bool needsCompositing, Offset offset, Matrix3 transform,
PaintingContextCallback painter) {
var effectiveTransform = MatrixUtils.makeTrans(offset.toVector())
* transform * MatrixUtils.makeTrans(-offset.toVector());
var effectiveTransform = Matrix3.makeTrans(offset.toVector())
* transform * Matrix3.makeTrans(-offset.toVector());
if (needsCompositing) {
this.pushLayer(

childPaintBounds: effectiveTransform.inverseTransformRect(this.estimatedBounds)
childPaintBounds: effectiveTransform.inverse().mapRect(this.estimatedBounds)
);
} else {
this.canvas.save();

16
Runtime/rendering/proxy_box.cs


}
public void rotateZ(double degrees) {
this._transform = MatrixUtils.makeRotate((float) degrees) * this._transform;
this._transform = Matrix3.makeRotate((float) degrees) * this._transform;
this._transform = MatrixUtils.makeTrans((float)x, (float)y) * this._transform;
this._transform = Matrix3.makeTrans((float)x, (float)y) * this._transform;
this.markNeedsPaint();
}

var result = Matrix3.I();
if (this._origin != null) {
result = MatrixUtils.makeTrans(new Vector2((float) this._origin.dx, (float) this._origin.dy)) *
result = Matrix3.makeTrans(new Vector2((float) this._origin.dx, (float) this._origin.dy)) *
result;
}

result = MatrixUtils.makeTrans(new Vector2((float) translation.dx, (float) translation.dy)) * result;
result = Matrix3.makeTrans(new Vector2((float) translation.dx, (float) translation.dy)) * result;
result = MatrixUtils.makeTrans(new Vector2((float) -translation.dx, (float) -translation.dy)) *
result = Matrix3.makeTrans(new Vector2((float) -translation.dx, (float) -translation.dy)) *
result = MatrixUtils.makeTrans(new Vector2((float) -this._origin.dx, (float) -this._origin.dy)) *
result = Matrix3.makeTrans(new Vector2((float) -this._origin.dx, (float) -this._origin.dy)) *
result;
}

protected override bool hitTestChildren(HitTestResult result, Offset position = null) {
if (this.transformHitTests) {
var transform = this._effectiveTransform;
if (transform.determinant == 0) {
if (!transform.invertable()) {
position = transform.inverse().transformPoint(position);
position = transform.inverse().mapPoint(position);
}
return base.hitTestChildren(result, position: position);

6
Runtime/rendering/sliver.cs


public Offset paintOffset = Offset.zero;
public void applyPaintTransform(ref Matrix3 transform) {
transform = MatrixUtils.makeTrans(this.paintOffset.toVector()) * transform;
transform = Matrix3.makeTrans(this.paintOffset.toVector()) * transform;
}
public override string ToString() {

delta = it.geometry.paintExtent - child.size.width - delta;
}
transform = MatrixUtils.makeTrans(new Vector2((float) delta, (float) crossAxisDelta)) * transform;
transform = Matrix3.makeTrans(new Vector2((float) delta, (float) crossAxisDelta)) * transform;
break;
case Axis.vertical:
if (!rightWayUp) {

transform = MatrixUtils.makeTrans(new Vector2((float) crossAxisDelta, (float) delta)) * transform;
transform = Matrix3.makeTrans(new Vector2((float) crossAxisDelta, (float) delta)) * transform;
break;
}
}

2
Runtime/rendering/view.cs


get
{
D.assert(_rootTransform != null);
return _rootTransform.transformRect(Offset.zero & size);
return _rootTransform.mapRect(Offset.zero & size);
}
}

8
Runtime/rendering/viewport.cs


RenderSliver pivotParent = (RenderSliver) pivot.parent;
transform = targetBox.getTransformTo(pivot);
Rect bounds = transform.transformRect(rect);
Rect bounds = transform.mapRect(rect);
double offset = 0.0;

transform = target.getTransformTo(this);
this.applyPaintTransform(child, ref transform);
Rect targetRect = transform.transformRect(rect);
Rect targetRect = transform.mapRect(rect);
switch (this.axisDirection) {
case AxisDirection.down:

targetOffset = trailingEdgeOffset;
} else {
var transform = descendant.getTransformTo(viewport.parent);
return transform.transformRect(rect ?? descendant.paintBounds);
return transform.mapRect(rect ?? descendant.paintBounds);
}
D.assert(targetOffset != null);

D.assert(child != null);
Offset offset = this.paintOffsetOf((RenderSliver) child);
transform = MatrixUtils.makeTrans(offset.toVector()) * transform;
transform = Matrix3.makeTrans(offset.toVector()) * transform;
}
protected override double computeChildMainAxisPosition(RenderSliver child, double parentMainAxisPosition) {

41
Runtime/ui/matrix.cs


m.setTranslate(dx, dy);
return m;
}
public static Matrix3 makeRotate(float degree)
{
var m = new Matrix3();
m.setRotate(degree);
return m;
}
public static Matrix3 makeTrans(Vector2 vector)
{
var m = new Matrix3();
m.setTranslate(vector.x, vector.y);
return m;
}
public static Matrix3 makeAll(
float scaleX, float skewX, float transX,

return "Matrix3(" + string.Join(",", Array.ConvertAll(fMat, i => i.ToString())) + ")";
}
public float determinant
public bool invertable()
{
var mask = this.getType();
var isPersp = (int) (mask & TypeMask.kPerspective_Mask);
var invDet = ScalarUtils.inv_determinant(fMat, isPersp);
return invDet != 0;
}
public Matrix3 inverse()
{
var m = new Matrix3();
var invertable = this.invert(m);
D.assert(invertable);
return m;
}
public Offset mapPoint(Offset point)
get
{
TypeMask mask = this.getType();
int isPersp = (int) (mask & TypeMask.kPerspective_Mask);
double invDet = ScalarUtils.inv_determinant(fMat, isPersp);
D.assert(invDet != 0);
return (float)(1.0 / invDet);
}
return this.mapXY((float)point.dx, (float)point.dy);
}
}

6
Runtime/widgets/basic.cs


Widget child = null
) : base(key, child) {
D.assert(transform != null);
this.transform = transform.Value();
this.transform = new Matrix3(transform);
this.origin = origin;
this.alignment = alignment;
this.transformHitTests = transformHitTests;

Widget child = null,
double degree = 0.0
) : base(key: key, child: child) {
this.transform = MatrixUtils.makeRotate((float) degree);
this.transform = Matrix3.makeRotate((float) degree);
this.origin = origin;
this.alignment = alignment;
this.transformHitTests = transformHitTests;

Widget child = null
) : base(key: key, child: child) {
D.assert(offset != null);
this.transform = MatrixUtils.makeTrans(new Vector2((float) offset.dx, (float) offset.dy));
this.transform = Matrix3.makeTrans(new Vector2((float) offset.dx, (float) offset.dy));
this.origin = null;
this.alignment = null;
this.transformHitTests = transformHitTests;

2
Runtime/widgets/container.cs


}
if (this.transform != null) {
current = new Transform(transform: this.transform.Value(), child: current);
current = new Transform(transform: new Matrix3(this.transform), child: current);
}
return current;

8
Runtime/widgets/widget_inspector.cs


) {
var hit = false;
if (!transform.invertable())
{
return false;
}
//var localPosition = MatrixUtils.transformPoint(inverse, position);
var localPosition = inverse.transformPoint(position);
var localPosition = inverse.mapPoint(position);
List<DiagnosticsNode> children = renderObject.debugDescribeChildren();
for (int i = children.Count - 1; i >= 0; --i)

正在加载...
取消
保存