| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Collections.Generic; |
| | 6 | | using GDX.Collections.Generic; |
| | 7 | | using UnityEngine.UIElements; |
| | 8 | |
|
| | 9 | | namespace GDX.Developer |
| | 10 | | { |
| | 11 | | #if UNITY_2022_2_OR_NEWER |
| | 12 | | public static class WatchProvider |
| | 13 | | { |
| 2 | 14 | | static bool s_ShouldPollEnabledWatches = true; |
| | 15 | | public static ushort Version; |
| | 16 | | static uint s_OverrideTicket; |
| 2 | 17 | | static readonly object k_Lock = new object(); |
| | 18 | |
|
| 2 | 19 | | static StringKeyDictionary<WatchBase> |
| | 20 | | s_KnownWatches = new StringKeyDictionary<WatchBase>(50); |
| | 21 | |
|
| 2 | 22 | | static readonly List<WatchBase> k_KnownActiveWatches = new List<WatchBase>(50); |
| | 23 | |
|
| | 24 | |
|
| | 25 | | public static void ToggleState(WatchBase watch) |
| 0 | 26 | | { |
| 0 | 27 | | SetState(watch, !k_KnownActiveWatches.Contains(watch)); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public static void SetGlobalState(bool enabled) |
| 0 | 31 | | { |
| 0 | 32 | | s_ShouldPollEnabledWatches = enabled; |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public static void SetAllEnabled() |
| 0 | 36 | | { |
| 0 | 37 | | WatchList list = GetWatchList(); |
| 0 | 38 | | int count = list.Count; |
| 0 | 39 | | for (int i = 0; i < count; i++) |
| 0 | 40 | | { |
| 0 | 41 | | SetState(GetWatch(list.Identfiers[i]), true); |
| 0 | 42 | | } |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public static void SetAllDisabled() |
| 0 | 46 | | { |
| 0 | 47 | | int count = k_KnownActiveWatches.Count; |
| 0 | 48 | | for (int i = count - 1; i >= 0; i--) |
| 0 | 49 | | { |
| 0 | 50 | | SetState(k_KnownActiveWatches[i], false); |
| 0 | 51 | | } |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public static void SetState(WatchBase watch, bool enabled) |
| 2 | 55 | | { |
| 2 | 56 | | if (enabled) |
| 0 | 57 | | { |
| 0 | 58 | | if (!k_KnownActiveWatches.Contains(watch)) |
| 0 | 59 | | { |
| 0 | 60 | | lock (k_Lock) |
| 0 | 61 | | { |
| 0 | 62 | | k_KnownActiveWatches.Add(watch); |
| 0 | 63 | | Version++; |
| 0 | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | if (watch.ContainerElement != null) |
| 0 | 68 | | { |
| 0 | 69 | | watch.ContainerElement.style.display = VisualElementStyles.DisplayVisible; |
| 0 | 70 | | } |
| 0 | 71 | | } |
| | 72 | | else |
| 2 | 73 | | { |
| 2 | 74 | | if (k_KnownActiveWatches.Contains(watch)) |
| 0 | 75 | | { |
| 0 | 76 | | lock (k_Lock) |
| 0 | 77 | | { |
| 0 | 78 | | k_KnownActiveWatches.Remove(watch); |
| 0 | 79 | | Version++; |
| 0 | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| 2 | 83 | | if (watch.ContainerElement != null) |
| 2 | 84 | | { |
| 2 | 85 | | watch.ContainerElement.style.display = VisualElementStyles.DisplayHidden; |
| 2 | 86 | | } |
| 2 | 87 | | } |
| 2 | 88 | | } |
| | 89 | |
|
| | 90 | | public static int GetTotalCount() |
| 0 | 91 | | { |
| 0 | 92 | | return s_KnownWatches.Count; |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public static bool HasActiveWatches() |
| 0 | 96 | | { |
| 0 | 97 | | if (!s_ShouldPollEnabledWatches) return false; |
| 0 | 98 | | return k_KnownActiveWatches.Count > 0; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public static WatchBase GetWatch(string identifier) |
| 0 | 102 | | { |
| 0 | 103 | | return !s_KnownWatches.ContainsKey(identifier) ? null : s_KnownWatches[identifier]; |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public static bool HasWatch(string identifier) |
| 0 | 107 | | { |
| 0 | 108 | | return s_KnownWatches.ContainsKey(identifier); |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | public static WatchList GetWatchList() |
| 0 | 112 | | { |
| 0 | 113 | | lock (k_Lock) |
| 0 | 114 | | { |
| 0 | 115 | | int count = s_KnownWatches.Count; |
| 0 | 116 | | WatchList details = new WatchList(count); |
| 0 | 117 | | int iteratedIndexCount = 0; |
| 0 | 118 | | int index = 0; |
| 0 | 119 | | while (s_KnownWatches.MoveNext(ref iteratedIndexCount, out StringKeyEntry<WatchBase> entry)) |
| 0 | 120 | | { |
| 0 | 121 | | WatchBase target = entry.Value; |
| 0 | 122 | | details.IsActive[index] = k_KnownActiveWatches.Contains(target); |
| 0 | 123 | | details.DisplayNames[index] = target.DisplayName; |
| 0 | 124 | | details.Identfiers[index] = target.Identifier; |
| 0 | 125 | | index++; |
| 0 | 126 | | } |
| | 127 | |
|
| 0 | 128 | | return details; |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public static VisualElement[] GetActiveElements() |
| 0 | 133 | | { |
| 0 | 134 | | lock (k_Lock) |
| 0 | 135 | | { |
| 0 | 136 | | int count = k_KnownActiveWatches.Count; |
| 0 | 137 | | SimpleList<VisualElement> elements = new SimpleList<VisualElement>(count); |
| 0 | 138 | | for (int i = 0; i < count; i++) |
| 0 | 139 | | { |
| 0 | 140 | | elements.AddUnchecked(k_KnownActiveWatches[i].GetElement()); |
| 0 | 141 | | } |
| | 142 | |
|
| 0 | 143 | | return elements.Array; |
| | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public static void Register(WatchBase watch, bool enabled = true) |
| 2 | 148 | | { |
| 2 | 149 | | lock (k_Lock) |
| 2 | 150 | | { |
| 2 | 151 | | if (s_KnownWatches.ContainsKey(watch.Identifier)) |
| 0 | 152 | | { |
| 0 | 153 | | s_OverrideTicket++; |
| 0 | 154 | | watch.SetOverrideIdentifier(s_OverrideTicket); |
| | 155 | | #if UNITY_EDITOR |
| 0 | 156 | | UnityEngine.Debug.LogWarning( |
| | 157 | | $"Duplicate registered watch identifier for '{watch.BaseIdentifier}' attempting @ {s_OverrideTic |
| | 158 | | #endif |
| 0 | 159 | | Register(watch, enabled); |
| 0 | 160 | | } |
| | 161 | | else |
| 2 | 162 | | { |
| 2 | 163 | | s_KnownWatches.AddWithExpandCheck(watch.Identifier, watch); |
| 2 | 164 | | SetState(watch, enabled); |
| 2 | 165 | | } |
| 2 | 166 | | } |
| 2 | 167 | | } |
| | 168 | |
|
| | 169 | | public static void Unregister(WatchBase watch, bool updateState = true) |
| 0 | 170 | | { |
| 0 | 171 | | lock (k_Lock) |
| 0 | 172 | | { |
| 0 | 173 | | if (s_KnownWatches.TryRemove(watch.Identifier)) |
| 0 | 174 | | { |
| 0 | 175 | | if (updateState) |
| 0 | 176 | | { |
| 0 | 177 | | SetState(watch, false); |
| 0 | 178 | | } |
| 0 | 179 | | } |
| | 180 | | #if UNITY_EDITOR |
| | 181 | | else |
| 0 | 182 | | { |
| 0 | 183 | | UnityEngine.Debug.LogWarning($"Unable to unregister '{watch.Identifier}'"); |
| 0 | 184 | | } |
| | 185 | | #endif |
| 0 | 186 | | } |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | public static void Poll() |
| 0 | 190 | | { |
| 0 | 191 | | if (!s_ShouldPollEnabledWatches) return; |
| | 192 | |
|
| | 193 | | // TODO: We could make this part of the managed update and self ticking at a rate? |
| 0 | 194 | | lock (k_Lock) |
| 0 | 195 | | { |
| 0 | 196 | | int count = k_KnownActiveWatches.Count; |
| 0 | 197 | | for (int i = 0; i < count; i++) |
| 0 | 198 | | { |
| 0 | 199 | | k_KnownActiveWatches[i].Poll(); |
| 0 | 200 | | } |
| 0 | 201 | | } |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | public struct WatchList |
| | 205 | | { |
| | 206 | | public int Count; |
| | 207 | | public bool[] IsActive; |
| | 208 | | public string[] Identfiers; |
| | 209 | | public string[] DisplayNames; |
| | 210 | |
|
| | 211 | | public WatchList(int totalCount) |
| 0 | 212 | | { |
| 0 | 213 | | Count = totalCount; |
| 0 | 214 | | IsActive = new bool[totalCount]; |
| 0 | 215 | | Identfiers = new string[totalCount]; |
| 0 | 216 | | DisplayNames = new string[totalCount]; |
| 0 | 217 | | } |
| | 218 | | } |
| | 219 | | } |
| | 220 | | #endif // UNITY_2022_2_OR_NEWER |
| | 221 | | } |