118 lines
3.7 KiB
C#
118 lines
3.7 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using PrimeTween;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
[UIView("title-screen")]
|
|
public class TitleScreenUIView : UIView
|
|
{
|
|
private VisualElement _fader;
|
|
|
|
private VisualElement _logo;
|
|
|
|
private VisualElement _panel;
|
|
|
|
private Button _startGameButton;
|
|
|
|
private Label _versionLabel;
|
|
|
|
private CancellationTokenSource _animationCancellation;
|
|
|
|
public event Action StartGame;
|
|
|
|
public override UniTask InitializeAsync(UIService uiService, VisualElement rootElement)
|
|
{
|
|
base.InitializeAsync(uiService, rootElement);
|
|
|
|
_fader = rootElement.Q(className: "title-screen__fader");
|
|
_logo = rootElement.Q(className: "title-screen__logo-image");
|
|
_panel = rootElement.Q(className: "dialog");
|
|
_startGameButton = rootElement.Q<Button>(className: "title-screen__start-button");
|
|
_versionLabel = rootElement.Q<Label>(className: "title-screen__version");
|
|
|
|
_fader.style.display = DisplayStyle.None;
|
|
_fader.style.opacity = 0;
|
|
_logo.style.display = DisplayStyle.None;
|
|
_logo.style.opacity = 0;
|
|
_panel.style.display = DisplayStyle.None;
|
|
_panel.style.opacity = 0;
|
|
|
|
_startGameButton.RegisterCallbackOnce<ClickEvent>(_ => StartGame?.Invoke());
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
_animationCancellation?.Cancel();
|
|
_animationCancellation?.Dispose();
|
|
_animationCancellation = null;
|
|
|
|
base.Dispose();
|
|
}
|
|
|
|
public void SetVersion(string versionText)
|
|
{
|
|
_versionLabel.text = versionText;
|
|
}
|
|
|
|
public async UniTask PlayIntroAsync()
|
|
{
|
|
Show(true);
|
|
|
|
await UniTask.NextFrame();
|
|
|
|
_logo.style.display = DisplayStyle.Flex;
|
|
_logo.style.top = Length.Percent(50);
|
|
await UIVisibilityAnimation.PlayShowTween(_logo);
|
|
|
|
await UniTask.WaitForSeconds(1);
|
|
|
|
await Tween.Custom(50, 33, 0.95f, value => _logo.style.top = Length.Percent(value), Ease.OutCubic);
|
|
|
|
await UniTask.WaitForSeconds(1);
|
|
|
|
_panel.style.display = DisplayStyle.Flex;
|
|
await UIVisibilityAnimation.PlayShowTween(_panel);
|
|
|
|
_logo.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50));
|
|
_logo.SetScale(1);
|
|
|
|
_animationCancellation = new CancellationTokenSource();
|
|
PlayLogoIdleAnimationAsync(_animationCancellation.Token).Forget();
|
|
}
|
|
|
|
public async UniTask PlayStartFadeAsync(float duration)
|
|
{
|
|
_fader.style.display = DisplayStyle.Flex;
|
|
await Tween.Custom(0, 1, duration, value => _fader.style.opacity = value, Ease.OutQuad);
|
|
}
|
|
|
|
private async UniTask PlayLogoIdleAnimationAsync(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
float t = 0;
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
var sin = Mathf.Sin(t);
|
|
|
|
_logo.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50 + 3 * sin));
|
|
_logo.SetScale(1 - 0.025f * sin);
|
|
|
|
t += Time.deltaTime;
|
|
|
|
await UniTask.NextFrame(cancellationToken);
|
|
}
|
|
}
|
|
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
} |