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
|
|
|
|
|
{
|
2019-12-07 17:35:48 +00:00
|
|
|
|
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>();
|
2014-06-21 14:33:33 +00:00
|
|
|
|
|
2014-06-21 17:27:35 +00:00
|
|
|
|
public Bk2ControllerAdapter()
|
2017-05-17 16:53:42 +00:00
|
|
|
|
{
|
2014-06-21 17:27:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-21 14:33:33 +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
|
2017-04-14 19:59:01 +00:00
|
|
|
|
.Select(group => group.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries).ToList())
|
2014-06-29 03:03:27 +00:00
|
|
|
|
.ToList();
|
2014-06-21 14:33:33 +00:00
|
|
|
|
|
|
|
|
|
_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];
|
2014-07-10 20:40:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-05-17 16:53:42 +00:00
|
|
|
|
if (_myBoolButtons.ContainsKey(button))
|
2014-07-10 20:40:50 +00:00
|
|
|
|
{
|
2017-05-17 16:53:42 +00:00
|
|
|
|
_myBoolButtons[button] = value;
|
2014-07-10 20:40:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-15 14:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-14 20:12:16 +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
|
|
|
|
|
|
2014-06-21 14:33:33 +00:00
|
|
|
|
private Bk2ControllerDefinition _type = new Bk2ControllerDefinition();
|
|
|
|
|
|
2016-12-12 18:30:32 +00:00
|
|
|
|
public ControllerDefinition Definition
|
2014-06-21 14:33:33 +00:00
|
|
|
|
{
|
2019-11-01 00:24:57 +00:00
|
|
|
|
get => _type;
|
2014-06-21 14:33:33 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_type = new Bk2ControllerDefinition(value);
|
|
|
|
|
SetLogOverride();
|
2019-12-07 17:35:48 +00:00
|
|
|
|
|
|
|
|
|
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-21 14:33:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-15 14:44:26 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// latches one player from the source
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void LatchPlayerFromSource(IController playerSource, int playerNum)
|
|
|
|
|
{
|
2016-12-12 18:30:32 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-14 18:42:15 +00:00
|
|
|
|
var val = playerSource.IsPressed(button);
|
2017-05-17 16:53:42 +00:00
|
|
|
|
_myBoolButtons[button] = val;
|
2014-06-15 14:44:26 +00:00
|
|
|
|
}
|
2015-01-25 19:21:50 +00:00
|
|
|
|
|
2016-12-12 18:30:32 +00:00
|
|
|
|
foreach (var button in Definition.FloatControls)
|
2015-01-25 19:21:50 +00:00
|
|
|
|
{
|
|
|
|
|
var bnp = ButtonNameParser.Parse(button);
|
|
|
|
|
|
2017-05-18 16:36:38 +00:00
|
|
|
|
if (bnp?.PlayerNum != playerNum)
|
2015-01-25 19:21:50 +00:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var val = playerSource.GetFloat(button);
|
|
|
|
|
|
2017-05-17 16:53:42 +00:00
|
|
|
|
_myFloatControls[button] = val;
|
2015-01-25 19:21:50 +00:00
|
|
|
|
}
|
2014-06-15 14:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// latches all buttons from the provided source
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void LatchFromSource(IController source)
|
|
|
|
|
{
|
2016-12-12 18:30:32 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-12-12 18:30:32 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-12 14:28:54 +00:00
|
|
|
|
/// <summary>
|
2017-05-18 16:36:38 +00:00
|
|
|
|
/// latches sticky buttons from <see cref="Global.AutofireStickyXORAdapter"/>
|
2017-03-12 14:28:54 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void LatchSticky()
|
|
|
|
|
{
|
|
|
|
|
foreach (var button in Definition.BoolButtons)
|
|
|
|
|
{
|
2017-05-17 16:53:42 +00:00
|
|
|
|
_myBoolButtons[button] = Global.AutofireStickyXORAdapter.IsSticky(button);
|
2017-03-12 14:28:54 +00:00
|
|
|
|
}
|
2018-03-08 18:15:18 +00:00
|
|
|
|
|
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++)
|
2018-03-08 18:15:18 +00:00
|
|
|
|
{
|
2019-06-16 07:40:57 +00:00
|
|
|
|
_myFloatControls[Definition.FloatControls[i]] = Definition.FloatRanges[i].Mid;
|
2018-03-08 18:15:18 +00:00
|
|
|
|
}
|
2017-03-12 14:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 14:44:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// latches all buttons from the supplied mnemonic string
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetControllersAsMnemonic(string mnemonic)
|
|
|
|
|
{
|
2014-06-17 00:33:33 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(mnemonic))
|
2014-06-16 01:39:36 +00:00
|
|
|
|
{
|
2017-05-10 11:45:23 +00:00
|
|
|
|
var trimmed = mnemonic.Replace("|", "");
|
2014-06-21 11:56:30 +00:00
|
|
|
|
var iterator = 0;
|
2014-06-17 00:33:33 +00:00
|
|
|
|
|
2019-12-07 17:35:48 +00:00
|
|
|
|
foreach (var key in _controlsOrdered)
|
2014-06-17 00:33:33 +00:00
|
|
|
|
{
|
2019-12-07 17:35:48 +00:00
|
|
|
|
if (key.IsBool)
|
2014-06-21 11:56:30 +00:00
|
|
|
|
{
|
2019-12-07 17:35:48 +00:00
|
|
|
|
_myBoolButtons[key.Name] = trimmed[iterator] != '.';
|
2014-06-21 11:56:30 +00:00
|
|
|
|
iterator++;
|
|
|
|
|
}
|
2019-12-07 17:35:48 +00:00
|
|
|
|
else if (key.IsFloat)
|
2014-06-21 11:56:30 +00:00
|
|
|
|
{
|
2018-04-28 14:48:24 +00:00
|
|
|
|
var commaIndex = trimmed.Substring(iterator).IndexOf(',');
|
|
|
|
|
var temp = trimmed.Substring(iterator, commaIndex);
|
2014-06-28 02:11:19 +00:00
|
|
|
|
var val = int.Parse(temp.Trim());
|
2019-12-07 17:35:48 +00:00
|
|
|
|
_myFloatControls[key.Name] = val;
|
2014-06-21 18:25:16 +00:00
|
|
|
|
|
2018-04-28 14:48:24 +00:00
|
|
|
|
iterator += commaIndex + 1;
|
2014-06-21 11:56:30 +00:00
|
|
|
|
}
|
2014-06-17 00:33:33 +00:00
|
|
|
|
}
|
2014-06-16 01:39:36 +00:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2014-06-21 14:33:33 +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>>();
|
2014-06-21 14:33:33 +00:00
|
|
|
|
|
2019-11-01 00:24:57 +00:00
|
|
|
|
public override IEnumerable<IEnumerable<string>> ControlsOrdered =>
|
|
|
|
|
ControlsFromLog.Any()
|
|
|
|
|
? ControlsFromLog
|
|
|
|
|
: base.ControlsOrdered;
|
2014-06-21 14:33:33 +00:00
|
|
|
|
}
|
2014-06-15 14:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|