-
Notifications
You must be signed in to change notification settings - Fork 0
/
UiComponentBuilder.cs
63 lines (47 loc) · 1.62 KB
/
UiComponentBuilder.cs
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
using System;
using JetBrains.Annotations;
using UnityEngine.UIElements;
namespace UniMob.UIToolkit
{
public class UiComponentBuilder : IDisposable
{
private readonly VisualElement _root;
private UiComponent _activeComponent;
private VisualTreeAsset _activeTemplate;
public UiComponentBuilder(Lifetime lifetime, [NotNull] VisualElement root)
{
_root = root ?? throw new ArgumentNullException(nameof(root), "root must be not null");
lifetime.Register(() => Build(null));
}
public void Dispose()
{
using var _ = Atom.NoWatch;
_activeComponent?.Dispose();
_activeComponent = null;
_root.Clear();
}
public void Build([CanBeNull] UiComponent component)
{
using var _ = Atom.NoWatch;
var template = component is UiTemplateComponent templateComponent ? templateComponent.Template : null;
var requiresTreeRebuild = component != null &&
(_activeTemplate == null || template == null || _activeTemplate != template);
_activeComponent?.Dispose();
if (requiresTreeRebuild)
{
_root.Clear();
}
_activeComponent = component;
if (_activeComponent == null)
{
return;
}
if (requiresTreeRebuild)
{
_activeTemplate = template;
_activeComponent?.BuildTree(_root);
}
_activeComponent?.Init(_root);
}
}
}