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

223 lines
5.2 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
{
private class ControlMap
{
public string Name { get; set; }
public bool IsBool { get; set; }
public bool IsFloat { get; set; }
}
private List<ControlMap> _controlsOrdered = new List<ControlMap>();
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;
}
}
2019-12-07 17:02:17 +00:00
// TODO: get rid of this, add a SetBool() method or something for the set access, replace get with IsPressed
2014-06-15 14:44:26 +00:00
public bool this[string button]
{
2019-12-07 17:02:17 +00:00
get => _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 => _type;
set
{
_type = new Bk2ControllerDefinition(value);
SetLogOverride();
var def = Global.Emulator.ControllerDefinition;
_controlsOrdered = Definition.ControlsOrdered
.SelectMany(c => c)
.Select(c => new ControlMap
{
Name = c,
IsBool = def.BoolButtons.Contains(c),
IsFloat = def.FloatControls.Contains(c)
})
.ToList();
}
}
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);
}
2019-06-16 07:40:57 +00:00
// float controls don't have sticky logic, so latch default value
for (int i = 0; i < Definition.FloatControls.Count; i++)
{
2019-06-16 07:40:57 +00:00
_myFloatControls[Definition.FloatControls[i]] = Definition.FloatRanges[i].Mid;
}
}
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))
{
2017-05-10 11:45:23 +00:00
var trimmed = mnemonic.Replace("|", "");
var iterator = 0;
foreach (var key in _controlsOrdered)
{
if (key.IsBool)
{
_myBoolButtons[key.Name] = trimmed[iterator] != '.';
iterator++;
}
else if (key.IsFloat)
{
var commaIndex = trimmed.Substring(iterator).IndexOf(',');
var temp = trimmed.Substring(iterator, commaIndex);
var val = int.Parse(temp.Trim());
_myFloatControls[key.Name] = 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 =>
ControlsFromLog.Any()
? ControlsFromLog
: base.ControlsOrdered;
}
2014-06-15 14:44:26 +00:00
}
}