Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 7b0ac2d

Browse files
authored
Merge pull request #197 from UnityTech/yczhang
Implement FractionallySizedBox, FractionalOffset
2 parents 8acb0a5 + a65c603 commit 7b0ac2d

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

Runtime/painting/fractional_offset.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Unity.UIWidgets.foundation;
2+
using Unity.UIWidgets.ui;
3+
4+
namespace Unity.UIWidgets.painting {
5+
public class FractionalOffset : Alignment {
6+
public FractionalOffset(float dx, float dy)
7+
: base(dx * 2.0f - 1.0f, dy * 2.0f - 1.0f) {
8+
}
9+
10+
public static FractionalOffset fromOffsetAndSize(Offset offset, Size size) {
11+
D.assert(size != null);
12+
D.assert(offset != null);
13+
return new FractionalOffset(
14+
offset.dx / size.width,
15+
offset.dy / size.height
16+
);
17+
}
18+
19+
public static FractionalOffset fromOffsetAndRect(Offset offset, Rect rect) {
20+
return fromOffsetAndSize(
21+
offset - rect.topLeft,
22+
rect.size
23+
);
24+
}
25+
26+
public float dx {
27+
get { return (this.x + 1.0f) / 2.0f; }
28+
}
29+
30+
public float dy {
31+
get { return (this.y + 1.0f) / 2.0f; }
32+
}
33+
34+
public readonly FractionalOffset topLeft = new FractionalOffset(0.0f, 0.0f);
35+
36+
public readonly FractionalOffset topCenter = new FractionalOffset(0.5f, 0.0f);
37+
38+
public readonly FractionalOffset topRight = new FractionalOffset(1.0f, 0.0f);
39+
40+
public readonly FractionalOffset centerLeft = new FractionalOffset(0.0f, 0.5f);
41+
42+
public readonly FractionalOffset center = new FractionalOffset(0.5f, 0.5f);
43+
44+
public readonly FractionalOffset centerRight = new FractionalOffset(1.0f, 0.5f);
45+
46+
public readonly FractionalOffset bottomLeft = new FractionalOffset(0.0f, 1.0f);
47+
48+
public readonly FractionalOffset bottomCenter = new FractionalOffset(0.5f, 1.0f);
49+
50+
public readonly FractionalOffset bottomRight = new FractionalOffset(1.0f, 1.0f);
51+
52+
public static Alignment operator -(FractionalOffset a, Alignment b) {
53+
if (!(b is FractionalOffset)) {
54+
return (a as Alignment) - b;
55+
}
56+
57+
FractionalOffset typedOther = (FractionalOffset) b;
58+
return new FractionalOffset(a.dx - typedOther.dx, a.dy - typedOther.dy);
59+
}
60+
61+
public static Alignment operator +(FractionalOffset a, Alignment b) {
62+
if (!(b is FractionalOffset)) {
63+
return (a as Alignment) + b;
64+
}
65+
66+
FractionalOffset typedOther = (FractionalOffset) b;
67+
return new FractionalOffset(a.dx + typedOther.dx, a.dy + typedOther.dy);
68+
}
69+
70+
public static FractionalOffset operator -(FractionalOffset a) {
71+
return new FractionalOffset(-a.dx, -a.dy);
72+
}
73+
74+
public static FractionalOffset operator *(FractionalOffset a, float b) {
75+
return new FractionalOffset(a.dx * b, a.dy * b);
76+
}
77+
78+
public static FractionalOffset operator /(FractionalOffset a, float b) {
79+
return new FractionalOffset(a.dx / b, a.dy / b);
80+
}
81+
82+
public static FractionalOffset operator %(FractionalOffset a, float b) {
83+
return new FractionalOffset(a.dx % b, a.dy % b);
84+
}
85+
86+
public static FractionalOffset lerp(FractionalOffset a, FractionalOffset b, float t) {
87+
if (a == null && b == null) {
88+
return null;
89+
}
90+
91+
if (a == null) {
92+
return new FractionalOffset(MathUtils.lerpFloat(0.5f, b.dx, t), MathUtils.lerpFloat(0.5f, b.dy, t));
93+
}
94+
95+
if (b == null) {
96+
return new FractionalOffset(MathUtils.lerpFloat(a.dx, 0.5f, t), MathUtils.lerpFloat(a.dy, 0.5f, t));
97+
}
98+
99+
return new FractionalOffset(MathUtils.lerpFloat(a.dx, b.dx, t), MathUtils.lerpFloat(a.dy, b.dy, t));
100+
}
101+
102+
public override string ToString() {
103+
return $"FractionalOffset({this.dx:0.0}, " +
104+
$"{this.dy:0.0})";
105+
}
106+
}
107+
}

Runtime/painting/fractional_offset.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/widgets/basic.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,50 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
524524
}
525525
}
526526

527+
public class FractionallySizedBox : SingleChildRenderObjectWidget {
528+
public FractionallySizedBox(
529+
Key key = null,
530+
Alignment alignment = null,
531+
float? widthFactor = null,
532+
float? heightFactor = null,
533+
Widget child = null
534+
) : base(key: key, child: child) {
535+
D.assert(widthFactor == null || widthFactor >= 0.0f);
536+
D.assert(heightFactor == null || heightFactor >= 0.0f);
537+
this.alignment = alignment ?? Alignment.center;
538+
this.widthFactor = widthFactor;
539+
this.heightFactor = heightFactor;
540+
}
541+
542+
public readonly float? widthFactor;
543+
544+
public readonly float? heightFactor;
545+
546+
public readonly Alignment alignment;
547+
548+
public override RenderObject createRenderObject(BuildContext context) {
549+
return new RenderFractionallySizedOverflowBox(
550+
alignment: this.alignment,
551+
widthFactor: this.widthFactor,
552+
heightFactor: this.heightFactor
553+
);
554+
}
555+
556+
public override void updateRenderObject(BuildContext context, RenderObject _renderObject) {
557+
RenderFractionallySizedOverflowBox renderObject = _renderObject as RenderFractionallySizedOverflowBox;
558+
renderObject.alignment = this.alignment;
559+
renderObject.widthFactor = this.widthFactor;
560+
renderObject.heightFactor = this.heightFactor;
561+
}
562+
563+
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
564+
base.debugFillProperties(properties);
565+
properties.add(new DiagnosticsProperty<Alignment>("alignment", this.alignment));
566+
properties.add(new FloatProperty("widthFactor", this.widthFactor, defaultValue: null));
567+
properties.add(new FloatProperty("heightFactor", this.heightFactor, defaultValue: null));
568+
}
569+
}
570+
527571
public class SliverToBoxAdapter : SingleChildRenderObjectWidget {
528572
public SliverToBoxAdapter(
529573
Key key = null,
@@ -2197,4 +2241,4 @@ public override Widget build(BuildContext context) {
21972241
return this.builder(context);
21982242
}
21992243
}
2200-
}
2244+
}

0 commit comments

Comments
 (0)