Skip to content

Commit 1e8b621

Browse files
committed
Extract helper method TAStudio.ToggleButtonStateOnFrames
1 parent 84fcc34 commit 1e8b621

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,45 @@ private bool TasView_QueryFrameLag(int index, bool hideWasLag)
370370
return (lag.Lagged.HasValue && lag.Lagged.Value) || (hideWasLag && lag.WasLagged.HasValue && lag.WasLagged.Value);
371371
}
372372

373+
/// <param name="selection">
374+
/// list of row indices sorted ascending, as returned by <c>TasView.SelectedRows</c>,
375+
/// and must not be empty (so there must be a selection)
376+
/// </param>
377+
/// <remarks>fills with <see langword="false"/> (unheld) if held on all selected frames, <see langword="true"/> (held) otherwise</remarks>
378+
private void ToggleButtonStateOnFrames(int[] selection, string buttonName)
379+
{
380+
// nifty taseditor logic
381+
// your TAS Editor logic failed us (it didn't account for non-contiguous `SelectedRows`) --yoshi
382+
var allPressed = selection.All(index => CurrentTasMovie.BoolIsPressed(index, buttonName))
383+
&& selection[selection.Length - 1] != CurrentTasMovie.FrameCount; // last movie frame can't have input, but can be selected
384+
var iSelection = 0;
385+
var lastFrameIndexSeen = selection[iSelection] - 1;
386+
while (iSelection < selection.Length)
387+
{
388+
var index = selection[iSelection];
389+
if (index - lastFrameIndexSeen is not 1) break;
390+
lastFrameIndexSeen = index;
391+
iSelection++;
392+
}
393+
// `iSelection` now points to the element after the last of the first contiguous block--if the whole selection is contiguous, it's pointing after the end of the array...
394+
CurrentTasMovie.SetBoolStates(frame: selection[0], count: iSelection, buttonName, val: !allPressed);
395+
var blockStart = iSelection;
396+
lastFrameIndexSeen = selection[iSelection] - 1;
397+
while (iSelection < selection.Length) // ...and these will be equal, so the loop will end immediately
398+
{
399+
var index = selection[iSelection];
400+
if (index - lastFrameIndexSeen is not 1)
401+
{
402+
// discontinuity; split off another block, and this is now the start of the next block
403+
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed);
404+
blockStart = iSelection;
405+
}
406+
lastFrameIndexSeen = index;
407+
iSelection++;
408+
}
409+
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed); // `count` arg will be 0 if the whole selection is contiguous
410+
}
411+
373412
private void TasView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e)
374413
{
375414
if (TasView.AnyRowsSelected)
@@ -388,37 +427,7 @@ private void TasView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e
388427
{
389428
if (ModifierKeys != Keys.Alt)
390429
{
391-
// nifty taseditor logic
392-
// your TAS Editor logic failed us (it didn't account for non-contiguous `SelectedRows`) --yoshi
393-
var selection = TasView.SelectedRows.ToArray(); // sorted asc, length >= 1
394-
var allPressed = selection.All(index => CurrentTasMovie.BoolIsPressed(index, buttonName))
395-
&& selection[selection.Length - 1] != CurrentTasMovie.FrameCount; // last movie frame can't have input, but can be selected
396-
var iSelection = 0;
397-
var lastFrameIndexSeen = selection[iSelection] - 1;
398-
while (iSelection < selection.Length)
399-
{
400-
var index = selection[iSelection];
401-
if (index - lastFrameIndexSeen is not 1) break;
402-
lastFrameIndexSeen = index;
403-
iSelection++;
404-
}
405-
// `iSelection` now points to the element after the last of the first contiguous block--if the whole selection is contiguous, it's pointing after the end of the array...
406-
CurrentTasMovie.SetBoolStates(frame: selection[0], count: iSelection, buttonName, val: !allPressed);
407-
var blockStart = iSelection;
408-
lastFrameIndexSeen = selection[iSelection] - 1;
409-
while (iSelection < selection.Length) // ...and these will be equal, so the loop will end immediately
410-
{
411-
var index = selection[iSelection];
412-
if (index - lastFrameIndexSeen is not 1)
413-
{
414-
// discontinuity; split off another block, and this is now the start of the next block
415-
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed);
416-
blockStart = iSelection;
417-
}
418-
lastFrameIndexSeen = index;
419-
iSelection++;
420-
}
421-
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed); // `count` arg will be 0 if the whole selection is contiguous
430+
ToggleButtonStateOnFrames(TasView.SelectedRows.ToArray(), buttonName);
422431
}
423432
else
424433
{

0 commit comments

Comments
 (0)