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