BizHawk/BizHawk.Emulation.Common/ControllerDefinitionMerger.cs

101 lines
2.4 KiB
C#
Raw Normal View History

2017-04-15 19:40:34 +00:00
using System;
using System.Collections.Generic;
namespace BizHawk.Emulation.Common
{
// the idea here is that various connected peripherals have their controls all merged
// into one definition, including logic to unmerge the data back so each one can work
// with it without knowing what else is connected
public static class ControllerDefinitionMerger
{
private static string Allocate(string input, ref int plr, ref int plrnext)
{
int offset = int.Parse(input.Substring(0, 1));
int currplr = plr + offset;
if (currplr >= plrnext)
2017-04-24 12:41:55 +00:00
{
2017-04-15 19:40:34 +00:00
plrnext = currplr + 1;
2017-04-24 12:41:55 +00:00
}
return $"P{currplr} {input.Substring(1)}";
2017-04-15 19:40:34 +00:00
}
/// <summary>
/// merge some controller definitions for different ports, and such. i promise to fully document this tomorrow
/// </summary>
2017-04-24 12:41:55 +00:00
public static ControllerDefinition GetMerged(IEnumerable<ControllerDefinition> controllers, out List<ControlDefUnMerger> unmergers)
2017-04-15 19:40:34 +00:00
{
ControllerDefinition ret = new ControllerDefinition();
2017-04-24 12:41:55 +00:00
unmergers = new List<ControlDefUnMerger>();
2017-04-15 19:40:34 +00:00
int plr = 1;
int plrnext = 1;
2017-04-24 12:41:55 +00:00
foreach (var def in controllers)
2017-04-15 19:40:34 +00:00
{
Dictionary<string, string> remaps = new Dictionary<string, string>();
foreach (string s in def.BoolButtons)
{
string r = Allocate(s, ref plr, ref plrnext);
ret.BoolButtons.Add(r);
remaps[s] = r;
}
2017-04-24 12:41:55 +00:00
2017-04-15 19:40:34 +00:00
foreach (string s in def.FloatControls)
{
string r = Allocate(s, ref plr, ref plrnext);
ret.FloatControls.Add(r);
remaps[s] = r;
}
2017-04-24 12:41:55 +00:00
2017-04-15 19:40:34 +00:00
ret.FloatRanges.AddRange(def.FloatRanges);
plr = plrnext;
2017-04-24 12:41:55 +00:00
unmergers.Add(new ControlDefUnMerger(remaps));
2017-04-15 19:40:34 +00:00
}
2017-04-24 12:41:55 +00:00
2017-04-15 19:40:34 +00:00
return ret;
}
}
public class ControlDefUnMerger
{
2017-04-27 17:07:34 +00:00
private readonly Dictionary<string, string> _remaps;
2017-04-15 19:40:34 +00:00
2017-04-24 12:41:55 +00:00
public ControlDefUnMerger(Dictionary<string, string> remaps)
2017-04-15 19:40:34 +00:00
{
2017-04-27 17:07:34 +00:00
_remaps = remaps;
2017-04-15 19:40:34 +00:00
}
private class DummyController : IController
{
2017-04-27 17:07:34 +00:00
private readonly IController _src;
private readonly Dictionary<string, string> _remaps;
2017-04-24 12:41:55 +00:00
2017-04-15 19:40:34 +00:00
public DummyController(IController src, Dictionary<string, string> remaps)
{
2017-04-27 17:07:34 +00:00
_src = src;
_remaps = remaps;
2017-04-15 19:40:34 +00:00
}
2017-04-27 17:07:34 +00:00
public ControllerDefinition Definition
{
get { throw new NotImplementedException(); }
}
2017-04-15 19:40:34 +00:00
public bool IsPressed(string button)
{
2017-04-27 17:07:34 +00:00
return _src.IsPressed(_remaps[button]);
2017-04-15 19:40:34 +00:00
}
public float GetFloat(string name)
{
2017-04-27 17:07:34 +00:00
return _src.GetFloat(_remaps[name]);
2017-04-15 19:40:34 +00:00
}
}
public IController UnMerge(IController c)
{
2017-04-27 17:07:34 +00:00
return new DummyController(c, _remaps);
2017-04-15 19:40:34 +00:00
}
}
}