2013-10-28 19:13:01 +00:00
using System ;
2017-07-10 04:51:02 +00:00
using NLua ;
2013-10-28 19:13:01 +00:00
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
2017-04-14 19:59:01 +00:00
public override string Name = > "joypad" ;
2013-10-31 13:07:42 +00:00
2018-03-04 17:48:38 +00:00
[LuaMethodExample("get", "local nljoyget = joypad.get( 1 );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("get", "returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller")]
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 ;
2016-12-12 18:30:32 +00:00
foreach ( var button in adaptor . Source . Definition . 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
{
2016-12-14 20:12:16 +00:00
buttons [ button ] = adaptor . IsPressed ( 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
{
2016-12-14 20:12:16 +00:00
buttons [ button . Substring ( 3 ) ] = adaptor . IsPressed ( "P" + controller + " " + button . Substring ( 3 ) ) ;
2013-10-28 19:13:01 +00:00
}
}
2016-12-12 18:30:32 +00:00
foreach ( var button in adaptor . Source . Definition . 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 ;
}
2016-12-14 20:12:16 +00:00
// TODO: what about float controls?
2018-03-04 17:48:38 +00:00
[LuaMethodExample("getimmediate", "local nljoyget = joypad.getimmediate( );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("getimmediate", "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 ( ) ;
2016-12-12 18:30:32 +00:00
foreach ( var button in Global . ActiveController . Definition . BoolButtons )
2013-10-31 13:07:42 +00:00
{
2016-12-14 20:12:16 +00:00
buttons [ button ] = Global . ActiveController . IsPressed ( 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 ;
}
2018-03-04 17:48:38 +00:00
[LuaMethodExample("setfrommnemonicstr", "joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("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")]
2014-05-18 22:54:41 +00:00
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
2016-12-12 18:30:32 +00:00
foreach ( var button in lg . Definition . BoolButtons )
2014-06-29 13:40:49 +00:00
{
Global . LuaAndAdaptor . SetButton ( button , lg . IsPressed ( button ) ) ;
}
2016-12-12 18:30:32 +00:00
foreach ( var floatButton in lg . Definition . FloatControls )
2014-06-29 13:40:49 +00:00
{
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
}
}
2018-03-04 17:48:38 +00:00
[LuaMethodExample("set", "joypad.set( { [\"Left\"] = true , [ \ "A\" ] = true, [ \"B\" ] = true } );" ) ]
2017-07-10 19:02:00 +00:00
[LuaMethod("set", "sets the given buttons to their provided values for the current frame")]
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
2017-04-14 19:59:01 +00:00
if ( ! string . IsNullOrWhiteSpace ( theValueStr ) )
2013-10-28 19:13:01 +00:00
{
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*/
2018-03-04 17:48:38 +00:00
}
}
2013-10-28 19:13:01 +00:00
2018-03-04 17:48:38 +00:00
[LuaMethodExample("setanalog", "joypad.setanalog( { [ \"Tilt X\" ] = true , [ \ "Tilt Y\" ] = false } );" ) ]
2017-07-10 19:02:00 +00:00
[LuaMethod("setanalog", "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 ( ) ;
2014-12-07 07:39:01 +00:00
float? theValue = null ;
2013-10-28 19:13:01 +00:00
2017-04-14 19:59:01 +00:00
if ( ! string . IsNullOrWhiteSpace ( theValueStr ) )
2013-10-28 19:13:01 +00:00
{
2014-12-07 07:39:01 +00:00
float f ;
if ( float . TryParse ( theValueStr , out f ) )
2017-04-14 19:59:01 +00:00
{
2014-12-07 07:39:01 +00:00
theValue = f ;
2017-04-14 19:59:01 +00:00
}
2014-12-07 07:39:01 +00:00
}
if ( controller = = null )
{
Global . StickyXORAdapter . SetFloat ( name . ToString ( ) , theValue ) ;
2013-10-28 19:13:01 +00:00
}
2014-12-07 07:39:01 +00:00
else
{
Global . StickyXORAdapter . SetFloat ( "P" + controller + " " + name , theValue ) ;
}
2013-10-28 19:13:01 +00:00
}
}
2017-05-17 16:16:55 +00:00
catch
{
/*Eat it*/
}
2013-10-28 19:13:01 +00:00
}
}
}