// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Animancer.Editor
{
/// [Editor-Only] An for .
/// https://kybernetik.com.au/animancer/api/Animancer.Editor/DelegateGUI
[CustomGUI(typeof(MulticastDelegate))]
public class DelegateGUI : CustomGUI
{
/************************************************************************************************************************/
private static readonly HashSet
ExpandedItems = new();
/************************************************************************************************************************/
/// Calculates the number of vertical pixels required to draw the specified .
public static float CalculateHeight(MulticastDelegate del)
=> AnimancerGUI.CalculateHeight(CalculateLineCount(del));
/// Calculates the number of lines required to draw the specified .
public static int CalculateLineCount(MulticastDelegate del)
=> del == null || !ExpandedItems.Contains(del)
? 1
: 1 + CalculateLineCount(AnimancerReflection.GetInvocationList(del));
/// Calculates the number of lines required to draw the specified `invocationList`.
public static int CalculateLineCount(Delegate[] invocationList)
=> invocationList == null
? 3
: invocationList.Length * 3;
/************************************************************************************************************************/
///
public override void DoGUI()
{
var area = AnimancerGUI.LayoutRect(CalculateHeight(Value));
DoGUI(ref area, Label, Value);
}
/// Draws the GUI for the given delegate.
public static void DoGUI(
ref Rect area,
GUIContent label,
MulticastDelegate del,
GUIContent valueLabel = null)
{
area.height = AnimancerGUI.LineHeight;
var delegates = AnimancerReflection.GetInvocationList(del);
var isExpanded = del != null && AnimancerGUI.DoHashedFoldoutGUI(area, ExpandedItems, del);
if (valueLabel != null)
{
EditorGUI.LabelField(area, label, valueLabel);
}
else
{
var count = delegates == null ? 0 : delegates.Length;
using (var countLabel = PooledGUIContent.Acquire(count.ToStringCached()))
EditorGUI.LabelField(area, label, countLabel);
}
AnimancerGUI.NextVerticalArea(ref area);
if (!isExpanded)
return;
EditorGUI.indentLevel++;
if (delegates == null)
{
DoSingleGUI(ref area, del);
}
else
{
for (int i = 0; i < delegates.Length; i++)
DoSingleGUI(ref area, delegates[i]);
}
EditorGUI.indentLevel--;
}
/************************************************************************************************************************/
private const int TargetFieldCacheCapacity = 128;
private static readonly Dictionary