2013-10-28 19:13:01 +00:00
using System ;
using LuaInterface ;
2013-11-01 22:56:55 +00:00
namespace BizHawk.Client.Common
2013-10-28 19:13:01 +00:00
{
2014-06-01 22:02:59 +00:00
public sealed class JoypadLuaLibrary : LuaLibraryBase
2013-10-28 19:13:01 +00:00
{
2013-10-31 13:07:42 +00:00
public JoypadLuaLibrary ( Lua lua )
2014-05-21 00:17:35 +00:00
: base ( lua ) { }
public JoypadLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2013-10-31 13:07:42 +00:00
public override string Name { get { return "joypad" ; } }
2014-01-26 00:01:40 +00:00
[ LuaMethodAttributes (
"get" ,
2014-01-26 18:36:27 +00:00
"returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller"
2014-01-26 00:01:40 +00:00
) ]
2014-04-13 18:46:06 +00:00
public LuaTable Get ( int? controller = null )
2013-10-28 19:13:01 +00:00
{
2014-05-20 20:34:51 +00:00
var buttons = Lua . NewTable ( ) ;
2014-05-24 03:24:05 +00:00
var adaptor = Global . AutofireStickyXORAdapter ;
foreach ( var button in adaptor . Source . Type . BoolButtons )
2013-10-28 19:13:01 +00:00
{
2014-04-13 18:46:06 +00:00
if ( ! controller . HasValue )
2013-10-28 19:13:01 +00:00
{
2014-05-24 03:24:05 +00:00
buttons [ button ] = adaptor [ button ] ;
2013-10-28 19:13:01 +00:00
}
2014-04-13 18:46:06 +00:00
else if ( button . Length > = 3 & & button . Substring ( 0 , 2 ) = = "P" + controller )
2013-10-28 19:13:01 +00:00
{
2014-05-24 03:24:05 +00:00
buttons [ button . Substring ( 3 ) ] = adaptor [ "P" + controller + " " + button . Substring ( 3 ) ] ;
2013-10-28 19:13:01 +00:00
}
}
2014-05-24 03:24:05 +00:00
foreach ( var button in adaptor . Source . Type . FloatControls )
2013-10-28 19:13:01 +00:00
{
if ( controller = = null )
{
2014-05-24 03:24:05 +00:00
buttons [ button ] = adaptor . GetFloat ( button ) ;
2013-10-28 19:13:01 +00:00
}
2014-04-13 18:46:06 +00:00
else if ( button . Length > = 3 & & button . Substring ( 0 , 2 ) = = "P" + controller )
2013-10-28 19:13:01 +00:00
{
2014-05-24 03:24:05 +00:00
buttons [ button . Substring ( 3 ) ] = adaptor . GetFloat ( "P" + controller + " " + button . Substring ( 3 ) ) ;
2013-10-28 19:13:01 +00:00
}
}
buttons [ "clear" ] = null ;
buttons [ "getluafunctionslist" ] = null ;
buttons [ "output" ] = null ;
return buttons ;
}
2014-01-26 00:01:40 +00:00
[ LuaMethodAttributes (
"getimmediate" ,
2014-01-26 18:36:27 +00:00
"returns a lua table of any controller buttons currently pressed by the user"
2014-01-26 00:01:40 +00:00
) ]
public LuaTable GetImmediate ( )
2013-10-28 19:13:01 +00:00
{
2014-05-20 20:34:51 +00:00
var buttons = Lua . NewTable ( ) ;
2013-12-30 01:58:44 +00:00
foreach ( var button in Global . ActiveController . Type . BoolButtons )
2013-10-31 13:07:42 +00:00
{
2013-10-28 19:13:01 +00:00
buttons [ button ] = Global . ActiveController [ button ] ;
2013-10-31 13:07:42 +00:00
}
2013-12-30 01:58:44 +00:00
2013-10-28 19:13:01 +00:00
return buttons ;
}
2014-05-18 22:54:41 +00:00
[ LuaMethodAttributes (
"setfrommnemonicstr" ,
"sets the given buttons to their provided values for the current frame, string will be interpretted the same way an entry from a movie input log would be"
) ]
public void SetFromMnemonicStr ( string inputLogEntry )
{
2014-06-29 13:40:49 +00:00
try
2014-05-18 22:54:41 +00:00
{
2014-06-29 13:40:49 +00:00
var lg = Global . MovieSession . MovieControllerInstance ( ) ;
lg . SetControllersAsMnemonic ( inputLogEntry ) ;
2014-05-18 22:54:41 +00:00
2014-06-29 13:40:49 +00:00
foreach ( var button in lg . Type . BoolButtons )
{
Global . LuaAndAdaptor . SetButton ( button , lg . IsPressed ( button ) ) ;
}
foreach ( var floatButton in lg . Type . FloatControls )
{
Global . LuaAndAdaptor . SetFloat ( floatButton , lg . GetFloat ( floatButton ) ) ;
}
}
catch ( Exception )
2014-05-18 22:54:41 +00:00
{
2014-06-29 13:40:49 +00:00
Log ( "invalid mnemonic string: " + inputLogEntry ) ;
2014-05-18 22:54:41 +00:00
}
}
2014-01-26 00:01:40 +00:00
[ LuaMethodAttributes (
"set" ,
2014-01-26 18:36:27 +00:00
"sets the given buttons to their provided values for the current frame"
2014-01-26 00:01:40 +00:00
) ]
2014-04-22 21:55:04 +00:00
public void Set ( LuaTable buttons , int? controller = null )
2013-10-28 19:13:01 +00:00
{
try
{
foreach ( var button in buttons . Keys )
{
2013-12-30 01:58:44 +00:00
var invert = false ;
2013-10-28 19:13:01 +00:00
bool? theValue ;
2013-12-30 01:58:44 +00:00
var theValueStr = buttons [ button ] . ToString ( ) ;
2013-10-28 19:13:01 +00:00
if ( ! String . IsNullOrWhiteSpace ( theValueStr ) )
{
if ( theValueStr . ToLower ( ) = = "false" )
{
theValue = false ;
}
else if ( theValueStr . ToLower ( ) = = "true" )
{
theValue = true ;
}
else
{
invert = true ;
theValue = null ;
}
}
else
{
theValue = null ;
}
2014-04-22 21:55:04 +00:00
var toPress = button . ToString ( ) ;
if ( controller . HasValue )
2014-03-27 00:53:22 +00:00
{
toPress = "P" + controller + " " + button ;
}
2013-10-28 19:13:01 +00:00
if ( ! invert )
{
2014-03-29 21:12:04 +00:00
if ( theValue . HasValue ) // Force
2013-10-28 19:13:01 +00:00
{
2014-04-22 21:55:04 +00:00
Global . LuaAndAdaptor . SetButton ( toPress , theValue . Value ) ;
2014-05-24 03:24:05 +00:00
Global . ActiveController . Overrides ( Global . LuaAndAdaptor ) ;
2013-10-28 19:13:01 +00:00
}
2014-03-29 21:12:04 +00:00
else // Unset
2013-10-28 19:13:01 +00:00
{
2014-04-22 21:55:04 +00:00
Global . LuaAndAdaptor . UnSet ( toPress ) ;
2014-05-24 03:24:05 +00:00
Global . ActiveController . Overrides ( Global . LuaAndAdaptor ) ;
2013-10-28 19:13:01 +00:00
}
}
2013-12-30 01:58:44 +00:00
else // Inverse
2013-10-28 19:13:01 +00:00
{
2014-04-22 21:55:04 +00:00
Global . LuaAndAdaptor . SetInverse ( toPress ) ;
2014-05-24 03:24:05 +00:00
Global . ActiveController . Overrides ( Global . LuaAndAdaptor ) ;
2013-10-28 19:13:01 +00:00
}
}
}
2013-12-30 01:58:44 +00:00
catch
{
/*Eat it*/
}
2013-10-28 19:13:01 +00:00
}
2014-01-26 00:01:40 +00:00
[ LuaMethodAttributes (
"setanalog" ,
2014-01-26 18:36:27 +00:00
"sets the given analog controls to their provided values for the current frame. Note that unlike set() there is only the logic of overriding with the given value."
2014-01-26 00:01:40 +00:00
) ]
public void SetAnalog ( LuaTable controls , object controller = null )
2013-10-28 19:13:01 +00:00
{
try
{
foreach ( var name in controls . Keys )
{
2013-12-30 01:58:44 +00:00
var theValueStr = controls [ name ] . ToString ( ) ;
2013-10-28 19:13:01 +00:00
if ( ! String . IsNullOrWhiteSpace ( theValueStr ) )
{
try
{
2013-12-30 01:58:44 +00:00
var theValue = float . Parse ( theValueStr ) ;
2013-10-28 19:13:01 +00:00
if ( controller = = null )
{
2013-11-01 22:56:55 +00:00
Global . StickyXORAdapter . SetFloat ( name . ToString ( ) , theValue ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2013-11-01 22:56:55 +00:00
Global . StickyXORAdapter . SetFloat ( "P" + controller + " " + name , theValue ) ;
2013-10-28 19:13:01 +00:00
}
}
catch { }
}
}
}
catch { /*Eat it*/ }
}
}
}