A game about forced loneliness, made by TACStudios
1using UnityEngine;
2using UnityEngine.Tilemaps;
3
4namespace UnityEditor.Tilemaps
5{
6 internal class SceneViewOpenTilePaletteHelper : ScriptableSingleton<SceneViewOpenTilePaletteHelper>
7 {
8 private bool m_RegisteredEventHandlers;
9 private bool m_IsSelectionValid;
10 private bool m_HighlightHelper;
11
12 internal static bool highlight
13 {
14 get => instance.m_HighlightHelper;
15 set => instance.m_HighlightHelper = value;
16 }
17
18 [InitializeOnLoadMethod]
19 private static void Initialize()
20 {
21 instance.RegisterEventHandlers();
22 }
23
24 private void OnEnable()
25 {
26 RegisterEventHandlers();
27 }
28
29 private void RegisterEventHandlers()
30 {
31 if (m_RegisteredEventHandlers)
32 return;
33
34 Selection.selectionChanged += SelectionChanged;
35 EditorApplication.hierarchyChanged += SelectionChanged;
36
37 m_IsSelectionValid = IsSelectionValid();
38
39 m_RegisteredEventHandlers = true;
40 }
41
42 private void OnDisable()
43 {
44 Selection.selectionChanged -= SelectionChanged;
45 EditorApplication.hierarchyChanged -= SelectionChanged;
46 m_RegisteredEventHandlers = false;
47 }
48
49 internal static void OpenTilePalette()
50 {
51 GridPaintPaletteWindow.OpenTilemapPalette();
52 instance.m_HighlightHelper = false;
53
54 var target = Selection.activeGameObject;
55 if (target != null)
56 {
57 if (PrefabUtility.IsPartOfPrefabAsset(target))
58 {
59 var path = AssetDatabase.GetAssetPath(target);
60 if (AssetDatabase.LoadAssetAtPath<GridPalette>(path))
61 {
62 GridPaintingState.palette = AssetDatabase.LoadAssetAtPath<GameObject>(path);
63 }
64 }
65 else if (GridPaintingState.validTargets != null)
66 {
67 var grid = target.GetComponent<GridLayout>();
68 if (grid != null)
69 {
70 foreach (var validTarget in GridPaintingState.validTargets)
71 {
72 if (validTarget == target)
73 {
74 GridPaintingState.scenePaintTarget = target;
75 break;
76 }
77 }
78 }
79 }
80 }
81 }
82
83 internal static bool IsActive()
84 {
85 if (GridPaintingState.isEditing)
86 return false;
87 return instance.m_IsSelectionValid;
88 }
89
90 internal static bool IsSelectionValid()
91 {
92 if (Selection.activeObject == null)
93 return false;
94 if (Selection.activeObject is TileBase)
95 return true;
96 if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<GridLayout>() != null)
97 return true;
98 return false;
99 }
100
101 private void SelectionChanged()
102 {
103 var old = m_IsSelectionValid;
104 m_IsSelectionValid = IsSelectionValid();
105 if (m_IsSelectionValid != old)
106 m_HighlightHelper = m_IsSelectionValid;
107 }
108
109 internal class SceneViewOpenTilePaletteProperties
110 {
111 public static readonly string showInSceneViewEditorPref = "OpenTilePalette.ShowInSceneView";
112 public static readonly string showInSceneViewLookup = "Show Open Tile Palette in Scene View";
113
114 public static readonly GUIContent showInSceneViewLabel = EditorGUIUtility.TrTextContent(showInSceneViewLookup, "Shows an overlay in the SceneView for opening the Tile Palette when selecting an object that interacts with the Tile Palette.");
115 }
116
117 internal static bool showInSceneViewActive
118 {
119 get { return EditorPrefs.GetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, true); }
120 set { EditorPrefs.SetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, value); }
121 }
122
123 internal static void PreferencesGUI()
124 {
125 using (new SettingsWindow.GUIScope())
126 {
127 EditorGUI.BeginChangeCheck();
128 var val = EditorGUILayout.Toggle(SceneViewOpenTilePaletteProperties.showInSceneViewLabel, showInSceneViewActive);
129 if (EditorGUI.EndChangeCheck())
130 {
131 showInSceneViewActive = val;
132 SceneView.RepaintAll();
133 }
134 }
135 }
136 }
137}