From 174e2a0a54e1d67396cd9399ae7c49d57af6c7e5 Mon Sep 17 00:00:00 2001 From: Joni Savolainen Date: Mon, 19 Sep 2022 20:50:36 +0300 Subject: [PATCH] feat: add IOnNavigationMove event interface --- .../Interfaces/UIComponentInterfaceTests.cs | 34 ++++++++++++++++--- .../Core/EventInterfaces/IOnClick.cs | 2 +- .../Core/EventInterfaces/IOnNavigationMove.cs | 16 +++++++++ .../EventInterfaces/IOnNavigationMove.cs.meta | 3 ++ Assets/UIComponents/Core/UIComponent.cs | 4 +++ 5 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs create mode 100644 Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs.meta diff --git a/Assets/UIComponents.Tests/Editor/Interfaces/UIComponentInterfaceTests.cs b/Assets/UIComponents.Tests/Editor/Interfaces/UIComponentInterfaceTests.cs index 275f87db..7efe5206 100644 --- a/Assets/UIComponents.Tests/Editor/Interfaces/UIComponentInterfaceTests.cs +++ b/Assets/UIComponents.Tests/Editor/Interfaces/UIComponentInterfaceTests.cs @@ -14,14 +14,29 @@ private class BaseTestComponent : UIComponent public bool Fired { get; protected set; } } - private static IEnumerator Assert_Registers_Event_Callback() + [SetUp] + public void SetUp() + { + _editorWindow = EditorWindow.GetWindow(); + } + + private InterfacesTestEditorWindow _editorWindow; + + [TearDown] + public void TearDown() + { + if (_editorWindow) + _editorWindow.Close(); + _editorWindow = null; + } + + private IEnumerator Assert_Registers_Event_Callback() where TComponent : BaseTestComponent, new() where TEvent : EventBase, new() { - var window = EditorWindow.GetWindow(); var component = new TComponent(); Assert.That(component.Fired, Is.False); - window.AddTestComponent(component); + _editorWindow.AddTestComponent(component); yield return component.WaitForInitializationEnumerator(); @@ -29,7 +44,6 @@ private static IEnumerator Assert_Registers_Event_Callback() component.SendEvent(evt); Assert.That(component.Fired, Is.True); - window.Close(); } private class UIComponentWithOnGeometryChanged : BaseTestComponent, IOnGeometryChanged @@ -98,6 +112,18 @@ public IEnumerator IOnClick_Registers_ClickEvent_Callback() { yield return Assert_Registers_Event_Callback(); } +#endif +#if UNITY_2021_3_OR_NEWER + private class UIComponentWithOnNavigationMove : BaseTestComponent, IOnNavigationMove + { + public void OnNavigationMove(NavigationMoveEvent evt) => Fired = true; + } + + [UnityTest] + public IEnumerator IOnNavigationMove_Registers_NavigationMoveEvent_Callback() + { + yield return Assert_Registers_Event_Callback(); + } #endif } } diff --git a/Assets/UIComponents/Core/EventInterfaces/IOnClick.cs b/Assets/UIComponents/Core/EventInterfaces/IOnClick.cs index f95bd83c..f560fc77 100644 --- a/Assets/UIComponents/Core/EventInterfaces/IOnClick.cs +++ b/Assets/UIComponents/Core/EventInterfaces/IOnClick.cs @@ -13,4 +13,4 @@ public interface IOnClick void OnClick(ClickEvent evt); } } -#endif \ No newline at end of file +#endif diff --git a/Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs b/Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs new file mode 100644 index 00000000..499543b4 --- /dev/null +++ b/Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs @@ -0,0 +1,16 @@ +#if UNITY_2021_3_OR_NEWER +using UnityEngine.UIElements; + +namespace UIComponents +{ + /// + /// When implemented by a , + /// a callback for is + /// automatically registered in the UIComponent constructor. + /// + public interface IOnNavigationMove + { + void OnNavigationMove(NavigationMoveEvent evt); + } +} +#endif diff --git a/Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs.meta b/Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs.meta new file mode 100644 index 00000000..f8620656 --- /dev/null +++ b/Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e04f403918934a448a9afc3a71c9b898 +timeCreated: 1663609181 \ No newline at end of file diff --git a/Assets/UIComponents/Core/UIComponent.cs b/Assets/UIComponents/Core/UIComponent.cs index 16b114a4..788f04e9 100644 --- a/Assets/UIComponents/Core/UIComponent.cs +++ b/Assets/UIComponents/Core/UIComponent.cs @@ -151,6 +151,10 @@ private void RegisterEventInterfaceCallbacks() #if UNITY_2020_3_OR_NEWER if (this is IOnClick onClick) RegisterCallback(onClick.OnClick); +#endif +#if UNITY_2021_3_OR_NEWER + if (this is IOnNavigationMove onNavigationMove) + RegisterCallback(onNavigationMove.OnNavigationMove); #endif }