tastudio lua: setinput functions, onbranch callbacks (#1098)

This commit is contained in:
TASeditor 2018-02-10 12:58:46 +01:00 committed by feos
parent 58f6e624ff
commit 36900b15d4
6 changed files with 286 additions and 35 deletions

View File

@ -1021,7 +1021,7 @@ namespace BizHawk.Client.Common
public void LoadBranch(int index)
{
if (index == -1) // backup branch is outsider
{
{
return;
}

View File

@ -23,6 +23,32 @@ namespace BizHawk.Client.EmuHawk
private TAStudio Tastudio => GlobalWin.Tools.Get<TAStudio>() as TAStudio;
private struct PendingChanges
{
public LuaChangeTypes type;
public InputChangeTypes inputType;
public int frame;
public int number;
public string button;
public bool valueBool;
public float valueFloat;
};
public enum LuaChangeTypes
{
InputChange,
InsertFrames,
DeleteFrames,
};
public enum InputChangeTypes
{
Bool,
Float,
};
private List<PendingChanges> changeList = new List<PendingChanges>(); //TODO: Initialize it to empty list on a script reload, and have each script have it's own list
[LuaMethod("engaged", "returns whether or not tastudio is currently engaged (active)")]
public bool Engaged()
{
@ -241,6 +267,42 @@ namespace BizHawk.Client.EmuHawk
}
}
[LuaMethod("onbranchload","called whenever a branch is loaded. luaf must be a function that takes the integer branch index as a parameter")]
public void OnBranchLoad(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchLoadedCallback = (int index) =>
{
luaf.Call(index);
};
}
}
[LuaMethod("onbranchsave", "called whenever a branch is created or updated. luaf must be a function that takes the integer branch index as a parameter")]
public void OnBranchSave(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchSavedCallback = (int index) =>
{
luaf.Call(index);
};
}
}
[LuaMethod("onbranchremove", "called whenever a branch is removed. luaf must be a function that takes the integer branch index as a parameter")]
public void OnBranchRemove(LuaFunction luaf)
{
if (Engaged())
{
Tastudio.BranchRemovedCallback = (int index) =>
{
luaf.Call(index);
};
}
}
[LuaMethod("getselection", "gets the currently selected frames")]
public LuaTable GetSelection()
{
@ -259,38 +321,6 @@ namespace BizHawk.Client.EmuHawk
return table;
}
[LuaMethod("insertframes", "inserts the given number of blank frames at the given insertion frame")]
public void InsertNumFrames(int insertionFrame, int numberOfFrames)
{
if (Engaged())
{
if (insertionFrame < Tastudio.CurrentTasMovie.InputLogLength)
{
Tastudio.InsertNumFrames(insertionFrame, numberOfFrames);
}
else
{
Log(insertionFrame + " is out of range");
}
}
}
[LuaMethod("deleteframes", "deletes the given number of blank frames beginning at the given frame")]
public void DeleteFrames(int beginningFrame, int numberOfFrames)
{
if (Engaged())
{
if (beginningFrame < Tastudio.CurrentTasMovie.InputLogLength)
{
Tastudio.DeleteFrames(beginningFrame, numberOfFrames);
}
else
{
Log(beginningFrame + " is out of range");
}
}
}
public class TastudioBranchInfo
{
public string Id { get; set; }
@ -359,5 +389,169 @@ namespace BizHawk.Client.EmuHawk
return table;
}
[LuaMethod("submitinputchange","")]
public void SubmitInputChange(int frame, string button, bool value)
{
if (Engaged())
{
if (frame >= 0)
{
PendingChanges newChange = new PendingChanges();
if (frame < Tastudio.CurrentTasMovie.InputLogLength)
{
if (Tastudio.CurrentTasMovie.BoolIsPressed(frame, button) != value) //Check if the button state is not already in the state the user set in the lua script
{
newChange.type = LuaChangeTypes.InputChange;
newChange.inputType = InputChangeTypes.Bool;
newChange.frame = frame;
newChange.button = button;
newChange.valueBool = value;
changeList.Add(newChange);
}
else
{
//Nothing to do here
}
}
else
{
newChange.type = LuaChangeTypes.InputChange;
newChange.inputType = InputChangeTypes.Bool;
newChange.frame = frame;
newChange.button = button;
newChange.valueBool = value;
changeList.Add(newChange);
}
}
}
}
[LuaMethod("submitanalogchange","")]
public void SubmitAnalogChange(int frame, string button, float value)
{
if (Engaged())
{
if (frame >= 0)
{
PendingChanges newChange = new PendingChanges();
if (frame < Tastudio.CurrentTasMovie.InputLogLength)
{
if (Tastudio.CurrentTasMovie.GetFloatState(frame, button) != value) //Check if the button state is not already in the state the user set in the lua script
{
newChange.type = LuaChangeTypes.InputChange;
newChange.inputType = InputChangeTypes.Float;
newChange.frame = frame;
newChange.button = button;
newChange.valueFloat = value;
changeList.Add(newChange);
}
else
{
//Nothing to do here
}
}
else
{
newChange.type = LuaChangeTypes.InputChange;
newChange.inputType = InputChangeTypes.Float;
newChange.frame = frame;
newChange.button = button;
newChange.valueFloat = value;
changeList.Add(newChange);
}
}
}
}
[LuaMethod("submitinsertframes", "")]
public void SubmitInsertFrames(int frame, int number)
{
if (Engaged())
{
if (frame >= 0 && frame < Tastudio.CurrentTasMovie.InputLogLength && number > 0)
{
PendingChanges newChange = new PendingChanges();
newChange.type = LuaChangeTypes.InsertFrames;
newChange.frame = frame;
newChange.number = number;
changeList.Add(newChange);
}
}
}
[LuaMethod("submitdeleteframes", "")]
public void SubmitDeleteFrames(int frame, int number)
{
if (Engaged())
{
if (frame >= 0 && frame < Tastudio.CurrentTasMovie.InputLogLength && number > 0)
{
PendingChanges newChange = new PendingChanges();
newChange.type = LuaChangeTypes.DeleteFrames;
newChange.frame = frame;
newChange.number = number;
changeList.Add(newChange);
}
}
}
[LuaMethod("applyinputchanges", "")]
public void ApplyInputChanges()
{
if (Engaged())
{
if (changeList.Count > 0)
{
int size = changeList.Count;
for (int i = 0; i < size; i++)
{
switch (changeList[i].type)
{
case LuaChangeTypes.InputChange:
switch (changeList[i].inputType)
{
case InputChangeTypes.Bool:
Tastudio.CurrentTasMovie.SetBoolState(changeList[i].frame, changeList[i].button, changeList[i].valueBool);
break;
case InputChangeTypes.Float:
Tastudio.CurrentTasMovie.SetFloatState(changeList[i].frame, changeList[i].button, changeList[i].valueFloat);
break;
}
break;
case LuaChangeTypes.InsertFrames:
Tastudio.InsertNumFrames(changeList[i].frame, changeList[i].number);
break;
case LuaChangeTypes.DeleteFrames:
Tastudio.DeleteFrames(changeList[i].frame, changeList[i].number);
break;
}
}
changeList.Clear();
Tastudio.JumpToGreenzone();
Tastudio.DoAutoRestore();
}
}
}
[LuaMethod("clearinputchanges","")]
public void ClearInputChanges()
{
if (Engaged())
changeList.Clear();
}
}
}

