Skip to content

Commit eeffb1a

Browse files
author
Miguel Cartier
committed
feat: add the DelayUiPresenter
fix: fixed the scripts identation
1 parent 43b9e00 commit eeffb1a

File tree

5 files changed

+190
-39
lines changed

5 files changed

+190
-39
lines changed

Runtime/DelayUiPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ internal override void InternalClose(bool destroy)
6666
/// </remarks>
6767
/// <typeparam name="T">The type of data held by the presenter.</typeparam>
6868
[RequireComponent(typeof(PresenterDelayerBase))]
69-
public abstract class DelayUiPresenterData<T> : UiPresenter<T> where T : struct
69+
public abstract class DelayUiPresenter<T> : UiPresenter<T> where T : struct
7070
{
7171
[SerializeField] private PresenterDelayerBase _delayer;
7272

Runtime/DelayUiToolkitPresenter.cs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using Cysharp.Threading.Tasks;
2+
using UnityEngine;
3+
4+
namespace GameLovers.UiService
5+
{
6+
/// <summary>
7+
/// Abstract base class for UI Toolkit-based presenters that delay their opening and closing.
8+
/// Combines the functionality of <see cref="UiToolkitPresenter"/> with delay capabilities from <see cref="DelayUiPresenter"/>.
9+
/// </summary>
10+
[RequireComponent(typeof(PresenterDelayerBase))]
11+
public abstract class DelayUiToolkitPresenter : UiToolkitPresenter
12+
{
13+
[SerializeField] private PresenterDelayerBase _delayer;
14+
15+
/// <summary>
16+
/// Provides access to the attached <see cref="PresenterDelayerBase"/> for derived presenters.
17+
/// </summary>
18+
protected PresenterDelayerBase Delayer => _delayer;
19+
20+
/// <inheritdoc />
21+
protected override void OnValidate()
22+
{
23+
base.OnValidate();
24+
25+
_delayer = _delayer != null ? _delayer : GetComponent<PresenterDelayerBase>();
26+
27+
OnEditorValidate();
28+
}
29+
30+
/// <inheritdoc />
31+
protected override void OnOpened()
32+
{
33+
_delayer.OpenWithDelay(OnOpenedCompleted).Forget();
34+
}
35+
36+
/// <inheritdoc />
37+
protected override void OnClosed()
38+
{
39+
_delayer.CloseWithDelay(OnClosedCompleted).Forget();
40+
}
41+
42+
/// <summary>
43+
/// Called only in the Editor. Called at the end of this object MonoBehaviour's OnValidate() -> <see cref="OnValidate"/>
44+
/// </summary>
45+
protected virtual void OnEditorValidate() { }
46+
47+
/// <summary>
48+
/// Called when the presenter's opening delay is completed.
49+
/// </summary>
50+
protected virtual void OnOpenedCompleted() { }
51+
52+
/// <summary>
53+
/// Called when the presenter's closing delay is completed.
54+
/// </summary>
55+
protected virtual void OnClosedCompleted() { }
56+
57+
internal override void InternalClose(bool destroy)
58+
{
59+
// Override the behaviour to not allow the presenter to be disabled until delay is done.
60+
// Not pretty inheritance with its dependency but it works
61+
if (destroy)
62+
{
63+
base.InternalClose(true);
64+
}
65+
else
66+
{
67+
OnClosed();
68+
}
69+
}
70+
}
71+
72+
/// <inheritdoc cref="DelayUiToolkitPresenter"/>
73+
/// <remarks>
74+
/// Extends the presenter behaviour to hold data of type <typeparamref name="T"/>
75+
/// </remarks>
76+
/// <typeparam name="T">The type of data held by the presenter.</typeparam>
77+
[RequireComponent(typeof(PresenterDelayerBase))]
78+
public abstract class DelayUiToolkitPresenter<T> : UiToolkitPresenter<T> where T : struct
79+
{
80+
[SerializeField] private PresenterDelayerBase _delayer;
81+
82+
/// <summary>
83+
/// Provides access to the attached <see cref="PresenterDelayerBase"/> for derived presenters.
84+
/// </summary>
85+
protected PresenterDelayerBase Delayer => _delayer;
86+
87+
/// <inheritdoc />
88+
protected override void OnValidate()
89+
{
90+
base.OnValidate();
91+
92+
_delayer = _delayer ?? GetComponent<PresenterDelayerBase>();
93+
94+
OnEditorValidate();
95+
}
96+
97+
/// <inheritdoc />
98+
protected override void OnOpened()
99+
{
100+
_delayer.OpenWithDelay(OnOpenedCompleted).Forget();
101+
}
102+
103+
/// <inheritdoc />
104+
protected override void OnClosed()
105+
{
106+
_delayer.CloseWithDelay(OnClosedCompleted).Forget();
107+
}
108+
109+
/// <summary>
110+
/// Called only in the Editor. Called at the end of this object MonoBehaviour's OnValidate() -> <see cref="OnValidate"/>
111+
/// </summary>
112+
protected virtual void OnEditorValidate() { }
113+
114+
/// <summary>
115+
/// Called when the presenter's opening delay is completed.
116+
/// </summary>
117+
protected virtual void OnOpenedCompleted() { }
118+
119+
/// <summary>
120+
/// Called when the presenter's closing delay is completed.
121+
/// </summary>
122+
protected virtual void OnClosedCompleted() { }
123+
124+
internal override void InternalClose(bool destroy)
125+
{
126+
// Override the behaviour to not allow the presenter to be disabled until delay is done.
127+
// Not pretty inheritance with its dependency but it works
128+
if (destroy)
129+
{
130+
base.InternalClose(true);
131+
}
132+
else
133+
{
134+
OnClosed();
135+
}
136+
}
137+
}
138+
}
139+

Runtime/DelayUiToolkitPresenter.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Delayers/AnimationDelayer.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace GameLovers.UiService
1111
[RequireComponent(typeof(Animation))]
1212
public class AnimationDelayer : PresenterDelayerBase
1313
{
14-
[SerializeField] protected Animation _animation;
15-
[SerializeField] protected AnimationClip _introAnimationClip;
16-
[SerializeField] protected AnimationClip _outroAnimationClip;
14+
[SerializeField] protected Animation _animation;
15+
[SerializeField] protected AnimationClip _introAnimationClip;
16+
[SerializeField] protected AnimationClip _outroAnimationClip;
1717

18-
/// <inheritdoc />
19-
public override float OpenDelayInSeconds => _introAnimationClip?.length ?? 0f;
18+
/// <inheritdoc />
19+
public override float OpenDelayInSeconds => _introAnimationClip?.length ?? 0f;
2020

21-
/// <inheritdoc />
22-
public override float CloseDelayInSeconds => _outroAnimationClip?.length ?? 0f;
21+
/// <inheritdoc />
22+
public override float CloseDelayInSeconds => _outroAnimationClip?.length ?? 0f;
2323

2424
private void OnValidate()
2525
{
@@ -28,12 +28,22 @@ private void OnValidate()
2828

2929
protected override void OnOpenStarted()
3030
{
31+
if (!_introAnimationClip)
32+
{
33+
return;
34+
}
35+
3136
_animation.clip = _introAnimationClip;
3237
_animation.Play();
3338
}
3439

3540
protected override void OnCloseStarted()
3641
{
42+
if (!_outroAnimationClip)
43+
{
44+
return;
45+
}
46+
3747
_animation.clip = _outroAnimationClip;
3848
_animation.Play();
3949
}

Runtime/Delayers/PresenterDelayerBase.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ public interface IPresenterDelayer
1919
/// </summary>
2020
float CloseDelayInSeconds { get; }
2121

22-
/// <summary>
23-
/// Gets the UniTask of the current's delay process
24-
/// </summary>
25-
UniTask CurrentDelayTask { get; }
22+
/// <summary>
23+
/// Gets the UniTask of the current's delay process
24+
/// </summary>
25+
UniTask CurrentDelayTask { get; }
2626
}
2727

2828
/// <inheritdoc />
2929
public abstract class PresenterDelayerBase : MonoBehaviour, IPresenterDelayer
3030
{
31-
/// <inheritdoc />
32-
public abstract float OpenDelayInSeconds { get; }
33-
/// <inheritdoc />
34-
public abstract float CloseDelayInSeconds { get; }
35-
/// <inheritdoc />
36-
public UniTask CurrentDelayTask { get; protected set; }
31+
/// <inheritdoc />
32+
public abstract float OpenDelayInSeconds { get; }
33+
/// <inheritdoc />
34+
public abstract float CloseDelayInSeconds { get; }
35+
/// <inheritdoc />
36+
public UniTask CurrentDelayTask { get; protected set; }
3737

3838
/// <summary>
3939
/// Called when the presenter's opening delay starts.
@@ -45,33 +45,33 @@ protected virtual void OnOpenStarted() { }
4545
/// </summary>
4646
protected virtual void OnCloseStarted() { }
4747

48-
internal async UniTask OpenWithDelay(Action onOpenedCompleted)
49-
{
50-
OnOpenStarted();
48+
internal async UniTask OpenWithDelay(Action onOpenedCompleted)
49+
{
50+
OnOpenStarted();
5151

52-
CurrentDelayTask = UniTask.Delay(TimeSpan.FromSeconds(OpenDelayInSeconds));
52+
CurrentDelayTask = UniTask.Delay(TimeSpan.FromSeconds(OpenDelayInSeconds));
5353

54-
await CurrentDelayTask;
54+
await CurrentDelayTask;
5555

56-
if (gameObject != null)
57-
{
58-
onOpenedCompleted();
59-
}
60-
}
56+
if (gameObject != null)
57+
{
58+
onOpenedCompleted();
59+
}
60+
}
6161

62-
internal async UniTask CloseWithDelay(Action onCloseCompleted)
63-
{
64-
OnCloseStarted();
62+
internal async UniTask CloseWithDelay(Action onCloseCompleted)
63+
{
64+
OnCloseStarted();
6565

66-
CurrentDelayTask = UniTask.Delay(TimeSpan.FromSeconds(CloseDelayInSeconds));
66+
CurrentDelayTask = UniTask.Delay(TimeSpan.FromSeconds(CloseDelayInSeconds));
6767

68-
await CurrentDelayTask;
68+
await CurrentDelayTask;
6969

70-
if (gameObject != null)
71-
{
72-
gameObject.SetActive(false);
73-
onCloseCompleted();
74-
}
75-
}
70+
if (gameObject != null)
71+
{
72+
gameObject.SetActive(false);
73+
onCloseCompleted();
74+
}
75+
}
7676
}
7777
}

0 commit comments

Comments
 (0)