223 lines
6.5 KiB
C#
223 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using AibisDream;
|
|
using AibisDream.Utility;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Localization.Components;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream.UI
|
|
{
|
|
public class TerminalChapterPanel : MonoBehaviour
|
|
{
|
|
[SerializeField] private TerminalPanelAnimator panelAnimator;
|
|
[SerializeField] private TerminalChapterItemView leftPreview;
|
|
[SerializeField] private TerminalChapterItemView currentView;
|
|
[SerializeField] private TerminalChapterItemView rightPreview;
|
|
[SerializeField] private TMP_Text emptyText;
|
|
[SerializeField] private TMP_Text selectedTitleText;
|
|
[SerializeField] private LocalizeStringEvent selectedTitleLocalize;
|
|
[SerializeField] private Button previousButton;
|
|
[SerializeField] private Button nextButton;
|
|
[SerializeField] private Button restartButton;
|
|
[SerializeField] private Button backButton;
|
|
|
|
private readonly List<ChapterVo> _chapters = new List<ChapterVo>();
|
|
private int _currentIndex;
|
|
|
|
public bool IsOpen => gameObject.activeSelf;
|
|
private TerminalPanel _owner;
|
|
|
|
public void SetOwner(TerminalPanel owner)
|
|
{
|
|
_owner = owner;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (selectedTitleLocalize == null && selectedTitleText != null)
|
|
{
|
|
selectedTitleLocalize = selectedTitleText.GetComponent<LocalizeStringEvent>();
|
|
}
|
|
|
|
previousButton?.onClick.AddListener(Previous);
|
|
nextButton?.onClick.AddListener(Next);
|
|
restartButton?.onClick.AddListener(Restart);
|
|
backButton?.onClick.AddListener(Close);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
previousButton?.onClick.RemoveListener(Previous);
|
|
nextButton?.onClick.RemoveListener(Next);
|
|
restartButton?.onClick.RemoveListener(Restart);
|
|
backButton?.onClick.RemoveListener(Close);
|
|
}
|
|
|
|
public void Show()
|
|
{
|
|
if (panelAnimator != null)
|
|
{
|
|
panelAnimator.Show(prepare: PrepareContent);
|
|
}
|
|
else
|
|
{
|
|
PrepareContent();
|
|
gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
public void Hide(Action onComplete = null)
|
|
{
|
|
if (panelAnimator != null)
|
|
{
|
|
panelAnimator.Hide(onComplete);
|
|
}
|
|
else
|
|
{
|
|
gameObject.SetActive(false);
|
|
onComplete?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void PrepareContent()
|
|
{
|
|
_owner?.SetHeader("terminal_header_chapter", "AUTH_VERIFIED");
|
|
LoadChapters();
|
|
RefreshViews();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
_owner.Back();
|
|
}
|
|
|
|
private void LoadChapters()
|
|
{
|
|
_chapters.Clear();
|
|
var chapters = ChapterController.Instance.GetChapterList();
|
|
_chapters.AddRange(chapters);
|
|
_currentIndex = Mathf.Clamp(_currentIndex, 0, Mathf.Max(0, _chapters.Count - 1));
|
|
}
|
|
|
|
private void RefreshViews()
|
|
{
|
|
var hasChapters = _chapters.Count > 0;
|
|
if (emptyText != null)
|
|
{
|
|
emptyText.gameObject.SetActive(!hasChapters);
|
|
}
|
|
|
|
var showLeftPreview = hasChapters && _currentIndex > 0;
|
|
var showRightPreview = hasChapters && _currentIndex < _chapters.Count - 1;
|
|
|
|
SetViewActive(leftPreview, showLeftPreview);
|
|
SetViewActive(currentView, hasChapters);
|
|
SetViewActive(rightPreview, showRightPreview);
|
|
|
|
if (!hasChapters)
|
|
{
|
|
SetSelectedTitle(null, "NO CHAPTER DATA");
|
|
|
|
restartButton?.gameObject.SetActive(false);
|
|
UpdateNavigationButtons(false);
|
|
return;
|
|
}
|
|
|
|
var current = _chapters[_currentIndex];
|
|
currentView?.Init(current, selected: true, preview: false);
|
|
|
|
if (showLeftPreview)
|
|
{
|
|
leftPreview?.Init(_chapters[_currentIndex - 1], selected: false, preview: true);
|
|
}
|
|
|
|
if (showRightPreview)
|
|
{
|
|
rightPreview?.Init(_chapters[_currentIndex + 1], selected: false, preview: true);
|
|
}
|
|
|
|
SetSelectedTitle(current.titleKey, current.title);
|
|
|
|
restartButton?.gameObject.SetActive(true);
|
|
UpdateNavigationButtons(true);
|
|
}
|
|
|
|
private void UpdateNavigationButtons(bool hasChapters)
|
|
{
|
|
if (previousButton != null)
|
|
{
|
|
previousButton.interactable = hasChapters && _currentIndex > 0;
|
|
}
|
|
|
|
if (nextButton != null)
|
|
{
|
|
nextButton.interactable = hasChapters && _currentIndex < _chapters.Count - 1;
|
|
}
|
|
}
|
|
|
|
private static void SetViewActive(TerminalChapterItemView view, bool active)
|
|
{
|
|
if (view != null)
|
|
{
|
|
view.gameObject.SetActive(active);
|
|
}
|
|
}
|
|
|
|
private void SetSelectedTitle(string titleKey, string fallbackTitle)
|
|
{
|
|
if (!string.IsNullOrEmpty(titleKey) && selectedTitleLocalize != null)
|
|
{
|
|
selectedTitleLocalize.enabled = true;
|
|
selectedTitleLocalize.SetTable(ConstRef.ChapterInfoTable);
|
|
selectedTitleLocalize.SetEntry(titleKey);
|
|
return;
|
|
}
|
|
|
|
if (selectedTitleLocalize != null)
|
|
{
|
|
selectedTitleLocalize.enabled = false;
|
|
}
|
|
|
|
if (selectedTitleText != null)
|
|
{
|
|
selectedTitleText.text = fallbackTitle;
|
|
}
|
|
}
|
|
|
|
private void Previous()
|
|
{
|
|
if (_chapters.Count == 0 || _currentIndex <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_currentIndex--;
|
|
RefreshViews();
|
|
}
|
|
|
|
private void Next()
|
|
{
|
|
if (_chapters.Count == 0 || _currentIndex >= _chapters.Count - 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_currentIndex++;
|
|
RefreshViews();
|
|
}
|
|
|
|
private void Restart()
|
|
{
|
|
if (_chapters.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var chapter = _chapters[_currentIndex];
|
|
_owner.CloseThen(() => ChapterController.Instance.StartWithChapter(chapter));
|
|
}
|
|
}
|
|
}
|