| | 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 UnityEngine.UIElements; |
| | 9 | |
|
| | 10 | | namespace GDX.Developer |
| | 11 | | { |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Thread-safe developer watch system. |
| | 15 | | /// </summary> |
| | 16 | | public class SimpleWatch : WatchBase |
| | 17 | | { |
| | 18 | | readonly Func<string> m_GetValue; |
| | 19 | | int m_CachedHash; |
| | 20 | |
|
| | 21 | | readonly Label m_DisplayNameLabel; |
| | 22 | | readonly Label m_ValueLabel; |
| | 23 | |
|
| | 24 | | public SimpleWatch(string uniqueIdentifier, string displayName, Func<string> getValue, bool enabled = true, int |
| 2 | 25 | | : base(uniqueIdentifier, displayName, enabled, minWidth, minHeight) |
| 2 | 26 | | { |
| 2 | 27 | | m_GetValue = getValue; |
| | 28 | |
|
| 2 | 29 | | m_DisplayNameLabel = new Label() { text = displayName }; |
| 2 | 30 | | m_DisplayNameLabel.AddToClassList("gdx-watch-left"); |
| | 31 | |
|
| 2 | 32 | | m_ValueLabel = new Label(); |
| 2 | 33 | | m_ValueLabel.AddToClassList("gdx-watch-right"); |
| | 34 | |
|
| 2 | 35 | | ContainerElement.Add(m_DisplayNameLabel); |
| 2 | 36 | | ContainerElement.Add(m_ValueLabel); |
| 2 | 37 | | } |
| | 38 | |
|
| | 39 | | public override void Poll() |
| 0 | 40 | | { |
| | 41 | | // Poll for our new value |
| 0 | 42 | | string getValue = m_GetValue(); |
| 0 | 43 | | int getValueHash = getValue.GetStableHashCode(); |
| | 44 | |
|
| | 45 | | // Only change if legit change |
| 0 | 46 | | if (m_CachedHash != getValueHash) |
| 0 | 47 | | { |
| 0 | 48 | | m_ValueLabel.text = getValue; |
| 0 | 49 | | m_CachedHash = getValueHash; |
| 0 | 50 | | } |
| 0 | 51 | | } |
| | 52 | | } |
| | 53 | | } |
| | 54 | |
|
| | 55 | | #endif // UNITY_2022_2_OR_NEWER |