< Summary

Class:GDX.Developer.FloatChartWatch
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/Watches/FloatChartWatch.cs
Covered lines:0
Uncovered lines:80
Coverable lines:80
Total lines:138
Line coverage:0% (0 of 80)
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
FloatChartWatch(...)0%6200%
GenerateVisualContent(...)0%72800%
Poll()0%72800%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/Watches/FloatChartWatch.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 Unity.Mathematics;
 10using UnityEngine;
 11using UnityEngine.UIElements;
 12
 13namespace GDX.Developer
 14{
 15    public class FloatChartWatch : WatchBase
 16    {
 17        CircularBuffer<float> m_ChartPercentage;
 18
 19        int m_ValuesCount;
 020        float m_MinimumValue = 0f;
 021        float m_MaximumValue = 1f;
 22        float m_Range;
 023        float m_CachedValue = float.MinValue;
 024        Sentiment m_CachedSentiment = Sentiment.Default;
 25
 26        bool m_ShowLatestValue;
 027        string m_LatestValueFormat = null;
 28        readonly Func<float> m_GetValue;
 29        readonly Func<float, Sentiment> m_GetSentiment;
 30
 31        readonly VisualElement m_ChartElement;
 32        readonly Label m_ValueLabel;
 33
 34        public FloatChartWatch(string uniqueIdentifier, string displayName, Func<float> getValue, Func<float, Sentiment>
 35            float minimumValue = 0, float maximumValue = 1, int historicalCount = 250, bool showLatestValue = true, stri
 036            : base(uniqueIdentifier, displayName, enabled, minWidth, minHeight)
 037        {
 038            m_GetValue = getValue;
 039            m_GetSentiment = getSentiment;
 40
 041            m_ValuesCount = historicalCount;
 042            m_ChartPercentage = new CircularBuffer<float>(historicalCount);
 43
 044            m_MinimumValue = minimumValue;
 045            m_MaximumValue = maximumValue;
 046            m_Range = m_MaximumValue - m_MinimumValue;
 47
 048            m_ShowLatestValue = showLatestValue;
 049            m_LatestValueFormat = latestValueFormat;
 50
 051            Label displayNameLabel = new Label() { text = displayName };
 052            displayNameLabel.AddToClassList("gdx-watch-left");
 53
 054            m_ChartElement = new VisualElement();
 055            m_ChartElement.AddToClassList("gdx-watch-right");
 056            m_ChartElement.AddToClassList("inner-box");
 57
 058            if (showLatestValue)
 059            {
 060                m_ValueLabel = new Label();
 061                m_ValueLabel.AddToClassList("default");
 062                m_ChartElement.Add(m_ValueLabel);
 063            }
 64
 065            ContainerElement.Add(displayNameLabel);
 066            ContainerElement.Add(m_ChartElement);
 67
 068            m_ChartElement.generateVisualContent = GenerateVisualContent;
 069        }
 70
 71        void GenerateVisualContent(MeshGenerationContext ctx)
 072        {
 073            Painter2D paint2D = ctx.painter2D;
 074            paint2D.strokeColor = m_CachedSentiment switch
 75            {
 076                Sentiment.Good => Color.green,
 077                Sentiment.Warning => Color.yellow,
 078                Sentiment.Bad => Color.red,
 079                _ => Color.white
 80            };
 081            paint2D.lineJoin = LineJoin.Round;
 82
 83
 084            float horizontalIncrement  = m_ChartElement.resolvedStyle.width / m_ValuesCount;
 085            float verticalIncrement = m_ChartElement.resolvedStyle.height;
 86
 087            paint2D.BeginPath();
 088            for (int i = 0; i < m_ValuesCount; i++)
 089            {
 090                paint2D.LineTo(new Vector2(horizontalIncrement * i,
 91                    m_ChartPercentage[i] * verticalIncrement));
 092            }
 093            paint2D.Stroke();
 094        }
 95
 96        public override void Poll()
 097        {
 098            float getValue = m_GetValue();
 099            if (m_GetSentiment != null)
 0100            {
 0101                m_CachedSentiment = m_GetSentiment(getValue);
 0102            }
 103
 0104            if (getValue <= m_MinimumValue)
 0105            {
 0106                m_ChartPercentage.Add(1f);
 0107            }
 0108            else if (getValue >= m_MaximumValue)
 0109            {
 0110                m_ChartPercentage.Add(0f);
 0111            }
 112            else
 0113            {
 0114                m_ChartPercentage.Add((1f - (getValue - m_MinimumValue) / m_Range));
 0115            }
 116
 0117            m_ChartElement.MarkDirtyRepaint();
 118
 119            // Latest Value Text
 0120            if (m_ShowLatestValue && math.lengthsq(getValue - m_CachedValue) != Platform.FloatTolerance)
 0121            {
 0122                m_ValueLabel.text = getValue.ToString(m_LatestValueFormat);
 0123                if (m_GetSentiment != null)
 0124                {
 0125                    Sentiment sentiment = m_GetSentiment(getValue);
 0126                    if (sentiment != m_CachedSentiment)
 0127                    {
 128                        // Add colors
 0129                        AddSentimentToElement(m_ValueLabel, sentiment);
 0130                        m_CachedValue = getValue;
 0131                    }
 0132                }
 0133            }
 0134        }
 135    }
 136}
 137
 138#endif // UNITY_2022_2_OR_NEWER