BizHawk/BizHawk.Client.Common/movie/bk2/Bk2ControllerAdapter.cs

105 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
public class Bk2ControllerAdapter : IMovieController
{
#region IController Implementation
public bool this[string button]
{
get { return MyBoolButtons[button]; }
}
public bool IsPressed(string button)
{
return MyBoolButtons[button];
}
public float GetFloat(string name)
{
return MyFloatControls[name];
}
#endregion
#region IMovieController Implementation
public ControllerDefinition Type { get; set; }
/// <summary>
/// latches one player from the source
/// </summary>
public void LatchPlayerFromSource(IController playerSource, int playerNum)
{
foreach (string button in playerSource.Type.BoolButtons)
{
var bnp = ButtonNameParser.Parse(button);
if (bnp == null)
{
continue;
}
if (bnp.PlayerNum != playerNum)
{
continue;
}
bool val = playerSource[button];
MyBoolButtons[button] = val;
}
}
/// <summary>
/// latches all buttons from the provided source
/// </summary>
public void LatchFromSource(IController source)
{
foreach (string button in Type.BoolButtons)
{
MyBoolButtons[button] = source[button];
}
foreach (string name in Type.FloatControls)
{
MyFloatControls[name] = source.GetFloat(name);
}
}
/// <summary>
/// latches all buttons from the supplied mnemonic string
/// </summary>
public void SetControllersAsMnemonic(string mnemonic)
{
if (!string.IsNullOrWhiteSpace(mnemonic))
{
var trimmed = mnemonic.Replace("|", "");
for (int i = 0; i < Type.BoolButtons.Count; i++)
{
MyBoolButtons[Type.BoolButtons[i]] = trimmed[i] == '.' ? false : true;
}
var floats = trimmed.Remove(0, Type.BoolButtons.Count);
for (int i = 0; i < Type.FloatControls.Count; i++)
{
var temp = floats.Substring(i * 4, 3);
var val = int.Parse(temp);
MyFloatControls[Type.FloatControls[i]] = val;
}
}
}
#endregion
private readonly WorkingDictionary<string, bool> MyBoolButtons = new WorkingDictionary<string, bool>();
private readonly WorkingDictionary<string, float> MyFloatControls = new WorkingDictionary<string, float>();
}
}