No Description
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.

TilePaletteElement.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using System;
  2. using UnityEditor.ShortcutManagement;
  3. using UnityEditor.Toolbars;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. /// <summary>
  8. /// A Visual Element which handles and displays a Tile Palette Clipboard
  9. /// and its associated tools.
  10. /// </summary>
  11. /// <description>
  12. /// This Visual Element includes other Visual Elements that help with managing
  13. /// the Tile Palette Clipboard, which includes a popup dropdown for selecting
  14. /// the active Palette and a toolbar for enabling edits and showing visual aids
  15. /// in the Tile Palette Clipboard.
  16. /// </description>
  17. [UxmlElement]
  18. public partial class TilePaletteElement : VisualElement
  19. {
  20. /// <summary>
  21. /// Factory for TilePaletteElement.
  22. /// </summary>
  23. [Obsolete("TilePaletteElementFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  24. public class TilePaletteElementFactory : UxmlFactory<TilePaletteElement, TilePaletteElementUxmlTraits> {}
  25. /// <summary>
  26. /// UxmlTraits for TilePaletteElement.
  27. /// </summary>
  28. [Obsolete("TilePaletteElementUxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  29. public class TilePaletteElementUxmlTraits : UxmlTraits {}
  30. private static readonly string ussClassName = "unity-tilepalette-element";
  31. private static readonly string toolbarUssClassName = ussClassName + "-toolbar";
  32. private static readonly string rightToolbarUssClassName = toolbarUssClassName + "-right";
  33. private static readonly string kTilePaletteElementHideOnPickEditorPref = "TilePaletteElementHideOnPick";
  34. private static readonly string k_Name = L10n.Tr("Tile Palette Element");
  35. private static string[] k_LeftToolbarElements = new[]
  36. {
  37. TilePaletteActivePalettePopupIcon.k_ToolbarId
  38. , TilePaletteActivePalettePopup.k_ToolbarId
  39. };
  40. private static string[] k_RightToolbarElements = new[] {
  41. TilePaletteEditToggle.k_ToolbarId
  42. , TilePaletteGridToggle.k_ToolbarId
  43. , TilePaletteGizmoToggle.k_ToolbarId
  44. , TilePaletteHideClipboardToggle.k_ToolbarId
  45. };
  46. private static bool[] k_TilePaletteWindowActiveRightToolbar = new[] { true, true, true, false };
  47. private static bool[] k_TilePaletteOverlayActiveRightToolbar = new[] { true, true, true, true };
  48. private VisualElement m_ToolbarElement;
  49. private TilePaletteClipboardElement m_ClipboardElement;
  50. private TilePaletteEditToggle m_EditToggle;
  51. private TilePaletteGridToggle m_GridToggle;
  52. private TilePaletteGizmoToggle m_GizmoToggle;
  53. private TilePaletteHideClipboardToggle m_HideToggle;
  54. private VisualElement m_RightToolbar;
  55. private event Action onBrushPickedInternal;
  56. /// <summary>
  57. /// Whether the clipboard is unlocked for editing.
  58. /// </summary>
  59. public bool clipboardUnlocked
  60. {
  61. get => m_ClipboardElement.clipboardUnlocked;
  62. set
  63. {
  64. m_ClipboardElement.clipboardUnlocked = value;
  65. m_EditToggle.SetValueWithoutNotify(value);
  66. }
  67. }
  68. /// <summary>
  69. /// Whether the clipboard is hidden when the Pick EditorTool is activated on it.
  70. /// </summary>
  71. public bool hideOnPick
  72. {
  73. get => EditorPrefs.GetBool(kTilePaletteElementHideOnPickEditorPref, true);
  74. set
  75. {
  76. EditorPrefs.SetBool(kTilePaletteElementHideOnPickEditorPref, value);
  77. m_HideToggle.SetValueWithoutNotify(value);
  78. }
  79. }
  80. /// <summary>
  81. /// Callback when the active Brush does a Pick on the Clipboard.
  82. /// </summary>
  83. public event Action onBrushPicked
  84. {
  85. add
  86. {
  87. onBrushPickedInternal += value;
  88. SetupRightToolbar();
  89. }
  90. remove
  91. {
  92. onBrushPickedInternal -= value;
  93. SetupRightToolbar();
  94. }
  95. }
  96. internal GridPaintPaletteClipboard clipboardView => m_ClipboardElement.clipboardView;
  97. /// <summary>
  98. /// Initializes and returns an instance of TilePaletteElement.
  99. /// </summary>
  100. public TilePaletteElement()
  101. {
  102. AddToClassList(ussClassName);
  103. name = k_Name;
  104. TilePaletteOverlayUtility.SetStyleSheet(this);
  105. m_ToolbarElement = new VisualElement();
  106. m_ToolbarElement.AddToClassList(toolbarUssClassName);
  107. Add(m_ToolbarElement);
  108. m_ClipboardElement = new TilePaletteClipboardElement();
  109. m_ClipboardElement.onBrushPicked += OnBrushPicked;
  110. Add(m_ClipboardElement);
  111. var leftToolbar = EditorToolbar.CreateOverlay(k_LeftToolbarElements);
  112. m_ToolbarElement.Add(leftToolbar);
  113. var rightToolbarElement = new VisualElement();
  114. rightToolbarElement.AddToClassList(rightToolbarUssClassName);
  115. m_ToolbarElement.Add(rightToolbarElement);
  116. m_RightToolbar = EditorToolbar.CreateOverlay(k_RightToolbarElements);
  117. SetupRightToolbar();
  118. rightToolbarElement.Add(m_RightToolbar);
  119. m_EditToggle = this.Q<TilePaletteEditToggle>();
  120. m_EditToggle.SetValueWithoutNotify(false);
  121. m_EditToggle.ToggleChanged += OnEditToggleChanged;
  122. m_GridToggle = this.Q<TilePaletteGridToggle>();
  123. m_GridToggle.SetValueWithoutNotify(GridPaintingState.drawGridGizmo);
  124. m_GridToggle.ToggleChanged += OnGridToggleChanged;
  125. m_GizmoToggle = this.Q<TilePaletteGizmoToggle>();
  126. m_GizmoToggle.SetValueWithoutNotify(GridPaintingState.drawGizmos);
  127. m_GizmoToggle.ToggleChanged += OnGizmoToggleChanged;
  128. m_HideToggle = this.Q<TilePaletteHideClipboardToggle>();
  129. m_HideToggle.SetValueWithoutNotify(hideOnPick);
  130. m_HideToggle.ToggleChanged += OnHideClipboardToggleChanged;
  131. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  132. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  133. UpdateToggleState();
  134. }
  135. private void OnAttachedToPanel(AttachToPanelEvent evt)
  136. {
  137. m_ClipboardElement.clipboardUnlockedChanged += OnUnlockedChanged;
  138. GridPaintingState.palettesChanged += UpdateToggleState;
  139. }
  140. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  141. {
  142. GridPaintingState.palettesChanged -= UpdateToggleState;
  143. m_ClipboardElement.clipboardUnlockedChanged -= OnUnlockedChanged;
  144. }
  145. private void OnBrushPicked()
  146. {
  147. if (hideOnPick)
  148. onBrushPickedInternal?.Invoke();
  149. }
  150. private void OnEditToggleChanged(bool unlock)
  151. {
  152. clipboardUnlocked = unlock;
  153. }
  154. private void OnUnlockedChanged(bool unlock)
  155. {
  156. m_EditToggle.SetValueWithoutNotify(unlock);
  157. }
  158. private void OnGridToggleChanged(bool drawGridGizmo)
  159. {
  160. GridPaintingState.drawGridGizmo = drawGridGizmo;
  161. }
  162. private void OnGizmoToggleChanged(bool drawGizmo)
  163. {
  164. GridPaintingState.drawGizmos = drawGizmo;
  165. }
  166. private void UpdateToggleState()
  167. {
  168. bool hasPalette = GridPaintingState.palettes != null && GridPaintingState.palettes.Count > 0;
  169. m_EditToggle.SetEnabled(hasPalette);
  170. m_GridToggle.SetEnabled(hasPalette);
  171. m_GizmoToggle.SetEnabled(hasPalette);
  172. }
  173. private void OnHideClipboardToggleChanged(bool hideClipboard)
  174. {
  175. hideOnPick = hideClipboard;
  176. }
  177. private void SetupRightToolbar()
  178. {
  179. TilePaletteOverlayUtility.SetupChildrenAsButtonStripForVisible(m_RightToolbar,
  180. onBrushPickedInternal != null ? k_TilePaletteOverlayActiveRightToolbar : k_TilePaletteWindowActiveRightToolbar);
  181. }
  182. }
  183. [EditorToolbarElement(k_ToolbarId)]
  184. internal sealed class TilePaletteEditToggle : EditorToolbarToggle
  185. {
  186. internal const string k_ToolbarId = "Tile Palette/Tile Palette Edit";
  187. private static readonly string k_ToolSettingsClass = "unity-tool-settings";
  188. private static readonly string k_ElementClass = "unity-tilepalette-element-tilepaletteedit";
  189. private static readonly string k_IconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.EditPalette.png";
  190. private static readonly string k_TooltipText = L10n.Tr("Toggles Tile Palette Edit");
  191. public Action<bool> ToggleChanged;
  192. public TilePaletteEditToggle()
  193. {
  194. name = "Tile Palette Edit";
  195. AddToClassList(k_ToolSettingsClass);
  196. AddToClassList(k_ElementClass);
  197. TilePaletteOverlayUtility.SetStyleSheet(this);
  198. icon = EditorGUIUtility.LoadIcon(k_IconPath);
  199. tooltip = k_TooltipText;
  200. }
  201. protected override void ToggleValue()
  202. {
  203. base.ToggleValue();
  204. ToggleChanged?.Invoke(value);
  205. }
  206. }
  207. [EditorToolbarElement(k_ToolbarId)]
  208. internal sealed class TilePaletteGridToggle : EditorToolbarToggle
  209. {
  210. internal const string k_ToolbarId = "Tile Palette/Tile Palette Grid";
  211. private static readonly string k_ToolSettingsClass = "unity-tool-settings";
  212. private static readonly string k_ElementClass = "unity-tilepalette-element-gridtoggle";
  213. private static readonly string k_IconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.ShowGrid.png";
  214. private static readonly string k_TooltipText = L10n.Tr("Toggle visibility of the Grid in the Tile Palette");
  215. public Action<bool> ToggleChanged;
  216. public TilePaletteGridToggle()
  217. {
  218. name = "Tile Palette Grid";
  219. AddToClassList(k_ToolSettingsClass);
  220. AddToClassList(k_ElementClass);
  221. TilePaletteOverlayUtility.SetStyleSheet(this);
  222. icon = EditorGUIUtility.LoadIcon(k_IconPath);
  223. tooltip = k_TooltipText;
  224. }
  225. protected override void ToggleValue()
  226. {
  227. base.ToggleValue();
  228. ToggleChanged?.Invoke(value);
  229. }
  230. }
  231. [EditorToolbarElement(k_ToolbarId)]
  232. internal sealed class TilePaletteBrushElementToggle : EditorToolbarToggle
  233. {
  234. internal const string k_ToolbarId = "Tile Palette/Tile Palette Brush Element";
  235. private static readonly string k_ToolSettingsClass = "unity-tool-settings";
  236. private static readonly string k_ElementClass = "unity-tilepalette-element-brushelement";
  237. private static readonly string k_IconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.BrushSettings.png";
  238. private static readonly string k_TooltipText = L10n.Tr("Toggle visibility of the Brush Inspector in the Tile Palette");
  239. public Action<bool> ToggleChanged;
  240. public TilePaletteBrushElementToggle()
  241. {
  242. name = "Tile Palette Grid";
  243. AddToClassList(k_ToolSettingsClass);
  244. AddToClassList(k_ElementClass);
  245. TilePaletteOverlayUtility.SetStyleSheet(this);
  246. icon = EditorGUIUtility.LoadIcon(k_IconPath);
  247. tooltip = k_TooltipText;
  248. }
  249. protected override void ToggleValue()
  250. {
  251. base.ToggleValue();
  252. ToggleChanged?.Invoke(value);
  253. }
  254. }
  255. [EditorToolbarElement(k_ToolbarId)]
  256. internal sealed class TilePaletteGizmoToggle : EditorToolbarToggle
  257. {
  258. internal const string k_ToolbarId = "Tile Palette/Tile Palette Gizmo";
  259. private static readonly string k_ToolSettingsClass = "unity-tool-settings";
  260. private static readonly string k_ElementClass = "unity-tilepalette-element-gizmotoggle";
  261. private static readonly string k_IconPath = "GizmosToggle";
  262. private static readonly string k_TooltipText = L10n.Tr("Toggle visibility of Gizmos in the Tile Palette");
  263. public Action<bool> ToggleChanged;
  264. public TilePaletteGizmoToggle()
  265. {
  266. name = "Tile Palette Gizmo";
  267. AddToClassList(k_ToolSettingsClass);
  268. AddToClassList(k_ElementClass);
  269. TilePaletteOverlayUtility.SetStyleSheet(this);
  270. icon = EditorGUIUtility.LoadIcon(k_IconPath);
  271. tooltip = k_TooltipText;
  272. }
  273. protected override void ToggleValue()
  274. {
  275. base.ToggleValue();
  276. ToggleChanged?.Invoke(value);
  277. }
  278. }
  279. [EditorToolbarElement(k_ToolbarId)]
  280. internal sealed class TilePaletteHideClipboardToggle : EditorToolbarToggle
  281. {
  282. internal const string k_ToolbarId = "Tile Palette/Tile Palette Hide Clipboard";
  283. private static readonly string k_ToolSettingsClass = "unity-tool-settings";
  284. private static readonly string k_ElementClass = "unity-tilepalette-element-hideclipboard";
  285. private static readonly string k_IconPath = "Packages/com.unity.2d.tilemap/Editor/Icons/Tilemap.ShowTilePalette.png";
  286. private static readonly string k_TooltipFormatText = L10n.Tr("Hides Tile Palette on Pick. ( {0} ) to show/hide Tile Palette.");
  287. private static readonly string k_ShortcutId = "Grid Painting/Toggle SceneView Palette";
  288. public Action<bool> ToggleChanged;
  289. public TilePaletteHideClipboardToggle()
  290. {
  291. name = "Tile Palette Hide Clipboard";
  292. AddToClassList(k_ToolSettingsClass);
  293. AddToClassList(k_ElementClass);
  294. TilePaletteOverlayUtility.SetStyleSheet(this);
  295. icon = EditorGUIUtility.LoadIcon(k_IconPath);
  296. RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
  297. RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
  298. }
  299. private void OnAttachedToPanel(AttachToPanelEvent evt)
  300. {
  301. ShortcutIntegration.instance.profileManager.shortcutBindingChanged += OnShortcutBindingChanged;
  302. UpdateTooltip();
  303. }
  304. private void OnDetachFromPanel(DetachFromPanelEvent evt)
  305. {
  306. ShortcutIntegration.instance.profileManager.shortcutBindingChanged -= OnShortcutBindingChanged;
  307. }
  308. private void OnShortcutBindingChanged(IShortcutProfileManager arg1, Identifier arg2, ShortcutBinding arg3, ShortcutBinding arg4)
  309. {
  310. UpdateTooltip();
  311. }
  312. private void UpdateTooltip()
  313. {
  314. tooltip = String.Format(k_TooltipFormatText, ShortcutManager.instance.GetShortcutBinding(k_ShortcutId));
  315. }
  316. protected override void ToggleValue()
  317. {
  318. base.ToggleValue();
  319. ToggleChanged?.Invoke(value);
  320. }
  321. }
  322. }