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 )
{
_lua = lua ;
}
public override string Name { get { return "joypad" ; } }
2013-12-30 01:58:44 +00:00
private readonly Lua _lua ;
2013-10-31 13:07:42 +00:00
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
) ]
public LuaTable Get ( object controller = null )
2013-10-28 19:13:01 +00:00
{
2013-12-30 01:58:44 +00:00
var buttons = _lua . NewTable ( ) ;
foreach ( var button in Global . ControllerOutput . Source . Type . BoolButtons )
2013-10-28 19:13:01 +00:00
{
if ( controller = = null )
{
buttons [ button ] = Global . ControllerOutput [ button ] ;
}
2013-12-30 01:58:44 +00:00
else if ( button . Length > = 3 & & button . Substring ( 0 , 2 ) = = "P" + LuaInt ( controller ) )
2013-10-28 19:13:01 +00:00
{
2013-10-31 13:07:42 +00:00
buttons [ button . Substring ( 3 ) ] = Global . ControllerOutput [ "P" + LuaInt ( 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 ) ;
}
2013-12-30 01:58:44 +00:00
else if ( button . Length > = 3 & & button . Substring ( 0 , 2 ) = = "P" + LuaInt ( controller ) )
2013-10-28 19:13:01 +00:00
{
2013-10-31 13:07:42 +00:00
buttons [ button . Substring ( 3 ) ] = Global . ControllerOutput . GetFloat ( "P" + LuaInt ( 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
{
2013-12-30 01:58:44 +00:00
var buttons = _lua . NewTable ( ) ;
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-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
) ]
public void Set ( LuaTable buttons , object 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 ;
}
if ( ! invert )
{
if ( theValue = = true )
{
2013-12-30 01:58:44 +00:00
if ( controller = = null ) // Force On
2013-10-28 19:13:01 +00:00
{
2013-11-01 22:56:55 +00:00
Global . ClickyVirtualPadController . Click ( button . ToString ( ) ) ;
Global . ForceOffAdaptor . SetSticky ( button . ToString ( ) , false ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2013-11-01 22:56:55 +00:00
Global . ClickyVirtualPadController . Click ( "P" + controller + " " + button ) ;
Global . ForceOffAdaptor . SetSticky ( "P" + controller + " " + button , false ) ;
2013-10-28 19:13:01 +00:00
}
}
2013-12-30 01:58:44 +00:00
else if ( theValue = = false ) // Force off
2013-10-28 19:13:01 +00:00
{
if ( controller = = null )
{
2013-11-01 22:56:55 +00:00
Global . ForceOffAdaptor . SetSticky ( button . ToString ( ) , true ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2013-11-01 22:56:55 +00:00
Global . ForceOffAdaptor . SetSticky ( "P" + controller + " " + button , true ) ;
2013-10-28 19:13:01 +00:00
}
}
else
{
2013-12-30 01:58:44 +00:00
// Turn everything off
2013-10-28 19:13:01 +00:00
if ( controller = = null )
{
2013-11-01 22:56:55 +00:00
Global . ForceOffAdaptor . SetSticky ( button . ToString ( ) , false ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2013-11-01 22:56:55 +00:00
Global . ForceOffAdaptor . SetSticky ( "P" + controller + " " + button , false ) ;
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
{
if ( controller = = null )
{
2013-11-01 22:56:55 +00:00
Global . StickyXORAdapter . SetSticky ( button . ToString ( ) , true ) ;
Global . ForceOffAdaptor . SetSticky ( button . ToString ( ) , false ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2013-11-01 22:56:55 +00:00
Global . StickyXORAdapter . SetSticky ( "P" + controller + " " + button , true ) ;
Global . ForceOffAdaptor . SetSticky ( "P" + controller + " " + button , false ) ;
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*/ }
}
}
}