< Summary

Class:GDX.Developer.WatchBase
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/WatchBase.cs
Covered lines:13
Uncovered lines:62
Coverable lines:75
Total lines:138
Line coverage:17.3% (13 of 75)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:13
Method coverage:38.4% (5 of 13)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WatchBase(...)0%6.15064.71%
Finalize()0%2100%
GetElement()0%2100%
SetOverrideIdentifier(...)0%2100%
SentimentRange(...)0%20400%
SentimentRange(...)0%20400%
SentimentRangeReversed(...)0%20400%
SentimentRangeReversed(...)0%20400%
AddSentimentToElement(...)0%30500%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/WatchBase.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 UnityEngine.Scripting;
 6using UnityEngine.UIElements;
 7
 8namespace GDX.Developer
 9{
 10#if UNITY_2022_2_OR_NEWER
 11    /// <summary>
 12    /// Base class for static watchers.
 13    /// </summary>
 14    [Preserve]
 15    public abstract class WatchBase
 16    {
 17        public enum Sentiment
 18        {
 19            Default,
 20            Good,
 21            Warning,
 22            Bad
 23        }
 24
 625        public string Identifier { get; private set; }
 426        public string BaseIdentifier { get; private set; }
 27
 28        public readonly string DisplayName;
 29
 30        public readonly VisualElement ContainerElement;
 31
 232        protected WatchBase(string uniqueIdentifier, string displayName, bool enabled = true, int minWidth =-1, int minH
 233        {
 234            BaseIdentifier = uniqueIdentifier;
 235            Identifier = BaseIdentifier;
 236            DisplayName = displayName;
 37
 238            ContainerElement = new VisualElement();
 239            ContainerElement.AddToClassList("gdx-watch");
 40
 241            if (minWidth != -1)
 042            {
 043                ContainerElement.style.minWidth = new StyleLength(new Length(minWidth, LengthUnit.Pixel));
 044            }
 45
 246            if (minHeight != -1)
 047            {
 048                ContainerElement.style.minHeight = new StyleLength(new Length(minHeight, LengthUnit.Pixel));
 049            }
 50
 251            WatchProvider.Register(this,
 52                WatchSettings.TryGetValue(uniqueIdentifier, out bool savedValue) ? savedValue : enabled);
 253        }
 54
 55        ~WatchBase()
 056        {
 57            // We have to ensure when we become out of scope that we no longer registered
 058            WatchProvider.Unregister(this, false);
 059        }
 60
 61        public VisualElement GetElement()
 062        {
 063            return ContainerElement;
 064        }
 65
 66        public void SetOverrideIdentifier(uint index)
 067        {
 068            Identifier = $"{BaseIdentifier}.{index.ToString()}";
 069        }
 70
 71        public abstract void Poll();
 72
 73        public static Sentiment SentimentRange(int min, int max, int value)
 074        {
 075            if (value < min)
 076            {
 077                return Sentiment.Bad;
 78            }
 079            return value > max ? Sentiment.Good : Sentiment.Warning;
 080        }
 81        public static Sentiment SentimentRange(float min, float max, float value)
 082        {
 083            if (value < min)
 084            {
 085                return Sentiment.Bad;
 86            }
 087            return value > max ? Sentiment.Good : Sentiment.Warning;
 088        }
 89        public static Sentiment SentimentRangeReversed(int min, int max, int value)
 090        {
 091            if (value < min)
 092            {
 093                return Sentiment.Good;
 94            }
 095            return value > max ? Sentiment.Bad : Sentiment.Warning;
 096        }
 97        public static Sentiment SentimentRangeReversed(float min, float max, float value)
 098        {
 099            if (value < min)
 0100            {
 0101                return Sentiment.Good;
 102            }
 0103            return value > max ? Sentiment.Bad : Sentiment.Warning;
 0104        }
 105
 106        public static void AddSentimentToElement(VisualElement element, Sentiment sentiment)
 0107        {
 0108            switch (sentiment)
 109            {
 110                case Sentiment.Good:
 0111                    element.RemoveFromClassList("warning");
 0112                    element.RemoveFromClassList("bad");
 0113                    element.RemoveFromClassList("default");
 0114                    element.AddToClassList("good");
 0115                    break;
 116                case Sentiment.Warning:
 0117                    element.RemoveFromClassList("good");
 0118                    element.RemoveFromClassList("bad");
 0119                    element.RemoveFromClassList("default");
 0120                    element.AddToClassList("warning");
 0121                    break;
 122                case Sentiment.Bad:
 0123                    element.RemoveFromClassList("good");
 0124                    element.RemoveFromClassList("warning");
 0125                    element.RemoveFromClassList("default");
 0126                    element.AddToClassList("bad");
 0127                    break;
 128                default:
 0129                    element.RemoveFromClassList("good");
 0130                    element.RemoveFromClassList("warning");
 0131                    element.RemoveFromClassList("bad");
 0132                    element.AddToClassList("default");
 0133                    break;
 134            }
 0135        }
 136    }
 137#endif // UNITY_2022_2_OR_NEWER
 138}