Skip to content

Commit

Permalink
Merge pull request #5 from jakobhellermann/split-lines-action
Browse files Browse the repository at this point in the history
Add action to split inputs per-frame (inverse of CombineInputs)
  • Loading branch information
psyGamer authored Jul 25, 2024
2 parents 81d6c7b + 0caa1e1 commit 03ba33f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Studio/CelesteStudio/Data/MenuEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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" },
Expand Down Expand Up @@ -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, [
Expand Down
10 changes: 7 additions & 3 deletions Studio/CelesteStudio/Editing/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -2094,6 +2095,36 @@ 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 (!ActionLine.TryParse(Document.Lines[row], out var actionLine) || actionLine.Frames == 0) {
continue;
}

extraLines += actionLine.Frames - 1;
Document.ReplaceLines(row, Enumerable.Repeat((actionLine with { Frames = 1 }).ToString(), actionLine.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

Expand Down

0 comments on commit 03ba33f

Please sign in to comment.