mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +08:00
107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
// ---------------------------------------------------------------------------------------------
|
|
// Copyright (c) 2021-2024, Jiaqi (0x7c13) Liu. All rights reserved.
|
|
// See LICENSE file in the project root for license information.
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
namespace Pal3.Game.GameSystems.Caption
|
|
{
|
|
using System;
|
|
using System.Collections;
|
|
using Command;
|
|
using Command.Extensions;
|
|
using Core.Command;
|
|
using Core.Command.SceCommands;
|
|
using Core.Utilities;
|
|
using Data;
|
|
using Engine.Animation;
|
|
using Engine.Core.Abstraction;
|
|
using Engine.Coroutine;
|
|
using Input;
|
|
using Script.Waiter;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
|
|
public sealed class CaptionRenderer : IDisposable,
|
|
#if PAL3A
|
|
ICommandExecutor<UIDisplayNextCaptionCommand>,
|
|
#endif
|
|
ICommandExecutor<UIDisplayCaptionCommand>
|
|
{
|
|
private const float CAPTION_ANIMATION_DURATION = 10f;
|
|
|
|
private readonly GameResourceProvider _gameResourceProvider;
|
|
private readonly PlayerInputActions _playerInputActions;
|
|
private readonly Image _captionImage;
|
|
|
|
private WaitUntilCanceled _skipCaptionWaiter;
|
|
private bool _skipCaptionRequested;
|
|
|
|
public CaptionRenderer(GameResourceProvider gameResourceProvider,
|
|
PlayerInputActions playerInputActions,
|
|
Image captionImage)
|
|
{
|
|
_gameResourceProvider = Requires.IsNotNull(gameResourceProvider, nameof(gameResourceProvider));
|
|
_playerInputActions = Requires.IsNotNull(playerInputActions, nameof(playerInputActions));
|
|
_captionImage = Requires.IsNotNull(captionImage, nameof(captionImage));
|
|
|
|
_captionImage.preserveAspect = true;
|
|
_playerInputActions.Cutscene.Continue.performed += CutsceneContinueOnPerformed;
|
|
|
|
CommandExecutorRegistry<ICommand>.Instance.Register(this);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_playerInputActions.Cutscene.Continue.performed -= CutsceneContinueOnPerformed;
|
|
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
|
|
}
|
|
|
|
private void CutsceneContinueOnPerformed(InputAction.CallbackContext obj)
|
|
{
|
|
_skipCaptionRequested = true;
|
|
}
|
|
|
|
private IEnumerator AnimateCaptionAsync(string textureName)
|
|
{
|
|
_skipCaptionWaiter = new WaitUntilCanceled();
|
|
Pal3.Instance.Execute(new ScriptRunnerAddWaiterRequest(_skipCaptionWaiter));
|
|
|
|
ITexture2D texture = _gameResourceProvider.GetCaptionTexture(textureName);
|
|
ISprite sprite = texture.CreateSprite(0f, 0f,
|
|
texture.Width, texture.Height,
|
|
0.5f, 0.5f);
|
|
|
|
_captionImage.sprite = sprite.NativeObject as Sprite;
|
|
_captionImage.color = Color.clear;
|
|
|
|
yield return CoreAnimation.EnumerateValueAsync(0, 1, CAPTION_ANIMATION_DURATION,
|
|
AnimationCurveType.Linear, alpha =>
|
|
{
|
|
_captionImage.color = new Color(1f, 1f, 1f, alpha);
|
|
});
|
|
|
|
_skipCaptionRequested = false;
|
|
yield return CoroutineYieldInstruction.WaitUntil(() => _skipCaptionRequested);
|
|
|
|
_captionImage.color = Color.clear;
|
|
texture.Destroy();
|
|
sprite.Destroy();
|
|
|
|
_skipCaptionWaiter.CancelWait();
|
|
_skipCaptionWaiter = null;
|
|
}
|
|
|
|
public void Execute(UIDisplayCaptionCommand command)
|
|
{
|
|
Pal3.Instance.StartCoroutine(AnimateCaptionAsync(command.TextureName));
|
|
}
|
|
|
|
#if PAL3A
|
|
public void Execute(UIDisplayNextCaptionCommand command)
|
|
{
|
|
Pal3.Instance.StartCoroutine(AnimateCaptionAsync(command.TextureName));
|
|
}
|
|
#endif
|
|
}
|
|
} |