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

221 lines
5.0 KiB
C#
Raw Normal View History

2014-06-15 14:44:26 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
public class Bk2ControllerAdapter : IMovieController
{
2017-05-10 11:45:23 +00:00
private readonly string _logKey = "";
2017-05-17 16:53:42 +00:00
private readonly WorkingDictionary<string, bool> _myBoolButtons = new WorkingDictionary<string, bool>();
private readonly WorkingDictionary<string, float> _myFloatControls = new WorkingDictionary<string, float>();
public Bk2ControllerAdapter()
2017-05-17 16:53:42 +00:00
{
}
public Bk2ControllerAdapter(string key)
{
_logKey = key;
SetLogOverride();
}
private void SetLogOverride()
{
if (!string.IsNullOrEmpty(_logKey))
{
var groups = _logKey.Split(new[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
2014-06-29 03:03:27 +00:00
var controls = groups
.Select(group => group.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries).ToList())
2014-06-29 03:03:27 +00:00
.ToList();
_type.ControlsFromLog = controls;
}
}
// TODO: get rid of this, add a SetBool() method or something for the set access, replace get wtih IsPressed
2014-06-15 14:44:26 +00:00
public bool this[string button]
{
get
{
2017-05-17 16:53:42 +00:00
return _myBoolButtons[button];
}
set
{
2017-05-17 16:53:42 +00:00
if (_myBoolButtons.ContainsKey(button))
{
2017-05-17 16:53:42 +00:00
_myBoolButtons[button] = value;
}
}
2014-06-15 14:44:26 +00:00
}
#region IController Implementation
2014-06-15 14:44:26 +00:00
public bool IsPressed(string button)
{
2017-05-17 16:53:42 +00:00
return _myBoolButtons[button];
2014-06-15 14:44:26 +00:00
}
public float GetFloat(string name)
{
2017-05-17 16:53:42 +00:00
return _myFloatControls[name];
2014-06-15 14:44:26 +00:00
}
#endregion
#region IMovieController Implementation
private Bk2ControllerDefinition _type = new Bk2ControllerDefinition();
public ControllerDefinition Definition
{
get
{
return _type;
}
set
{
_type = new Bk2ControllerDefinition(value);
SetLogOverride();
}
}
2014-06-15 14:44:26 +00:00
/// <summary>
/// latches one player from the source
/// </summary>
public void LatchPlayerFromSource(IController playerSource, int playerNum)
{
foreach (var button in playerSource.Definition.BoolButtons)
2014-06-15 14:44:26 +00:00
{
var bnp = ButtonNameParser.Parse(button);
2017-05-18 16:36:38 +00:00
if (bnp?.PlayerNum != playerNum)
2014-06-15 14:44:26 +00:00
{
continue;
}
var val = playerSource.IsPressed(button);
2017-05-17 16:53:42 +00:00
_myBoolButtons[button] = val;
2014-06-15 14:44:26 +00:00
}
foreach (var button in Definition.FloatControls)
{
var bnp = ButtonNameParser.Parse(button);
2017-05-18 16:36:38 +00:00
if (bnp?.PlayerNum != playerNum)
{
continue;
}
var val = playerSource.GetFloat(button);
2017-05-17 16:53:42 +00:00
_myFloatControls[button] = val;
}
2014-06-15 14:44:26 +00:00
}
/// <summary>
/// latches all buttons from the provided source
/// </summary>
public void LatchFromSource(IController source)
{
foreach (var button in Definition.BoolButtons)
2014-06-15 14:44:26 +00:00
{
2017-05-17 16:53:42 +00:00
_myBoolButtons[button] = source.IsPressed(button);
2014-06-15 14:44:26 +00:00
}
foreach (var name in Definition.FloatControls)
2014-06-15 14:44:26 +00:00
{
2017-05-17 16:53:42 +00:00
_myFloatControls[name] = source.GetFloat(name);
2014-06-15 14:44:26 +00:00
}
}
/// <summary>
2017-05-18 16:36:38 +00:00
/// latches sticky buttons from <see cref="Global.AutofireStickyXORAdapter"/>
/// </summary>
public void LatchSticky()
{
foreach (var button in Definition.BoolButtons)
{
2017-05-17 16:53:42 +00:00
_myBoolButtons[button] = Global.AutofireStickyXORAdapter.IsSticky(button);
}
// float controls don't have sticky logic. but old values remain in MovieOutputHardpoint if we don't update this here
foreach (var name in Definition.FloatControls)
{
_myFloatControls[name] = Global.AutofireStickyXORAdapter.GetFloat(name);
}
}
2014-06-15 14:44:26 +00:00
/// <summary>
/// latches all buttons from the supplied mnemonic string
/// </summary>
public void SetControllersAsMnemonic(string mnemonic)
{
if (!string.IsNullOrWhiteSpace(mnemonic))
{
var def = Global.Emulator.ControllerDefinition;
2017-05-10 11:45:23 +00:00
var trimmed = mnemonic.Replace("|", "");
2017-05-18 16:36:38 +00:00
var buttons = Definition.ControlsOrdered.SelectMany(c => c).ToList();
var iterator = 0;
2014-06-29 03:03:27 +00:00
foreach (var key in buttons)
{
2014-06-29 03:03:27 +00:00
if (def.BoolButtons.Contains(key))
{
2017-05-17 16:53:42 +00:00
_myBoolButtons[key] = trimmed[iterator] != '.';
iterator++;
}
2014-06-29 03:03:27 +00:00
else if (def.FloatControls.Contains(key))
{
var commaIndex = trimmed.Substring(iterator).IndexOf(',');
var temp = trimmed.Substring(iterator, commaIndex);
var val = int.Parse(temp.Trim());
2017-05-17 16:53:42 +00:00
_myFloatControls[key] = val;
iterator += commaIndex + 1;
}
}
}
2014-06-15 14:44:26 +00:00
}
#endregion
2014-07-11 16:26:19 +00:00
public void SetFloat(string buttonName, float value)
{
2017-05-17 16:53:42 +00:00
_myFloatControls[buttonName] = value;
2014-07-11 16:26:19 +00:00
}
public class Bk2ControllerDefinition : ControllerDefinition
{
public Bk2ControllerDefinition()
{
}
public Bk2ControllerDefinition(ControllerDefinition source)
: base(source)
{
}
2017-05-17 16:53:42 +00:00
public List<List<string>> ControlsFromLog { private get; set; } = new List<List<string>>();
public override IEnumerable<IEnumerable<string>> ControlsOrdered
{
get
{
if (ControlsFromLog.Any())
{
return ControlsFromLog;
}
return base.ControlsOrdered;
}
}
}
2014-06-15 14:44:26 +00:00
}
}