| | 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 Unity.Mathematics; |
| | 9 | | using UnityEngine.UIElements; |
| | 10 | |
|
| | 11 | | namespace GDX.Developer |
| | 12 | | { |
| | 13 | | public class FloatRangeWatch : WatchBase |
| | 14 | | { |
| | 15 | | readonly Func<float> m_GetValue; |
| | 16 | | readonly Func<float, Sentiment> m_GetSentiment; |
| | 17 | |
|
| | 18 | | Sentiment m_CachedSentiment; |
| 0 | 19 | | float m_CachedValue = float.MinValue; |
| | 20 | |
|
| | 21 | | readonly Label m_ValueLabel; |
| | 22 | |
|
| | 23 | | public FloatRangeWatch(string uniqueIdentifier, string displayName, Func<float> getValue, Func<float, Sentiment> |
| 0 | 24 | | : base(uniqueIdentifier, displayName, enabled, minWidth, minHeight) |
| 0 | 25 | | { |
| 0 | 26 | | m_GetValue = getValue; |
| 0 | 27 | | m_GetSentiment = getSentiment; |
| | 28 | |
|
| 0 | 29 | | Label displayNameLabel = new Label() { text = displayName }; |
| 0 | 30 | | displayNameLabel.AddToClassList("gdx-watch-left"); |
| | 31 | |
|
| 0 | 32 | | m_ValueLabel = new Label(); |
| 0 | 33 | | m_ValueLabel.AddToClassList("gdx-watch-right"); |
| 0 | 34 | | m_ValueLabel.AddToClassList("default"); |
| | 35 | |
|
| 0 | 36 | | ContainerElement.Add(displayNameLabel); |
| 0 | 37 | | ContainerElement.Add(m_ValueLabel); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public override void Poll() |
| 0 | 41 | | { |
| 0 | 42 | | float getValue = m_GetValue(); |
| 0 | 43 | | if (math.lengthsq(getValue - m_CachedValue) != Platform.FloatTolerance) |
| 0 | 44 | | { |
| 0 | 45 | | m_ValueLabel.text = getValue.ToString(); |
| 0 | 46 | | Sentiment sentiment = m_GetSentiment(getValue); |
| 0 | 47 | | if (sentiment != m_CachedSentiment) |
| 0 | 48 | | { |
| | 49 | | // Add colors |
| 0 | 50 | | AddSentimentToElement(m_ValueLabel, sentiment); |
| 0 | 51 | | m_CachedSentiment = sentiment; |
| 0 | 52 | | m_CachedValue = getValue; |
| 0 | 53 | | } |
| 0 | 54 | | } |
| 0 | 55 | | } |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | #endif // UNITY_2022_2_OR_NEWER |