浏览代码

Implement FractionallySizedBox, FractionalOffset

/main
Yuncong Zhang 6 年前
当前提交
d89467db
共有 3 个文件被更改,包括 164 次插入1 次删除
  1. 46
      Runtime/widgets/basic.cs
  2. 108
      Runtime/painting/fractional_offset.cs
  3. 11
      Runtime/painting/fractional_offset.cs.meta

46
Runtime/widgets/basic.cs


}
}
public class FractionallySizedBox : SingleChildRenderObjectWidget {
public FractionallySizedBox(
Key key = null,
Alignment alignment = null,
float? widthFactor = null,
float? heightFactor = null,
Widget child = null
) : base(key: key, child: child) {
D.assert(widthFactor == null || widthFactor >= 0.0f);
D.assert(heightFactor == null || heightFactor >= 0.0f);
this.alignment = alignment ?? Alignment.center;
this.widthFactor = widthFactor;
this.heightFactor = heightFactor;
}
public readonly float? widthFactor;
public readonly float? heightFactor;
public readonly Alignment alignment;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderFractionallySizedOverflowBox(
alignment: this.alignment,
widthFactor: this.widthFactor,
heightFactor: this.heightFactor
);
}
public override void updateRenderObject(BuildContext context, RenderObject _renderObject) {
RenderFractionallySizedOverflowBox renderObject = _renderObject as RenderFractionallySizedOverflowBox;
renderObject.alignment = this.alignment;
renderObject.widthFactor = this.widthFactor;
renderObject.heightFactor = this.heightFactor;
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<Alignment>("alignment", this.alignment));
properties.add(new FloatProperty("widthFactor", this.widthFactor, defaultValue: null));
properties.add(new FloatProperty("heightFactor", this.heightFactor, defaultValue: null));
}
}
public class SliverToBoxAdapter : SingleChildRenderObjectWidget {
public SliverToBoxAdapter(
Key key = null,

return this.builder(context);
}
}
}
}

108
Runtime/painting/fractional_offset.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.painting {
public class FractionalOffset : Alignment {
public FractionalOffset(float dx, float dy)
: base(dx * 2.0f - 1.0f, dy * 2.0f - 1.0f) {
}
public static FractionalOffset fromOffsetAndSize(Offset offset, Size size) {
D.assert(size != null);
D.assert(offset != null);
return new FractionalOffset(
offset.dx / size.width,
offset.dy / size.height
);
}
public static FractionalOffset fromOffsetAndRect(Offset offset, Rect rect) {
return fromOffsetAndSize(
offset - rect.topLeft,
rect.size
);
}
public float dx {
get { return (this.x + 1.0f) / 2.0f; }
}
public float dy {
get { return (this.y + 1.0f) / 2.0f; }
}
public readonly FractionalOffset topLeft = new FractionalOffset(0.0f, 0.0f);
public readonly FractionalOffset topCenter = new FractionalOffset(0.5f, 0.0f);
public readonly FractionalOffset topRight = new FractionalOffset(1.0f, 0.0f);
public readonly FractionalOffset centerLeft = new FractionalOffset(0.0f, 0.5f);
public readonly FractionalOffset center = new FractionalOffset(0.5f, 0.5f);
public readonly FractionalOffset centerRight = new FractionalOffset(1.0f, 0.5f);
public readonly FractionalOffset bottomLeft = new FractionalOffset(0.0f, 1.0f);
public readonly FractionalOffset bottomCenter = new FractionalOffset(0.5f, 1.0f);
public readonly FractionalOffset bottomRight = new FractionalOffset(1.0f, 1.0f);
public static Alignment operator -(FractionalOffset a, Alignment b) {
if (!(b is FractionalOffset)) {
return (a as Alignment) - b;
}
FractionalOffset typedOther = (FractionalOffset) b;
return new FractionalOffset(a.dx - typedOther.dx, a.dy - typedOther.dy);
}
public static Alignment operator +(FractionalOffset a, Alignment b) {
if (!(b is FractionalOffset)) {
return (a as Alignment) + b;
}
FractionalOffset typedOther = (FractionalOffset) b;
return new FractionalOffset(a.dx + typedOther.dx, a.dy + typedOther.dy);
}
public static FractionalOffset operator -(FractionalOffset a) {
return new FractionalOffset(-a.dx, -a.dy);
}
public static FractionalOffset operator *(FractionalOffset a, float b) {
return new FractionalOffset(a.dx * b, a.dy * b);
}
public static FractionalOffset operator /(FractionalOffset a, float b) {
return new FractionalOffset(a.dx / b, a.dy / b);
}
public static FractionalOffset operator %(FractionalOffset a, float b) {
return new FractionalOffset(a.dx % b, a.dy % b);
}
static FractionalOffset lerp(FractionalOffset a, FractionalOffset b, float t) {
D.assert(t != null);
if (a == null && b == null) {
return null;
}
if (a == null) {
return new FractionalOffset(MathUtils.lerpFloat(0.5f, b.dx, t), MathUtils.lerpFloat(0.5f, b.dy, t));
}
if (b == null) {
return new FractionalOffset(MathUtils.lerpFloat(a.dx, 0.5f, t), MathUtils.lerpFloat(a.dy, 0.5f, t));
}
return new FractionalOffset(MathUtils.lerpFloat(a.dx, b.dx, t), MathUtils.lerpFloat(a.dy, b.dy, t));
}
public override string ToString() {
return $"FractionalOffset({this.dx:0.0}, " +
$"{this.dy:0.0})";
}
}
}

11
Runtime/painting/fractional_offset.cs.meta


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