199 lines
8.1 KiB
C#
199 lines
8.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading;
|
||
using Code.Infrastructure.UI;
|
||
using Code.OfficeGame.BattleMode.Timeline;
|
||
using Cysharp.Threading.Tasks;
|
||
using LitMotion;
|
||
using LitMotion.Extensions;
|
||
using R3;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
namespace Code.OfficeGame.BattleMode.UI
|
||
{
|
||
public class BattleTimelinePanel : UIPanel
|
||
{
|
||
[SerializeField] private float duration = 0.35f;
|
||
[SerializeField] private TMP_Text prevAction;
|
||
[SerializeField] private TMP_Text frameCounterText;
|
||
|
||
[Header("Управление ползунком")]
|
||
[SerializeField] private Slider timelineSlider;
|
||
[SerializeField] private Button playPauseButton;
|
||
[SerializeField] private Button stepBackButton;
|
||
[SerializeField] private Button stepForwardButton;
|
||
[SerializeField] private Button returnToCommitButton;
|
||
|
||
[Header("Маркеры и цветные полосы событий")]
|
||
[SerializeField] private RectTransform markersContainer;
|
||
[SerializeField] private Image markerStripePrefab;
|
||
[SerializeField] private RectTransform commitMarkerLine;
|
||
|
||
[SerializeField] private Color[] stageInterruptionColors =
|
||
{
|
||
new(1.0f, 0.85f, 0.2f, 0.95f), // 1-е прерывание
|
||
new(1.0f, 0.55f, 0.2f, 0.95f), // 2-е прерывание
|
||
new(0.95f, 0.3f, 0.5f, 0.95f) // 3+ прерывание
|
||
};
|
||
[SerializeField] private Color hitEventColor = new(1f, 0.3f, 0.1f, 0.9f);
|
||
[SerializeField] private Color deathEventColor = new(0.85f, 0.05f, 0.15f, 1f);
|
||
|
||
private readonly List<Image> _spawnedMarkers = new();
|
||
private bool _suppressSliderCallback;
|
||
private MotionHandle _motionHandle;
|
||
|
||
public void SetPreviousActionText(string text)
|
||
{
|
||
if (prevAction != null) prevAction.text = text;
|
||
}
|
||
|
||
public IDisposable BindTimeline(BattleTimeline timeline)
|
||
{
|
||
var bag = new DisposableBag();
|
||
|
||
timelineSlider.wholeNumbers = true;
|
||
|
||
// 1. Границы ползунка растут до конца последнего действия
|
||
timeline.MaxFrame
|
||
.Subscribe(maxFrame =>
|
||
{
|
||
timelineSlider.minValue = 0;
|
||
timelineSlider.maxValue = Mathf.Max(1, maxFrame);
|
||
UpdateFrameCounter(timeline.DisplayFrame.CurrentValue, maxFrame);
|
||
})
|
||
.AddTo(ref bag);
|
||
|
||
// 2. Текущий кадр двигает ползунок без рекурсии
|
||
timeline.DisplayFrame
|
||
.Subscribe(frame =>
|
||
{
|
||
_suppressSliderCallback = true;
|
||
timelineSlider.SetValueWithoutNotify(frame);
|
||
_suppressSliderCallback = false;
|
||
UpdateFrameCounter(frame, timeline.MaxFrame.CurrentValue);
|
||
})
|
||
.AddTo(ref bag);
|
||
|
||
// 3. Ползунок заблокирован только во время реального исполнения
|
||
timeline.Mode
|
||
.Subscribe(mode => timelineSlider.interactable = mode != TimelineMode.BattlePlaying)
|
||
.AddTo(ref bag);
|
||
|
||
// 4. Обновление цветных полос прерываний, ударов и смертей при перепекании
|
||
timeline.OnTrackRebaked
|
||
.Subscribe(track => RebuildMarkers(track, timeline.CommittedFrame.CurrentValue))
|
||
.AddTo(ref bag);
|
||
|
||
timeline.CommittedFrame
|
||
.Subscribe(commitFrame => UpdateCommitLine(commitFrame, timeline.MaxFrame.CurrentValue))
|
||
.AddTo(ref bag);
|
||
|
||
// 5. Пользователь тянет ползунок -> скраббинг таймлайна
|
||
timelineSlider.OnValueChangedAsObservable()
|
||
.Subscribe(val =>
|
||
{
|
||
if (_suppressSliderCallback) return;
|
||
timeline.ScrubToFrame((int)val);
|
||
})
|
||
.AddTo(ref bag);
|
||
|
||
if (playPauseButton != null)
|
||
playPauseButton.OnClickAsObservable().Subscribe(_ => timeline.TogglePreviewPlayPause()).AddTo(ref bag);
|
||
|
||
if (stepBackButton != null)
|
||
stepBackButton.OnClickAsObservable().Subscribe(_ => timeline.StepFrame(-1)).AddTo(ref bag);
|
||
|
||
if (stepForwardButton != null)
|
||
stepForwardButton.OnClickAsObservable().Subscribe(_ => timeline.StepFrame(+1)).AddTo(ref bag);
|
||
|
||
if (returnToCommitButton != null)
|
||
returnToCommitButton.OnClickAsObservable().Subscribe(_ => timeline.ReturnToCommittedFrame()).AddTo(ref bag);
|
||
|
||
return bag;
|
||
}
|
||
|
||
private void RebuildMarkers(BattleTimelineTrack track, int committedFrame)
|
||
{
|
||
for (int i = 0; i < _spawnedMarkers.Count; i++)
|
||
if (_spawnedMarkers[i] != null) Destroy(_spawnedMarkers[i].gameObject);
|
||
_spawnedMarkers.Clear();
|
||
|
||
float maxFrame = Mathf.Max(1, track.LastFrame);
|
||
UpdateCommitLine(committedFrame, track.LastFrame);
|
||
|
||
if (markerStripePrefab == null || markersContainer == null) return;
|
||
|
||
var events = track.AllEvents;
|
||
for (int i = 0; i < events.Count; i++)
|
||
{
|
||
var ev = events[i];
|
||
Color? color = ev.EventType switch
|
||
{
|
||
TimelineEventType.ActionCompleted => GetInterruptionColor(ev.InterruptionStage),
|
||
TimelineEventType.HitLanded => hitEventColor,
|
||
TimelineEventType.EntityDied => deathEventColor,
|
||
_ => null
|
||
};
|
||
|
||
if (!color.HasValue) continue;
|
||
|
||
var stripe = Instantiate(markerStripePrefab, markersContainer);
|
||
stripe.color = color.Value;
|
||
PlaceNormalizedX((RectTransform)stripe.transform, ev.Frame / maxFrame);
|
||
_spawnedMarkers.Add(stripe);
|
||
}
|
||
}
|
||
|
||
private Color GetInterruptionColor(int stage)
|
||
{
|
||
if (stageInterruptionColors == null || stageInterruptionColors.Length == 0)
|
||
return Color.yellow;
|
||
int idx = Mathf.Clamp(stage - 1, 0, stageInterruptionColors.Length - 1);
|
||
return stageInterruptionColors[idx];
|
||
}
|
||
|
||
private void UpdateCommitLine(int committedFrame, int maxFrame)
|
||
{
|
||
if (commitMarkerLine == null) return;
|
||
PlaceNormalizedX(commitMarkerLine, committedFrame / (float)Mathf.Max(1, maxFrame));
|
||
}
|
||
|
||
private static void PlaceNormalizedX(RectTransform rect, float normalized01)
|
||
{
|
||
normalized01 = Mathf.Clamp01(normalized01);
|
||
rect.anchorMin = new Vector2(normalized01, 0f);
|
||
rect.anchorMax = new Vector2(normalized01, 1f);
|
||
rect.anchoredPosition = Vector2.zero;
|
||
}
|
||
|
||
private void UpdateFrameCounter(int current, int max)
|
||
{
|
||
if (frameCounterText != null)
|
||
frameCounterText.SetText($"{current} / {max}");
|
||
}
|
||
|
||
public override async UniTask Open(CancellationToken cts = default)
|
||
{
|
||
gameObject.SetActive(true);
|
||
float startY = -((RectTransform)transform).rect.height;
|
||
_motionHandle = LMotion.Create(startY, 0f, duration)
|
||
.WithEase(Ease.OutCubic)
|
||
.WithScheduler(MotionScheduler.UpdateIgnoreTimeScale)
|
||
.BindToAnchoredPositionY((RectTransform)transform);
|
||
await _motionHandle.ToUniTask(cts);
|
||
}
|
||
|
||
public override async UniTask Close(CancellationToken cts = default)
|
||
{
|
||
float targetY = ((RectTransform)transform).rect.height;
|
||
_motionHandle = LMotion.Create(0f, targetY, duration)
|
||
.WithEase(Ease.InBack)
|
||
.WithScheduler(MotionScheduler.UpdateIgnoreTimeScale)
|
||
.BindToAnchoredPositionY((RectTransform)transform);
|
||
await _motionHandle.ToUniTask(cts);
|
||
gameObject.SetActive(false);
|
||
}
|
||
}
|
||
} |