129 lines
3.6 KiB
C#
129 lines
3.6 KiB
C#
using System.Collections.Generic;
|
|
using AibisDream.Framework;
|
|
using AibisDream.SaveSystem;
|
|
using AibisDream.Utility;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class TaskPanel : MonoBehaviour, ILineView
|
|
{
|
|
private const float HiddenLocalX = -10.32f;
|
|
private const float ShownLocalX = -7.12f;
|
|
|
|
#region 索引
|
|
|
|
[SerializeField] private Transform taskRoot;
|
|
[SerializeField] private Transform taskListParent;
|
|
private GameObject _taskItemPrefab;
|
|
private readonly Dictionary<string, GameObject> _taskDict = new();
|
|
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
_taskItemPrefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.TaskItemName);
|
|
}
|
|
|
|
public void ShowPanel()
|
|
{
|
|
taskRoot.DOLocalMoveX(ShownLocalX, 1f);
|
|
}
|
|
|
|
public void CompleteTask(string lineId)
|
|
{
|
|
if (_taskDict.Remove(lineId, out var taskItem))
|
|
{
|
|
Destroy(taskItem);
|
|
}
|
|
}
|
|
|
|
public void ClearTasks()
|
|
{
|
|
foreach (var task in _taskDict)
|
|
{
|
|
Destroy(task.Value);
|
|
}
|
|
_taskDict.Clear();
|
|
}
|
|
|
|
public void RunLine(LineSyncToken token)
|
|
{
|
|
if (_taskDict.ContainsKey(token.lineInfo.lineId))
|
|
{
|
|
EnumEventSystem.Global.Send(DialogEventEnum.LineShown);
|
|
token.ForceAdvance();
|
|
return;
|
|
}
|
|
|
|
AddTaskItem(token.lineInfo.lineId, $"\u25cf {token.lineInfo.lineText}");
|
|
token.TextStart();
|
|
token.TextShown();
|
|
token.ForceAdvance();
|
|
}
|
|
|
|
public void CaptureSnapshot(out bool isPanelVisible, List<TaskEntrySnapshotDto> tasks)
|
|
{
|
|
isPanelVisible = Mathf.Approximately(taskRoot.localPosition.x, ShownLocalX);
|
|
tasks.Clear();
|
|
|
|
foreach (var kv in _taskDict)
|
|
{
|
|
var text = kv.Value.GetComponent<TMP_Text>()?.text ?? string.Empty;
|
|
tasks.Add(new TaskEntrySnapshotDto
|
|
{
|
|
lineId = kv.Key,
|
|
text = text
|
|
});
|
|
}
|
|
}
|
|
|
|
public void ApplySnapshot(bool isPanelVisible, List<TaskEntrySnapshotDto> tasks, bool immediate)
|
|
{
|
|
ClearTasks();
|
|
|
|
if (tasks != null)
|
|
{
|
|
foreach (var entry in tasks)
|
|
{
|
|
if (string.IsNullOrEmpty(entry.lineId))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
AddTaskItem(entry.lineId, entry.text);
|
|
}
|
|
}
|
|
|
|
if (immediate)
|
|
{
|
|
taskRoot.DOKill();
|
|
SetPanelVisibleImmediate(isPanelVisible);
|
|
}
|
|
else if (isPanelVisible)
|
|
{
|
|
ShowPanel();
|
|
}
|
|
}
|
|
|
|
private void AddTaskItem(string lineId, string text)
|
|
{
|
|
var taskItem = Instantiate(_taskItemPrefab, taskListParent);
|
|
var task = taskItem.GetComponent<TMP_Text>();
|
|
task.text = text;
|
|
_taskDict[lineId] = taskItem;
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(taskListParent.GetComponent<RectTransform>());
|
|
}
|
|
|
|
private void SetPanelVisibleImmediate(bool visible)
|
|
{
|
|
var pos = taskRoot.localPosition;
|
|
pos.x = visible ? ShownLocalX : HiddenLocalX;
|
|
taskRoot.localPosition = pos;
|
|
}
|
|
}
|
|
}
|