154 lines
3.7 KiB
C#
154 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using Yarn.Unity;
|
|
using System.Linq; // 添加这个 using 语句
|
|
using DG.Tweening;
|
|
using AibisDream;
|
|
|
|
|
|
public class TaskEntry
|
|
{
|
|
public string Text { get; set; }
|
|
public bool IsCompleted { get; set; }
|
|
public int IndentLevel { get; set; }
|
|
public List<TaskEntry> SubTasks { get; set; }
|
|
|
|
public TaskEntry(string text, int indentLevel = 0)
|
|
{
|
|
Text = text;
|
|
IsCompleted = false;
|
|
IndentLevel = indentLevel;
|
|
SubTasks = new List<TaskEntry>();
|
|
}
|
|
}
|
|
public class TaskManager : MonoBehaviour
|
|
{
|
|
public GameObject taskTextPrefab; // 用于显示任务的文本预制体
|
|
public Transform taskListParent; // 任务列表的父物体
|
|
private List<TaskEntry> tasks = new List<TaskEntry>();
|
|
|
|
private RectTransform pos;
|
|
|
|
public void Awake()
|
|
{
|
|
pos=GetComponent<RectTransform>();
|
|
}
|
|
|
|
|
|
[YarnCommand("task_moveIn")]
|
|
public void Task_moveIn()
|
|
{
|
|
AudioManager.PlayInteraction("task_inout");
|
|
pos.DOAnchorPosY(-170f,1f);
|
|
}
|
|
|
|
|
|
[YarnCommand("task_moveOut")]
|
|
public void Task_moveOut()
|
|
{
|
|
AudioManager.PlayInteraction("task_inout");
|
|
pos.DOAnchorPosY(175,1f);
|
|
}
|
|
|
|
[YarnCommand("add_task")]
|
|
public void AddTask(string text, string parentTaskText = null)
|
|
{
|
|
TaskEntry newTask = new TaskEntry(text);
|
|
|
|
if (!string.IsNullOrEmpty(parentTaskText))
|
|
{
|
|
TaskEntry parentTask = FindTaskByText(tasks, parentTaskText);
|
|
if (parentTask != null)
|
|
{
|
|
newTask.IndentLevel = parentTask.IndentLevel + 1;
|
|
parentTask.SubTasks.Add(newTask);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Parent task not found");
|
|
}
|
|
}
|
|
else if(FindTaskByText(tasks, text)!=null)
|
|
{
|
|
Debug.LogWarning("Allreday Added");
|
|
}
|
|
else
|
|
{
|
|
tasks.Add(newTask);
|
|
}
|
|
|
|
UpdateTaskUI();
|
|
}
|
|
|
|
[YarnCommand("complete_task")]
|
|
public void CompleteTask(string taskText)
|
|
{
|
|
TaskEntry task = FindTaskByText(tasks, taskText);
|
|
if (task != null)
|
|
{
|
|
task.IsCompleted = true;
|
|
UpdateTaskUI();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Task" +taskText +"not found");
|
|
}
|
|
}
|
|
|
|
[YarnCommand("clear_tasks")]
|
|
public void ClearTasks()
|
|
{
|
|
tasks.Clear(); // 清空任务列表
|
|
UpdateTaskUI(); // 更新 UI,清除所有显示的任务
|
|
}
|
|
|
|
private TaskEntry FindTaskByText(List<TaskEntry> tasks, string text)
|
|
{
|
|
foreach (var task in tasks)
|
|
{
|
|
if (task.Text == text) return task;
|
|
|
|
TaskEntry found = FindTaskByText(task.SubTasks, text);
|
|
if (found != null) return found;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private void UpdateTaskUI()
|
|
{
|
|
foreach (Transform child in taskListParent)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
foreach (var task in tasks)
|
|
{
|
|
CreateTaskUI(task);
|
|
}
|
|
}
|
|
|
|
private void CreateTaskUI(TaskEntry task)
|
|
{
|
|
GameObject taskObject = Instantiate(taskTextPrefab, taskListParent);
|
|
TextMeshProUGUI taskText = taskObject.GetComponent<TextMeshProUGUI>();
|
|
|
|
if (task.IsCompleted)
|
|
{
|
|
taskText.text = "<s>" + new string(' ', task.IndentLevel * 4) + task.Text + "</s>";
|
|
taskText.color = Color.gray;
|
|
}
|
|
else
|
|
{
|
|
taskText.text = new string(' ', task.IndentLevel * 4) + task.Text;
|
|
}
|
|
|
|
foreach (var subTask in task.SubTasks)
|
|
{
|
|
CreateTaskUI(subTask);
|
|
}
|
|
}
|
|
}
|