Skip to content

Commit b87b346

Browse files
committed
Extract helper method TAStudio.ToggleButtonStateOnFrames
1 parent fb397a4 commit b87b346

File tree

1 file changed

+73
-64
lines changed

1 file changed

+73
-64
lines changed

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

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,78 @@ private bool TasView_QueryFrameLag(int index, bool hideWasLag)
371371
return (lag.Lagged.HasValue && lag.Lagged.Value) || (hideWasLag && lag.WasLagged.HasValue && lag.WasLagged.Value);
372372
}
373373

374+
/// <param name="selection">
375+
/// list of row indices sorted ascending, as returned by <c>TasView.SelectedRows</c>,
376+
/// and must not be empty (so there must be a selection)
377+
/// </param>
378+
/// <remarks>fills with <see langword="false"/> (unheld) if held on all selected frames, <see langword="true"/> (held) otherwise</remarks>
379+
private void ToggleButtonStateOnFrames(int[] selection, string buttonName)
380+
{
381+
// nifty taseditor logic
382+
// your TAS Editor logic failed us (it didn't account for non-contiguous `SelectedRows`) --yoshi
383+
bool allPressed = true;
384+
var contiguous = true;
385+
var iSelection = 0;
386+
var lastFrameIndexSeen = selection[iSelection] - 1;
387+
while (iSelection < selection.Length)
388+
{
389+
var index = selection[iSelection];
390+
if (index - lastFrameIndexSeen is not 1)
391+
{
392+
contiguous = false;
393+
break;
394+
}
395+
lastFrameIndexSeen = index;
396+
if (index == CurrentTasMovie.FrameCount // last movie frame can't have input, but can be selected
397+
|| !CurrentTasMovie.BoolIsPressed(index, buttonName))
398+
{
399+
allPressed = false;
400+
break;
401+
}
402+
iSelection++;
403+
}
404+
if (!allPressed)
405+
{
406+
// still need to check for discontinuity
407+
while (iSelection < selection.Length)
408+
{
409+
var index = selection[iSelection];
410+
if (index - lastFrameIndexSeen is not 1)
411+
{
412+
contiguous = false;
413+
break;
414+
}
415+
lastFrameIndexSeen = index;
416+
iSelection++;
417+
}
418+
}
419+
else if (!contiguous)
420+
{
421+
// still need to check for `false` values
422+
allPressed = allPressed && selection.Skip(iSelection - 1) // break'd before checking the next one, so less 1 (and can't get here if `iSelection` is 0 so that's safe)
423+
.All(index => index < CurrentTasMovie.FrameCount // last movie frame can't have input, but can be selected
424+
&& CurrentTasMovie.BoolIsPressed(index, buttonName));
425+
}
426+
// else all contiguous and all `true`
427+
428+
CurrentTasMovie.SetBoolStates(frame: selection[0], count: iSelection, buttonName, val: !allPressed);
429+
var blockStart = iSelection;
430+
lastFrameIndexSeen = selection[iSelection] - 1;
431+
while (iSelection < selection.Length)
432+
{
433+
var index = selection[iSelection];
434+
if (index - lastFrameIndexSeen is not 1)
435+
{
436+
// discontinuity; split off another block, and this is now the start of the next block
437+
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed);
438+
blockStart = iSelection;
439+
}
440+
lastFrameIndexSeen = index;
441+
iSelection++;
442+
}
443+
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed);
444+
}
445+
374446
private void TasView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e)
375447
{
376448
if (TasView.AnyRowsSelected)
@@ -389,70 +461,7 @@ private void TasView_ColumnClick(object sender, InputRoll.ColumnClickEventArgs e
389461
{
390462
if (ModifierKeys != Keys.Alt)
391463
{
392-
// nifty taseditor logic
393-
// your TAS Editor logic failed us (it didn't account for non-contiguous `SelectedRows`) --yoshi
394-
var selection = TasView.SelectedRows.ToArray(); // sorted asc, length >= 1
395-
bool allPressed = true;
396-
var contiguous = true;
397-
var iSelection = 0;
398-
var lastFrameIndexSeen = selection[iSelection] - 1;
399-
while (iSelection < selection.Length)
400-
{
401-
var index = selection[iSelection];
402-
if (index - lastFrameIndexSeen is not 1)
403-
{
404-
contiguous = false;
405-
break;
406-
}
407-
lastFrameIndexSeen = index;
408-
if (index == CurrentTasMovie.FrameCount // last movie frame can't have input, but can be selected
409-
|| !CurrentTasMovie.BoolIsPressed(index, buttonName))
410-
{
411-
allPressed = false;
412-
break;
413-
}
414-
iSelection++;
415-
}
416-
if (!allPressed)
417-
{
418-
// still need to check for discontinuity
419-
while (iSelection < selection.Length)
420-
{
421-
var index = selection[iSelection];
422-
if (index - lastFrameIndexSeen is not 1)
423-
{
424-
contiguous = false;
425-
break;
426-
}
427-
lastFrameIndexSeen = index;
428-
iSelection++;
429-
}
430-
}
431-
else if (!contiguous)
432-
{
433-
// still need to check for `false` values
434-
allPressed = allPressed && selection.Skip(iSelection - 1) // break'd before checking the next one, so less 1 (and can't get here if `iSelection` is 0 so that's safe)
435-
.All(index => index < CurrentTasMovie.FrameCount // last movie frame can't have input, but can be selected
436-
&& CurrentTasMovie.BoolIsPressed(index, buttonName));
437-
}
438-
// else all contiguous and all `true`
439-
440-
CurrentTasMovie.SetBoolStates(frame: selection[0], count: iSelection, buttonName, val: !allPressed);
441-
var blockStart = iSelection;
442-
lastFrameIndexSeen = selection[iSelection] - 1;
443-
while (iSelection < selection.Length)
444-
{
445-
var index = selection[iSelection];
446-
if (index - lastFrameIndexSeen is not 1)
447-
{
448-
// discontinuity; split off another block, and this is now the start of the next block
449-
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed);
450-
blockStart = iSelection;
451-
}
452-
lastFrameIndexSeen = index;
453-
iSelection++;
454-
}
455-
CurrentTasMovie.SetBoolStates(frame: selection[blockStart], count: iSelection - blockStart, buttonName, val: !allPressed);
464+
ToggleButtonStateOnFrames(TasView.SelectedRows.ToArray(), buttonName);
456465
}
457466
else
458467
{

0 commit comments

Comments
 (0)