using System.Linq; using UnityEditor.Toolbars; using UnityEngine; using UnityEngine.UIElements; namespace UnityEditor.Tilemaps { /// /// Popup Field for selecting the Active Palette for Grid Painting. /// [EditorToolbarElement(k_ToolbarId)] public sealed class TilePaletteActivePalettePopup : PopupField { internal const string k_ToolbarId = "Tools/Tile Palette Active Palette"; private static string kNullGameObjectName = L10n.Tr("No Valid Palette"); private static string kLabelTooltip = L10n.Tr("Specifies the currently active Palette used for painting in the Scene View."); /// /// Factory for TilePaletteActivePalettePopup. /// public class TilePaletteActivePalettePopupFactory : UxmlFactory {} /// /// UxmlTraits for TilePaletteActivePalettePopup. /// public class TilePaletteActivePalettePopupUxmlTraits : UxmlTraits {} /// /// USS class name of elements of this type. /// private new static readonly string ussClassName = "unity-tilepalette-activepalettes-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 TilePaletteActivePalettesPopup. /// public TilePaletteActivePalettePopup() : this(null) {} /// /// Initializes and returns an instance of TilePaletteActivePalettesPopup. /// /// Label name for the Popup public TilePaletteActivePalettePopup(string label) : base(label, GridPaintingState.palettes.ToList(), GetActivePaletteIndex()) { AddToClassList(ussClassName); labelElement.AddToClassList(labelUssClassName); visualInput.AddToClassList(inputUssClassName); TilePaletteOverlayUtility.SetStyleSheet(this); labelElement.tooltip = kLabelTooltip; RegisterCallback(OnAttachedToPanel); RegisterCallback(OnDetachFromPanel); m_FormatSelectedValueCallback += FormatSelectedValueCallback; createMenuCallback += CreateMenuCallback; UpdateTargets(); SetValueWithoutNotify(GridPaintingState.palette); } private void OnAttachedToPanel(AttachToPanelEvent evt) { GridPaintingState.paletteChanged += OnPaletteChanged; GridPaintingState.palettesChanged += UpdateTargets; } private void OnDetachFromPanel(DetachFromPanelEvent evt) { GridPaintingState.palettesChanged += UpdateTargets; GridPaintingState.paletteChanged -= OnPaletteChanged; } private void OnPaletteChanged(GameObject _) { UpdateActivePalette(); } private string FormatSelectedValueCallback(GameObject go) { if (go != null) return go.name; return kNullGameObjectName; } private IGenericMenu CreateMenuCallback() { return new TilePaletteActivePaletteDropdownMenu(); } private static int GetActivePaletteIndex() { return GridPaintingState.palettes.IndexOf(GridPaintingState.palette); } private void UpdateChoices() { choices.Clear(); foreach (var target in GridPaintingState.palettes) { choices.Add(target); } } private void UpdateActivePalette() { var newIndex = GetActivePaletteIndex(); if (newIndex != -1 && choices.Count < newIndex) { UpdateChoices(); newIndex = GetActivePaletteIndex(); } index = newIndex; } private void UpdateTargets() { UpdateChoices(); UpdateActivePalette(); } } [EditorToolbarElement(k_ToolbarId)] internal class TilePaletteActivePalettePopupIcon : VisualElement { internal const string k_ToolbarId = "Tools/Tile Palette Active Palette Icon"; private static string kTooltip = L10n.Tr("Active Palette"); /// /// USS class name of elements of this type. /// public static readonly string ussClassName = "unity-tilepalette-activepalette-icon"; private readonly string k_IconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.TilePalette.png"; public TilePaletteActivePalettePopupIcon() { AddToClassList(ussClassName); TilePaletteOverlayUtility.SetStyleSheet(this); style.backgroundImage = EditorGUIUtility.LoadIcon(k_IconPath); tooltip = kTooltip; } } }