@@ -25,10 +25,10 @@ namespace Ichni.Editor
|
||||
FindingModule = new FindingModule();
|
||||
}
|
||||
|
||||
public void AddSelectElement(GameElement gameElement)
|
||||
{
|
||||
if (gameElement == null || gameElement.connectedTab == null) return;
|
||||
if (currentSelectedElements.Contains(gameElement)) return;
|
||||
public void AddSelectElement(GameElement gameElement)
|
||||
{
|
||||
if (gameElement == null || gameElement.connectedTab == null) return;
|
||||
if (currentSelectedElements.Contains(gameElement)) return;
|
||||
|
||||
currentSelectedElements.Add(gameElement);
|
||||
|
||||
@@ -36,12 +36,63 @@ namespace Ichni.Editor
|
||||
if (gameElement.connectedTab.BgImage != null)
|
||||
{
|
||||
gameElement.connectedTab.BgImage.color = new Color(0f, 0f, 0f, 0.8f);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveSelectElement(GameElement gameElement)
|
||||
{
|
||||
if (gameElement == null) return;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
currentSelectedElements.Remove(gameElement);
|
||||
|
||||
@@ -115,81 +166,135 @@ namespace Ichni.Editor
|
||||
{
|
||||
public GameElement copiedElement;
|
||||
public List<GameElement> pastedElementList;
|
||||
private Dictionary<Guid, GameElement> pastedElementBySourceGuid;
|
||||
|
||||
public void CopyElement(GameElement gameElement)
|
||||
{
|
||||
if (gameElement == null)
|
||||
{
|
||||
LogWindow.Log("No element selected to copy.", Color.red);
|
||||
return;
|
||||
}
|
||||
|
||||
LogWindow.Log("Copied element: " + gameElement.elementName);
|
||||
copiedElement = gameElement;
|
||||
}
|
||||
|
||||
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 (copiedElement == null)
|
||||
{
|
||||
LogWindow.Log("No element copied.");
|
||||
return;
|
||||
}
|
||||
|
||||
CommandManager.ExecuteCommand(new PasteElementCommand(this, copiedElement, parentElement));
|
||||
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)
|
||||
{
|
||||
if (sourceElement == null || parentElement == null) return null;
|
||||
|
||||
LogWindow.Log("Pasted element: " + sourceElement.elementName + " to " + parentElement.elementName);
|
||||
pastedElementList = new List<GameElement>();
|
||||
pastedElementBySourceGuid = new Dictionary<Guid, GameElement>();
|
||||
AffiliatedPaste(sourceElement, parentElement);
|
||||
ResolvePastedReferences();
|
||||
|
||||
// 粘贴完成后,对所有新生成的元素执行 AfterInitialize 和 Refresh,
|
||||
// 与 EditorManager.LoadProject 的加载后流程对齐,确保 Manager 注册等关键步骤不遗漏。
|
||||
foreach (GameElement pasted in pastedElementList)
|
||||
{
|
||||
if (pasted == null) continue;
|
||||
pasted.AfterInitialize();
|
||||
pasted.Refresh();
|
||||
}
|
||||
|
||||
return pastedElementList.FirstOrDefault();
|
||||
ElementClipboardSnapshot snapshot = sourceElement == copiedElement && clipboardSnapshot != null
|
||||
? clipboardSnapshot
|
||||
: ElementClipboardSnapshot.CaptureSingle(sourceElement);
|
||||
return PasteElementsRaw(snapshot, parentElement).FirstOrDefault();
|
||||
}
|
||||
|
||||
private void ResolvePastedReferences()
|
||||
public List<GameElement> PasteElementsRaw(ElementClipboardSnapshot snapshot, GameElement parentElement)
|
||||
{
|
||||
if (pastedElementBySourceGuid == null || pastedElementBySourceGuid.Count == 0) return;
|
||||
pastedElementList = new List<GameElement>();
|
||||
if (snapshot == null || parentElement == null) return new List<GameElement>();
|
||||
|
||||
foreach (GameElement pastedElement in pastedElementList)
|
||||
{
|
||||
if (pastedElement is IPastedReferenceResolver referenceResolver)
|
||||
{
|
||||
referenceResolver.ResolvePastedReferences(pastedElementBySourceGuid);
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
if (gameElement == null)
|
||||
{
|
||||
LogWindow.Log("No element selected to delete.", Color.red);
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
CommandManager.ExecuteCommand(new DeleteElementCommand(gameElement));
|
||||
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)
|
||||
@@ -216,64 +321,12 @@ namespace Ichni.Editor
|
||||
}
|
||||
}
|
||||
|
||||
EditorManager.instance.operationManager.RemoveSelectElement(gameElement);
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用递归的方式复制粘贴物体及其所有子物体
|
||||
/// </summary>
|
||||
/// <param name="gameElement">将要被粘贴的物体</param>
|
||||
/// <param name="parent">(将要)被粘贴物体的父物体</param>
|
||||
/// <returns></returns>
|
||||
private void AffiliatedPaste(GameElement gameElement, GameElement parent)
|
||||
{
|
||||
if (gameElement == null || parent == null) return;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
GameElement pastedElement = gameElementBM.DuplicateBM(parent);
|
||||
if (pastedElement == null)
|
||||
{
|
||||
LogWindow.Log("Paste failed: " + gameElement.elementName + " could not be duplicated.", Color.red);
|
||||
return;
|
||||
}
|
||||
|
||||
pastedElementList.Add(pastedElement);
|
||||
pastedElementBySourceGuid[gameElement.elementGuid] = 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()); }
|
||||
});
|
||||
|
||||
if (gameElement.childElementList != null)
|
||||
{
|
||||
foreach (GameElement Element in gameElement.childElementList.Where(x => !pastedElementList.Contains(x)))
|
||||
{
|
||||
AffiliatedPaste(Element, pastedElement);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IPastedReferenceResolver
|
||||
|
||||
Reference in New Issue
Block a user