Skip to content

Commit 79d79e7

Browse files
committed
Fix various undo/redo batching bugs by tying them to TAStudio edit batches.
1 parent 5a105a1 commit 79d79e7

File tree

5 files changed

+47
-46
lines changed

5 files changed

+47
-46
lines changed

src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/TAStudioLuaLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public void ApplyInputChanges()
345345
}
346346
}
347347
_changeList.Clear();
348-
});
348+
}, "Lua: tastudio.applyinputchanges");
349349

350350
_luaLibsImpl.IsUpdateSupressed = false;
351351
}

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public partial class TAStudio
2727
private int _startRow;
2828
private int _batchEditMinFrame = -1;
2929
private bool _batchEditing;
30+
private bool _batchEditControlsUndoBatch;
3031
private bool _editIsFromLua;
3132

3233
// Editing analog input
@@ -766,15 +767,22 @@ private void TasView_MouseDown(object sender, MouseEventArgs e)
766767
/// <summary>
767768
/// Begins a batch of edits, for auto-restore purposes. Auto-restore will be delayed until EndBatchEdit is called.
768769
/// </summary>
769-
private void BeginBatchEdit()
770+
/// <param name="undoHistoryBatchName">The name to give to the item in the undo history. Pass null to not make an undo history batch.</param>
771+
private void BeginBatchEdit(string undoHistoryBatchName)
770772
{
771773
_batchEditing = true;
774+
if (undoHistoryBatchName != null)
775+
_batchEditControlsUndoBatch = CurrentTasMovie.ChangeLog.BeginNewBatch(undoHistoryBatchName, true);
776+
else
777+
_batchEditControlsUndoBatch = false;
772778
}
773779

