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

Commit ca222ee

Browse files
author
Yuncong Zhang
committed
[1.5.4] Upgrade decoration image.
1 parent 90d7c2e commit ca222ee

File tree

3 files changed

+81
-49
lines changed

3 files changed

+81
-49
lines changed

Runtime/painting/decoration_image.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ public static void paintImage(
242242
outputSize += sliceBorder;
243243
destinationSize += sliceBorder;
244244
D.assert(sourceSize == inputSize,
245-
() => $"centerSlice was used with a BoxFit {fit} that does not guarantee that the image is fully visible.");
245+
() =>
246+
$"centerSlice was used with a BoxFit {fit} that does not guarantee that the image is fully visible.");
246247
}
247248

248249
if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize) {
@@ -281,13 +282,23 @@ public static void paintImage(
281282
Rect sourceRect = alignment.inscribe(
282283
sourceSize, Offset.zero & inputSize
283284
);
284-
foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) {
285-
canvas.drawImageRect(image, sourceRect, tileRect, paint);
285+
if (repeat == ImageRepeat.noRepeat) {
286+
canvas.drawImageRect(image, sourceRect, destinationRect, paint);
287+
}
288+
else {
289+
foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) {
290+
canvas.drawImageRect(image, sourceRect, tileRect, paint);
291+
}
286292
}
287293
}
288294
else {
289-
foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) {
290-
canvas.drawImageNine(image, centerSlice, tileRect, paint);
295+
if (repeat == ImageRepeat.noRepeat) {
296+
canvas.drawImageNine(image, centerSlice, destinationRect, paint);
297+
}
298+
else {
299+
foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) {
300+
canvas.drawImageNine(image, centerSlice, tileRect, paint);
301+
}
291302
}
292303
}
293304

