Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tastudio Lua Branch Action Callbacks, getselectedbranch() #4216

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ public LuaTable GetBranches()
indexFrom: 0);
}

[LuaMethodExample("local branch = tastudio.getselectedbranch();")]
[LuaMethod("getselectedbranch", "gets the index of the first selected branch in the branches list")]
public int? GetSelectedBranch()
{
if (Engaged())
{
return Tastudio.GetSelectedBranch();
}

return null;
}

[LuaMethodExample("local nltasget = tastudio.getbranchinput( \"97021544-2454-4483-824f-47f75e7fcb6a\", 500 );")]
[LuaMethod("getbranchinput", "Gets the controller state of the given frame with the given branch identifier")]
public LuaTable GetBranchInput(string branchId, int frame)
Expand Down Expand Up @@ -592,5 +604,57 @@ public void OnBranchRemove(LuaFunction luaf)
};
}
}

[LuaMethodExample("function LoadUndone()\r\n\tconsole.log(\"You did a branch load undo\")\r\nend\r\ntastudio.onbranchundoload(LoadUndone)")]
[LuaMethod("onbranchundoload", "called whenever a branch load is undone. luaf must be a function without parameters")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use existing docs as inspiration because they suck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What needs rewrite? LuaMethodExample or LuaMethod?

public void OnBranchUndoLoad(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchLoadUndoneCallback = () =>
{
luaf.Call();
};
}
}

[LuaMethodExample("function UpdateUndone(index)\r\n\tconsole.log(\"You did a branch updaate undo for branch \"..index)\r\nend\r\ntastudio.onbranchundoupdate(UpdateUndone)")]
[LuaMethod("onbranchundoupdate", "called whenever a branch update is undone. luaf must be a function that takes the integer branch index as a parameter")]
public void OnBranchUndoUpdate(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchUpdateUndoneCallback = index =>
{
luaf.Call(index);
};
}
}

[LuaMethodExample("function RemoveUndone(index)\r\n\tconsole.log(\"You did a branch remove undo for branch \"..index)\r\nend\r\ntastudio.onbranchundoremove(RemoveUndone)")]
[LuaMethod("onbranchundoremove", "called whenever a branch removal is undone. luaf must be a function that takes the integer branch index as a parameter")]
public void OnBranchUndoRemove(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchRemoveUndoneCallback = index =>
{
luaf.Call(index);
};
}
}

[LuaMethodExample("function Reordered(oldindex, newindex)\r\n\tconsole.log(\"You reordered branches from \"..oldIndex..\" to \"..newIndex)\r\nend\r\ntastudio.onbranchesreorder(Reordered)")]
[LuaMethod("onbranchesreorder", "called whenever branches are reordered in the branches box. luaf must be a function that takes two integers for the branch indices as parameters")]
public void OnBranchesReorder(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchesReorderedCallback = (oldIndex, newIndex) =>
{
luaf.Call(oldIndex, newIndex);
};
}
}
}
}
20 changes: 17 additions & 3 deletions src/BizHawk.Client.EmuHawk/tools/TAStudio/BookmarksBranchesBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ private enum BranchUndo

public Action<int> RemovedCallback { get; set; }

public Action LoadUndoneCallback { get; set; }

public Action<int> UpdateUndoneCallback { get; set; }

public Action<int> RemoveUndoneCallback { get; set; }

public Action<int, int> ReorderedCallback { get; set; }

public TAStudio Tastudio { get; set; }

public IDialogController DialogController => Tastudio.MainForm;
Expand Down Expand Up @@ -358,7 +366,7 @@ private void UndoBranchToolStripMenuItem_Click(object sender, EventArgs e)
if (_branchUndo == BranchUndo.Load)
{
LoadBranch(_backupBranch);
LoadedCallback?.Invoke(Branches.IndexOf(_backupBranch));
LoadUndoneCallback?.Invoke();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick glance at this PR and it seems you're adding a bunch more callbacks when ones already exist? Would it not be suitable to subscribe to those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it only properly works with undoing branch loads, because there it's possible to get that a branch action undo has been occured, since the the index of the backup branch is -1 when doing an undo load.
For Saving and Removing it's not possible to figure out in lua whether it was a normal action or an undo. The index for undoing saving is just the same as doing a regular branch save, so no chance to figure out which occured. And remove undo is just like adding a new branch.
The user might want to do different things in lua depending on what has happened with the branches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand—you're changing this callsite to differentiate it from the callsite in PrepareHistoryAndLoadSelectedBranch? I think adding an argument would be a better approach. @feos Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's something I thought about doing too, but only have one onbranchaction method and make luaf a function that takes an integer for the index and a string for every action "save", "load", "remove", "undoload" "undoupdate" and "undoremove" and make the current deprecated. Reordering then still has it's own method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding an argument would be a better approach.

Agreed.

That's something I thought about doing too, but only have one onbranchaction method and make luaf a function that takes an integer for the index and a string for every action "save", "load", "remove", "undoload" "undoupdate" and "undoremove" and make the current deprecated.

That's not what Yoshi is suggesting. Just expand the existing ones a little bit to include the info on whether they come from the undo operation. Combining everything into one megacallback passing around strings is not better (tho I can't verbalize why).

