A game about forced loneliness, made by TACStudios
at master 2.4 kB view raw
1using System; 2using UnityEditor.EditorTools; 3using UnityEditor.ShortcutManagement; 4using UnityEditor.Toolbars; 5using UnityEngine; 6using UnityEngine.UIElements; 7 8namespace UnityEditor.Tilemaps 9{ 10 internal class TilemapEditorToolButton : EditorToolbarToggle 11 { 12 private TilemapEditorTool m_TilemapEditorTool; 13 14 public TilemapEditorToolButton(TilemapEditorTool tool) 15 { 16 focusable = false; 17 18 if (tool != null) 19 { 20 name = tool.name; 21 icon = tool.toolbarIcon?.image as Texture2D; 22 tooltip = tool.toolbarIcon?.tooltip; 23 m_TilemapEditorTool = tool; 24 } 25 26 this.RegisterValueChangedCallback((evt) => 27 { 28 SetToolActive(); 29 }); 30 31 RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel); 32 RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel); 33 34 UpdateState(); 35 } 36 37 private void OnAttachedToPanel(AttachToPanelEvent evt) 38 { 39 ToolManager.activeToolChanged += UpdateState; 40 ToolManager.activeContextChanged += UpdateState; 41 ShortcutIntegration.instance.profileManager.shortcutBindingChanged += UpdateTooltips; 42 UpdateState(); 43 } 44 45 private void OnDetachFromPanel(DetachFromPanelEvent evt) 46 { 47 ShortcutIntegration.instance.profileManager.shortcutBindingChanged -= UpdateTooltips; 48 ToolManager.activeToolChanged -= UpdateState; 49 ToolManager.activeContextChanged -= UpdateState; 50 } 51 52 protected void SetToolActive() 53 { 54 var active = EditorToolManager.activeTool; 55 if (active == m_TilemapEditorTool) 56 ToolManager.RestorePreviousPersistentTool(); 57 else 58 ToolManager.SetActiveTool(m_TilemapEditorTool); 59 UpdateState(); 60 } 61 62 private void UpdateState() 63 { 64 var activeTool = m_TilemapEditorTool == EditorToolManager.activeTool; 65 SetValueWithoutNotify(activeTool); 66 } 67 68 private void UpdateTooltips(IShortcutProfileManager arg1, Identifier arg2, ShortcutBinding arg3, ShortcutBinding arg4) 69 { 70 tooltip = m_TilemapEditorTool != null ? m_TilemapEditorTool.toolbarIcon.tooltip : String.Empty; 71 } 72 } 73}