774780
/// <returns>Returns true if the input list was redrawn.</returns>
775781
private bool EndBatchEdit()
776782
{
777783
_batchEditing = false;
784+
if (_batchEditControlsUndoBatch) CurrentTasMovie.ChangeLog.EndBatch();
785+
778786
if (_batchEditMinFrame != -1)
779787
{
780788
return FrameEdited(_batchEditMinFrame);
@@ -783,11 +791,11 @@ private bool EndBatchEdit()
783791
return false;
784792
}
785793

786-
public void ApiHawkBatchEdit(Action action)
794+
public void ApiHawkBatchEdit(Action action, string undoHistoryBatchName)
787795
{
788796
// This is only caled from Lua.
789797
_editIsFromLua = true;
790-
BeginBatchEdit();
798+
BeginBatchEdit(undoHistoryBatchName);
791799
try
792800
{
793801
action();
@@ -1322,7 +1330,7 @@ public void EditAnalogProgrammatically(KeyEventArgs e)
13221330
return;
13231331
}
13241332

1325-
BeginBatchEdit();
1333+
BeginBatchEdit(null);
13261334

13271335
int value = CurrentTasMovie.GetAxisState(_axisEditRow, _axisEditColumn);
13281336
string prevTyped = _axisTypedValue;

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,9 @@ private void EditSubMenu_DropDownOpened(object sender, EventArgs e)
281281

282282
private void UndoMenuItem_Click(object sender, EventArgs e)
283283
{
284-
if (CurrentTasMovie.ChangeLog.Undo() < Emulator.Frame)
285-
{
286-
GoToFrame(CurrentTasMovie.ChangeLog.PreviousUndoFrame);
287-
}
288-
else
289-
{
290-
RefreshDialog();
291-
}
284+
BeginBatchEdit(null);
285+
CurrentTasMovie.ChangeLog.Undo();
286+
EndBatchEdit();
292287

293288
// Currently I don't have a way to easily detect when CanUndo changes, so this button should be enabled always.
294289
// UndoMenuItem.Enabled = CurrentTasMovie.ChangeLog.CanUndo;
@@ -297,14 +292,9 @@ private void UndoMenuItem_Click(object sender, EventArgs e)
297292

298293
private void RedoMenuItem_Click(object sender, EventArgs e)
299294
{
300-
if (CurrentTasMovie.ChangeLog.Redo() < Emulator.Frame)
301-
{
302-
GoToFrame(CurrentTasMovie.ChangeLog.PreviousRedoFrame);
303-
}
304-
else
305-
{
306-
RefreshDialog();
307-
}
295+
BeginBatchEdit(null);
296+
CurrentTasMovie.ChangeLog.Redo();
297+
EndBatchEdit();
308298

309299
// Currently I don't have a way to easily detect when CanUndo changes, so this button should be enabled always.
310300
// UndoMenuItem.Enabled = CurrentTasMovie.ChangeLog.CanUndo;
@@ -487,7 +477,7 @@ private void CutMenuItem_Click(object sender, EventArgs e)
487477
}
488478

489479
Clipboard.SetDataObject(sb.ToString());
490-
BeginBatchEdit(); // movie's RemoveFrames may make multiple separate invalidations
480+
BeginBatchEdit($"Delete selected frames starting at {TasView.FirstSelectedRowIndex}"); // movie's RemoveFrames may make multiple separate invalidations
491481
CurrentTasMovie.RemoveFrames(list);
492482
EndBatchEdit();
493483
SetSplicer();
@@ -499,14 +489,12 @@ private void ClearFramesMenuItem_Click(object sender, EventArgs e)
499489
{
500490
if (TasView.Focused && TasView.AnyRowsSelected)
501491
{
502-
BeginBatchEdit();
503-
CurrentTasMovie.ChangeLog.BeginNewBatch($"Clear frames {TasView.SelectionStartIndex}-{TasView.SelectionEndIndex}");
492+
BeginBatchEdit($"Clear selected frames starting at {TasView.FirstSelectedRowIndex}");
504493
foreach (int frame in TasView.SelectedRows)
505494
{
506495
CurrentTasMovie.ClearFrame(frame);
507496
}
508497

509-
CurrentTasMovie.ChangeLog.EndBatch();
510498
EndBatchEdit();
511499
}
512500
}
@@ -524,7 +512,7 @@ private void DeleteFramesMenuItem_Click(object sender, EventArgs e)
524512
return;
525513
}
526514

527-
BeginBatchEdit(); // movie's RemoveFrames may make multiple separate invalidations
515+
BeginBatchEdit($"Delete selected frames starting at {selectionStart}"); // movie's RemoveFrames may make multiple separate invalidations
528516
CurrentTasMovie.RemoveFrames(TasView.SelectedRows.ToArray());
529517
EndBatchEdit();
530518
SetTasViewRowCount();
@@ -548,10 +536,13 @@ private void CloneFramesXTimesMenuItem_Click(object sender, EventArgs e)
548536

549537
private void CloneFramesXTimes(int timesToClone)
550538
{
551-
BeginBatchEdit();
552-
for (int i = 0; i < timesToClone; i++)
539+
if (TasView.Focused && TasView.AnyRowsSelected)
553540
{
554-
if (TasView.Focused && TasView.AnyRowsSelected)
541+
string batchName = $"Clone selected frames starting at {TasView.FirstSelectedRowIndex}";
542+
if (timesToClone != 1) batchName += $"{timesToClone} times";
543+
BeginBatchEdit(batchName);
544+
545+
for (int i = 0; i < timesToClone; i++)
555546
{
556547
var framesToInsert = TasView.SelectedRows;
557548
var insertionFrame = Math.Min((TasView.SelectionEndIndex ?? 0) + 1, CurrentTasMovie.InputLogLength);
@@ -562,8 +553,8 @@ private void CloneFramesXTimes(int timesToClone)
562553

563554
CurrentTasMovie.InsertInput(insertionFrame, inputLog);
564555
}
556+
EndBatchEdit();
565557
}
566-
EndBatchEdit();
567558
}
568559

569560
private void InsertFrameMenuItem_Click(object sender, EventArgs e)

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,22 +869,17 @@ public void DeleteFrames(int beginningFrame, int numberOfFrames)
869869
{
870870
if (beginningFrame < CurrentTasMovie.InputLogLength)
871871
{
872-
// movie's RemoveFrames might do multiple separate invalidations
873-
BeginBatchEdit();
874-
875872
int[] framesToRemove = Enumerable.Range(beginningFrame, numberOfFrames).ToArray();
876873
CurrentTasMovie.RemoveFrames(framesToRemove);
877874
SetSplicer();
878-
879-
EndBatchEdit();
880875
}
881876
}
882877

883878
public void ClearFrames(int beginningFrame, int numberOfFrames)
884879
{
885880
if (beginningFrame < CurrentTasMovie.InputLogLength)
886881
{
887-
BeginBatchEdit();
882+
BeginBatchEdit($"Clear frames {beginningFrame}-{beginningFrame+numberOfFrames}");
888883

889884
int last = Math.Min(beginningFrame + numberOfFrames, CurrentTasMovie.InputLogLength);
890885
for (int i = beginningFrame; i < last; i++)

src/BizHawk.Client.EmuHawk/tools/TAStudio/UndoHistoryForm.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,16 @@ private int SelectedItem
8686
private void UndoToHere(int index)
8787
{
8888
int earliestFrame = int.MaxValue;
89-
while (Log.UndoIndex > index)
89+
_tastudio.ApiHawkBatchEdit(() =>
9090
{
91-
int frame = Log.Undo();
92-
if (frame < earliestFrame)
93-
earliestFrame = frame;
94-
}
91+
while (Log.UndoIndex > index)
92+
{
93+
int frame = Log.Undo();
94+
if (frame < earliestFrame)
95+
earliestFrame = frame;
96+
}
97+
}, null);
98+
9599

96100
UpdateValues();
97101

@@ -137,12 +141,15 @@ private void UndoHereMenuItem_Click(object sender, EventArgs e)
137141
private void RedoHereMenuItem_Click(object sender, EventArgs e)
138142
{
139143
int earliestFrame = int.MaxValue;
140-
while (Log.UndoIndex < SelectedItem)
144+
_tastudio.ApiHawkBatchEdit(() =>
141145
{
142-
int frame = Log.Redo();
143-
if (earliestFrame < frame)
144-
earliestFrame = frame;
145-
}
146+
while (Log.UndoIndex < SelectedItem)
147+
{
148+
int frame = Log.Redo();
149+
if (earliestFrame < frame)
150+
earliestFrame = frame;
151+
}
152+
}, null);
146153

147154
UpdateValues();
148155

0 commit comments

Comments
 (0)