2011-01-11 02:55:51 +00:00
using System ;
using System.Collections.Generic ;
2011-05-08 00:06:43 +00:00
using System.Text ;
2011-08-29 03:28:34 +00:00
using System.Linq ;
2011-01-11 02:55:51 +00:00
namespace BizHawk.MultiClient
{
2011-06-19 23:39:25 +00:00
public class Controller : IController
{
private ControllerDefinition type ;
2011-07-24 20:37:10 +00:00
private WorkingDictionary < string , List < string > > bindings = new WorkingDictionary < string , List < string > > ( ) ;
private WorkingDictionary < string , bool > buttons = new WorkingDictionary < string , bool > ( ) ;
public Controller ( ControllerDefinition definition )
{
type = definition ;
}
2011-08-09 22:13:57 +00:00
public ControllerDefinition Type { get { return type ; } }
public bool this [ string button ] { get { return IsPressed ( button ) ; } }
public bool IsPressed ( string button )
{
return buttons [ button ] ;
}
public float GetFloat ( string name ) { throw new NotImplementedException ( ) ; }
public void UpdateControls ( int frame ) { }
//look for bindings which are activated by the supplied physical button.
public List < string > SearchBindings ( string button )
{
var ret = new List < string > ( ) ;
foreach ( var kvp in bindings )
{
foreach ( var bound_button in kvp . Value )
{
if ( bound_button = = button )
ret . Add ( kvp . Key ) ;
}
}
return ret ;
}
/// <summary>
/// uses the bindings to latch our own logical button state from the source controller's button state (which are assumed to be the physical side of the binding).
/// this will clobber any existing data (use OR_* or other functions to layer in additional input sources)
/// </summary>
public void LatchFromPhysical ( IController controller )
{
buttons . Clear ( ) ;
foreach ( var kvp in bindings )
{
buttons [ kvp . Key ] = false ;
foreach ( var bound_button in kvp . Value )
{
if ( controller [ bound_button ] )
buttons [ kvp . Key ] = true ;
}
}
}
/// <summary>
/// merges pressed logical buttons from the supplied controller, effectively ORing it with the current state
/// </summary>
public void OR_FromLogical ( IController controller )
{
foreach ( string button in type . BoolButtons )
{
if ( controller . IsPressed ( button ) )
{
buttons [ button ] = true ;
Console . WriteLine ( button ) ;
}
}
}
public void BindButton ( string button , string control )
{
bindings [ button ] . Add ( control ) ;
}
public void BindMulti ( string button , string controlString )
{
if ( string . IsNullOrEmpty ( controlString ) )
return ;
string [ ] controlbindings = controlString . Split ( ',' ) ;
foreach ( string control in controlbindings )
bindings [ button ] . Add ( control . Trim ( ) ) ;
}
2012-09-10 17:49:33 +00:00
/// <summary>
/// Returns a list of all keys mapped and the name of the button they are mapped to
/// </summary>
/// <returns></returns>
public List < KeyValuePair < string , string > > MappingList ( )
{
List < KeyValuePair < string , string > > list = new List < KeyValuePair < string , string > > ( ) ;
foreach ( KeyValuePair < string , List < string > > key in bindings )
{
foreach ( string binding in key . Value )
{
list . Add ( new KeyValuePair < string , string > ( binding , key . Key ) ) ;
}
}
return list ;
}
2012-09-14 21:31:00 +00:00
public List < string > PressedButtons
{
get
{
List < string > list = new List < string > ( ) ;
foreach ( var button in buttons )
{
if ( button . Value )
{
list . Add ( button . Key ) ;
}
}
return list ;
}
}
2012-09-10 17:49:33 +00:00
}
2011-08-09 22:13:57 +00:00
public class AutofireController : IController
{
private ControllerDefinition type ;
private WorkingDictionary < string , List < string > > bindings = new WorkingDictionary < string , List < string > > ( ) ;
private WorkingDictionary < string , bool > buttons = new WorkingDictionary < string , bool > ( ) ;
2011-08-29 03:28:34 +00:00
public WorkingDictionary < string , int > buttonStarts = new WorkingDictionary < string , int > ( ) ;
2011-08-09 22:13:57 +00:00
private bool autofire = true ;
public bool Autofire { get { return false ; } set { autofire = value ; } }
public int On { get ; set ; }
public int Off { get ; set ; }
public AutofireController ( ControllerDefinition definition )
{
On = Global . Config . AutofireOn < 1 ? 0 : Global . Config . AutofireOn ;
Off = Global . Config . AutofireOff < 1 ? 0 : Global . Config . AutofireOff ;
type = definition ;
}
2011-07-24 20:37:10 +00:00
public ControllerDefinition Type { get { return type ; } }
public bool this [ string button ] { get { return IsPressed ( button ) ; } }
2011-08-08 23:35:13 +00:00
public bool IsPressed ( string button )
{
if ( autofire )
{
2011-08-10 00:34:33 +00:00
int a = ( Global . Emulator . Frame - buttonStarts [ button ] ) % ( On + Off ) ;
2011-08-09 22:13:57 +00:00
if ( a < On )
2011-08-08 23:35:13 +00:00
return buttons [ button ] ;
else
return false ;
}
else
return buttons [ button ] ;
}
2011-07-24 20:37:10 +00:00
public float GetFloat ( string name ) { throw new NotImplementedException ( ) ; }
public void UpdateControls ( int frame ) { }
2011-06-19 23:39:25 +00:00
2011-07-10 07:39:40 +00:00
//look for bindings which are activated by the supplied physical button.
2011-07-09 22:09:39 +00:00
public List < string > SearchBindings ( string button )
{
var ret = new List < string > ( ) ;
foreach ( var kvp in bindings )
{
foreach ( var bound_button in kvp . Value )
{
if ( bound_button = = button )
ret . Add ( kvp . Key ) ;
}
}
return ret ;
}
2011-07-24 20:23:27 +00:00
/// <summary>
2011-08-04 02:47:05 +00:00
/// uses the bindings to latch our own logical button state from the source controller's button state (which are assumed to be the physical side of the binding).
/// this will clobber any existing data (use OR_* or other functions to layer in additional input sources)
2011-07-24 20:23:27 +00:00
/// </summary>
2011-07-10 02:14:58 +00:00
public void LatchFromPhysical ( IController controller )
{
2011-08-10 00:34:33 +00:00
foreach ( var kvp in bindings )
{
foreach ( var bound_button in kvp . Value )
{
if ( buttons [ kvp . Key ] = = false & & controller [ bound_button ] = = true )
buttonStarts [ kvp . Key ] = Global . Emulator . Frame ;
}
}
2011-08-04 02:47:05 +00:00
buttons . Clear ( ) ;
2011-07-10 02:14:58 +00:00
foreach ( var kvp in bindings )
{
2011-07-24 20:37:10 +00:00
buttons [ kvp . Key ] = false ;
2011-07-10 02:14:58 +00:00
foreach ( var bound_button in kvp . Value )
{
2011-08-09 22:13:57 +00:00
if ( controller [ bound_button ] )
2011-08-10 00:34:33 +00:00
{
2011-07-24 20:37:10 +00:00
buttons [ kvp . Key ] = true ;
2011-08-10 00:34:33 +00:00
}
2011-07-10 02:14:58 +00:00
}
}
}
2011-07-24 20:23:27 +00:00
/// <summary>
/// merges pressed logical buttons from the supplied controller, effectively ORing it with the current state
/// </summary>
public void OR_FromLogical ( IController controller )
{
foreach ( string button in type . BoolButtons )
{
if ( controller . IsPressed ( button ) )
2011-08-04 02:47:05 +00:00
{
2011-07-24 20:37:10 +00:00
buttons [ button ] = true ;
2011-08-04 02:47:05 +00:00
Console . WriteLine ( button ) ;
}
2011-07-24 20:23:27 +00:00
}
}
2011-06-19 23:39:25 +00:00
public void BindButton ( string button , string control )
{
bindings [ button ] . Add ( control ) ;
}
public void BindMulti ( string button , string controlString )
{
if ( string . IsNullOrEmpty ( controlString ) )
return ;
string [ ] controlbindings = controlString . Split ( ',' ) ;
foreach ( string control in controlbindings )
2011-06-19 00:18:02 +00:00
bindings [ button ] . Add ( control . Trim ( ) ) ;
2011-06-19 23:39:25 +00:00
}
2011-08-29 03:28:34 +00:00
public void IncrementStarts ( )
{
foreach ( var key in buttonStarts . Keys . ToArray ( ) ) buttonStarts [ key ] + + ;
}
2012-09-14 21:31:00 +00:00
public List < string > PressedButtons
{
get
{
List < string > list = new List < string > ( ) ;
foreach ( var button in buttons )
{
if ( button . Value )
{
list . Add ( button . Key ) ;
}
}
return list ;
}
}
2011-06-19 23:39:25 +00:00
}
2011-01-14 03:38:26 +00:00
}