/
CanvasSceneComponent.cs
82 lines (66 loc) · 2.24 KB
/
CanvasSceneComponent.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using UniAquarium.Core.Paints;
using UnityEditor;
using UnityEngine.UIElements;
namespace UniAquarium.Core.Components
{
public abstract class CanvasSceneComponent<T, TActor, TOption> : VisualElement, IDisposable
where T : CanvasScene<TActor> where TOption : ISceneOption where TActor : IActor
{
private readonly bool _interactive;
private T _scene;
protected TOption SceneOption;
protected CanvasSceneComponent(bool interactive = true)
{
_interactive = interactive;
style.height = Length.Percent(100f);
style.width = Length.Percent(100f);
}
protected abstract float DeltaTime { get; }
public void Dispose()
{
Disable();
}
public virtual void Enable()
{
generateVisualContent -= OnGenerateVisualContent;
generateVisualContent += OnGenerateVisualContent;
EditorApplication.update -= Update;
EditorApplication.update += Update;
if (_interactive)
RegisterCallback<MouseDownEvent>(OnMouseDown);
else
pickingMode = PickingMode.Ignore;
}
public virtual void Disable()
{
generateVisualContent -= OnGenerateVisualContent;
EditorApplication.update -= Update;
if (_interactive)
UnregisterCallback<MouseDownEvent>(OnMouseDown);
}
public virtual void Update()
{
_scene?.Update(DeltaTime);
MarkDirtyRepaint();
}
protected abstract void Initialize(TOption sceneOption);
protected abstract T CreateScene();
protected abstract TOption CreateSceneOption(T scene);
private void OnGenerateVisualContent(MeshGenerationContext context)
{
var painter = context.painter2D;
if (_scene == null)
{
_scene = CreateScene();
SceneOption = CreateSceneOption(_scene);
Initialize(SceneOption);
}
_scene?.Draw(painter, DeltaTime);
}
private void OnMouseDown(MouseDownEvent evt)
{
_scene?.Press(evt);
}
}
}