From 7568e998e4d9f1cfacc232837ee5482ec655f9f7 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Wed, 24 Jul 2024 16:26:23 +0200 Subject: [PATCH 1/2] add action for splitting lines into 1-frame actions --- Studio/CelesteStudio/Data/MenuEntry.cs | 6 ++-- Studio/CelesteStudio/Editing/Document.cs | 10 +++++-- Studio/CelesteStudio/Editing/Editor.cs | 35 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Studio/CelesteStudio/Data/MenuEntry.cs b/Studio/CelesteStudio/Data/MenuEntry.cs index d0e06611e..5fcf43bd3 100644 --- a/Studio/CelesteStudio/Data/MenuEntry.cs +++ b/Studio/CelesteStudio/Data/MenuEntry.cs @@ -20,7 +20,7 @@ public enum MenuEntry { Editor_DeleteSelectedLines, Editor_InsertRemoveBreakpoint, Editor_InsertRemoveSavestateBreakpoint, Editor_RemoveAllUncommentedBreakpoints, Editor_RemoveAllBreakpoints, Editor_CommentUncommentAllBreakpoints, Editor_CommentUncommentInputs, Editor_CommentUncommentText, Editor_InsertRoomName, Editor_InsertCurrentTime, Editor_RemoveAllTimestamps, Editor_InsertModInfo, Editor_InsertConsoleLoadCommand, Editor_InsertSimpleConsoleLoadCommand, - Editor_SwapSelectedLR, Editor_SwapSelectedJK, Editor_SwapSelectedXC, Editor_CombineConsecutiveSameInputs, Editor_ForceCombineInputFrames, + Editor_SwapSelectedLR, Editor_SwapSelectedJK, Editor_SwapSelectedXC, Editor_CombineConsecutiveSameInputs, Editor_ForceCombineInputFrames, Editor_SplitFrames, Editor_OpenReadFileGoToPlayLine, Status_CopyGameInfoToClipboard, Status_ReconenctStudioCeleste, @@ -77,6 +77,7 @@ public static class MenuEntryExtensions { { MenuEntry.Editor_SwapSelectedXC, Keys.None }, { MenuEntry.Editor_CombineConsecutiveSameInputs, Application.Instance.CommonModifier | Keys.L }, { MenuEntry.Editor_ForceCombineInputFrames, Application.Instance.CommonModifier | Keys.Shift | Keys.L }, + { MenuEntry.Editor_SplitFrames, Keys.None }, { MenuEntry.Editor_OpenReadFileGoToPlayLine, Keys.None }, { MenuEntry.Status_CopyGameInfoToClipboard, Application.Instance.CommonModifier | Keys.Shift | Keys.C }, @@ -132,6 +133,7 @@ public static class MenuEntryExtensions { { MenuEntry.Editor_SwapSelectedXC, "Swap Selected X and C" }, { MenuEntry.Editor_CombineConsecutiveSameInputs, "Combine Consecutive Same Inputs" }, { MenuEntry.Editor_ForceCombineInputFrames, "Force Combine Input Frames" }, + { MenuEntry.Editor_SplitFrames, "Split Input Frames" }, { MenuEntry.Editor_OpenReadFileGoToPlayLine, "Open Read File / Go To Play Line" }, { MenuEntry.Status_CopyGameInfoToClipboard, "&Copy Game Info to Clipboard" }, @@ -159,7 +161,7 @@ public static class MenuEntryExtensions { MenuEntry.Editor_DeleteSelectedLines, MenuEntry.Editor_InsertRemoveBreakpoint, MenuEntry.Editor_InsertRemoveSavestateBreakpoint, MenuEntry.Editor_RemoveAllUncommentedBreakpoints, MenuEntry.Editor_RemoveAllBreakpoints, MenuEntry.Editor_CommentUncommentAllBreakpoints, MenuEntry.Editor_CommentUncommentInputs, MenuEntry.Editor_CommentUncommentText, MenuEntry.Editor_InsertRoomName, MenuEntry.Editor_InsertCurrentTime, MenuEntry.Editor_RemoveAllTimestamps, MenuEntry.Editor_InsertModInfo, MenuEntry.Editor_InsertConsoleLoadCommand, MenuEntry.Editor_InsertSimpleConsoleLoadCommand, - MenuEntry.Editor_SwapSelectedLR, MenuEntry.Editor_SwapSelectedJK, MenuEntry.Editor_SwapSelectedXC, MenuEntry.Editor_CombineConsecutiveSameInputs, MenuEntry.Editor_ForceCombineInputFrames, + MenuEntry.Editor_SwapSelectedLR, MenuEntry.Editor_SwapSelectedJK, MenuEntry.Editor_SwapSelectedXC, MenuEntry.Editor_CombineConsecutiveSameInputs, MenuEntry.Editor_ForceCombineInputFrames, MenuEntry.Editor_SplitFrames, MenuEntry.Editor_OpenReadFileGoToPlayLine] }, { MenuEntryCategory.Status, [ diff --git a/Studio/CelesteStudio/Editing/Document.cs b/Studio/CelesteStudio/Editing/Document.cs index 238ec7264..2585dae80 100644 --- a/Studio/CelesteStudio/Editing/Document.cs +++ b/Studio/CelesteStudio/Editing/Document.cs @@ -469,9 +469,13 @@ public void InsertLine(int row, string text) { } public void ReplaceLine(int row, string text) { + var newLines = text.SplitDocumentLines(); + ReplaceLines(row, newLines); + } + + public void ReplaceLines(int row, string[] newLines) { PushUndoState(); - var newLines = text.SplitDocumentLines(); if (newLines.Length == 0) { CurrentLines[row] = string.Empty; } else if (newLines.Length == 1) { @@ -480,8 +484,8 @@ public void ReplaceLine(int row, string text) { CurrentLines[row] = newLines[0]; CurrentLines.InsertRange(row + 1, newLines[1..]); } - - int newLineCount = text.Count(c => c == NewLine); + + int newLineCount = newLines.Length > 0 ? newLines.Length-1 : 0; if (Caret.Row >= row) Caret.Row += newLineCount; diff --git a/Studio/CelesteStudio/Editing/Editor.cs b/Studio/CelesteStudio/Editing/Editor.cs index b25951206..c80997719 100644 --- a/Studio/CelesteStudio/Editing/Editor.cs +++ b/Studio/CelesteStudio/Editing/Editor.cs @@ -243,6 +243,7 @@ void GenerateCommandMenu() { MenuEntry.Editor_SwapSelectedXC.ToAction(() => SwapSelectedActions(Actions.Dash, Actions.Dash2)), MenuEntry.Editor_CombineConsecutiveSameInputs.ToAction(() => CombineInputs(sameActions: true)), MenuEntry.Editor_ForceCombineInputFrames.ToAction(() => CombineInputs(sameActions: false)), + MenuEntry.Editor_SplitFrames.ToAction(SplitLines), // TODO: Is this feature even unused? // MenuUtils.CreateAction("Convert Dash to Demo Dash"), new SeparatorMenuItem(), @@ -2055,6 +2056,40 @@ private void CombineInputs(bool sameActions) { Document.Caret = ClampCaret(Document.Caret); } } + + private void SplitLines() { + using var _ = Document.Update(); + + (int minRow, int maxRow) = Document.Selection.Empty + ? (Document.Caret.Row, Document.Caret.Row) + : (Document.Selection.Min.Row, Document.Selection.Max.Row); + + int extraLines = 0; + + for (int row = maxRow; row >= minRow; row--) { + if (!TryParseAndFormatActionLine(row, out var currActionLine)) + continue; + + if (currActionLine.Frames == 0) { + continue; + } + + extraLines += currActionLine.Frames - 1; + var currentActionSingleFrame = currActionLine with {Frames = 1}; + Document.ReplaceLines(row, Enumerable.Repeat(currentActionSingleFrame.ToString(), currActionLine.Frames).ToArray()); + } + + if (!Document.Selection.Empty) { + int endRow = maxRow + extraLines; + Document.Selection = new Selection { + Start = new CaretPosition(minRow, 0), + End = new CaretPosition(endRow, Document.Lines[endRow].Length), + }; + } + + Document.Caret.Row = maxRow; + Document.Caret = ClampCaret(Document.Caret); + } #endregion From 0caa1e16368bed40c28e914116753d0ae4cc0d86 Mon Sep 17 00:00:00 2001 From: psyGamer Date: Thu, 25 Jul 2024 10:35:18 +0200 Subject: [PATCH 2/2] Fix some code-style issues --- Studio/CelesteStudio/Editing/Editor.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Studio/CelesteStudio/Editing/Editor.cs b/Studio/CelesteStudio/Editing/Editor.cs index c80997719..f33ba8a02 100644 --- a/Studio/CelesteStudio/Editing/Editor.cs +++ b/Studio/CelesteStudio/Editing/Editor.cs @@ -2058,7 +2058,7 @@ private void CombineInputs(bool sameActions) { } private void SplitLines() { - using var _ = Document.Update(); + using var __ = Document.Update(); (int minRow, int maxRow) = Document.Selection.Empty ? (Document.Caret.Row, Document.Caret.Row) @@ -2067,16 +2067,12 @@ private void SplitLines() { int extraLines = 0; for (int row = maxRow; row >= minRow; row--) { - if (!TryParseAndFormatActionLine(row, out var currActionLine)) - continue; - - if (currActionLine.Frames == 0) { + if (!ActionLine.TryParse(Document.Lines[row], out var actionLine) || actionLine.Frames == 0) { continue; } - extraLines += currActionLine.Frames - 1; - var currentActionSingleFrame = currActionLine with {Frames = 1}; - Document.ReplaceLines(row, Enumerable.Repeat(currentActionSingleFrame.ToString(), currActionLine.Frames).ToArray()); + extraLines += actionLine.Frames - 1; + Document.ReplaceLines(row, Enumerable.Repeat((actionLine with { Frames = 1 }).ToString(), actionLine.Frames).ToArray()); } if (!Document.Selection.Empty) {