SignalReceiverInspector.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. using UnityObject = UnityEngine.Object;
  3. using UnityEditor.IMGUI.Controls;
  4. using UnityEngine.Events;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline.Signals
  7. {
  8. [CustomEditor(typeof(SignalReceiver))]
  9. class SignalReceiverInspector : Editor
  10. {
  11. SignalReceiver m_Target;
  12. [SerializeField] TreeViewState m_TreeState;
  13. [SerializeField] MultiColumnHeaderState m_MultiColumnHeaderState;
  14. internal SignalReceiverTreeView m_TreeView;
  15. SignalEmitter signalEmitterContext
  16. {
  17. get { return m_Context as SignalEmitter;}
  18. }
  19. void OnEnable()
  20. {
  21. m_Target = target as SignalReceiver;
  22. InitTreeView(serializedObject);
  23. Undo.undoRedoPerformed += OnUndoRedo;
  24. }
  25. void OnDisable()
  26. {
  27. Undo.undoRedoPerformed -= OnUndoRedo;
  28. }
  29. void OnUndoRedo()
  30. {
  31. m_TreeView.dirty = true;
  32. }
  33. public override void OnInspectorGUI()
  34. {
  35. serializedObject.Update();
  36. using (var changeCheck = new EditorGUI.ChangeCheckScope())
  37. {
  38. m_TreeView.RefreshIfDirty();
  39. DrawEmitterControls(); // Draws buttons coming from the Context (SignalEmitter)
  40. EditorGUILayout.Space();
  41. m_TreeView.Draw();
  42. if (signalEmitterContext == null)
  43. DrawAddRemoveButtons();
  44. if (changeCheck.changed)
  45. {
  46. serializedObject.ApplyModifiedProperties();
  47. m_TreeView.dirty = true;
  48. }
  49. }
  50. }
  51. void DrawEmitterControls()
  52. {
  53. var context = signalEmitterContext;
  54. if (context != null)
  55. {
  56. var currentSignal = context.asset;
  57. if (currentSignal != null && !m_Target.IsSignalAssetHandled(currentSignal))
  58. {
  59. EditorGUILayout.Separator();
  60. var message = string.Format(Styles.NoReaction, currentSignal.name);
  61. SignalUtility.DrawCenteredMessage(message);
  62. if (SignalUtility.DrawCenteredButton(Styles.AddReactionButton))
  63. m_Target.AddNewReaction(currentSignal); // Add reaction on the first
  64. EditorGUILayout.Separator();
  65. }
  66. }
  67. }
  68. internal void SetAssetContext(SignalAsset asset)
  69. {
  70. m_TreeView.SetSignalContext(asset);
  71. }
  72. void DrawAddRemoveButtons()
  73. {
  74. using (new GUILayout.HorizontalScope())
  75. {
  76. GUILayout.FlexibleSpace();
  77. if (GUILayout.Button(Styles.AddReactionButton))
  78. {
  79. Undo.RecordObject(m_Target, Styles.UndoAddReaction);
  80. m_Target.AddEmptyReaction(new UnityEvent());
  81. PrefabUtility.RecordPrefabInstancePropertyModifications(m_Target);
  82. }
  83. GUILayout.Space(18.0f);
  84. }
  85. }
  86. void InitTreeView(SerializedObject so)
  87. {
  88. m_TreeState = SignalListFactory.CreateViewState();
  89. m_MultiColumnHeaderState = SignalListFactory.CreateHeaderState();
  90. var header = SignalListFactory.CreateHeader(m_MultiColumnHeaderState, SignalReceiverUtility.headerHeight);
  91. var context = signalEmitterContext;
  92. m_TreeView = SignalListFactory.CreateSignalInspectorList(m_TreeState, header, m_Target, so);
  93. m_TreeView.readonlySignals = context != null;
  94. if (context != null)
  95. m_TreeView.SetSignalContext(context.asset);
  96. }
  97. }
  98. }