SignalReceiverTreeView.cs
5.48 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
using System;
using System.Collections.Generic;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline.Signals
{
class SignalReceiverTreeView : TreeView
{
public bool dirty { private get; set; }
SerializedProperty signals { get; set; }
SerializedProperty events { get; set; }
readonly SignalReceiver m_Target;
const float k_VerticalPadding = 5;
const float k_HorizontalPadding = 5;
public SignalReceiverTreeView(TreeViewState state, MultiColumnHeader multiColumnHeader, SignalReceiver receiver, SerializedObject serializedObject)
: base(state, multiColumnHeader)
{
m_Target = receiver;
useScrollView = true;
SetSerializedProperties(serializedObject);
getNewSelectionOverride = (item, selection, shift) => new List<int>(); // Disable Selection
}
SignalAsset signalAssetContext { get; set; }
public bool readonlySignals { get; set; }
public void SetSignalContext(SignalAsset assetContext = null)
{
signalAssetContext = assetContext;
dirty = true;
}
void SetSerializedProperties(SerializedObject serializedObject)
{
signals = SignalReceiverUtility.FindSignalsProperty(serializedObject);
events = SignalReceiverUtility.FindEventsProperty(serializedObject);
Reload();
}
public void Draw()
{
var rect = EditorGUILayout.GetControlRect(true, GetTotalHeight());
OnGUI(rect);
}
public void RefreshIfDirty()
{
var signalsListSizeHasChanged = signals.arraySize != GetRows().Count;
if (dirty || signalsListSizeHasChanged)
Reload();
dirty = false;
}
public static MultiColumnHeaderState.Column[] GetColumns()
{
return new[]
{
new MultiColumnHeaderState.Column
{
headerContent = EditorGUIUtility.TrTextContent("Signal"),
contextMenuText = "",
headerTextAlignment = TextAlignment.Center,
width = 50, minWidth = 50,
autoResize = true,
allowToggleVisibility = false,
canSort = false
},
new MultiColumnHeaderState.Column
{
headerContent = EditorGUIUtility.TrTextContent("Reaction"),
contextMenuText = "",
headerTextAlignment = TextAlignment.Center,
width = 120, minWidth = 120,
autoResize = true,
allowToggleVisibility = false,
canSort = false
}
};
}
protected override TreeViewItem BuildRoot()
{
var root = new TreeViewItem(-1, -1) { children = new List<TreeViewItem>() };
var matchingId = signalAssetContext != null && readonlySignals ? FindIdForSignal(signals, signalAssetContext) : -1;
if (matchingId >= 0)
AddItem(root, matchingId);
for (var i = 0; i < signals.arraySize; ++i)
{
if (i == matchingId) continue;
AddItem(root, i, !readonlySignals);
}
return root;
}
protected override void RowGUI(RowGUIArgs args)
{
var item = (SignalReceiverItem)args.item;
for (var i = 0; i < args.GetNumVisibleColumns(); ++i)
{
var rect = args.GetCellRect(i);
rect.y += k_VerticalPadding;
item.Draw(rect, args.GetColumn(i), args.row, k_HorizontalPadding, m_Target);
}
}
protected override float GetCustomRowHeight(int row, TreeViewItem treeItem)
{
var item = treeItem as SignalReceiverItem;
return item.GetHeight() + k_VerticalPadding;
}
void AddItem(TreeViewItem root, int id, bool enabled = true)
{
var signal = signals.GetArrayElementAtIndex(id);
var evt = events.GetArrayElementAtIndex(id);
root.children.Add(new SignalReceiverItem(signal, evt, id, readonlySignals, enabled, this));
}
float GetTotalHeight()
{
var height = 0.0f;
foreach (var item in GetRows())
{
var signalListItem = item as SignalReceiverItem;
height += signalListItem.GetHeight() + k_VerticalPadding;
}
var scrollbarPadding = showingHorizontalScrollBar ? GUI.skin.horizontalScrollbar.fixedHeight : k_VerticalPadding;
return height + multiColumnHeader.height + scrollbarPadding;
}
static int FindIdForSignal(SerializedProperty signals, SignalAsset signalToFind)
{
for (var i = 0; i < signals.arraySize; ++i)
{
//signal in the receiver that matches the current signal asset will be displayed first
var serializedProperty = signals.GetArrayElementAtIndex(i);
var signalReferenceValue = serializedProperty.objectReferenceValue;
var signalToFindRefValue = signalToFind;
if (signalReferenceValue != null && signalReferenceValue == signalToFindRefValue)
return i;
}
return -1;
}
}
}