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

337 lines
12 KiB
C#
Raw Normal View History

using System;
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
}
public void AddSelectElement(GameElement gameElement)
{
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);
}
}
public void AddSelectRangeToElement(GameElement targetElement)
{
if (targetElement == null) return;
GameElement parentElement = targetElement.parentElement;
List<GameElement> siblingElements = parentElement?.childElementList;
if (siblingElements == null || siblingElements.Count == 0)
{
AddSelectElement(targetElement);
return;
}
int targetIndex = siblingElements.IndexOf(targetElement);
if (targetIndex < 0)
{
AddSelectElement(targetElement);
return;
}
GameElement rangeStartElement = currentSelectedElements
.Where(element => element != null && element.parentElement == parentElement)
.Select(element => new
{
Element = element,
Index = siblingElements.IndexOf(element)
})
.Where(entry => entry.Index >= 0)
.OrderByDescending(entry => entry.Index)
.Select(entry => entry.Element)
.FirstOrDefault();
if (rangeStartElement == null)
{
if (currentSelectedElements.Count > 0)
{
LogWindow.Log("Shift range select requires sibling elements.", Color.red);
}
AddSelectElement(targetElement);
return;
}
int startIndex = siblingElements.IndexOf(rangeStartElement);
int minIndex = Mathf.Min(startIndex, targetIndex);
int maxIndex = Mathf.Max(startIndex, targetIndex);
for (int i = minIndex; i <= maxIndex; i++)
{
AddSelectElement(siblingElements[i]);
}
}
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
}
public class CopyPasteDeleteModule
{
public GameElement copiedElement;
public List<GameElement> pastedElementList;
private ElementClipboardSnapshot clipboardSnapshot;
public void CopyElement(GameElement gameElement)
{
CopyElements(new[] { gameElement });
}
public void CopyElements(IEnumerable<GameElement> gameElements)
{
if (!ElementClipboardSnapshot.TryCaptureSiblingRoots(
gameElements,
out ElementClipboardSnapshot snapshot,
out List<GameElement> orderedRoots,
out string errorMessage))
{
LogWindow.Log(errorMessage, Color.red);
return;
}
clipboardSnapshot = snapshot;
copiedElement = orderedRoots.FirstOrDefault();
string elementText = orderedRoots.Count == 1
? orderedRoots[0].elementName
: orderedRoots.Count + " sibling elements";
LogWindow.Log("Copied element: " + elementText);
}
public void CutElement(GameElement gameElement)
{
CutElements(new[] { gameElement });
}
public void CutElements(IEnumerable<GameElement> gameElements)
{
if (!ElementClipboardSnapshot.TryCaptureSiblingRoots(
gameElements,
out ElementClipboardSnapshot snapshot,
out List<GameElement> orderedRoots,
out string errorMessage))
{
LogWindow.Log(errorMessage, Color.red);
return;
}
clipboardSnapshot = snapshot;
copiedElement = orderedRoots.FirstOrDefault();
ExecuteDeleteElements(orderedRoots);
LogWindow.Log("Cut element: " + (orderedRoots.Count == 1 ? orderedRoots[0].elementName : orderedRoots.Count + " sibling elements"));
}
public void PasteElement(GameElement parentElement)
{
if (parentElement == null)
{
LogWindow.Log("No parent element selected to paste.", Color.red);
return;
}
if (clipboardSnapshot == null || clipboardSnapshot.RootCount == 0)
{
LogWindow.Log("No element copied.");
return;
}
CommandManager.ExecuteCommand(new PasteElementCommand(this, clipboardSnapshot, parentElement));
}
public GameElement PasteElementRaw(GameElement sourceElement, GameElement parentElement)
{
ElementClipboardSnapshot snapshot = sourceElement == copiedElement && clipboardSnapshot != null
? clipboardSnapshot
: ElementClipboardSnapshot.CaptureSingle(sourceElement);
return PasteElementsRaw(snapshot, parentElement).FirstOrDefault();
}
public List<GameElement> PasteElementsRaw(ElementClipboardSnapshot snapshot, GameElement parentElement)
{
pastedElementList = new List<GameElement>();
if (snapshot == null || parentElement == null) return new List<GameElement>();
ElementPasteResult pasteResult = snapshot.PasteTo(parentElement);
pastedElementList = pasteResult.AllElements;
string pastedText = pasteResult.RootElements.Count == 1
? pasteResult.RootElements[0].elementName
: pasteResult.RootElements.Count + " elements";
LogWindow.Log("Pasted element: " + pastedText + " to " + parentElement.elementName);
return pasteResult.RootElements;
}
public void DeleteElement(GameElement gameElement)
{
DeleteElements(new[] { gameElement });
}
public void DeleteElements(IEnumerable<GameElement> gameElements)
{
List<GameElement> roots = ElementClipboardSnapshot.GetTopLevelElements(gameElements);
if (roots.Count == 0)
{
LogWindow.Log("No element selected to delete.", Color.red);
return;
}
ExecuteDeleteElements(roots);
}
private void ExecuteDeleteElements(IEnumerable<GameElement> gameElements)
{
List<GameElement> roots = ElementClipboardSnapshot.GetTopLevelElements(gameElements);
if (roots.Count == 0) return;
if (roots.Count == 1)
{
CommandManager.ExecuteCommand(new DeleteElementCommand(roots[0]));
return;
}
CommandGroup commandGroup = new CommandGroup();
foreach (GameElement root in roots)
{
commandGroup.Add(new DeleteElementCommand(root));
}
if (commandGroup.Count > 0)
{
CommandManager.ExecuteCommand(commandGroup);
}
}
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();
}
}
deletedElements.ForEach(element => EditorManager.instance.operationManager.RemoveSelectElement(element));
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();
}
}
public interface IPastedReferenceResolver
{
void ResolvePastedReferences(IReadOnlyDictionary<Guid, GameElement> pastedElementBySourceGuid);
}
}