| | 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 | | using UnityEngine.Scripting; |
| | 6 | | using UnityEngine.UIElements; |
| | 7 | |
|
| | 8 | | namespace GDX.Developer |
| | 9 | | { |
| | 10 | | #if UNITY_2022_2_OR_NEWER |
| | 11 | | /// <summary> |
| | 12 | | /// Base class for static watchers. |
| | 13 | | /// </summary> |
| | 14 | | [Preserve] |
| | 15 | | public abstract class WatchBase |
| | 16 | | { |
| | 17 | | public enum Sentiment |
| | 18 | | { |
| | 19 | | Default, |
| | 20 | | Good, |
| | 21 | | Warning, |
| | 22 | | Bad |
| | 23 | | } |
| | 24 | |
|
| 6 | 25 | | public string Identifier { get; private set; } |
| 4 | 26 | | public string BaseIdentifier { get; private set; } |
| | 27 | |
|
| | 28 | | public readonly string DisplayName; |
| | 29 | |
|
| | 30 | | public readonly VisualElement ContainerElement; |
| | 31 | |
|
| 2 | 32 | | protected WatchBase(string uniqueIdentifier, string displayName, bool enabled = true, int minWidth =-1, int minH |
| 2 | 33 | | { |
| 2 | 34 | | BaseIdentifier = uniqueIdentifier; |
| 2 | 35 | | Identifier = BaseIdentifier; |
| 2 | 36 | | DisplayName = displayName; |
| | 37 | |
|
| 2 | 38 | | ContainerElement = new VisualElement(); |
| 2 | 39 | | ContainerElement.AddToClassList("gdx-watch"); |
| | 40 | |
|
| 2 | 41 | | if (minWidth != -1) |
| 0 | 42 | | { |
| 0 | 43 | | ContainerElement.style.minWidth = new StyleLength(new Length(minWidth, LengthUnit.Pixel)); |
| 0 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | if (minHeight != -1) |
| 0 | 47 | | { |
| 0 | 48 | | ContainerElement.style.minHeight = new StyleLength(new Length(minHeight, LengthUnit.Pixel)); |
| 0 | 49 | | } |
| | 50 | |
|
| 2 | 51 | | WatchProvider.Register(this, |
| | 52 | | WatchSettings.TryGetValue(uniqueIdentifier, out bool savedValue) ? savedValue : enabled); |
| 2 | 53 | | } |
| | 54 | |
|
| | 55 | | ~WatchBase() |
| 0 | 56 | | { |
| | 57 | | // We have to ensure when we become out of scope that we no longer registered |
| 0 | 58 | | WatchProvider.Unregister(this, false); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public VisualElement GetElement() |
| 0 | 62 | | { |
| 0 | 63 | | return ContainerElement; |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | public void SetOverrideIdentifier(uint index) |
| 0 | 67 | | { |
| 0 | 68 | | Identifier = $"{BaseIdentifier}.{index.ToString()}"; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public abstract void Poll(); |
| | 72 | |
|
| | 73 | | public static Sentiment SentimentRange(int min, int max, int value) |
| 0 | 74 | | { |
| 0 | 75 | | if (value < min) |
| 0 | 76 | | { |
| 0 | 77 | | return Sentiment.Bad; |
| | 78 | | } |
| 0 | 79 | | return value > max ? Sentiment.Good : Sentiment.Warning; |
| 0 | 80 | | } |
| | 81 | | public static Sentiment SentimentRange(float min, float max, float value) |
| 0 | 82 | | { |
| 0 | 83 | | if (value < min) |
| 0 | 84 | | { |
| 0 | 85 | | return Sentiment.Bad; |
| | 86 | | } |
| 0 | 87 | | return value > max ? Sentiment.Good : Sentiment.Warning; |
| 0 | 88 | | } |
| | 89 | | public static Sentiment SentimentRangeReversed(int min, int max, int value) |
| 0 | 90 | | { |
| 0 | 91 | | if (value < min) |
| 0 | 92 | | { |
| 0 | 93 | | return Sentiment.Good; |
| | 94 | | } |
| 0 | 95 | | return value > max ? Sentiment.Bad : Sentiment.Warning; |
| 0 | 96 | | } |
| | 97 | | public static Sentiment SentimentRangeReversed(float min, float max, float value) |
| 0 | 98 | | { |
| 0 | 99 | | if (value < min) |
| 0 | 100 | | { |
| 0 | 101 | | return Sentiment.Good; |
| | 102 | | } |
| 0 | 103 | | return value > max ? Sentiment.Bad : Sentiment.Warning; |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public static void AddSentimentToElement(VisualElement element, Sentiment sentiment) |
| 0 | 107 | | { |
| 0 | 108 | | switch (sentiment) |
| | 109 | | { |
| | 110 | | case Sentiment.Good: |
| 0 | 111 | | element.RemoveFromClassList("warning"); |
| 0 | 112 | | element.RemoveFromClassList("bad"); |
| 0 | 113 | | element.RemoveFromClassList("default"); |
| 0 | 114 | | element.AddToClassList("good"); |
| 0 | 115 | | break; |
| | 116 | | case Sentiment.Warning: |
| 0 | 117 | | element.RemoveFromClassList("good"); |
| 0 | 118 | | element.RemoveFromClassList("bad"); |
| 0 | 119 | | element.RemoveFromClassList("default"); |
| 0 | 120 | | element.AddToClassList("warning"); |
| 0 | 121 | | break; |
| | 122 | | case Sentiment.Bad: |
| 0 | 123 | | element.RemoveFromClassList("good"); |
| 0 | 124 | | element.RemoveFromClassList("warning"); |
| 0 | 125 | | element.RemoveFromClassList("default"); |
| 0 | 126 | | element.AddToClassList("bad"); |
| 0 | 127 | | break; |
| | 128 | | default: |
| 0 | 129 | | element.RemoveFromClassList("good"); |
| 0 | 130 | | element.RemoveFromClassList("warning"); |
| 0 | 131 | | element.RemoveFromClassList("bad"); |
| 0 | 132 | | element.AddToClassList("default"); |
| 0 | 133 | | break; |
| | 134 | | } |
| 0 | 135 | | } |
| | 136 | | } |
| | 137 | | #endif // UNITY_2022_2_OR_NEWER |
| | 138 | | } |