2014-09-19 21:43:16 +00:00
using System ;
using System.ComponentModel ;
2017-05-19 14:47:18 +00:00
using System.Drawing ;
2014-09-19 21:43:16 +00:00
2017-07-10 04:51:02 +00:00
using NLua ;
2017-05-19 14:47:18 +00:00
using BizHawk.Client.Common ;
2014-09-19 21:43:16 +00:00
namespace BizHawk.Client.EmuHawk
{
[Description("A library for manipulating the Tastudio dialog of the EmuHawk client")]
2017-07-10 19:02:00 +00:00
[LuaLibrary(released: true)]
2014-09-19 21:43:16 +00:00
public sealed class TastudioLuaLibrary : LuaLibraryBase
{
public TastudioLuaLibrary ( Lua lua )
: base ( lua ) { }
public TastudioLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2017-05-19 14:47:18 +00:00
public override string Name = > "tastudio" ;
2014-09-21 15:17:29 +00:00
2017-05-19 14:47:18 +00:00
private TAStudio Tastudio = > GlobalWin . Tools . Get < TAStudio > ( ) as TAStudio ;
2014-09-21 15:17:29 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("engaged", "returns whether or not tastudio is currently engaged (active)")]
2014-09-19 21:43:16 +00:00
public bool Engaged ( )
{
return GlobalWin . Tools . Has < TAStudio > ( ) ; // TODO: eventually tastudio should have an engaged flag
}
2014-09-21 15:17:29 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("getrecording", "returns whether or not TAStudio is in recording mode")]
2017-03-08 16:56:44 +00:00
public bool GetRecording ( )
{
return Tastudio . TasPlaybackBox . RecordingMode ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setrecording", "sets the recording mode on/off depending on the parameter")]
2017-03-08 16:56:44 +00:00
public void SetRecording ( bool val )
{
if ( Tastudio . TasPlaybackBox . RecordingMode ! = val )
2017-05-19 14:47:18 +00:00
{
2017-03-08 16:56:44 +00:00
Tastudio . ToggleReadOnly ( ) ;
2017-05-19 14:47:18 +00:00
}
2017-03-08 16:56:44 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("togglerecording", "toggles tastudio recording mode on/off depending on its current state")]
2017-03-08 16:56:44 +00:00
public void SetRecording ( )
{
Tastudio . ToggleReadOnly ( ) ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setbranchtext", "adds the given message to the existing branch, or to the branch that will be created next if branch index is not specified")]
2015-11-25 21:12:25 +00:00
public void SetBranchText ( string text , int? index = null )
{
if ( index ! = null )
{
Tastudio . CurrentTasMovie . GetBranch ( index . Value ) . UserText = text ;
}
else
{
Tastudio . CurrentTasMovie . NewBranchText = text ;
}
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getmarker", "returns the marker text at the given frame, or an empty string if there is no marker for the given frame")]
2014-09-21 15:17:29 +00:00
public string GetMarker ( int frame )
{
if ( Engaged ( ) )
{
2014-10-20 19:04:59 +00:00
var marker = Tastudio . CurrentTasMovie . Markers . Get ( frame ) ;
2014-09-21 15:17:29 +00:00
if ( marker ! = null )
{
return marker . Message ;
}
}
2017-05-10 11:45:23 +00:00
return "" ;
2014-09-21 15:17:29 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("removemarker", "if there is a marker for the given frame, it will be removed")]
2014-09-21 15:17:29 +00:00
public void RemoveMarker ( int frame )
{
if ( Engaged ( ) )
{
2014-10-20 19:04:59 +00:00
var marker = Tastudio . CurrentTasMovie . Markers . Get ( frame ) ;
2014-09-21 15:17:29 +00:00
if ( marker ! = null )
{
2014-10-20 19:04:59 +00:00
Tastudio . CurrentTasMovie . Markers . Remove ( marker ) ;
2014-09-21 15:17:29 +00:00
Tastudio . UpdateValues ( ) ;
}
}
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setmarker", "Adds or sets a marker at the given frame, with an optional message")]
2017-02-04 21:11:14 +00:00
public void SetMarker ( int frame , string message = null )
2014-09-21 15:17:29 +00:00
{
if ( Engaged ( ) )
{
2014-10-20 19:04:59 +00:00
var marker = Tastudio . CurrentTasMovie . Markers . Get ( frame ) ;
2014-09-21 15:17:29 +00:00
if ( marker ! = null )
{
marker . Message = message ;
}
else
{
2014-10-20 19:04:59 +00:00
Tastudio . CurrentTasMovie . Markers . Add ( frame , message ) ;
2014-09-21 15:17:29 +00:00
Tastudio . UpdateValues ( ) ;
}
}
}
2014-09-22 21:58:45 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("islag", "Returns whether or not the given frame was a lag frame, null if unknown")]
2014-09-22 21:58:45 +00:00
public bool? IsLag ( int frame )
{
if ( Engaged ( ) )
{
2014-10-20 19:04:59 +00:00
if ( frame < Tastudio . CurrentTasMovie . InputLogLength )
2014-09-22 21:58:45 +00:00
{
2014-10-20 19:04:59 +00:00
return Tastudio . CurrentTasMovie [ frame ] . Lagged ;
2014-09-22 21:58:45 +00:00
}
}
return null ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setlag", "Sets the lag information for the given frame, if the frame does not exist in the lag log, it will be added. If the value is null, the lag information for that frame will be removed")]
2015-07-09 17:16:55 +00:00
public void SetLag ( int frame , bool? value )
{
if ( Engaged ( ) )
{
Tastudio . CurrentTasMovie . SetLag ( frame , value ) ;
}
}
2017-07-10 19:02:00 +00:00
[LuaMethod("hasstate", "Returns whether or not the given frame has a savestate associated with it")]
2014-09-22 21:58:45 +00:00
public bool HasState ( int frame )
{
if ( Engaged ( ) )
{
2014-10-20 19:04:59 +00:00
if ( frame < Tastudio . CurrentTasMovie . InputLogLength )
2014-09-22 21:58:45 +00:00
{
2014-10-20 19:04:59 +00:00
return Tastudio . CurrentTasMovie [ frame ] . HasState ;
2014-09-22 21:58:45 +00:00
}
}
return false ;
}
2015-07-01 23:01:29 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("setplayback", "Seeks the given frame (a number) or marker (a string)")]
2017-02-05 03:40:29 +00:00
public void SetPlayback ( object frame )
{
if ( Engaged ( ) )
{
int f ;
2017-05-19 14:47:18 +00:00
if ( frame is double )
{
f = ( int ) ( double ) frame ;
}
2017-02-05 03:40:29 +00:00
else
{
f = Tastudio . CurrentTasMovie . Markers . FindIndex ( ( string ) frame ) ;
2017-05-19 14:47:18 +00:00
if ( f = = - 1 )
{
return ;
}
2017-02-05 03:40:29 +00:00
f = Tastudio . CurrentTasMovie . Markers [ f ] . Frame ;
}
2017-05-19 14:47:18 +00:00
if ( f < Tastudio . CurrentTasMovie . InputLogLength & & f > = 0 )
2017-02-05 03:40:29 +00:00
{
2017-05-19 14:47:18 +00:00
Tastudio . GoToFrame ( f , true ) ;
2017-02-05 03:40:29 +00:00
}
}
}
2017-07-10 19:02:00 +00:00
[LuaMethod("onqueryitembg", "called during the background draw event of the tastudio listview. luaf must be a function that takes 2 params: index, column. The first is the integer row index of the listview, and the 2nd is the string column name. luaf should return a value that can be parsed into a .NET Color object (string color name, or integer value)")]
2015-07-01 23:01:29 +00:00
public void OnQueryItemBg ( LuaFunction luaf )
{
if ( Engaged ( ) )
{
Tastudio . QueryItemBgColorCallback = ( int index , string name ) = >
{
var result = luaf . Call ( index , name ) ;
if ( result ! = null )
{
var color = ToColor ( result [ 0 ] ) ;
return color ;
}
return null ;
} ;
}
}
2015-07-01 23:46:14 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("onqueryitemtext", "called during the text draw event of the tastudio listview. luaf must be a function that takes 2 params: index, column. The first is the integer row index of the listview, and the 2nd is the string column name. luaf should return a value that can be parsed into a .NET Color object (string color name, or integer value)")]
2015-07-01 23:46:14 +00:00
public void OnQueryItemText ( LuaFunction luaf )
{
if ( Engaged ( ) )
{
Tastudio . QueryItemTextCallback = ( int index , string name ) = >
{
var result = luaf . Call ( index , name ) ;
2017-05-19 14:47:18 +00:00
return result ? [ 0 ] ? . ToString ( ) ;
2015-07-01 23:46:14 +00:00
} ;
}
}
2015-07-02 18:51:42 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("onqueryitemicon", "called during the icon draw event of the tastudio listview. luaf must be a function that takes 2 params: index, column. The first is the integer row index of the listview, and the 2nd is the string column name. luaf should return a value that can be parsed into a .NET Color object (string color name, or integer value)")]
2015-07-09 16:47:59 +00:00
public void OnQueryItemIcon ( LuaFunction luaf )
{
if ( Engaged ( ) )
{
Tastudio . QueryItemIconCallback = ( int index , string name ) = >
{
var result = luaf . Call ( index , name ) ;
2017-05-19 14:47:18 +00:00
if ( result ? [ 0 ] ! = null )
2015-07-09 16:47:59 +00:00
{
2017-05-19 14:47:18 +00:00
string path = result [ 0 ] . ToString ( ) ;
Icon icon = new Icon ( path ) ;
return icon . ToBitmap ( ) ;
2015-07-09 16:47:59 +00:00
}
return ( Bitmap ) null ;
} ;
}
}
2017-07-10 19:02:00 +00:00
[LuaMethod("ongreenzoneinvalidated", "called whenever the greenzone is invalidated and returns the first frame that was invalidated")]
2015-07-02 18:51:42 +00:00
public void OnGreenzoneInvalidated ( LuaFunction luaf )
{
if ( Engaged ( ) )
{
Tastudio . GreenzoneInvalidatedCallback = ( int index ) = >
{
luaf . Call ( index ) ;
} ;
}
}
2017-04-23 01:50:03 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("getselection", "gets the currently selected frames")]
2017-04-23 01:50:03 +00:00
public LuaTable GetSelection ( )
{
LuaTable table = Lua . NewTable ( ) ;
if ( Engaged ( ) )
{
var selection = Tastudio . GetSelection ( ) ;
foreach ( var row in selection )
{
table [ row ] = row ;
}
}
return table ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("insertframes", "inserts the given number of blank frames at the given insertion frame")]
2017-04-23 01:50:03 +00:00
public void InsertNumFrames ( int insertionFrame , int numberOfFrames )
{
if ( Engaged ( ) )
{
if ( insertionFrame < Tastudio . CurrentTasMovie . InputLogLength )
{
Tastudio . InsertNumFrames ( insertionFrame , numberOfFrames ) ;
}
else
{
2017-05-19 14:47:18 +00:00
Log ( insertionFrame + " is out of range" ) ;
2017-04-23 01:50:03 +00:00
}
}
}
2017-07-10 19:02:00 +00:00
[LuaMethod("deleteframes", "deletes the given number of blank frames beginning at the given frame")]
2017-04-23 01:50:03 +00:00
public void DeleteFrames ( int beginningFrame , int numberOfFrames )
{
if ( Engaged ( ) )
{
if ( beginningFrame < Tastudio . CurrentTasMovie . InputLogLength )
{
Tastudio . DeleteFrames ( beginningFrame , numberOfFrames ) ;
}
else
{
2017-05-19 14:47:18 +00:00
Log ( beginningFrame + " is out of range" ) ;
2017-04-23 01:50:03 +00:00
}
}
}
2014-09-19 21:43:16 +00:00
}
}