< Summary

Class:GDX.Developer.WatchProvider
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/WatchProvider.cs
Covered lines:24
Uncovered lines:121
Coverable lines:145
Total lines:221
Line coverage:16.5% (24 of 145)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:16
Method coverage:18.7% (3 of 16)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WatchProvider()0%110100%
ToggleState(...)0%2100%
SetGlobalState(...)0%2100%
SetAllEnabled()0%6200%
SetAllDisabled()0%6200%
SetState(...)0%27.898032.26%
GetTotalCount()0%2100%
HasActiveWatches()0%6200%
GetWatch(...)0%6200%
HasWatch(...)0%2100%
GetWatchList()0%12300%
GetActiveElements()0%12300%
Register(...)0%3.473062.5%
Unregister(...)0%20400%
Poll()0%20400%
WatchList(...)0%2100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/WatchProvider.cs

#LineLine coverage
 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
 5using System.Collections.Generic;
 6using GDX.Collections.Generic;
 7using UnityEngine.UIElements;
 8
 9namespace GDX.Developer
 10{
 11#if UNITY_2022_2_OR_NEWER
 12    public static class WatchProvider
 13    {
 214        static bool s_ShouldPollEnabledWatches = true;
 15        public static ushort Version;
 16        static uint s_OverrideTicket;
 217        static readonly object k_Lock = new object();
 18
 219        static StringKeyDictionary<WatchBase>
 20            s_KnownWatches = new StringKeyDictionary<WatchBase>(50);
 21
 222        static readonly List<WatchBase> k_KnownActiveWatches = new List<WatchBase>(50);
 23
 24
 25        public static void ToggleState(WatchBase watch)
 026        {
 027            SetState(watch, !k_KnownActiveWatches.Contains(watch));
 028        }
 29
 30        public static void SetGlobalState(bool enabled)
 031        {
 032            s_ShouldPollEnabledWatches = enabled;
 033        }
 34
 35        public static void SetAllEnabled()
 036        {
 037            WatchList list = GetWatchList();
 038            int count = list.Count;
 039            for (int i = 0; i < count; i++)
 040            {
 041                SetState(GetWatch(list.Identfiers[i]), true);
 042            }
 043        }
 44
 45        public static void SetAllDisabled()
 046        {
 047            int count = k_KnownActiveWatches.Count;
 048            for (int i = count - 1; i >= 0; i--)
 049            {
 050                SetState(k_KnownActiveWatches[i], false);
 051            }
 052        }
 53
 54        public static void SetState(WatchBase watch, bool enabled)
 255        {
 256            if (enabled)
 057            {
 058                if (!k_KnownActiveWatches.Contains(watch))
 059                {
 060                    lock (k_Lock)
 061                    {
 062                        k_KnownActiveWatches.Add(watch);
 063                        Version++;
 064                    }
 065                }
 66
 067                if (watch.ContainerElement != null)
 068                {
 069                    watch.ContainerElement.style.display = VisualElementStyles.DisplayVisible;
 070                }
 071            }
 72            else
 273            {
 274                if (k_KnownActiveWatches.Contains(watch))
 075                {
 076                    lock (k_Lock)
 077                    {
 078                        k_KnownActiveWatches.Remove(watch);
 079                        Version++;
 080                    }
 081                }
 82
 283                if (watch.ContainerElement != null)
 284                {
 285                    watch.ContainerElement.style.display = VisualElementStyles.DisplayHidden;
 286                }
 287            }
 288        }
 89
 90        public static int GetTotalCount()
 091        {
 092            return s_KnownWatches.Count;
 093        }
 94
 95        public static bool HasActiveWatches()
 096        {
 097            if (!s_ShouldPollEnabledWatches) return false;
 098            return k_KnownActiveWatches.Count > 0;
 099        }
 100
 101        public static WatchBase GetWatch(string identifier)
 0102        {
 0103            return !s_KnownWatches.ContainsKey(identifier) ? null : s_KnownWatches[identifier];
 0104        }
 105
 106        public static bool HasWatch(string identifier)
 0107        {
 0108            return s_KnownWatches.ContainsKey(identifier);
 0109        }
 110
 111        public static WatchList GetWatchList()
 0112        {
 0113            lock (k_Lock)
 0114            {
 0115                int count = s_KnownWatches.Count;
 0116                WatchList details = new WatchList(count);
 0117                int iteratedIndexCount = 0;
 0118                int index = 0;
 0119                while (s_KnownWatches.MoveNext(ref iteratedIndexCount, out StringKeyEntry<WatchBase> entry))
 0120                {
 0121                    WatchBase target = entry.Value;
 0122                    details.IsActive[index] = k_KnownActiveWatches.Contains(target);
 0123                    details.DisplayNames[index] = target.DisplayName;
 0124                    details.Identfiers[index] = target.Identifier;
 0125                    index++;
 0126                }
 127
 0128                return details;
 129            }
 0130        }
 131
 132        public static VisualElement[] GetActiveElements()
 0133        {
 0134            lock (k_Lock)
 0135            {
 0136                int count = k_KnownActiveWatches.Count;
 0137                SimpleList<VisualElement> elements = new SimpleList<VisualElement>(count);
 0138                for (int i = 0; i < count; i++)
 0139                {
 0140                    elements.AddUnchecked(k_KnownActiveWatches[i].GetElement());
 0141                }
 142
 0143                return elements.Array;
 144            }
 0145        }
 146
 147        public static void Register(WatchBase watch, bool enabled = true)
 2148        {
 2149            lock (k_Lock)
 2150            {
 2151                if (s_KnownWatches.ContainsKey(watch.Identifier))
 0152                {
 0153                    s_OverrideTicket++;
 0154                    watch.SetOverrideIdentifier(s_OverrideTicket);
 155#if UNITY_EDITOR
 0156                    UnityEngine.Debug.LogWarning(
 157                        $"Duplicate registered watch identifier for '{watch.BaseIdentifier}' attempting @ {s_OverrideTic
 158#endif
 0159                    Register(watch, enabled);
 0160                }
 161                else
 2162                {
 2163                    s_KnownWatches.AddWithExpandCheck(watch.Identifier, watch);
 2164                    SetState(watch, enabled);
 2165                }
 2166            }
 2167        }
 168
 169        public static void Unregister(WatchBase watch, bool updateState = true)
 0170        {
 0171            lock (k_Lock)
 0172            {
 0173                if (s_KnownWatches.TryRemove(watch.Identifier))
 0174                {
 0175                    if (updateState)
 0176                    {
 0177                        SetState(watch, false);
 0178                    }
 0179                }
 180#if UNITY_EDITOR
 181                else
 0182                {
 0183                    UnityEngine.Debug.LogWarning($"Unable to unregister '{watch.Identifier}'");
 0184                }
 185#endif
 0186            }
 0187        }
 188
 189        public static void Poll()
 0190        {
 0191            if (!s_ShouldPollEnabledWatches) return;
 192
 193            // TODO: We could make this part of the managed update and self ticking at a rate?
 0194            lock (k_Lock)
 0195            {
 0196                int count = k_KnownActiveWatches.Count;
 0197                for (int i = 0; i < count; i++)
 0198                {
 0199                    k_KnownActiveWatches[i].Poll();
 0200                }
 0201            }
 0202        }
 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)
 0212            {
 0213                Count = totalCount;
 0214                IsActive = new bool[totalCount];
 0215                Identfiers = new string[totalCount];
 0216                DisplayNames = new string[totalCount];
 0217            }
 218        }
 219    }
 220#endif // UNITY_2022_2_OR_NEWER
 221}