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
{
2013-10-31 13:07:42 +00:00
public 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 ( ) ;
2013-12-30 01:58:44 +00:00
foreach ( var button in Global . ControllerOutput . 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
{
buttons [ button ] = Global . ControllerOutput [ button ] ;
}
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-04-13 18:46:06 +00:00
buttons [ button . Substring ( 3 ) ] = Global . ControllerOutput [ "P" + controller + " " + button . Substring ( 3 ) ] ;
2013-10-28 19:13:01 +00:00
}
}
2013-12-30 01:58:44 +00:00
foreach ( var button in Global . ControllerOutput . Source . Type . FloatControls )
2013-10-28 19:13:01 +00:00
{
if ( controller = = null )
{
buttons [ button ] = Global . ControllerOutput . GetFloat ( button ) ;
}
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-04-13 18:46:06 +00:00
buttons [ button . Substring ( 3 ) ] = Global . ControllerOutput . 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 )
{
var m = new MovieControllerAdapter { Type = Global . MovieSession . MovieControllerAdapter . Type } ;
m . SetControllersAsMnemonic ( inputLogEntry ) ;
foreach ( var button in m . Type . BoolButtons )
{
Global . LuaAndAdaptor . SetButton ( button , m . IsPressed ( button ) ) ;
}
foreach ( var floatButton in m . Type . FloatControls )
{
2014-05-21 03:25:41 +00:00
Global . LuaAndAdaptor . SetFloat ( floatButton , m . GetFloat ( floatButton ) ) ;
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 ) ;
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 ) ;
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 ) ;
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*/ }
}
}
}