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

Commit 4f58791

Browse files
author
Yuncong Zhang
committed
Merge branch 'yczhang' into gallery
2 parents 9c5f392 + 2f278f6 commit 4f58791

File tree

8 files changed

+96
-28
lines changed

8 files changed

+96
-28
lines changed

README-ZH.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ UIWidgets Inspector工具用于可视化和浏览窗口小部件树。 你可以
210210

211211
#### Wiki
212212

213-
目前开发团队仍在改进UIWidgets Wiki。 由于UIWidgets主要来源于Flutter,你也可以参考Flutter Wiki中与UIWidgets API对应部分的详细描述。同时,你可以加入我们的讨论组(https://connect.unity.com/g/uiwidgets)。
213+
目前开发团队仍在改进UIWidgets Wiki。 由于UIWidgets主要来源于Flutter,你也可以参考Flutter Wiki中与UIWidgets API对应部分的详细描述。同时,你可以加入我们的讨论组( https://connect.unity.com/g/uiwidgets )。
214214

215215
#### 常问问题解答
216216

Runtime/material/text_theme.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ public override bool Equals(object obj) {
317317
return !Equals(left, right);
318318
}
319319

320+
int? _cachedHashCode = null;
320321
public override int GetHashCode() {
322+
if (this._cachedHashCode != null) {
323+
return this._cachedHashCode.Value;
324+
}
321325
unchecked {
322326
var hashCode = this.display4.GetHashCode();
323327
hashCode = (hashCode * 397) ^ this.display3.GetHashCode();
@@ -332,6 +336,8 @@ public override int GetHashCode() {
332336
hashCode = (hashCode * 397) ^ this.button.GetHashCode();
333337
hashCode = (hashCode * 397) ^ this.subtitle.GetHashCode();
334338
hashCode = (hashCode * 397) ^ this.overline.GetHashCode();
339+
340+
this._cachedHashCode = hashCode;
335341
return hashCode;
336342
}
337343
}

Runtime/material/theme_data.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,12 @@ public override bool Equals(object obj) {
749749
return !Equals(left, right);
750750
}
751751

752+
int? _cachedHashCode = null;
753+
752754
public override int GetHashCode() {
755+
if (this._cachedHashCode != null) {
756+
return this._cachedHashCode.Value;
757+
}
753758
unchecked {
754759
var hashCode = this.brightness.GetHashCode();
755760
hashCode = (hashCode * 397) ^ this.primaryColor.GetHashCode();
@@ -795,6 +800,8 @@ public override int GetHashCode() {
795800
hashCode = (hashCode * 397) ^ this.colorScheme.GetHashCode();
796801
hashCode = (hashCode * 397) ^ this.dialogTheme.GetHashCode();
797802
hashCode = (hashCode * 397) ^ this.typography.GetHashCode();
803+
804+
this._cachedHashCode = hashCode;
798805
return hashCode;
799806
}
800807
}
@@ -900,8 +907,8 @@ public bool Equals(_IdentityThemeDataCacheKey other) {
900907
return true;
901908
}
902909

903-
return this.baseTheme == other.baseTheme &&
904-
this.localTextGeometry == other.localTextGeometry;
910+
return ReferenceEquals(this.baseTheme, other.baseTheme) &&
911+
ReferenceEquals(this.localTextGeometry, other.localTextGeometry);
905912
}
906913

907914
public override bool Equals(object obj) {
@@ -948,9 +955,10 @@ public _FifoCache(int maximumSize) {
948955
public V putIfAbsent(K key, Func<V> value) {
949956
D.assert(key != null);
950957
D.assert(value != null);
951-
952-
if (this._cache.ContainsKey(key)) {
953-
return this._cache[key];
958+
959+
V get_value;
960+
if (this._cache.TryGetValue(key, out get_value)) {
961+
return get_value;
954962
}
955963

956964
if (this._cache.Count == this._maximumSize) {

Runtime/painting/image_cache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public ImageStreamCompleter putIfAbsent(object key, Func<ImageStreamCompleter> l
110110
D.assert(this._pendingImages.ContainsKey(key));
111111
this._pendingImages.Remove(key);
112112

113-
int imageSize = info?.image == null ? 0 : info.image.width & (info.image.height * 4);
113+
int imageSize = info?.image == null ? 0 : info.image.width * info.image.height * 4;
114114
_CachedImage cachedImage = new _CachedImage {
115115
completer = result,
116116
sizeBytes = imageSize,

Runtime/ui/painting/path.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,15 +1631,15 @@ public static void roundCapStart(this List<Vector3> dst, PathPoint p,
16311631
float dlx = dy;
16321632
float dly = -dx;
16331633

1634-
1635-
dst.Add(new Vector2(px + dlx * w, py + dly * w));
1636-
dst.Add(new Vector2(px - dlx * w, py - dly * w));
16371634
for (var i = 0; i < ncap; i++) {
1638-
float a = i / (ncap - 1) * Mathf.PI;
1635+
float a = (float) i / (ncap - 1) * Mathf.PI;
16391636
float ax = Mathf.Cos(a) * w, ay = Mathf.Sin(a) * w;
1637+
dst.Add(new Vector2(px - dlx * ax - dx * ay, py - dly * ax - dy * ay));
16401638
dst.Add(new Vector2(px, py));
1641-
dst.Add(new Vector2(px - dlx * ax + dx * ay, py - dly * ax + dy * ay));
16421639
}
1640+
1641+
dst.Add(new Vector2(px + dlx * w, py + dly * w));
1642+
dst.Add(new Vector2(px - dlx * w, py - dly * w));
16431643
}
16441644

16451645
public static void roundCapEnd(this List<Vector3> dst, PathPoint p,
@@ -1649,15 +1649,15 @@ public static void roundCapEnd(this List<Vector3> dst, PathPoint p,
16491649
float dlx = dy;
16501650
float dly = -dx;
16511651

1652+
dst.Add(new Vector2(px + dlx * w, py + dly * w));
1653+
dst.Add(new Vector2(px - dlx * w, py - dly * w));
1654+
16521655
for (var i = 0; i < ncap; i++) {
1653-
float a = i / (ncap - 1) * Mathf.PI;
1656+
float a = (float) i / (ncap - 1) * Mathf.PI;
16541657
float ax = Mathf.Cos(a) * w, ay = Mathf.Sin(a) * w;
1655-
dst.Add(new Vector2(px - dlx * ax - dx * ay, py - dly * ax - dy * ay));
16561658
dst.Add(new Vector2(px, py));
1659+
dst.Add(new Vector2(px - dlx * ax + dx * ay, py - dly * ax + dy * ay));
16571660
}
1658-
1659-
dst.Add(new Vector2(px + dlx * w, py + dly * w));
1660-
dst.Add(new Vector2(px - dlx * w, py - dly * w));
16611661
}
16621662

16631663
public static void chooseBevel(bool bevel, PathPoint p0, PathPoint p1, float w,
@@ -1704,7 +1704,7 @@ public static void roundJoin(this List<Vector3> dst, PathPoint p0, PathPoint p1,
17041704

17051705
var n = Mathf.CeilToInt((a0 - a1) / Mathf.PI * ncap).clamp(2, ncap);
17061706
for (var i = 0; i < n; i++) {
1707-
float u = i / (n - 1);
1707+
float u = (float) i / (n - 1);
17081708
float a = a0 + u * (a1 - a0);
17091709
float rx = p1.x + Mathf.Cos(a) * rw;
17101710
float ry = p1.y + Mathf.Sin(a) * rw;
@@ -1732,7 +1732,7 @@ public static void roundJoin(this List<Vector3> dst, PathPoint p0, PathPoint p1,
17321732

17331733
var n = Mathf.CeilToInt((a1 - a0) / Mathf.PI * ncap).clamp(2, ncap);
17341734
for (var i = 0; i < n; i++) {
1735-
float u = i / (n - 1);
1735+
float u = (float) i / (n - 1);
17361736
float a = a0 + u * (a1 - a0);
17371737
float lx = p1.x + Mathf.Cos(a) * lw;
17381738
float ly = p1.y + Mathf.Sin(a) * lw;

Runtime/ui/painting/shadow_utils.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using UnityEngine;
22

33
namespace Unity.UIWidgets.ui {
4-
public static class ShadowUtils {
5-
public const bool _drawShadowFlag = false;
6-
4+
static class ShadowUtils {
5+
public static bool kUseFastShadow = true;
6+
77
public const float kAmbientHeightFactor = 1.0f / 128.0f;
88
public const float kAmbientGeomFactor = 64.0f;
99

@@ -137,10 +137,16 @@ float heightFunc(float x, float y) {
137137

138138
public static void drawShadow(Canvas canvas, Path path, Vector3 zPlaneParams, Vector3 devLightPos,
139139
float lightRadius, Color ambientColor, Color spotColor, int flags) {
140-
if (!_drawShadowFlag) {
141-
return;
140+
if (kUseFastShadow) {
141+
drawShadowFast(canvas, path, zPlaneParams, devLightPos, lightRadius, ambientColor, spotColor, flags);
142142
}
143-
143+
else {
144+
drawShadowFull(canvas, path, zPlaneParams, devLightPos, lightRadius, ambientColor, spotColor, flags);
145+
}
146+
}
147+
148+
static void drawShadowFull(Canvas canvas, Path path, Vector3 zPlaneParams, Vector3 devLightPos,
149+
float lightRadius, Color ambientColor, Color spotColor, int flags) {
144150
Matrix3 viewMatrix = canvas.getTotalMatrix();
145151

146152
//ambient light
@@ -177,5 +183,35 @@ public static void drawShadow(Canvas canvas, Path path, Vector3 zPlaneParams, Ve
177183

178184
canvas.restore();
179185
}
186+
187+
static void drawShadowFast(Canvas canvas, Path path, Vector3 zPlaneParams, Vector3 devLightPos,
188+
float lightRadius, Color ambientColor, Color spotColor, int flags) {
189+
Matrix3 viewMatrix = canvas.getTotalMatrix();
190+
191+
//ambient light
192+
float devSpaceOutset = ambientBlurRadius(zPlaneParams.z);
193+
float oneOverA = ambientRecipAlpha(zPlaneParams.z);
194+
float blurRadius = 0.5f * devSpaceOutset * oneOverA;
195+
float strokeWidth = 0.5f * (devSpaceOutset - blurRadius);
196+
197+
Paint paint = new Paint {color = ambientColor, strokeWidth = strokeWidth, style = PaintingStyle.fill};
198+
canvas.drawPath(path, paint);
199+
200+
//spot light
201+
Matrix3 shadowMatrix = Matrix3.I();
202+
float radius = 0.0f;
203+
204+
if (!getSpotShadowTransform(devLightPos, lightRadius, viewMatrix, zPlaneParams, path.getBounds(),
205+
shadowMatrix, ref radius)) {
206+
return;
207+
}
208+
209+
canvas.save();
210+
canvas.setMatrix(shadowMatrix);
211+
Paint paint2 = new Paint {color = spotColor};
212+
canvas.drawPath(path, paint2);
213+
214+
canvas.restore();
215+
}
180216
}
181217
}

Runtime/widgets/automatic_keep_alive.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ public abstract class AutomaticKeepAliveClientWithTickerProviderStateMixin<T> :
218218

219219
public Ticker createTicker(TickerCallback onTick) {
220220
this._tickers = this._tickers ?? new HashSet<Ticker>();
221-
var result = new _AutomaticWidgetTicker<T>(onTick, this, debugLabel: "created by " + this);
221+
222+
var debugLabel = "";
223+
D.assert(() => {
224+
debugLabel = "created by " + this;
225+
return true;
226+
});
227+
var result = new _AutomaticWidgetTicker<T>(onTick, this, debugLabel: debugLabel);
222228
this._tickers.Add(result);
223229
return result;
224230
}

Runtime/widgets/ticker_provider.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public Ticker createTicker(TickerCallback onTick) {
4747
"mixing in a SingleTickerProviderStateMixin, use a regular TickerProviderStateMixin."
4848
);
4949
});
50-
this._ticker = new Ticker(onTick, debugLabel: "created by " + this);
50+
51+
var debugLabel = "";
52+
D.assert(() => {
53+
debugLabel = "created by " + this;
54+
return true;
55+
});
56+
this._ticker = new Ticker(onTick, debugLabel: debugLabel);
5157
return this._ticker;
5258
}
5359

@@ -108,7 +114,13 @@ public abstract class TickerProviderStateMixin<T> : State<T>, TickerProvider whe
108114

109115
public Ticker createTicker(TickerCallback onTick) {
110116
this._tickers = this._tickers ?? new HashSet<Ticker>();
111-
var result = new _WidgetTicker<T>(onTick, this, debugLabel: "created by " + this);
117+
118+
var debugLabel = "";
119+
D.assert(() => {
120+
debugLabel = "created by " + this;
121+
return true;
122+
});
123+
var result = new _WidgetTicker<T>(onTick, this, debugLabel: debugLabel);
112124
this._tickers.Add(result);
113125
return result;
114126
}

0 commit comments

Comments
 (0)