暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TilePaletteBrushesPopup.cs 3.5KB

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