Files
ichni_Creator_Studio/Assets/Scripts/Manager/OperationManager.cs

261 lines
9.4 KiB
C#
Raw Normal View History

2025-02-19 09:15:51 -05:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ichni.Editor.Commands;
using Ichni.RhythmGame;
using Ichni.RhythmGame.Beatmap;
2025-02-19 09:15:51 -05:00
using UnityEngine;
2025-04-16 08:33:34 -04:00
using UnityEngine.InputSystem;
2025-02-19 09:15:51 -05:00
2025-02-19 19:01:21 -05:00
namespace Ichni.Editor
2025-02-19 09:15:51 -05:00
{
2025-02-19 19:01:21 -05:00
public class OperationManager
2025-02-19 09:15:51 -05:00
{
2025-04-02 18:18:25 -04:00
public List<GameElement> currentSelectedElements { get; private set; }
public TempOutlineModule TempOutlineModule = new TempOutlineModule();
2025-02-19 19:06:26 -05:00
public CopyPasteDeleteModule CopyPasteDeleteModule;
2025-04-13 14:04:17 +08:00
2025-04-02 18:18:25 -04:00
public FindingModule FindingModule;
2025-02-19 19:01:21 -05:00
public OperationManager()
{
2025-04-02 18:18:25 -04:00
currentSelectedElements = new List<GameElement>();
2025-02-19 19:06:26 -05:00
CopyPasteDeleteModule = new CopyPasteDeleteModule();
2025-04-02 18:18:25 -04:00
FindingModule = new FindingModule();
2025-02-19 19:01:21 -05:00
}
2025-04-02 18:18:25 -04:00
public void AddSelectElement(GameElement gameElement)
2025-02-19 19:01:21 -05:00
{
if (gameElement == null || gameElement.connectedTab == null) return;
if (currentSelectedElements.Contains(gameElement)) return;
2025-04-02 18:18:25 -04:00
currentSelectedElements.Add(gameElement);
2025-04-13 14:04:17 +08:00
2025-04-02 18:18:25 -04:00
gameElement.connectedTab.isSelected = true;
2025-04-09 17:54:29 -04:00
if (gameElement.connectedTab.BgImage != null)
{
2025-05-11 14:12:47 +08:00
gameElement.connectedTab.BgImage.color = new Color(0f, 0f, 0f, 0.8f);
2025-04-09 17:54:29 -04:00
}
2025-04-02 18:18:25 -04:00
}
2025-04-13 14:04:17 +08:00
2025-04-02 18:18:25 -04:00
public void RemoveSelectElement(GameElement gameElement)
{
if (gameElement == null) return;
2025-04-02 18:18:25 -04:00
currentSelectedElements.Remove(gameElement);
2025-04-13 14:04:17 +08:00
if (gameElement.connectedTab == null) return;
2025-04-02 18:18:25 -04:00
gameElement.connectedTab.isSelected = false;
2025-04-09 17:54:29 -04:00
if (gameElement.connectedTab.BgImage != null)
{
gameElement.connectedTab.BgImage.color = new Color(0.5f, 0.5f, 0.5f, 0);
}
2025-04-02 18:18:25 -04:00
}
2025-04-13 14:04:17 +08:00
2025-04-02 18:18:25 -04:00
public void ClearSelectedElements()
{
currentSelectedElements.RemoveAll(gameElement => gameElement == null);
2025-04-02 18:18:25 -04:00
currentSelectedElements.ForEach(gameElement =>
{
if (gameElement == null || gameElement.connectedTab == null) return;
2025-04-02 18:18:25 -04:00
gameElement.connectedTab.isSelected = false;
2025-04-09 17:54:29 -04:00
if (gameElement.connectedTab.BgImage != null)
{
gameElement.connectedTab.BgImage.color = new Color(0.5f, 0.5f, 0.5f, 0);
}
2025-04-02 18:18:25 -04:00
});
2025-04-13 14:04:17 +08:00
2025-04-02 18:18:25 -04:00
currentSelectedElements.Clear();
}
}
public class TempOutlineModule
{
public List<GameElement> outlinedElements;
public Material outlineMaterial => EditorManager.instance.basePrefabs.outlineShaderMaterial;
public TempOutlineModule()
{
outlinedElements = new List<GameElement>();
}
public void AddOutline(GameElement gameElement)
{
outlinedElements.Add(gameElement);
}
2025-04-02 18:18:25 -04:00
public void RemoveOutline(GameElement gameElement)
{
outlinedElements.Remove(gameElement);
}
public void ClearOutline()
{
outlinedElements.Clear();
}
}
2025-04-02 18:18:25 -04:00
public class FindingModule
{
public GameElement FindGameElementByName(string elementName)
{
foreach (GameElement element in EditorManager.instance.beatmapContainer.gameElementList)
{
if (element.elementName == elementName)
2025-03-20 19:30:42 -04:00
{
2025-04-02 18:18:25 -04:00
return element;
2025-03-20 19:30:42 -04:00
}
}
2025-04-13 14:04:17 +08:00
2025-04-02 18:18:25 -04:00
LogWindow.Log("Element not found: " + elementName, Color.red);
return null;
2025-02-19 19:01:21 -05:00
}
2025-02-19 09:15:51 -05:00
}
2025-02-19 19:06:26 -05:00
public class CopyPasteDeleteModule
2025-02-19 09:15:51 -05:00
{
2025-02-19 19:01:21 -05:00
public GameElement copiedElement;
public List<GameElement> pastedElementList;
2025-02-19 19:01:21 -05:00
public void CopyElement(GameElement gameElement)
{
if (gameElement == null)
{
LogWindow.Log("No element selected to copy.", Color.red);
return;
}
2025-02-19 19:01:21 -05:00
LogWindow.Log("Copied element: " + gameElement.elementName);
copiedElement = gameElement;
}
public void PasteElement(GameElement parentElement)
{
if (parentElement == null)
{
LogWindow.Log("No parent element selected to paste.", Color.red);
return;
}
2025-02-19 19:01:21 -05:00
if (copiedElement == null)
{
LogWindow.Log("No element copied.");
return;
}
CommandManager.ExecuteCommand(new PasteElementCommand(this, copiedElement, parentElement));
}
public GameElement PasteElementRaw(GameElement sourceElement, GameElement parentElement)
{
if (sourceElement == null || parentElement == null) return null;
LogWindow.Log("Pasted element: " + sourceElement.elementName + " to " + parentElement.elementName);
pastedElementList = new List<GameElement>();
AffiliatedPaste(sourceElement, parentElement);
// 粘贴完成后,对所有新生成的元素执行 AfterInitialize 和 Refresh
// 与 EditorManager.LoadProject 的加载后流程对齐,确保 Manager 注册等关键步骤不遗漏。
2026-05-23 09:43:16 -04:00
foreach (GameElement pasted in pastedElementList)
{
if (pasted == null) continue;
pasted.AfterInitialize();
pasted.Refresh();
}
return pastedElementList.FirstOrDefault();
}
public void DeleteElement(GameElement gameElement)
2025-02-19 19:06:26 -05:00
{
if (gameElement == null)
{
LogWindow.Log("No element selected to delete.", Color.red);
return;
}
CommandManager.ExecuteCommand(new DeleteElementCommand(gameElement));
}
public void DeleteElementRaw(GameElement gameElement, bool writeLog = true)
{
if (gameElement == null) return;
var deletedElements = gameElement.GetAllGameElementsFromThis();
var deletedGuids = deletedElements.Select(element => element.elementGuid).ToList();
var deletedTabs = deletedElements
.Select(element => element.connectedTab)
.Where(tab => tab != null)
.ToList();
if (writeLog)
LogWindow.Log("Deleted element: " + gameElement.elementName + " and all its children.");
if (gameElement.parentElement != null)
{
gameElement.parentElement.childElementList.Remove(gameElement); //从父物体的子物体列表中移除避免报null
if (gameElement.parentElement.connectedTab != null)
{
gameElement.parentElement.connectedTab.childTabList.Remove(gameElement.connectedTab);
gameElement.parentElement.connectedTab.SetStatus();
}
}
EditorManager.instance.operationManager.RemoveSelectElement(gameElement);
gameElement.Delete();
deletedGuids.ForEach(guid => GameElement_BM.identifier.Remove(guid));
deletedTabs.ForEach(tab => EditorManager.instance.uiManager.hierarchy.tabList.Remove(tab));
EditorManager.instance.uiManager.inspector.ClearInspector();
}
2025-02-20 19:36:49 -05:00
/// <summary>
/// 使用递归的方式复制粘贴物体及其所有子物体
/// </summary>
/// <param name="gameElement">将要被粘贴的物体</param>
/// <param name="parent">(将要)被粘贴物体的父物体</param>
/// <returns></returns>
private void AffiliatedPaste(GameElement gameElement, GameElement parent)
2025-02-19 19:01:21 -05:00
{
if (gameElement == null || parent == null) return;
2025-02-19 19:01:21 -05:00
gameElement.SaveBM();
if (gameElement.matchedBM is not GameElement_BM gameElementBM)
{
LogWindow.Log("Paste failed: " + gameElement.elementName + " did not create a valid BM.", Color.red);
return;
}
2025-02-19 19:01:21 -05:00
GameElement pastedElement = gameElementBM.DuplicateBM(parent);
if (pastedElement == null)
2025-02-19 19:01:21 -05:00
{
LogWindow.Log("Paste failed: " + gameElement.elementName + " could not be duplicated.", Color.red);
return;
}
pastedElementList.Add(pastedElement);
gameElement.submoduleList.ForEach(submodule =>
{
if (submodule == null) return;
Debug.Log(submodule.GetType() + " is pasted.");
submodule.SaveBM();
if (submodule.matchedBM is not Submodule_BM submoduleBM) return;
SubmoduleBase existingSubmodule = pastedElement.submoduleList
.FirstOrDefault(pastedSubmodule => pastedSubmodule != null && pastedSubmodule.GetType() == submodule.GetType());
existingSubmodule?.Delete();
try { submoduleBM.DuplicateBM(pastedElement); }
catch { Debug.LogWarning("Submodule paste error: " + submodule.GetType()); }
});
2025-02-19 19:01:21 -05:00
if (gameElement.childElementList != null)
{
foreach (GameElement Element in gameElement.childElementList.Where(x => !pastedElementList.Contains(x)))
2025-02-19 19:01:21 -05:00
{
AffiliatedPaste(Element, pastedElement);
2025-02-19 19:01:21 -05:00
}
2025-02-19 19:01:21 -05:00
}
}
2025-02-19 09:15:51 -05:00
}
}