using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.Tilemaps
{
///
/// Popup Field for selecting the Active Brush for Grid Painting.
///
public sealed class TilePaletteBrushesPopup : PopupField
{
private static string k_NullGameObjectName = L10n.Tr("No Valid Brush");
private static string k_LabelTooltip =
L10n.Tr("Specifies the currently active Brush used for painting in the Scene View.");
///
/// Factory for TilePaletteBrushesPopup.
///
public class TilePaletteBrushesPopupFactory : UxmlFactory {}
///
/// UxmlTraits for TilePaletteBrushesPopup.
///
public class TilePaletteBrushesPopupUxmlTraits : UxmlTraits {}
///
/// USS class name of elements of this type.
///
private new static readonly string ussClassName = "unity-tilepalette-brushes-field";
///
/// USS class name of labels in elements of this type.
///
private new static readonly string labelUssClassName = ussClassName + "__label";
///
/// USS class name of input elements in elements of this type.
///
private new static readonly string inputUssClassName = ussClassName + "__input";
///
/// Initializes and returns an instance of TilePaletteBrushesPopup.
///
public TilePaletteBrushesPopup() : this(null) {}
///
/// Initializes and returns an instance of TilePaletteBrushesPopup.
///
/// Label name for the Popup
public TilePaletteBrushesPopup(string label)
: base(label, new List(GridPaintingState.brushes), GetBrushIndex())
{
AddToClassList(ussClassName);
labelElement.AddToClassList(labelUssClassName);
visualInput.AddToClassList(inputUssClassName);
TilePaletteOverlayUtility.SetStyleSheet(this);
labelElement.tooltip = k_LabelTooltip;
RegisterCallback(OnAttachedToPanel);
RegisterCallback(OnDetachFromPanel);
m_FormatSelectedValueCallback += FormatSelectedValueCallback;
createMenuCallback += CreateMenuCallback;
SetValueWithoutNotify(GridPaintingState.gridBrush);
}
private void OnAttachedToPanel(AttachToPanelEvent evt)
{
GridPaintingState.brushChanged += OnBrushChanged;
}
private void OnBrushChanged(GridBrushBase obj)
{
UpdateBrush();
}
private void OnDetachFromPanel(DetachFromPanelEvent evt)
{
GridPaintingState.brushChanged -= OnBrushChanged;
}
private string FormatSelectedValueCallback(GridBrushBase brush)
{
if (brush != null)
return brush.name;
return k_NullGameObjectName;
}
private IGenericMenu CreateMenuCallback()
{
return new TilePaletteBrushesDropdownMenu();
}
private static int GetBrushIndex()
{
return GridPaintingState.brushes.IndexOf(GridPaintingState.gridBrush);
}
private void UpdateBrush()
{
index = GetBrushIndex();
}
}
}