Copy link
Member

@YoshiRulz YoshiRulz Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because then the script author would be forced to care.
If you add a parameter to the existing callback, existing scripts will continue to work, and authors can opt-in to caring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing with string would mean the user can then just make a lua table with functions and make each table index a string. Then there wouldn't be many if-statements in the callback function.
Either way, I don't mind how it's done as long as it finally works

Tastudio.MainForm.AddOnScreenMessage("Branch Load canceled");
}
else if (_branchUndo == BranchUndo.Update)
Expand All @@ -367,7 +375,7 @@ private void UndoBranchToolStripMenuItem_Click(object sender, EventArgs e)
if (branch != null)
{
Branches.Replace(branch, _backupBranch);
SavedCallback?.Invoke(Branches.IndexOf(_backupBranch));
UpdateUndoneCallback?.Invoke(Branches.IndexOf(_backupBranch));
Tastudio.MainForm.AddOnScreenMessage("Branch Update canceled");
}
}
Expand All @@ -385,7 +393,7 @@ private void UndoBranchToolStripMenuItem_Click(object sender, EventArgs e)
{
Branches.Add(_backupBranch);
BranchView.RowCount = Branches.Count;
SavedCallback?.Invoke(Branches.IndexOf(_backupBranch));
RemoveUndoneCallback?.Invoke(Branches.IndexOf(_backupBranch));
Tastudio.MainForm.AddOnScreenMessage("Branch Removal canceled");
}

Expand Down Expand Up @@ -453,6 +461,11 @@ public void RemoveBranchExternal()
RemoveBranchToolStripMenuItem_Click(null, null);
}

public int? GetSelectedBranch()
{
return BranchView.SelectionStartIndex;
}

public void SelectBranchExternal(int slot)
{
if (Tastudio.AxisEditingMode)
Expand Down Expand Up @@ -635,6 +648,7 @@ private void BranchView_CellDropped(object sender, InputRoll.CellEventArgs e)
: Branches[Branches.Current].Uuid;

Branches.Swap(e.OldCell.RowIndex.Value, e.NewCell.RowIndex.Value);
ReorderedCallback?.Invoke(e.OldCell.RowIndex.Value, e.NewCell.RowIndex.Value);
int newIndex = Branches.IndexOfHash(guid);
Branches.Current = newIndex;
Select(newIndex, true);
Expand Down
24 changes: 24 additions & 0 deletions src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.Callbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public partial class TAStudio
public Action<int> BranchLoadedCallback { get; set; }
public Action<int> BranchSavedCallback { get; set; }
public Action<int> BranchRemovedCallback { get; set; }
public Action BranchLoadUndoneCallback { get; set; }
public Action<int> BranchUpdateUndoneCallback { get; set; }
public Action<int> BranchRemoveUndoneCallback { get; set; }
public Action<int, int> BranchesReorderedCallback { get; set; }

private void GreenzoneInvalidated(int index)
{
Expand All @@ -33,5 +37,25 @@ private void BranchRemoved(int index)
{
BranchRemovedCallback?.Invoke(index);
}

private void BranchLoadUndone()
{
BranchLoadUndoneCallback?.Invoke();
}

private void BranchUpdateUndone(int index)
{
BranchUpdateUndoneCallback?.Invoke(index);
}

private void BranchRemoveUndone(int index)
{
BranchRemoveUndoneCallback?.Invoke(index);
}

private void BranchesReordered(int oldIndex, int newIndex)
{
BranchesReorderedCallback?.Invoke(oldIndex, newIndex);
}
}
}
5 changes: 5 additions & 0 deletions src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public TAStudio()
BookMarkControl.LoadedCallback = BranchLoaded;
BookMarkControl.SavedCallback = BranchSaved;
BookMarkControl.RemovedCallback = BranchRemoved;
BookMarkControl.LoadUndoneCallback = BranchLoadUndone;
BookMarkControl.UpdateUndoneCallback = BranchUpdateUndone;
BookMarkControl.RemoveUndoneCallback = BranchRemoveUndone;
BookMarkControl.ReorderedCallback = BranchesReordered;
TasView.MouseLeave += TAStudio_MouseLeave;
TasView.CellHovered += (_, e) =>
{
Expand Down Expand Up @@ -436,6 +440,7 @@ public void AddColumn(string name, string text, int widthUnscaled)
public void SelectAllExternal() => SelectAllMenuItem_Click(null, null);
public void ReselectClipboardExternal() => ReselectClipboardMenuItem_Click(null, null);

public int? GetSelectedBranch() => BookMarkControl.GetSelectedBranch();
public IMovieController GetBranchInput(string branchId, int frame)
{
var branch = CurrentTasMovie.Branches.FirstOrDefault(b => b.Uuid.ToString() == branchId);
Expand Down