Skip to content

Commit

Permalink
feat: add IOnClick event interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Jun 11, 2022
1 parent 34b370b commit 0172a64
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Assets/Samples/EventInterfaces/EventInterfacesView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UIComponents.Samples.EventInterfaces
[Dependency(typeof(IEventLogService), provide: typeof(EventLogService))]
public class EventInterfacesView : UIComponent,
IOnAttachToPanel, IOnGeometryChanged, IOnDetachFromPanel,
IOnMouseLeave, IOnMouseEnter
IOnMouseLeave, IOnMouseEnter, IOnClick
{
private readonly IEventLogService _eventLogService;

Expand Down Expand Up @@ -49,6 +49,11 @@ public void OnMouseLeave(MouseLeaveEvent evt)
_eventLogService.Log(evt);
}

public void OnClick(ClickEvent evt)
{
_eventLogService.Log(evt);
}

private void OnClearButtonClicked()
{
_eventLogService.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,16 @@ public void IOnMouseLeave_Registers_MouseLeaveEvent_Callback()
{
Assert_Registers_Event_Callback<UIComponentWithOnMouseLeave, MouseLeaveEvent>();
}

private class UIComponentWithOnClick : BaseTestComponent, IOnClick
{
public void OnClick(ClickEvent evt) => Fired = true;
}

[Test]
public void IOnClick_Registers_ClickEvent_Callback()
{
Assert_Registers_Event_Callback<UIComponentWithOnClick, ClickEvent>();
}
}
}
14 changes: 14 additions & 0 deletions Assets/UIComponents/Core/EventInterfaces/IOnClick.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEngine.UIElements;

namespace UIComponents
{
/// <summary>
/// When implemented by a <see cref="UIComponent"/>,
/// a callback for <see cref="ClickEvent"/> is
/// automatically registered in the UIComponent constructor.
/// </summary>
public interface IOnClick
{
void OnClick(ClickEvent evt);
}
}
3 changes: 3 additions & 0 deletions Assets/UIComponents/Core/EventInterfaces/IOnClick.cs.meta

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

3 changes: 3 additions & 0 deletions Assets/UIComponents/Core/UIComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ private void RegisterEventInterfaceCallbacks()

if (this is IOnMouseLeave onMouseLeave)
RegisterCallback<MouseLeaveEvent>(onMouseLeave.OnMouseLeave);

if (this is IOnClick onClick)
RegisterCallback<ClickEvent>(onClick.OnClick);
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ The interfaces are:
- `IOnGeometryChanged` for `GeometryChangedEvent`
- `IOnMouseEnter` for `MouseEnterEvent`
- `IOnMouseLeave` for `MouseLeaveEvent`
- `IOnClick` for `ClickEvent`

```c#
public class ComponentWithCallbacks : UIComponent,
IOnAttachToPanel,
IOnGeometryChanged,
IOnDetachFromPanel,
IOnMouseEnter,
IOnMouseLeave
IOnMouseLeave,
IOnClick
{
public void OnAttachToPanel(AttachToPanelEvent evt)
{
Expand All @@ -190,6 +192,11 @@ public class ComponentWithCallbacks : UIComponent,
{
Debug.Log("Bye mouse");
}

public void OnClick(ClickEvent evt)
{
Debug.Log("Clicked!");
}
}
```

Expand Down

0 comments on commit 0172a64

Please sign in to comment.