1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using Unity . UIWidgets . foundation ;
4
- using UnityEngine ;
5
4
6
5
namespace Unity . UIWidgets . ui {
7
6
public class Picture {
8
- public Picture ( List < DrawCmd > drawCmds , Rect paintBounds ) {
7
+ public Picture ( List < DrawCmd > drawCmds , Rect paintBounds , bool isDynamic = false ) {
9
8
this . drawCmds = drawCmds ;
10
9
this . paintBounds = paintBounds ;
10
+ this . _isDynamic = isDynamic ;
11
11
}
12
12
13
13
public readonly List < DrawCmd > drawCmds ;
14
14
public readonly Rect paintBounds ;
15
+
16
+ public bool isDynamic {
17
+ get { return this . _isDynamic ; }
18
+ }
19
+
20
+ bool _isDynamic ;
15
21
}
16
22
17
23
public class PictureRecorder {
18
24
readonly List < DrawCmd > _drawCmds = new List < DrawCmd > ( ) ;
19
25
20
26
readonly List < CanvasState > _states = new List < CanvasState > ( ) ;
21
27
28
+ bool _isDynamic ;
29
+
22
30
public PictureRecorder ( ) {
23
31
this . reset ( ) ;
24
32
}
@@ -34,6 +42,7 @@ public Matrix3 getTotalMatrix() {
34
42
35
43
public void reset ( ) {
36
44
this . _drawCmds . Clear ( ) ;
45
+ this . _isDynamic = false ;
37
46
this . _states . Clear ( ) ;
38
47
this . _states . Add ( new CanvasState {
39
48
xform = Matrix3 . I ( ) ,
@@ -49,8 +58,8 @@ public Picture endRecording() {
49
58
throw new Exception ( "unmatched save/restore commands" ) ;
50
59
}
51
60
52
- var state = this . _getState ( ) ;
53
- return new Picture ( new List < DrawCmd > ( this . _drawCmds ) , state . paintBounds ) ;
61
+ var state = this . _getState ( ) ;
62
+ return new Picture ( new List < DrawCmd > ( this . _drawCmds ) , state . paintBounds , this . _isDynamic ) ;
54
63
}
55
64
56
65
public void addDrawCmd ( DrawCmd drawCmd ) {
@@ -70,80 +79,95 @@ public void addDrawCmd(DrawCmd drawCmd) {
70
79
} ) ;
71
80
break ;
72
81
}
82
+
73
83
case DrawRestore _: {
74
84
var stateToRestore = this . _getState ( ) ;
75
85
this . _states . RemoveAt ( this . _states . Count - 1 ) ;
76
86
var state = this . _getState ( ) ;
77
87
78
88
if ( ! stateToRestore . saveLayer ) {
79
89
state . paintBounds = stateToRestore . paintBounds ;
80
- } else {
90
+ }
91
+ else {
81
92
var paintBounds = stateToRestore . paintBounds . shift ( stateToRestore . layerOffset ) ;
82
93
paintBounds = state . xform . mapRect ( paintBounds ) ;
83
94
this . _addPaintBounds ( paintBounds ) ;
84
95
}
96
+
85
97
break ;
86
98
}
99
+
87
100
case DrawTranslate cmd : {
88
101
var state = this . _getState ( ) ;
89
102
state . xform = new Matrix3 ( state . xform ) ;
90
103
state . xform . preTranslate ( cmd . dx , cmd . dy ) ;
91
104
break ;
92
105
}
106
+
93
107
case DrawScale cmd : {
94
108
var state = this . _getState ( ) ;
95
109
state . xform = new Matrix3 ( state . xform ) ;
96
110
state . xform . preScale ( cmd . sx , ( cmd . sy ?? cmd . sx ) ) ;
97
111
break ;
98
112
}
113
+
99
114
case DrawRotate cmd : {
100
115
var state = this . _getState ( ) ;
101
116
state . xform = new Matrix3 ( state . xform ) ;
102
117
if ( cmd . offset == null ) {
103
118
state . xform . preRotate ( cmd . radians ) ;
104
- } else {
119
+ }
120
+ else {
105
121
state . xform . preRotate ( cmd . radians ,
106
122
cmd . offset . dx ,
107
123
cmd . offset . dy ) ;
108
124
}
125
+
109
126
break ;
110
127
}
128
+
111
129
case DrawSkew cmd : {
112
130
var state = this . _getState ( ) ;
113
131
state . xform = new Matrix3 ( state . xform ) ;
114
132
state . xform . preSkew ( cmd . sx , cmd . sy ) ;
115
133
break ;
116
134
}
135
+
117
136
case DrawConcat cmd : {
118
137
var state = this . _getState ( ) ;
119
138
state . xform = new Matrix3 ( state . xform ) ;
120
139
state . xform . preConcat ( cmd . matrix ) ;
121
140
break ;
122
141
}
142
+
123
143
case DrawResetMatrix _: {
124
144
var state = this . _getState ( ) ;
125
145
state . xform = Matrix3 . I ( ) ;
126
146
break ;
127
147
}
148
+
128
149
case DrawSetMatrix cmd : {
129
150
var state = this . _getState ( ) ;
130
151
state . xform = new Matrix3 ( cmd . matrix ) ;
131
152
break ;
132
153
}
154
+
133
155
case DrawClipRect cmd : {
134
156
var state = this . _getState ( ) ;
135
157
136
158
var rect = state . xform . mapRect ( cmd . rect ) ;
137
159
state . scissor = state . scissor == null ? rect : state . scissor . intersect ( rect ) ;
138
160
break ;
139
161
}
162
+
140
163
case DrawClipRRect cmd : {
141
164
var state = this . _getState ( ) ;
142
165
143
166
var rect = state . xform . mapRect ( cmd . rrect . outerRect ) ;
144
167
state . scissor = state . scissor == null ? rect : state . scissor . intersect ( rect ) ;
145
168
break ;
146
169
}
170
+
147
171
case DrawClipPath cmd : {
148
172
var state = this . _getState ( ) ;
149
173
var scale = XformUtils . getScale ( state . xform ) ;
@@ -154,6 +178,7 @@ public void addDrawCmd(DrawCmd drawCmd) {
154
178
state . scissor = state . scissor == null ? rect : state . scissor . intersect ( rect ) ;
155
179
break ;
156
180
}
181
+
157
182
case DrawPath cmd : {
158
183
var state = this . _getState ( ) ;
159
184
var scale = XformUtils . getScale ( state . xform ) ;
@@ -165,7 +190,8 @@ public void addDrawCmd(DrawCmd drawCmd) {
165
190
if ( paint . style == PaintingStyle . fill ) {
166
191
var cache = path . flatten ( scale * devicePixelRatio ) ;
167
192
mesh = cache . getFillMesh ( out _ ) . transform ( state . xform ) ;
168
- } else {
193
+ }
194
+ else {
169
195
float strokeWidth = ( paint . strokeWidth * scale ) . clamp ( 0 , 200.0f ) ;
170
196
float fringeWidth = 1 / devicePixelRatio ;
171
197
@@ -180,59 +206,84 @@ public void addDrawCmd(DrawCmd drawCmd) {
180
206
paint . strokeJoin ,
181
207
paint . strokeMiterLimit ) . transform ( state . xform ) ;
182
208
}
183
-
209
+
184
210
if ( paint . maskFilter != null && paint . maskFilter . sigma != 0 ) {
185
211
float sigma = scale * paint . maskFilter . sigma ;
186
212
float sigma3 = 3 * sigma ;
187
213
this . _addPaintBounds ( mesh . bounds . inflate ( sigma3 ) ) ;
188
- } else {
214
+ }
215
+ else {
189
216
this . _addPaintBounds ( mesh . bounds ) ;
190
217
}
218
+
191
219
break ;
192
220
}
221
+
193
222
case DrawImage cmd : {
194
223
var state = this . _getState ( ) ;
195
224
var rect = Rect . fromLTWH ( cmd . offset . dx , cmd . offset . dy ,
196
225
cmd . image . width , cmd . image . height ) ;
197
226
rect = state . xform . mapRect ( rect ) ;
198
227
this . _addPaintBounds ( rect ) ;
228
+ if ( cmd . image . isDynamic ) {
229
+ this . _isDynamic = true ;
230
+ }
231
+
199
232
break ;
200
233
}
234
+
201
235
case DrawImageRect cmd : {
202
236
var state = this . _getState ( ) ;
203
237
var rect = state . xform . mapRect ( cmd . dst ) ;
204
238
this . _addPaintBounds ( rect ) ;
239
+ if ( cmd . image . isDynamic ) {
240
+ this . _isDynamic = true ;
241
+ }
242
+
205
243
break ;
206
244
}
245
+
207
246
case DrawImageNine cmd : {
208
247
var state = this . _getState ( ) ;
209
248
var rect = state . xform . mapRect ( cmd . dst ) ;
210
249
this . _addPaintBounds ( rect ) ;
250
+ if ( cmd . image . isDynamic ) {
251
+ this . _isDynamic = true ;
252
+ }
253
+
211
254
break ;
212
255
}
256
+
213
257
case DrawPicture cmd : {
214
258
var state = this . _getState ( ) ;
215
259
var rect = state . xform . mapRect ( cmd . picture . paintBounds ) ;
216
260
this . _addPaintBounds ( rect ) ;
261
+ if ( cmd . picture . isDynamic ) {
262
+ this . _isDynamic = true ;
263
+ }
264
+
217
265
break ;
218
266
}
267
+
219
268
case DrawTextBlob cmd : {
220
269
var state = this . _getState ( ) ;
221
270
var scale = XformUtils . getScale ( state . xform ) ;
222
271
var rect = cmd . textBlob . boundsInText . shift ( cmd . offset ) ;
223
272
rect = state . xform . mapRect ( rect ) ;
224
-
273
+
225
274
var paint = cmd . paint ;
226
275
if ( paint . maskFilter != null && paint . maskFilter . sigma != 0 ) {
227
276
float sigma = scale * paint . maskFilter . sigma ;
228
277
float sigma3 = 3 * sigma ;
229
278
this . _addPaintBounds ( rect . inflate ( sigma3 ) ) ;
230
- } else {
279
+ }
280
+ else {
231
281
this . _addPaintBounds ( rect ) ;
232
282
}
233
-
283
+
234
284
break ;
235
285
}
286
+
236
287
default :
237
288
throw new Exception ( "unknown drawCmd: " + drawCmd ) ;
238
289
}
@@ -250,7 +301,8 @@ void _addPaintBounds(Rect paintBounds) {
250
301
251
302
if ( state . paintBounds . isEmpty ) {
252
303
state . paintBounds = paintBounds ;
253
- } else {
304
+ }
305
+ else {
254
306
state . paintBounds = state . paintBounds . expandToInclude ( paintBounds ) ;
255
307
}
256
308
}
0 commit comments