@@ -298,11 +309,6 @@ public static void paintImage(
298309

299310
static IEnumerable<Rect> _generateImageTileRects(Rect outputRect, Rect fundamentalRect,
300311
ImageRepeat repeat) {
301-
if (repeat == ImageRepeat.noRepeat) {
302-
yield return fundamentalRect;
303-
yield break;
304-
}
305-
306312
int startX = 0;
307313
int startY = 0;
308314
int stopX = 0;

Runtime/painting/gradient.cs

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,46 @@ public _ColorsAndStops(List<Color> colors, List<float> stops) {
1717
public readonly List<Color> colors;
1818
public readonly List<float> stops;
1919

20-
public static _ColorsAndStops _interpolateColorsAndStops(
21-
List<Color> aColors, List<float> aStops, List<Color> bColors, List<float> bStops, float t) {
22-
D.assert(aColors.Count == bColors.Count,
23-
() => "Cannot interpolate between two gradients with a different number of colors.");
24-
D.assert((aStops == null && aColors.Count == 2) || (aStops != null && aStops.Count == aColors.Count));
25-
D.assert((bStops == null && bColors.Count == 2) || (bStops != null && bStops.Count == bColors.Count));
26-
List<Color> interpolatedColors = new List<Color>();
20+
static Color _sample(List<Color> colors, List<float> stops, float t) {
21+
D.assert(colors != null);
22+
D.assert(colors.isNotEmpty);
23+
D.assert(stops != null);
24+
D.assert(stops.isNotEmpty);
2725

28-
for (int i = 0; i < aColors.Count; i += 1) {
29-
interpolatedColors.Add(Color.lerp(aColors[i], bColors[i], t));
26+
if (t < stops.first()) {
27+
return colors.first();
3028
}
3129

32-
List<float> interpolatedStops = null;
33-
if (aStops != null || bStops != null) {
34-
aStops = aStops ?? new List<float> {0.0f, 1.0f};
35-
bStops = bStops ?? new List<float> {0.0f, 1.0f};
36-
37-
D.assert(aStops.Count == bStops.Count);
38-
interpolatedStops = new List<float>();
39-
for (int i = 0; i < aStops.Count; i += 1) {
40-
interpolatedStops.Add(MathUtils.lerpFloat(aStops[i], bStops[i], t).clamp(0.0f, 1.0f));
41-
}
30+
if (t < stops.last()) {
31+
return colors.last();
4232
}
4333

34+
int index = stops.FindLastIndex((float s) => { return s <= t; });
35+
D.assert(index != -1);
36+
return Color.lerp(colors[index], colors[index + 1],
37+
(t - stops[index]) / (stops[index + 1] - stops[index]));
38+
}
39+
40+
internal static _ColorsAndStops _interpolateColorsAndStops(
41+
List<Color> aColors,
42+
List<float> aStops,
43+
List<Color> bColors,
44+
List<float> bStops,
45+
float t) {
46+
D.assert(aColors.Count >= 2);
47+
D.assert(bColors.Count >= 2);
48+
D.assert(aStops.Count == aColors.Count);
49+
D.assert(bStops.Count == bColors.Count);
50+
51+
SplayTree<float, bool> stops = new SplayTree<float, bool>();
52+
stops.AddAll(aStops);
53+
stops.AddAll(bStops);
54+
55+
List<float> interpolatedStops = stops.Keys.ToList();
56+
List<Color> interpolatedColors = interpolatedStops.Select<float, Color>((float stop) => {
57+
return Color.lerp(_sample(aColors, aStops, stop), _sample(bColors, bStops, stop), t);
58+
}).ToList();
59+
4460
return new _ColorsAndStops(interpolatedColors, interpolatedStops);
4561
}
4662
}
@@ -65,10 +81,6 @@ protected List<float> _impliedStops() {
6581
return this.stops;
6682
}
6783

68-
if (this.colors.Count == 2) {
69-
return null;
70-
}
71-
7284
D.assert(this.colors.Count >= 2, () => "colors list must have at least two colors");
7385
float separation = 1.0f / (this.colors.Count - 1);
7486

@@ -159,16 +171,16 @@ public override Gradient scale(float factor) {
159171
}
160172

161173
protected override Gradient lerpFrom(Gradient a, float t) {
162-
if (a == null || (a is LinearGradient && a.colors.Count == this.colors.Count)) {
163-
return LinearGradient.lerp((LinearGradient) a, this, t);
174+
if (a == null || (a is LinearGradient)) {
175+
return lerp((LinearGradient) a, this, t);
164176
}
165177

166178
return base.lerpFrom(a, t);
167179
}
168180

169181
protected override Gradient lerpTo(Gradient b, float t) {
170-
if (b == null || (b is LinearGradient && b.colors.Count == this.colors.Count)) {
171-
return LinearGradient.lerp(this, (LinearGradient) b, t);
182+
if (b == null || (b is LinearGradient)) {
183+
return lerp(this, (LinearGradient) b, t);
172184
}
173185

174186
return base.lerpTo(b, t);
@@ -187,8 +199,12 @@ public static LinearGradient lerp(LinearGradient a, LinearGradient b, float t) {
187199
return (LinearGradient) a.scale(1.0f - t);
188200
}
189201

190-
_ColorsAndStops interpolated =
191-
_ColorsAndStops._interpolateColorsAndStops(a.colors, a.stops, b.colors, b.stops, t);
202+
_ColorsAndStops interpolated = _ColorsAndStops._interpolateColorsAndStops(
203+
a.colors,
204+
a._impliedStops(),
205+
b.colors,
206+
b._impliedStops(),
207+
t);
192208
return new LinearGradient(
193209
begin: Alignment.lerp(a.begin, b.begin, t),
194210
end: Alignment.lerp(a.end, b.end, t),
@@ -250,7 +266,7 @@ public override int GetHashCode() {
250266
return !Equals(left, right);
251267
}
252268

253-
public override String ToString() {
269+
public override string ToString() {
254270
return $"{this.GetType()}({this.begin}, {this.end}," +
255271
$"{this.colors.toStringList()}, {this.stops.toStringList()}, {this.tileMode})";
256272
}
@@ -296,16 +312,16 @@ public override Gradient scale(float factor) {
296312
}
297313

298314
protected override Gradient lerpFrom(Gradient a, float t) {
299-
if (a == null || (a is RadialGradient && a.colors.Count == this.colors.Count)) {
300-
return RadialGradient.lerp((RadialGradient) a, this, t);
315+
if (a == null || (a is RadialGradient)) {
316+
return lerp((RadialGradient) a, this, t);
301317
}
302318

303319
return base.lerpFrom(a, t);
304320
}
305321

306322
protected override Gradient lerpTo(Gradient b, float t) {
307-
if (b == null || (b is RadialGradient && b.colors.Count == this.colors.Count)) {
308-
return RadialGradient.lerp(this, (RadialGradient) b, t);
323+
if (b == null || (b is RadialGradient)) {
324+
return lerp(this, (RadialGradient) b, t);
309325
}
310326

311327
return base.lerpTo(b, t);
@@ -324,8 +340,12 @@ public static RadialGradient lerp(RadialGradient a, RadialGradient b, float t) {
324340
return (RadialGradient) a.scale(1.0f - t);
325341
}
326342

327-
_ColorsAndStops interpolated =
328-
_ColorsAndStops._interpolateColorsAndStops(a.colors, a.stops, b.colors, b.stops, t);
343+
_ColorsAndStops interpolated = _ColorsAndStops._interpolateColorsAndStops(
344+
a.colors,
345+
a._impliedStops(),
346+
b.colors,
347+
b._impliedStops(),
348+
t);
329349
return new RadialGradient(
330350
center: Alignment.lerp(a.center, b.center, t),
331351
radius: Mathf.Max(0.0f, MathUtils.lerpFloat(a.radius, b.radius, t)),
@@ -387,7 +407,7 @@ public override int GetHashCode() {
387407
return !Equals(left, right);
388408
}
389409

390-
public override String ToString() {
410+
public override string ToString() {
391411
return $"{this.GetType()}({this.center}, {this.radius}," +
392412
$"{this.colors.toStringList()}, {this.stops.toStringList()}, {this.tileMode})";
393413
}
@@ -439,7 +459,7 @@ public override Gradient scale(float factor) {
439459

440460
protected override Gradient lerpFrom(Gradient a, float t) {
441461
if (a == null || (a is SweepGradient && a.colors.Count == this.colors.Count)) {
442-
return SweepGradient.lerp((SweepGradient) a, this, t);
462+
return lerp((SweepGradient) a, this, t);
443463
}
444464

445465
return base.lerpFrom(a, t);
@@ -532,7 +552,7 @@ public override int GetHashCode() {
532552
return !Equals(left, right);
533553
}
534554

535-
public override String ToString() {
555+
public override string ToString() {
536556
return $"{this.GetType()}({this.center}, {this.startAngle}, {this.endAngle}, " +
537557
$"{this.colors.toStringList()}, {this.stops.toStringList()}, {this.tileMode})";
538558
}

Runtime/ui/txt/paragraph.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,12 @@ public void Add(KeyValuePair<TKey, TValue> item) {
912912
this.Set(item.Key, item.Value, throwOnExisting: true);
913913
}
914914

915+
public void AddAll(IEnumerable<TKey> list) {
916+
foreach (var key in list) {
917+
this.Add(new KeyValuePair<TKey, TValue>(key, default));
918+
}
919+
}
920+
915921
void Set(TKey key, TValue value, bool throwOnExisting) {
916922
if (this.count == 0) {
917923
this.version++;

0 commit comments

Comments
 (0)