暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TilePaletteBrushesPopup.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. /// <summary>
  8. /// Popup Field for selecting the Active Brush for Grid Painting.
  9. /// </summary>
  10. [UxmlElement]
  11. public sealed partial class TilePaletteBrushesPopup : PopupField<GridBrushBase>
  12. {
  13. private static string k_NullGameObjectName = L10n.Tr("No Valid Brush");
  14. private static string k_LabelTooltip =
  15. L10n.Tr("Specifies the currently active Brush used for painting in the Scene View.");
  16. /// <summary>
  17. /// Factory for TilePaletteBrushesPopup.
  18. /// </summary>
  19. [Obsolete("TilePaletteBrushesPopupFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  20. public class TilePaletteBrushesPopupFactory : UxmlFactory<TilePaletteBrushesPopup, TilePaletteBrushesPopupUxmlTraits> {}
  21. /// <summary>
  22. /// UxmlTraits for TilePaletteBrushesPopup.
  23. /// </summary>
  24. [Obsolete("TilePaletteBrushesPopupUxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  25. public class TilePaletteBrushesPopupUxmlTraits : UxmlTraits {}
  26. /// <summary>
  27. /// USS class name of elements of this type.
  28. /// </summary>
  29. private new static readonly string ussClassName = "unity-tilepalette-brushes-field";
  30. /// <summary>
  31. /// USS class name of labels in elements of this type.
  32. /// </summary>
  33. private new static readonly string labelUssClassName = ussClassName + "__label";
  34. /// <summary>
  35. /// USS class name of input elements in elements of this type.
  36. /// </summary>
  37. private new static readonly string inputUssClassName = ussClassName + "__input";
  38. /// <summary>
  39. /// Initializes and returns an instance of TilePaletteBrushesPopup.
  40. /// </summary>
  41. public TilePaletteBrushesPopup() : this(null) {}
  42. /// <summary>
  43. /// Initializes and returns an instance of TilePaletteBrushesPopup.
  44. /// </summary>
  45. /// <param name="label">Label name for the Popup</param>
  46. public TilePaletteBrushesPopup(string label)
  47. : base(label, new List<GridBrushBase>(GridPaintingState.brushes), GetBrushIndex())
  48. {
  49. AddToClassList(ussClassName);
  50. labelElement.AddToClassList(labelUssClassName);
  51. visualInput.AddToClassList(inputUssClassName);
  52. TilePaletteOverlayUtility.SetStyleSheet(this);
  53. labelElement.tooltip = k_LabelTooltip;
  54. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  55. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  56. m_FormatSelectedValueCallback += FormatSelectedValueCallback;
  57. createMenuCallback += CreateMenuCallback;
  58. SetValueWithoutNotify(GridPaintingState.gridBrush);
  59. }
  60. private void OnAttachedToPanel(AttachToPanelEvent evt)
  61. {
  62. GridPaintingState.brushChanged += OnBrushChanged;
  63. SetValueWithoutNotify(GridPaintingState.gridBrush);
  64. }
  65. private void OnBrushChanged(GridBrushBase obj)
  66. {
  67. choices = new List<GridBrushBase>(GridPaintingState.brushes);
  68. UpdateBrush();
  69. }
  70. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  71. {
  72. GridPaintingState.brushChanged -= OnBrushChanged;
  73. }
  74. private string FormatSelectedValueCallback(GridBrushBase brush)
  75. {
  76. if (brush != null)
  77. return brush.name;
  78. return k_NullGameObjectName;
  79. }
  80. private IGenericMenu CreateMenuCallback()
  81. {
  82. return new TilePaletteBrushesDropdownMenu();
  83. }
  84. private static int GetBrushIndex()
  85. {
  86. return GridPaintingState.brushes.IndexOf(GridPaintingState.gridBrush);
  87. }
  88. private void UpdateBrush()
  89. {
  90. index = GetBrushIndex();
  91. }
  92. }
  93. }