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

Commit 90d7c2e

Browse files
author
Yuncong Zhang
committed
Add continuous_rectangle_border.
1 parent 7579cbd commit 90d7c2e

File tree

3 files changed

+177
-1
lines changed

3 files changed

+177
-1
lines changed

Runtime/painting/box_border.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ public Border(
2222
this.left = left ?? BorderSide.none;
2323
}
2424

25+
public static Border fromBorderSide(BorderSide side) {
26+
D.assert(side != null);
27+
return new Border(top: side, right: side, bottom: side, left: side);
28+
}
29+
2530
public static Border all(
2631
Color color = null,
2732
float width = 1.0f,
2833
BorderStyle style = BorderStyle.solid
2934
) {
3035
BorderSide side = new BorderSide(color: color, width: width, style: style);
31-
return new Border(top: side, right: side, bottom: side, left: side);
36+
return Border.fromBorderSide(side);
3237
}
3338

3439
public static Border merge(Border a, Border b) {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System.Runtime.CompilerServices;
2+
using Unity.UIWidgets.foundation;
3+
using Unity.UIWidgets.ui;
4+
using UnityEngine;
5+
using Canvas = Unity.UIWidgets.ui.Canvas;
6+
using Rect = Unity.UIWidgets.ui.Rect;
7+
8+
namespace Unity.UIWidgets.painting {
9+
public class ContinuousRectangleBorder : ShapeBorder {
10+
11+
public ContinuousRectangleBorder(
12+
BorderSide side = null,
13+
BorderRadius borderRadius = null) {
14+
this.side = side ?? BorderSide.none;
15+
this.borderRadius = borderRadius ?? BorderRadius.zero;
16+
}
17+
18+
public readonly BorderRadius borderRadius;
19+
20+
public readonly BorderSide side;
21+
22+
public override EdgeInsets dimensions {
23+
get {
24+
return EdgeInsets.all(this.side.width);
25+
}
26+
}
27+
28+
public override ShapeBorder scale(float t) {
29+
return new ContinuousRectangleBorder(
30+
side: this.side.scale(t),
31+
borderRadius: this.borderRadius * t
32+
);
33+
}
34+
35+
public override ShapeBorder lerpFrom(ShapeBorder a, float t) {
36+
D.assert(t != null);
37+
if (a is ContinuousRectangleBorder) {
38+
return new ContinuousRectangleBorder(
39+
side: BorderSide.lerp((a as ContinuousRectangleBorder).side, this.side, t),
40+
borderRadius: BorderRadius.lerp((a as ContinuousRectangleBorder).borderRadius,
41+
this.borderRadius, t)
42+
);
43+
}
44+
return base.lerpFrom(a, t);
45+
}
46+
47+
public override ShapeBorder lerpTo(ShapeBorder b, float t) {
48+
D.assert(t != null);
49+
if (b is ContinuousRectangleBorder) {
50+
return new ContinuousRectangleBorder(
51+
side: BorderSide.lerp(this.side, (b as ContinuousRectangleBorder).side, t),
52+
borderRadius: BorderRadius.lerp(this.borderRadius,
53+
(b as ContinuousRectangleBorder).borderRadius, t)
54+
);
55+
}
56+
return base.lerpTo(b, t);
57+
}
58+
59+
float _clampToShortest(RRect rrect, float value) {
60+
return value > rrect.shortestSide ? rrect.shortestSide : value;
61+
}
62+
63+
Path _getPath(RRect rrect) {
64+
float left = rrect.left;
65+
float right = rrect.right;
66+
float top = rrect.top;
67+
float bottom = rrect.bottom;
68+
69+
float tlRadiusX = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.tlRadiusX));
70+
float tlRadiusY = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.tlRadiusY));
71+
float trRadiusX = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.trRadiusX));
72+
float trRadiusY = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.trRadiusY));
73+
float blRadiusX = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.blRadiusX));
74+
float blRadiusY = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.blRadiusY));
75+
float brRadiusX = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.brRadiusX));
76+
float brRadiusY = Mathf.Max(0.0f, this._clampToShortest(rrect, rrect.brRadiusY));
77+
78+
Path path = new Path();
79+
path.moveTo(left, top + tlRadiusX);
80+
path.cubicTo(left, top, left, top, left + tlRadiusY, top);
81+
path.lineTo(right - trRadiusX, top);
82+
path.cubicTo(right, top, right, top, right, top + trRadiusY);
83+
path.lineTo(right, bottom - blRadiusX);
84+
path.cubicTo(right, bottom, right, bottom, right - blRadiusY, bottom);
85+
path.lineTo(left + brRadiusX, bottom);
86+
path.cubicTo(left, bottom, left, bottom, left, bottom - brRadiusY);
87+
path.close();
88+
return path;
89+
}
90+
91+
public override Path getInnerPath(Rect rect) {
92+
return this._getPath(this.borderRadius.toRRect(rect).deflate(this.side.width));
93+
}
94+
95+
public override Path getOuterPath(Rect rect) {
96+
return this._getPath(this.borderRadius.toRRect(rect));
97+
}
98+
99+
public override void paint(Canvas canvas, Rect rect) {
100+
if (rect.isEmpty) {
101+
return;
102+
}
103+
104+
switch (this.side.style) {
105+
case BorderStyle.none:
106+
break;
107+
case BorderStyle.solid:
108+
Path path = this.getOuterPath(rect);
109+
Paint paint = this.side.toPaint();
110+
canvas.drawPath(path, paint);
111+
break;
112+
}
113+
}
114+
public bool Equals(ContinuousRectangleBorder other) {
115+
if (ReferenceEquals(null, other)) {
116+
return false;
117+
}
118+
119+
if (ReferenceEquals(this, other)) {
120+
return true;
121+
}
122+
123+
return this.side == other.side && this.borderRadius == other.borderRadius;
124+
}
125+
126+
public override bool Equals(object obj) {
127+
if (ReferenceEquals(null, obj)) {
128+
return false;
129+
}
130+
131+
if (ReferenceEquals(this, obj)) {
132+
return true;
133+
}
134+
135+
if (obj.GetType() != this.GetType()) {
136+
return false;
137+
}
138+
139+
return this.Equals((ContinuousRectangleBorder) obj);
140+
}
141+
142+
public override int GetHashCode() {
143+
var hashCode = (this.side != null ? this.side.GetHashCode() : 0);
144+
hashCode = (hashCode * 397) ^ (this.borderRadius != null ? this.borderRadius.GetHashCode() : 0);
145+
return hashCode;
146+
}
147+
148+
public static bool operator ==(ContinuousRectangleBorder left, ContinuousRectangleBorder right) {
149+
return Equals(left, right);
150+
}
151+
152+
public static bool operator !=(ContinuousRectangleBorder left, ContinuousRectangleBorder right) {
153+
return !Equals(left, right);
154+
}
155+
156+
public override string ToString() {
157+
return $"{this.GetType()}({this.side}, {this.borderRadius})";
158+
}
159+
}
160+
}

Runtime/painting/continuous_rectangle_border.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.

0 commit comments

Comments
 (0)