View File

@ -28,6 +28,27 @@ namespace BizHawk.Client.EmuHawk
Load, Update, Text, Remove, None
}
public Action<int> LoadedCallback { get; set; }
private void CallLoadedCallback(int index)
{
LoadedCallback?.Invoke(index);
}
public Action<int> SavedCallback { get; set; }
private void CallSavedCallback(int index)
{
SavedCallback?.Invoke(index);
}
public Action<int> RemovedCallback { get; set; }
private void CallRemovedCallback(int index)
{
RemovedCallback?.Invoke(index);
}
public TAStudio Tastudio { get; set; }
public int HoverInterval
@ -210,6 +231,7 @@ namespace BizHawk.Client.EmuHawk
private void AddBranchToolStripMenuItem_Click(object sender, EventArgs e)
{
Branch();
CallSavedCallback(Tastudio.CurrentTasMovie.BranchCount - 1);
GlobalWin.OSD.AddMessage("Added branch " + Movie.CurrentBranch.ToString());
}
@ -217,6 +239,7 @@ namespace BizHawk.Client.EmuHawk
{
Branch();
EditBranchTextPopUp(Movie.CurrentBranch);
CallSavedCallback(Tastudio.CurrentTasMovie.BranchCount - 1);
GlobalWin.OSD.AddMessage("Added branch " + Movie.CurrentBranch.ToString());
}
@ -237,6 +260,7 @@ namespace BizHawk.Client.EmuHawk
_branchUndo = BranchUndo.Load;
LoadSelectedBranch();
CallLoadedCallback(BranchView.SelectedRows.First());
}
private void UpdateBranchToolStripMenuItem_Click(object sender, EventArgs e)
@ -252,6 +276,7 @@ namespace BizHawk.Client.EmuHawk
_branchUndo = BranchUndo.Update;
UpdateBranch(SelectedBranch);
CallSavedCallback(Movie.CurrentBranch);
GlobalWin.OSD.AddMessage("Saved branch " + Movie.CurrentBranch);
}
}
@ -316,6 +341,7 @@ namespace BizHawk.Client.EmuHawk
BranchView.SelectRow(Movie.BranchCount - 1, true);
}
CallRemovedCallback(index);
Tastudio.RefreshDialog();
GlobalWin.OSD.AddMessage("Removed branch " + index.ToString());
}
@ -326,11 +352,13 @@ namespace BizHawk.Client.EmuHawk
if (_branchUndo == BranchUndo.Load)
{
LoadBranch(_backupBranch);
CallLoadedCallback(Tastudio.CurrentTasMovie.Branches.IndexOf(_backupBranch));
GlobalWin.OSD.AddMessage("Branch Load canceled");
}
else if (_branchUndo == BranchUndo.Update)
{
Movie.UpdateBranch(Movie.GetBranch(_backupBranch.UniqueIdentifier), _backupBranch);
CallSavedCallback(Tastudio.CurrentTasMovie.Branches.IndexOf(_backupBranch));
GlobalWin.OSD.AddMessage("Branch Update canceled");
}
else if (_branchUndo == BranchUndo.Text)
@ -342,6 +370,7 @@ namespace BizHawk.Client.EmuHawk
{
Movie.AddBranch(_backupBranch);
BranchView.RowCount = Movie.BranchCount;
CallSavedCallback(Tastudio.CurrentTasMovie.Branches.IndexOf(_backupBranch));
GlobalWin.OSD.AddMessage("Branch Removal canceled");
}

