< Summary

Class:GDX.Developer.IntegerChartWatch
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/Watches/IntegerChartWatch.cs
Covered lines:0
Uncovered lines:78
Coverable lines:78
Total lines:134
Line coverage:0% (0 of 78)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:3
Method coverage:0% (0 of 3)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
IntegerChartWatch(...)0%6200%
GenerateVisualContent(...)0%72800%
Poll()0%72800%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/Watches/IntegerChartWatch.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
 5#if UNITY_2022_2_OR_NEWER
 6
 7using System;
 8using GDX.Collections.Generic;
 9using UnityEngine;
 10using UnityEngine.UIElements;
 11
 12namespace GDX.Developer
 13{
 14    public class IntegerChartWatch : WatchBase
 15    {
 16        CircularBuffer<float> m_ChartPercentage;
 17
 18        int m_ValuesCount;
 019        int m_MinimumValue = 0;
 020        int m_MaximumValue = 100;
 21        int m_Range;
 022        int m_CachedValue = int.MinValue;
 023        Sentiment m_CachedSentiment = Sentiment.Default;
 24
 25        bool m_ShowLatestValue;
 26        readonly Func<int> m_GetValue;
 27        readonly Func<int, Sentiment> m_GetSentiment;
 28
 29        readonly VisualElement m_ChartElement;
 30        readonly Label m_ValueLabel;
 31
 32        public IntegerChartWatch(string uniqueIdentifier, string displayName, Func<int> getValue, Func<int, Sentiment> g
 33            int minimumValue = 0, int maximumValue = 100, int historicalCount = 250, bool showLatestValue = true, bool e
 034            : base(uniqueIdentifier, displayName, enabled, minWidth, minHeight)
 035        {
 036            m_GetValue = getValue;
 037            m_GetSentiment = getSentiment;
 38
 039            m_ValuesCount = historicalCount;
 040            m_ChartPercentage = new CircularBuffer<float>(historicalCount);
 41
 042            m_MinimumValue = minimumValue;
 043            m_MaximumValue = maximumValue;
 044            m_Range = m_MaximumValue - m_MinimumValue;
 45
 046            m_ShowLatestValue = showLatestValue;
 47
 048            Label displayNameLabel = new Label() { text = displayName };
 049            displayNameLabel.AddToClassList("gdx-watch-left");
 50
 051            m_ChartElement = new VisualElement();
 052            m_ChartElement.AddToClassList("gdx-watch-right");
 053            m_ChartElement.AddToClassList("inner-box");
 54
 055            if (showLatestValue)
 056            {
 057                m_ValueLabel = new Label();
 058                m_ValueLabel.AddToClassList("default");
 059                m_ChartElement.Add(m_ValueLabel);
 060            }
 61
 062            ContainerElement.Add(displayNameLabel);
 063            ContainerElement.Add(m_ChartElement);
 64
 065            m_ChartElement.generateVisualContent = GenerateVisualContent;
 066        }
 67
 68        void GenerateVisualContent(MeshGenerationContext ctx)
 069        {
 070            Painter2D paint2D = ctx.painter2D;
 071            paint2D.strokeColor = m_CachedSentiment switch
 72            {
 073                Sentiment.Good => Color.green,
 074                Sentiment.Warning => Color.yellow,
 075                Sentiment.Bad => Color.red,
 076                _ => Color.white
 77            };
 078            paint2D.lineJoin = LineJoin.Round;
 79
 080            float horizontalIncrement  = m_ChartElement.resolvedStyle.width / m_ValuesCount;
 081            float verticalIncrement = m_ChartElement.resolvedStyle.height;
 82
 083            paint2D.BeginPath();
 084            for (int i = 0; i < m_ValuesCount; i++)
 085            {
 086                paint2D.LineTo(new Vector2(horizontalIncrement * i,
 87                    m_ChartPercentage[i] * verticalIncrement));
 088            }
 089            paint2D.Stroke();
 090        }
 91
 92        public override void Poll()
 093        {
 094            int getValue = m_GetValue();
 095            if (m_GetSentiment != null)
 096            {
 097                m_CachedSentiment = m_GetSentiment(getValue);
 098            }
 99
 0100            if (getValue <= m_MinimumValue)
 0101            {
 0102                m_ChartPercentage.Add(1f);
 0103            }
 0104            else if (getValue >= m_MaximumValue)
 0105            {
 0106                m_ChartPercentage.Add(0f);
 0107            }
 108            else
 0109            {
 0110                m_ChartPercentage.Add((1f - (getValue - m_MinimumValue) / m_Range));
 0111            }
 112
 0113            m_ChartElement.MarkDirtyRepaint();
 114
 115            // Latest Value Text
 0116            if (m_ShowLatestValue && getValue != m_CachedValue)
 0117            {
 0118                m_ValueLabel.text = getValue.ToString();
 0119                if (m_GetSentiment != null)
 0120                {
 0121                    Sentiment sentiment = m_GetSentiment(getValue);
 0122                    if (sentiment != m_CachedSentiment)
 0123                    {
 124                        // Add colors
 0125                        AddSentimentToElement(m_ValueLabel, sentiment);
 0126                        m_CachedValue = getValue;
 0127                    }
 0128                }
 0129            }
 0130        }
 131    }
 132}
 133
 134#endif // UNITY_2022_2_OR_NEWER