Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add IOnNavigationMove event interface #50

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,36 @@ private class BaseTestComponent : UIComponent
public bool Fired { get; protected set; }
}

private static IEnumerator Assert_Registers_Event_Callback<TComponent, TEvent>()
[SetUp]
public void SetUp()
{
_editorWindow = EditorWindow.GetWindow<InterfacesTestEditorWindow>();
}

private InterfacesTestEditorWindow _editorWindow;

[TearDown]
public void TearDown()
{
if (_editorWindow)
_editorWindow.Close();
_editorWindow = null;
}

private IEnumerator Assert_Registers_Event_Callback<TComponent, TEvent>()
where TComponent : BaseTestComponent, new()
where TEvent : EventBase<TEvent>, new()
{
var window = EditorWindow.GetWindow<InterfacesTestEditorWindow>();
var component = new TComponent();
Assert.That(component.Fired, Is.False);
window.AddTestComponent(component);
_editorWindow.AddTestComponent(component);

yield return component.WaitForInitializationEnumerator();

using (var evt = new TEvent() { target = component })
component.SendEvent(evt);

Assert.That(component.Fired, Is.True);
window.Close();
}

private class UIComponentWithOnGeometryChanged : BaseTestComponent, IOnGeometryChanged
Expand Down Expand Up @@ -98,6 +112,18 @@ public IEnumerator IOnClick_Registers_ClickEvent_Callback()
{
yield return Assert_Registers_Event_Callback<UIComponentWithOnClick, ClickEvent>();
}
#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<UIComponentWithOnNavigationMove, NavigationMoveEvent>();
}
#endif
}
}
2 changes: 1 addition & 1 deletion Assets/UIComponents/Core/EventInterfaces/IOnClick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public interface IOnClick
void OnClick(ClickEvent evt);
}
}
#endif
#endif
16 changes: 16 additions & 0 deletions Assets/UIComponents/Core/EventInterfaces/IOnNavigationMove.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if UNITY_2021_3_OR_NEWER
using UnityEngine.UIElements;

namespace UIComponents
{
/// <summary>
/// When implemented by a <see cref="UIComponent"/>,
/// a callback for <see cref="NavigationMoveEvent"/> is
/// automatically registered in the UIComponent constructor.
/// </summary>
public interface IOnNavigationMove
{
void OnNavigationMove(NavigationMoveEvent evt);
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Assets/UIComponents/Core/UIComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ private void RegisterEventInterfaceCallbacks()
#if UNITY_2020_3_OR_NEWER
if (this is IOnClick onClick)
RegisterCallback<ClickEvent>(onClick.OnClick);
#endif
#if UNITY_2021_3_OR_NEWER
if (this is IOnNavigationMove onNavigationMove)
RegisterCallback<NavigationMoveEvent>(onNavigationMove.OnNavigationMove);
#endif
}

Expand Down