View File

@ -11,6 +11,9 @@ namespace BizHawk.Client.EmuHawk
public Func<int, string, Bitmap> QueryItemIconCallback { get; set; }
public Action<int> GreenzoneInvalidatedCallback { get; set; }
public Action<int> BranchLoadedCallback { get; set; }
public Action<int> BranchSavedCallback { get; set; }
public Action<int> BranchRemovedCallback { get; set; }
private Color? GetColorOverride(int index, InputRoll.RollColumn column)
{
@ -31,5 +34,20 @@ namespace BizHawk.Client.EmuHawk
{
GreenzoneInvalidatedCallback?.Invoke(index);
}
private void BranchLoaded(int index)
{
BranchLoadedCallback?.Invoke(index);
}
private void BranchSaved(int index)
{
BranchSavedCallback?.Invoke(index);
}
private void BranchRemoved(int index)
{
BranchRemovedCallback?.Invoke(index);
}
}
}

View File

@ -63,7 +63,7 @@ namespace BizHawk.Client.EmuHawk
public AutoPatternBool[] BoolPatterns;
public AutoPatternFloat[] FloatPatterns;
private void JumpToGreenzone()
public void JumpToGreenzone()
{
if (Emulator.Frame > CurrentTasMovie.LastValidFrame)
{

View File

@ -534,6 +534,10 @@ namespace BizHawk.Client.EmuHawk
newMovie.TasStateManager.InvalidateCallback = GreenzoneInvalidated;
newMovie.Filename = file.FullName;
BookMarkControl.LoadedCallback = BranchLoaded;
BookMarkControl.SavedCallback = BranchSaved;
BookMarkControl.RemovedCallback = BranchRemoved;
if (!HandleMovieLoadStuff(newMovie))
{
return false;
@ -582,8 +586,14 @@ namespace BizHawk.Client.EmuHawk
{
Global.MovieSession.Movie = new TasMovie(false, _seekBackgroundWorker);
var stateManager = ((TasMovie)Global.MovieSession.Movie).TasStateManager;
stateManager.MountWriteAccess();
stateManager.InvalidateCallback = GreenzoneInvalidated;
BookMarkControl.LoadedCallback = BranchLoaded;
BookMarkControl.SavedCallback = BranchSaved;
BookMarkControl.RemovedCallback = BranchRemoved;
CurrentTasMovie.PropertyChanged += TasMovie_OnPropertyChanged;
CurrentTasMovie.Filename = DefaultTasProjName(); // TODO don't do this, take over any mainform actions that can crash without a filename
CurrentTasMovie.PopulateWithDefaultHeaderValues();
@ -803,7 +813,7 @@ namespace BizHawk.Client.EmuHawk
_lastRefresh = Emulator.Frame;
}
private void DoAutoRestore()
public void DoAutoRestore()
{
if (Settings.AutoRestoreLastPosition && LastPositionFrame != -1)
{