forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUQuery.cs
More file actions
665 lines (552 loc) · 21.6 KB
/
Copy pathUQuery.cs
File metadata and controls
665 lines (552 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEngine.StyleSheets;
namespace UnityEngine.Experimental.UIElements
{
public static class UQuery
{
//This scheme saves us 20 bytes instead of saving a Func<object, bool> directly (12 vs 32 bytes)
internal interface IVisualPredicateWrapper
{
bool Predicate(object e);
}
internal class IsOfType<T> : IVisualPredicateWrapper where T : VisualElement
{
public static IsOfType<T> s_Instance = new IsOfType<T>();
public bool Predicate(object e)
{
return e is T;
}
}
internal class PredicateWrapper<T> : IVisualPredicateWrapper where T : VisualElement
{
private Func<T, bool> predicate;
public PredicateWrapper(Func<T, bool> p)
{
predicate = p;
}
public bool Predicate(object e)
{
T element = e as T;
if (element != null)
{
return predicate(element);
}
return false;
}
}
private abstract class UQueryMatcher : HierarchyTraversal
{
internal List<RuleMatcher> m_Matchers;
public override bool ShouldSkipElement(VisualElement element)
{
return false;
}
protected override bool MatchSelectorPart(VisualElement element, StyleSelector selector, StyleSelectorPart part)
{
if (part.type == StyleSelectorType.Predicate)
{
IVisualPredicateWrapper w = part.tempData as IVisualPredicateWrapper;
return w != null && w.Predicate(element);
}
return base.MatchSelectorPart(element, selector, part);
}
public override void Traverse(VisualElement element)
{
TraverseRecursive(element, 0, new List<RuleMatcher>(m_Matchers));
}
public virtual void Run(VisualElement root, List<RuleMatcher> matchers)
{
m_Matchers = matchers;
Traverse(root);
}
}
private abstract class SingleQueryMatcher : UQueryMatcher
{
public VisualElement match { get; set; }
public override void Run(VisualElement root, List<RuleMatcher> matchers)
{
match = null;
base.Run(root, matchers);
}
}
private class FirstQueryMatcher : SingleQueryMatcher
{
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element)
{
if (match == null)
match = element;
return true;
}
}
private class LastQueryMatcher : SingleQueryMatcher
{
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element)
{
match = element;
return false;
}
}
private class IndexQueryMatcher : SingleQueryMatcher
{
private int matchCount = -1;
private int _matchIndex;
public int matchIndex
{
get { return _matchIndex; }
set
{
matchCount = -1;
_matchIndex = value;
}
}
public override void Run(VisualElement root, List<RuleMatcher> matchers)
{
matchCount = -1;
base.Run(root, matchers);
}
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element)
{
++matchCount;
if (matchCount == _matchIndex)
{
match = element;
}
return matchCount >= _matchIndex;
}
}
//this makes it non-thread safe. But saves on allocations...
private static FirstQueryMatcher s_First = new FirstQueryMatcher();
private static LastQueryMatcher s_Last = new LastQueryMatcher();
private static IndexQueryMatcher s_Index = new IndexQueryMatcher();
public struct QueryBuilder<T> where T : VisualElement
{
private List<StyleSelector> m_StyleSelectors;
private List<StyleSelector> styleSelectors { get { return m_StyleSelectors ?? (m_StyleSelectors = new List<StyleSelector>()); } }
private List<StyleSelectorPart> m_Parts;
private List<StyleSelectorPart> parts { get { return m_Parts ?? (m_Parts = new List<StyleSelectorPart>()); } }
private VisualElement m_Element;
private List<RuleMatcher> m_Matchers;
private StyleSelectorRelationship m_Relationship;
private int pseudoStatesMask;
private int negatedPseudoStatesMask;
public QueryBuilder(VisualElement visualElement)
: this()
{
m_Element = visualElement;
m_Parts = null;
m_StyleSelectors = null;
m_Relationship = StyleSelectorRelationship.None;
m_Matchers = new List<RuleMatcher>();
pseudoStatesMask = negatedPseudoStatesMask = 0;
}
public QueryBuilder<T> Class(string classname)
{
AddClass(classname);
return this;
}
public QueryBuilder<T> Name(string id)
{
AddName(id);
return this;
}
public QueryBuilder<T2> Descendents<T2>(string name = null, params string[] classNames) where T2 : VisualElement
{
FinishCurrentSelector();
AddType<T2>();
AddName(name);
AddClasses(classNames);
return AddRelationship<T2>(StyleSelectorRelationship.Descendent);
}
public QueryBuilder<T2> Descendents<T2>(string name = null, string classname = null) where T2 : VisualElement
{
FinishCurrentSelector();
AddType<T2>();
AddName(name);
AddClass(classname);
return AddRelationship<T2>(StyleSelectorRelationship.Descendent);
}
public QueryBuilder<T2> Children<T2>(string name = null, params string[] classes) where T2 : VisualElement
{
FinishCurrentSelector();
AddType<T2>();
AddName(name);
AddClasses(classes);
return AddRelationship<T2>(StyleSelectorRelationship.Child);
}
public QueryBuilder<T2> Children<T2>(string name = null, string className = null) where T2 : VisualElement
{
FinishCurrentSelector();
AddType<T2>();
AddName(name);
AddClass(className);
return AddRelationship<T2>(StyleSelectorRelationship.Child);
}
public QueryBuilder<T2> OfType<T2>(string name = null, params string[] classes) where T2 : VisualElement
{
AddType<T2>();
AddName(name);
AddClasses(classes);
return AddRelationship<T2>(StyleSelectorRelationship.None);
}
public QueryBuilder<T2> OfType<T2>(string name = null, string className = null) where T2 : VisualElement
{
AddType<T2>();
AddName(name);
AddClass(className);
return AddRelationship<T2>(StyleSelectorRelationship.None);
}
public QueryBuilder<T> Where(Func<T, bool> selectorPredicate)
{
//we can't use a static instance as in the QueryState<T>.ForEach below since the query might be long lived
parts.Add(StyleSelectorPart.CreatePredicate(new PredicateWrapper<T>(selectorPredicate)));
return this;
}
private void AddClass(string c)
{
if (c != null)
parts.Add(StyleSelectorPart.CreateClass(c));
}
private void AddClasses(params string[] classes)
{
if (classes != null)
{
for (int i = 0; i < classes.Length; i++)
AddClass(classes[i]);
}
}
private void AddName(string id)
{
if (id != null)
parts.Add(StyleSelectorPart.CreateId(id));
}
private void AddType<T2>() where T2 : VisualElement
{
if (typeof(T2) != typeof(VisualElement))
parts.Add(StyleSelectorPart.CreatePredicate(IsOfType<T2>.s_Instance));
}
private QueryBuilder<T> AddPseudoState(PseudoStates s)
{
pseudoStatesMask = pseudoStatesMask | (int)s;
return this;
}
private QueryBuilder<T> AddNegativePseudoState(PseudoStates s)
{
negatedPseudoStatesMask = negatedPseudoStatesMask | (int)s;
return this;
}
public QueryBuilder<T> Active()
{
return AddPseudoState(PseudoStates.Active);
}
public QueryBuilder<T> NotActive()
{
return AddNegativePseudoState(PseudoStates.Active);
}
public QueryBuilder<T> Visible()
{
return Where(e => e.visible);
}
public QueryBuilder<T> NotVisible()
{
return Where(e => !e.visible);
}
public QueryBuilder<T> Hovered()
{
return AddPseudoState(PseudoStates.Hover);
}
public QueryBuilder<T> NotHovered()
{
return AddNegativePseudoState(PseudoStates.Hover);
}
public QueryBuilder<T> Checked()
{
return AddPseudoState(PseudoStates.Checked);
}
public QueryBuilder<T> NotChecked()
{
return AddNegativePseudoState(PseudoStates.Checked);
}
public QueryBuilder<T> Selected()
{
return AddPseudoState(PseudoStates.Selected);
}
public QueryBuilder<T> NotSelected()
{
return AddNegativePseudoState(PseudoStates.Selected);
}
public QueryBuilder<T> Enabled()
{
return AddNegativePseudoState(PseudoStates.Disabled);
}
public QueryBuilder<T> NotEnabled()
{
return AddPseudoState(PseudoStates.Disabled);
}
public QueryBuilder<T> Focused()
{
return AddPseudoState(PseudoStates.Focus);
}
public QueryBuilder<T> NotFocused()
{
return AddNegativePseudoState(PseudoStates.Focus);
}
private QueryBuilder<T2> AddRelationship<T2>(StyleSelectorRelationship relationship) where T2 : VisualElement
{
return new QueryBuilder<T2>(m_Element)
{
m_Matchers = m_Matchers,
m_Parts = m_Parts,
m_StyleSelectors = m_StyleSelectors,
m_Relationship = relationship == StyleSelectorRelationship.None ? m_Relationship : relationship,
pseudoStatesMask = pseudoStatesMask,
negatedPseudoStatesMask = negatedPseudoStatesMask
};
}
void AddPseudoStatesRuleIfNecessasy()
{
if (pseudoStatesMask != 0 ||
negatedPseudoStatesMask != 0)
{
parts.Add(new StyleSelectorPart() {type = StyleSelectorType.PseudoClass});
}
}
private void FinishSelector()
{
FinishCurrentSelector();
if (styleSelectors.Count > 0)
{
var selector = new StyleComplexSelector();
selector.selectors = styleSelectors.ToArray();
styleSelectors.Clear();
m_Matchers.Add(new RuleMatcher { complexSelector = selector });
}
}
private bool CurrentSelectorEmpty()
{
return parts.Count == 0 &&
m_Relationship == StyleSelectorRelationship.None &&
pseudoStatesMask == 0 &&
negatedPseudoStatesMask == 0;
}
private void FinishCurrentSelector()
{
if (!CurrentSelectorEmpty())
{
StyleSelector sel = new StyleSelector();
sel.previousRelationship = m_Relationship;
AddPseudoStatesRuleIfNecessasy();
sel.parts = m_Parts.ToArray();
sel.pseudoStateMask = pseudoStatesMask;
sel.negatedPseudoStateMask = negatedPseudoStatesMask;
styleSelectors.Add(sel);
m_Parts.Clear();
pseudoStatesMask = negatedPseudoStatesMask = 0;
}
}
public QueryState<T> Build()
{
FinishSelector();
return new QueryState<T>(m_Element, m_Matchers);
}
// Quick One-liners accessors
public static implicit operator T(QueryBuilder<T> s)
{
return s.First();
}
public T First()
{
return Build().First();
}
public T Last()
{
return Build().Last();
}
public List<T> ToList()
{
return Build().ToList();
}
public void ToList(List<T> results)
{
Build().ToList(results);
}
public T AtIndex(int index)
{
return Build().AtIndex(index);
}
public void ForEach<T2>(List<T2> result, Func<T, T2> funcCall)
{
Build().ForEach(result, funcCall);
}
public List<T2> ForEach<T2>(Func<T, T2> funcCall)
{
return Build().ForEach(funcCall);
}
public void ForEach(Action<T> funcCall)
{
Build().ForEach(funcCall);
}
}
public struct QueryState<T> where T : VisualElement
{
private readonly VisualElement m_Element;
private readonly List<RuleMatcher> m_Matchers;
internal QueryState(VisualElement element, List<RuleMatcher> matchers)
{
m_Element = element;
m_Matchers = matchers;
}
public QueryState<T> RebuildOn(VisualElement element)
{
return new QueryState<T>(element, m_Matchers);
}
public T First()
{
s_First.Run(m_Element, m_Matchers);
// We need to make sure we don't leak a ref to the VisualElement.
var match = s_First.match as T;
s_First.match = null;
return match;
}
public T Last()
{
s_Last.Run(m_Element, m_Matchers);
// We need to make sure we don't leak a ref to the VisualElement.
var match = s_Last.match as T;
s_Last.match = null;
return match;
}
private class ListQueryMatcher : UQueryMatcher
{
public List<T> matches { get; set; }
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element)
{
matches.Add(element as T);
return false;
}
public void Reset()
{
matches = null;
}
}
private static readonly ListQueryMatcher s_List = new ListQueryMatcher();
public void ToList(List<T> results)
{
s_List.matches = results;
s_List.Run(m_Element, m_Matchers);
s_List.Reset();
}
public List<T> ToList()
{
List<T> result = new List<T>();
ToList(result);
return result;
}
public T AtIndex(int index)
{
s_Index.matchIndex = index;
s_Index.Run(m_Element, m_Matchers);
// We need to make sure we don't leak a ref to the VisualElement.
var match = s_Index.match as T;
s_Index.match = null;
return match;
}
//Convoluted trick so save on allocating memory for delegates or lambdas
private class ActionQueryMatcher : UQueryMatcher
{
internal Action<T> callBack { get; set; }
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element)
{
T castedElement = element as T;
if (castedElement != null)
{
callBack(castedElement);
}
return false;
}
};
private static ActionQueryMatcher s_Action = new ActionQueryMatcher();
public void ForEach(Action<T> funcCall)
{
s_Action.callBack = funcCall;
s_Action.Run(m_Element, m_Matchers);
s_Action.callBack = null;
}
private class DelegateQueryMatcher<TReturnType> : UQueryMatcher
{
public Func<T, TReturnType> callBack { get; set; }
public List<TReturnType> result { get; set; }
public static DelegateQueryMatcher<TReturnType> s_Instance = new DelegateQueryMatcher<TReturnType>();
public override bool OnRuleMatchedElement(RuleMatcher matcher, VisualElement element)
{
T castedElement = element as T;
if (castedElement != null)
{
result.Add(callBack(castedElement));
}
return false;
}
}
public void ForEach<T2>(List<T2> result, Func<T, T2> funcCall)
{
var matcher = DelegateQueryMatcher<T2>.s_Instance;
matcher.callBack = funcCall;
matcher.result = result;
matcher.Run(m_Element, m_Matchers);
matcher.callBack = null;
matcher.result = null;
}
public List<T2> ForEach<T2>(Func<T, T2> funcCall)
{
List<T2> result = new List<T2>();
ForEach(result, funcCall);
return result;
}
}
}
public static class UQueryExtensions
{
public static T Q<T>(this VisualElement e, string name = null, params string[] classes) where T : VisualElement
{
return e.Query<T>(name, classes).Build().First();
}
public static T Q<T>(this VisualElement e, string name = null, string className = null) where T : VisualElement
{
return e.Query<T>(name, className).Build().First();
}
public static VisualElement Q(this VisualElement e, string name = null, params string[] classes)
{
return e.Query<VisualElement>(name, classes).Build().First();
}
public static VisualElement Q(this VisualElement e, string name = null, string className = null)
{
return e.Query<VisualElement>(name, className).Build().First();
}
public static UQuery.QueryBuilder<VisualElement> Query(this VisualElement e, string name = null, params string[] classes)
{
return e.Query<VisualElement>(name, classes);
}
public static UQuery.QueryBuilder<VisualElement> Query(this VisualElement e, string name = null, string className = null)
{
return e.Query<VisualElement>(name, className);
}
public static UQuery.QueryBuilder<T> Query<T>(this VisualElement e, string name = null, params string[] classes) where T : VisualElement
{
var queryBuilder = new UQuery.QueryBuilder<VisualElement>(e).OfType<T>(name, classes);
return queryBuilder;
}
public static UQuery.QueryBuilder<T> Query<T>(this VisualElement e, string name = null, string className = null) where T : VisualElement
{
var queryBuilder = new UQuery.QueryBuilder<VisualElement>(e).OfType<T>(name, className);
return queryBuilder;
}
public static UQuery.QueryBuilder<VisualElement> Query(this VisualElement e)
{
return new UQuery.QueryBuilder<VisualElement>(e);
}
}
}