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

Commit 1e929c4

Browse files
author
Yuncong Zhang
committed
Implement FractionallySizedBox, FractionalOffset
1 parent be81a85 commit 1e929c4

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

Runtime/painting/fractional_offset.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
static FractionalOffset lerp(FractionalOffset a, FractionalOffset b, float t) {
87+
D.assert(t != null);
88+
if (a == null && b == null) {
89+
return null;
90+
}
91+
92+
if (a == null) {
93+
return new FractionalOffset(MathUtils.lerpFloat(0.5f, b.dx, t), MathUtils.lerpFloat(0.5f, b.dy, t));
94+
}
95+
96+
if (b == null) {
97+
return new FractionalOffset(MathUtils.lerpFloat(a.dx, 0.5f, t), MathUtils.lerpFloat(a.dy, 0.5f, t));
98+
}
99+
100+
return new FractionalOffset(MathUtils.lerpFloat(a.dx, b.dx, t), MathUtils.lerpFloat(a.dy, b.dy, t));
101+
}
102+
103+
public override string ToString() {
104+
return $"FractionalOffset({this.dx:0.0}, " +
105+
$"{this.dy:0.0})";
106+
}
107+
}
108+
}

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
@@ -523,6 +523,50 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
523523
}
524524
}
525525

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

0 commit comments

Comments
 (0)