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

Commit 57392e6

Browse files
author
Yuncong Zhang
committed
Implement rotated box.
1 parent 6776893 commit 57392e6

File tree

3 files changed

+156
-7
lines changed

3 files changed

+156
-7
lines changed

Runtime/rendering/rotated_box.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Unity.UIWidgets.foundation;
2+
using Unity.UIWidgets.gestures;
3+
using Unity.UIWidgets.rendering;
4+
using Unity.UIWidgets.ui;
5+
using UnityEngine;
6+
7+
namespace UIWidgets.Runtime.rendering {
8+
class RotatedBoxUtils {
9+
public const float _kQuarterTurnsInRadians = Mathf.PI / 2.0f;
10+
}
11+
12+
public class RenderRotatedBox : RenderObjectWithChildMixinRenderBox<RenderBox> {
13+
public RenderRotatedBox(
14+
int quarterTurns,
15+
RenderBox child = null
16+
) {
17+
this.child = child;
18+
this._quarterTurns = quarterTurns;
19+
}
20+
21+
public int quarterTurns {
22+
get { return this._quarterTurns; }
23+
set {
24+
if (this._quarterTurns == value) {
25+
return;
26+
}
27+
28+
this._quarterTurns = value;
29+
this.markNeedsLayout();
30+
}
31+
}
32+
33+
int _quarterTurns;
34+
35+
public bool _isVertical {
36+
get { return this.quarterTurns % 2 == 1; }
37+
}
38+
39+
protected override float computeMinIntrinsicWidth(float height) {
40+
if (this.child == null) {
41+
return 0.0f;
42+
}
43+
44+
return this._isVertical
45+
? this.child.getMinIntrinsicHeight(height)
46+
: this.child.getMinIntrinsicWidth(height);
47+
}
48+
49+
protected override float computeMaxIntrinsicWidth(float height) {
50+
if (this.child == null) {
51+
return 0.0f;
52+
}
53+
54+
return this._isVertical
55+
? this.child.getMaxIntrinsicHeight(height)
56+
: this.child.getMaxIntrinsicWidth(height);
57+
}
58+
59+
protected override float computeMinIntrinsicHeight(float width) {
60+
if (this.child == null) {
61+
return 0.0f;
62+
}
63+
64+
return this._isVertical ? this.child.getMinIntrinsicWidth(width) : this.child.getMinIntrinsicHeight(width);
65+
}
66+
67+
protected override float computeMaxIntrinsicHeight(float width) {
68+
if (this.child == null) {
69+
return 0.0f;
70+
}
71+
72+
return this._isVertical ? this.child.getMaxIntrinsicWidth(width) : this.child.getMaxIntrinsicHeight(width);
73+
}
74+
75+
Matrix3 _paintTransform;
76+
77+
protected override void performLayout() {
78+
this._paintTransform = null;
79+
if (this.child != null) {
80+
this.child.layout(this._isVertical ? this.constraints.flipped : this.constraints, parentUsesSize: true);
81+
this.size = this._isVertical
82+
? new Size(this.child.size.height, this.child.size.width)
83+
: this.child.size;
84+
this._paintTransform = Matrix3.I();
85+
this._paintTransform.preTranslate(this.size.width / 2.0f, this.size.height / 2.0f);
86+
this._paintTransform.preRotate(RotatedBoxUtils._kQuarterTurnsInRadians * (this.quarterTurns % 4));
87+
this._paintTransform.preTranslate(-this.child.size.width / 2.0f, -this.child.size.height / 2.0f);
88+
}
89+
else {
90+
this.performResize();
91+
}
92+
}
93+
94+
protected override bool hitTestChildren(
95+
HitTestResult result,
96+
Offset position = null
97+
) {
98+
D.assert(this._paintTransform != null || this.debugNeedsLayout || this.child == null);
99+
if (this.child == null || this._paintTransform == null) {
100+
return false;
101+
}
102+
103+
104+
Matrix3 inverse = Matrix3.I();
105+
this._paintTransform.invert(inverse);
106+
return this.child.hitTest(result, position: inverse.mapPoint(position));
107+
}
108+
109+
void _paintChild(PaintingContext context, Offset offset) {
110+
context.paintChild(this.child, offset);
111+
}
112+
113+
public override void paint(PaintingContext context, Offset offset) {
114+
if (this.child != null) {
115+
context.pushTransform(this.needsCompositing, offset, this._paintTransform, this._paintChild);
116+
}
117+
}
118+
119+
public override void applyPaintTransform(RenderObject child, Matrix3 transform) {
120+
if (this._paintTransform != null) {
121+
transform.preConcat(this._paintTransform);
122+
}
123+
124+
base.applyPaintTransform(child, transform);
125+
}
126+
}
127+
}

Runtime/rendering/rotated_box.cs.meta

Lines changed: 3 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: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using UIWidgets.Runtime.rendering;
45
using Unity.UIWidgets.foundation;
56
using Unity.UIWidgets.painting;
67
using Unity.UIWidgets.rendering;
@@ -661,15 +662,11 @@ public IntrinsicWidth(Key key = null, float? stepWidth = null, float? stepHeight
661662
public readonly float? stepHeight;
662663

663664
float? _stepWidth {
664-
get {
665-
return this.stepWidth == 0.0f ? null : this.stepWidth;
666-
}
665+
get { return this.stepWidth == 0.0f ? null : this.stepWidth; }
667666
}
668-
667+
669668
float? _stepHeight {
670-
get {
671-
return this.stepHeight == 0.0f ? null : this.stepHeight;
672-
}
669+
get { return this.stepHeight == 0.0f ? null : this.stepHeight; }
673670
}
674671

675672
public override RenderObject createRenderObject(BuildContext context) {
@@ -1237,6 +1234,28 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
12371234
}
12381235
}
12391236

1237+
public class RotatedBox : SingleChildRenderObjectWidget {
1238+
public RotatedBox(
1239+
Key key = null,
1240+
int? quarterTurns = null,
1241+
Widget child = null
1242+
) : base(key: key, child: child) {
1243+
D.assert(quarterTurns != null);
1244+
this.quarterTurns = quarterTurns;
1245+
}
1246+
1247+
1248+
public readonly int? quarterTurns;
1249+
1250+
public override RenderObject createRenderObject(BuildContext context) {
1251+
return new RenderRotatedBox(this.quarterTurns ?? 0);
1252+
}
1253+
1254+
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
1255+
(renderObject as RenderRotatedBox).quarterTurns = this.quarterTurns ?? 0;
1256+
}
1257+
}
1258+
12401259

12411260
public class Padding : SingleChildRenderObjectWidget {
12421261
public Padding(

0 commit comments

Comments
 (0)