| | 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 | |
|
| | 7 | | using System; |
| | 8 | | using GDX.Collections.Generic; |
| | 9 | | using Unity.Mathematics; |
| | 10 | | using UnityEngine; |
| | 11 | | using UnityEngine.UIElements; |
| | 12 | |
|
| | 13 | | namespace GDX.Developer |
| | 14 | | { |
| | 15 | | public class FloatChartWatch : WatchBase |
| | 16 | | { |
| | 17 | | CircularBuffer<float> m_ChartPercentage; |
| | 18 | |
|
| | 19 | | int m_ValuesCount; |
| 0 | 20 | | float m_MinimumValue = 0f; |
| 0 | 21 | | float m_MaximumValue = 1f; |
| | 22 | | float m_Range; |
| 0 | 23 | | float m_CachedValue = float.MinValue; |
| 0 | 24 | | Sentiment m_CachedSentiment = Sentiment.Default; |
| | 25 | |
|
| | 26 | | bool m_ShowLatestValue; |
| 0 | 27 | | 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 |
| 0 | 36 | | : base(uniqueIdentifier, displayName, enabled, minWidth, minHeight) |
| 0 | 37 | | { |
| 0 | 38 | | m_GetValue = getValue; |
| 0 | 39 | | m_GetSentiment = getSentiment; |
| | 40 | |
|
| 0 | 41 | | m_ValuesCount = historicalCount; |
| 0 | 42 | | m_ChartPercentage = new CircularBuffer<float>(historicalCount); |
| | 43 | |
|
| 0 | 44 | | m_MinimumValue = minimumValue; |
| 0 | 45 | | m_MaximumValue = maximumValue; |
| 0 | 46 | | m_Range = m_MaximumValue - m_MinimumValue; |
| | 47 | |
|
| 0 | 48 | | m_ShowLatestValue = showLatestValue; |
| 0 | 49 | | m_LatestValueFormat = latestValueFormat; |
| | 50 | |
|
| 0 | 51 | | Label displayNameLabel = new Label() { text = displayName }; |
| 0 | 52 | | displayNameLabel.AddToClassList("gdx-watch-left"); |
| | 53 | |
|
| 0 | 54 | | m_ChartElement = new VisualElement(); |
| 0 | 55 | | m_ChartElement.AddToClassList("gdx-watch-right"); |
| 0 | 56 | | m_ChartElement.AddToClassList("inner-box"); |
| | 57 | |
|
| 0 | 58 | | if (showLatestValue) |
| 0 | 59 | | { |
| 0 | 60 | | m_ValueLabel = new Label(); |
| 0 | 61 | | m_ValueLabel.AddToClassList("default"); |
| 0 | 62 | | m_ChartElement.Add(m_ValueLabel); |
| 0 | 63 | | } |
| | 64 | |
|
| 0 | 65 | | ContainerElement.Add(displayNameLabel); |
| 0 | 66 | | ContainerElement.Add(m_ChartElement); |
| | 67 | |
|
| 0 | 68 | | m_ChartElement.generateVisualContent = GenerateVisualContent; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | void GenerateVisualContent(MeshGenerationContext ctx) |
| 0 | 72 | | { |
| 0 | 73 | | Painter2D paint2D = ctx.painter2D; |
| 0 | 74 | | paint2D.strokeColor = m_CachedSentiment switch |
| | 75 | | { |
| 0 | 76 | | Sentiment.Good => Color.green, |
| 0 | 77 | | Sentiment.Warning => Color.yellow, |
| 0 | 78 | | Sentiment.Bad => Color.red, |
| 0 | 79 | | _ => Color.white |
| | 80 | | }; |
| 0 | 81 | | paint2D.lineJoin = LineJoin.Round; |
| | 82 | |
|
| | 83 | |
|
| 0 | 84 | | float horizontalIncrement = m_ChartElement.resolvedStyle.width / m_ValuesCount; |
| 0 | 85 | | float verticalIncrement = m_ChartElement.resolvedStyle.height; |
| | 86 | |
|
| 0 | 87 | | paint2D.BeginPath(); |
| 0 | 88 | | for (int i = 0; i < m_ValuesCount; i++) |
| 0 | 89 | | { |
| 0 | 90 | | paint2D.LineTo(new Vector2(horizontalIncrement * i, |
| | 91 | | m_ChartPercentage[i] * verticalIncrement)); |
| 0 | 92 | | } |
| 0 | 93 | | paint2D.Stroke(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public override void Poll() |
| 0 | 97 | | { |
| 0 | 98 | | float getValue = m_GetValue(); |
| 0 | 99 | | if (m_GetSentiment != null) |
| 0 | 100 | | { |
| 0 | 101 | | m_CachedSentiment = m_GetSentiment(getValue); |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | if (getValue <= m_MinimumValue) |
| 0 | 105 | | { |
| 0 | 106 | | m_ChartPercentage.Add(1f); |
| 0 | 107 | | } |
| 0 | 108 | | else if (getValue >= m_MaximumValue) |
| 0 | 109 | | { |
| 0 | 110 | | m_ChartPercentage.Add(0f); |
| 0 | 111 | | } |
| | 112 | | else |
| 0 | 113 | | { |
| 0 | 114 | | m_ChartPercentage.Add((1f - (getValue - m_MinimumValue) / m_Range)); |
| 0 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | m_ChartElement.MarkDirtyRepaint(); |
| | 118 | |
|
| | 119 | | // Latest Value Text |
| 0 | 120 | | if (m_ShowLatestValue && math.lengthsq(getValue - m_CachedValue) != Platform.FloatTolerance) |
| 0 | 121 | | { |
| 0 | 122 | | m_ValueLabel.text = getValue.ToString(m_LatestValueFormat); |
| 0 | 123 | | if (m_GetSentiment != null) |
| 0 | 124 | | { |
| 0 | 125 | | Sentiment sentiment = m_GetSentiment(getValue); |
| 0 | 126 | | if (sentiment != m_CachedSentiment) |
| 0 | 127 | | { |
| | 128 | | // Add colors |
| 0 | 129 | | AddSentimentToElement(m_ValueLabel, sentiment); |
| 0 | 130 | | m_CachedValue = getValue; |
| 0 | 131 | | } |
| 0 | 132 | | } |
| 0 | 133 | | } |
| 0 | 134 | | } |
| | 135 | | } |
| | 136 | | } |
| | 137 | |
|
| | 138 | | #endif // UNITY_2022_2_OR_NEWER |