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

Commit edd1e93

Browse files
author
Yuncong Zhang
committed
Add AntialiasSVGSample.
1 parent 23a5856 commit edd1e93

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System.Collections.Generic;
2+
using Unity.UIWidgets.engine;
3+
using Unity.UIWidgets.foundation;
4+
using Unity.UIWidgets.material;
5+
using Unity.UIWidgets.ui;
6+
using Unity.UIWidgets.widgets;
7+
using UnityEngine;
8+
using Canvas = Unity.UIWidgets.ui.Canvas;
9+
using Color = Unity.UIWidgets.ui.Color;
10+
using Gradient = Unity.UIWidgets.ui.Gradient;
11+
using Image = Unity.UIWidgets.ui.Image;
12+
using UIWidgetRect = Unity.UIWidgets.ui.Rect;
13+
14+
15+
public class AntialiasSVGSample : UIWidgetsPanel {
16+
protected override void OnEnable() {
17+
FontManager.instance.addFont(Resources.Load<Font>("MaterialIcons-Regular"), "Material Icons");
18+
FontManager.instance.addFont(Resources.Load<Font>("GalleryIcons"), "GalleryIcons");
19+
20+
base.OnEnable();
21+
}
22+
23+
protected override Widget createWidget() {
24+
Debug.Log("[SVG Testbed Panel Created]");
25+
26+
return new SVGTestbedPanelWidget();
27+
}
28+
}
29+
30+
public class SVGTestbedPanelWidget : StatefulWidget {
31+
public SVGTestbedPanelWidget(Key key = null) : base(key) { }
32+
33+
public override State createState() {
34+
return new SVGTestbedPanelWidgetState();
35+
}
36+
}
37+
38+
public class SVGTestbedPanelWidgetState : State<SVGTestbedPanelWidget> {
39+
static Texture2D texture6;
40+
41+
public class CurvePainter : AbstractCustomPainter {
42+
public override void paint(Canvas canvas, Size size) {
43+
var paint = new Paint()
44+
{
45+
color = new Color(0xFFFF0000),
46+
};
47+
48+
paint.color = Colors.yellow;
49+
paint.style = PaintingStyle.stroke;
50+
paint.strokeWidth = 3;
51+
52+
var startPoint = new Offset(0, size.height / 6);
53+
var controlPoint1 = new Offset(size.width / 4, 0);
54+
var controlPoint2 = new Offset(3 * size.width / 4, 0);
55+
var endPoint = new Offset(size.width, size.height / 6);
56+
57+
var path = new Path();
58+
path.moveTo(startPoint.dx, startPoint.dy);
59+
path.cubicTo(
60+
controlPoint1.dx, controlPoint1.dy,
61+
controlPoint2.dx, controlPoint2.dy,
62+
endPoint.dx, endPoint.dy
63+
);
64+
65+
path.moveTo(10, 10);
66+
path.lineTo(90, 10);
67+
path.lineTo(10, 90);
68+
path.lineTo(90, 90);
69+
path.winding(PathWinding.clockwise);
70+
path.close();
71+
72+
path.moveTo(110, 10);
73+
path.lineTo(190, 10);
74+
path.lineTo(110, 90);
75+
path.lineTo(190, 90);
76+
path.close();
77+
78+
path.addRect(UIWidgetRect.fromLTWH(10, 25, 180, 50));
79+
80+
path.addRect(UIWidgetRect.fromLTWH(200, 0, 100, 100));
81+
path.addRRect(RRect.fromRectAndRadius(UIWidgetRect.fromLTWH(225, 25, 50, 50), 10));
82+
path.winding(PathWinding.clockwise);
83+
path.addOval(UIWidgetRect.fromLTWH(200, 50, 100, 100));
84+
path.winding(PathWinding.clockwise);
85+
86+
87+
canvas.drawPath(path, paint);
88+
89+
paint = new Paint {
90+
color = new Color(0xFFFF0000),
91+
shader = Gradient.linear(
92+
new Offset(0, 0),
93+
new Offset(size.width, 200),
94+
new List<Color>() {
95+
Colors.red, Colors.black, Colors.green
96+
}, null, TileMode.clamp),
97+
};
98+
canvas.translate(0, 200);
99+
canvas.drawPath(path, paint);
100+
101+
canvas.translate(0, 200);
102+
// paint.maskFilter = MaskFilter.blur(BlurStyle.normal, 5);
103+
paint.shader = new ImageShader(new Image(texture6, true), TileMode.mirror);
104+
canvas.drawPath(path, paint);
105+
106+
canvas.translate(0, 200);
107+
paint = new Paint {
108+
color = new Color(0xFF00FF00),
109+
shader = Gradient.sweep(
110+
new Offset(size.width / 2, 100),
111+
new List<Color>() {
112+
Colors.red, Colors.black, Colors.green, Colors.red,
113+
}, null, TileMode.clamp, 0 * Mathf.PI / 180, 360 * Mathf.PI / 180),
114+
};
115+
canvas.drawPath(path, paint);
116+
117+
118+
paint.shader = Gradient.radial(
119+
new Offset(size.width / 2, 100), 200f,
120+
new List<Color>()
121+
{
122+
Colors.red, Colors.black, Colors.green
123+
}, null, TileMode.clamp);
124+
canvas.translate(0, 200);
125+
canvas.drawPath(path, paint);
126+
}
127+
128+
public override bool shouldRepaint(CustomPainter oldDelegate) {
129+
return true;
130+
}
131+
}
132+
133+
public override Widget build(BuildContext context) {
134+
texture6 = Resources.Load<Texture2D>("6");
135+
136+
return new SingleChildScrollView(
137+
child: new Container(
138+
child: new CustomPaint(
139+
painter: new CurvePainter(),
140+
child: new Container()
141+
),
142+
height: 1500
143+
)
144+
);
145+
}
146+
}

Samples/UIWidgetSample/AntialiasSVGSample.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)