135 lines
3.5 KiB
C#
135 lines
3.5 KiB
C#
using Core.TaskSystem;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace UI.Tasks
|
|
{
|
|
public class TaskPanelController : MonoBehaviour
|
|
{
|
|
[Header("System")]
|
|
[SerializeField] private TaskService taskService;
|
|
|
|
[Header("List")]
|
|
[SerializeField] private RectTransform listContent;
|
|
[SerializeField] private TaskListItemView taskItemPrefab;
|
|
|
|
[Header("Details")]
|
|
[SerializeField] private TMP_Text detailText;
|
|
[SerializeField] private TMP_Text titleText;
|
|
|
|
[Header("Selection")]
|
|
[SerializeField] private bool autoSelectFirstTask = true;
|
|
|
|
TaskService boundService;
|
|
string selectedTaskId;
|
|
|
|
void OnEnable()
|
|
{
|
|
BindService();
|
|
RefreshAll();
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
UnbindService();
|
|
}
|
|
|
|
void BindService()
|
|
{
|
|
TaskService service = taskService != null ? taskService : TaskService.Instance;
|
|
if (service == null)
|
|
{
|
|
Debug.LogError($"{nameof(TaskPanelController)} requires a TaskService reference.", this);
|
|
return;
|
|
}
|
|
|
|
if (boundService == service) { return; }
|
|
UnbindService();
|
|
|
|
boundService = service;
|
|
boundService.Changed += RefreshAll;
|
|
}
|
|
|
|
void UnbindService()
|
|
{
|
|
if (boundService == null) { return; }
|
|
boundService.Changed -= RefreshAll;
|
|
boundService = null;
|
|
}
|
|
|
|
void RefreshAll()
|
|
{
|
|
if (boundService == null) { return; }
|
|
if (listContent == null || taskItemPrefab == null) { return; }
|
|
|
|
for (int i = listContent.childCount - 1; i >= 0; i--)
|
|
{
|
|
Destroy(listContent.GetChild(i).gameObject);
|
|
}
|
|
|
|
for (int i = 0; i < boundService.Tasks.Count; i++)
|
|
{
|
|
TaskEntry task = boundService.Tasks[i];
|
|
TaskListItemView view = Instantiate(taskItemPrefab, listContent);
|
|
view.Bind(task, OnTaskClicked);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(selectedTaskId) && boundService.TryGetTask(selectedTaskId, out TaskEntry selected))
|
|
{
|
|
ShowDetails(selected);
|
|
return;
|
|
}
|
|
|
|
if (autoSelectFirstTask && boundService.Tasks.Count > 0)
|
|
{
|
|
Select(boundService.Tasks[0]);
|
|
return;
|
|
}
|
|
|
|
ClearDetails();
|
|
}
|
|
|
|
void OnTaskClicked(TaskEntry task)
|
|
{
|
|
Select(task);
|
|
}
|
|
|
|
void Select(TaskEntry task)
|
|
{
|
|
if (task == null)
|
|
{
|
|
selectedTaskId = null;
|
|
ClearDetails();
|
|
return;
|
|
}
|
|
|
|
selectedTaskId = task.id;
|
|
ShowDetails(task);
|
|
}
|
|
|
|
void ShowDetails(TaskEntry task)
|
|
{
|
|
if (titleText != null) { titleText.text = task.ShortName; }
|
|
|
|
if (detailText != null)
|
|
{
|
|
string statusLabel = task.status switch
|
|
{
|
|
TaskStatus.Completed => "完成",
|
|
TaskStatus.Expired => "超时",
|
|
_ => "待做"
|
|
};
|
|
|
|
detailText.text = $"{statusLabel}\n\n{task.Description}";
|
|
}
|
|
}
|
|
|
|
void ClearDetails()
|
|
{
|
|
if (titleText != null) { titleText.text = string.Empty; }
|
|
if (detailText != null) { detailText.text = string.Empty; }
|
|
}
|
|
}
|
|
}
|
|
|