| | 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 | | using System.Net.Mail; |
| | 7 | | using GDX.Developer.ConsoleVariables; |
| | 8 | | using GDX.RuntimeContent; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.UIElements; |
| | 11 | | #endif |
| | 12 | |
|
| | 13 | | namespace GDX.Developer |
| | 14 | | { |
| | 15 | | #if UNITY_2022_2_OR_NEWER |
| | 16 | | public class RuntimeWatchController |
| | 17 | | { |
| | 18 | | const string k_GameObjectName = "GDX_Watches"; |
| | 19 | |
|
| 0 | 20 | | public UIDocument Document { get; private set; } |
| 0 | 21 | | public GameObject WatchesGameObject { get; private set; } |
| | 22 | |
|
| 0 | 23 | | int m_FontSize = 14; |
| | 24 | | ushort m_LastVersion; |
| | 25 | | readonly VisualElement m_RootElement; |
| 0 | 26 | | bool m_ShouldShow = false; |
| | 27 | |
|
| 0 | 28 | | static readonly BooleanConsoleVariable k_WatchesEnabled = |
| | 29 | | new BooleanConsoleVariable("watches.enabled", "Watches Enabled", true, |
| | 30 | | ConsoleVariableBase.ConsoleVariableFlags.Setting); |
| | 31 | |
|
| 0 | 32 | | public RuntimeWatchController(GameObject parentGameObject, int initialFontSize, int position, bool visible) |
| 0 | 33 | | { |
| | 34 | | // UIDocuments do not allow multiple components per Game Object so we have to make a child object. |
| 0 | 35 | | WatchesGameObject = new GameObject(k_GameObjectName); |
| 0 | 36 | | WatchesGameObject.transform.SetParent(parentGameObject.transform, false); |
| | 37 | |
|
| | 38 | | // Create isolated UI document (thanks Damian, boy do I feel stupid.) |
| 0 | 39 | | Document = WatchesGameObject.AddComponent<UIDocument>(); |
| 0 | 40 | | Document.sortingOrder = float.MaxValue; // Above all |
| 0 | 41 | | Document.visualTreeAsset = ResourceProvider.GetUIElements().Watches; |
| | 42 | |
|
| 0 | 43 | | m_RootElement = Document.rootVisualElement.Q<VisualElement>("gdx-watches"); |
| | 44 | |
|
| 0 | 45 | | k_WatchesEnabled.OnValueChanged += UpdateShow; |
| | 46 | |
|
| 0 | 47 | | UpdateFontSize(initialFontSize); |
| 0 | 48 | | UpdatePosition(position); |
| 0 | 49 | | UpdateShow(visible); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void UpdateFontSize(int fontSize) |
| 0 | 53 | | { |
| 0 | 54 | | m_FontSize = fontSize; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void UpdatePosition(int position) |
| 0 | 58 | | { |
| 0 | 59 | | m_RootElement.ApplyAlignment((VisualElementStyles.Alignment)position); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | public void UpdateShow(bool show) |
| 0 | 63 | | { |
| 0 | 64 | | WatchProvider.SetGlobalState(show); |
| 0 | 65 | | if (show) |
| 0 | 66 | | { |
| 0 | 67 | | m_RootElement.Show(); |
| 0 | 68 | | m_ShouldShow = true; |
| 0 | 69 | | } |
| | 70 | | else |
| 0 | 71 | | { |
| 0 | 72 | | m_RootElement.Hide(); |
| 0 | 73 | | m_ShouldShow = false; |
| 0 | 74 | | } |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Tick() |
| 0 | 78 | | { |
| 0 | 79 | | m_ShouldShow = WatchProvider.HasActiveWatches(); |
| 0 | 80 | | if (m_ShouldShow && !m_RootElement.IsVisible()) |
| 0 | 81 | | { |
| 0 | 82 | | m_RootElement.Show(); |
| 0 | 83 | | } |
| 0 | 84 | | else if(!m_ShouldShow && m_RootElement.IsVisible()) |
| 0 | 85 | | { |
| 0 | 86 | | m_RootElement.Hide(); |
| 0 | 87 | | } |
| | 88 | |
|
| 0 | 89 | | if (m_ShouldShow) |
| 0 | 90 | | { |
| 0 | 91 | | WatchProvider.Poll(); |
| | 92 | |
|
| 0 | 93 | | if (WatchProvider.Version != m_LastVersion) |
| 0 | 94 | | { |
| | 95 | | // We need to check that all of our elements are accounted for |
| 0 | 96 | | VisualElement[] elements = WatchProvider.GetActiveElements(); |
| 0 | 97 | | int elementCount = elements.Length; |
| | 98 | |
|
| | 99 | | // Clear from root |
| 0 | 100 | | m_RootElement.Clear(); |
| 0 | 101 | | for (int i = 0; i < elementCount; i++) |
| 0 | 102 | | { |
| 0 | 103 | | m_RootElement.Add(elements[i]); |
| 0 | 104 | | } |
| | 105 | |
|
| 0 | 106 | | m_LastVersion = WatchProvider.Version; |
| 0 | 107 | | } |
| 0 | 108 | | } |
| | 109 | | else |
| 0 | 110 | | { |
| | 111 | |
|
| 0 | 112 | | } |
| | 113 | |
|
| 0 | 114 | | } |
| | 115 | | } |
| | 116 | | #endif // UNITY_2022_2_OR_NEWER |
| | 117 | | } |