separate the logic of creating an inputlog from generate on screen input display. Some duplicated code by doing this but I'll take that over tangled code, and more importantly, this separates the need of having an instaniated movie in order to display input

This commit is contained in:
adelikat 2020-05-23 09:27:47 -05:00
parent c1cd1b9e0f
commit 049a79c996
6 changed files with 153 additions and 57 deletions

View File

@ -0,0 +1,69 @@
using System.Linq;
using System.Text;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
public interface IInputDisplayGenerator
{
/// <summary>
/// Generates a display friendly version of the input log entry
/// </summary>
string Generate();
}
/// <summary>
/// An implementation of <see cref="IInputDisplayGenerator"/> that
/// uses .bk2 mnemonics as the basis for display
/// </summary>
public class Bk2InputDisplayGenerator
{
private readonly string _systemId;
private readonly IController _source;
public Bk2InputDisplayGenerator(string systemId, IController source)
{
_systemId = systemId;
_source = source;
}
public string Generate()
{
var sb = new StringBuilder();
foreach (var group in _source.Definition.ControlsOrdered)
{
if (group.Any())
{
foreach (var button in group)
{
if (_source.Definition.AxisControls.Contains(button))
{
int i = _source.Definition.AxisControls.IndexOf(button);
var mid = _source.Definition.AxisRanges[i].Mid;
var val = (int)_source.AxisValue(button);
if (val == mid)
{
sb.Append(" ");
}
else
{
sb.Append(val.ToString().PadLeft(5, ' ')).Append(',');
}
}
else if (_source.Definition.BoolButtons.Contains(button))
{
sb.Append(_source.IsPressed(button)
? Bk2MnemonicLookup.Lookup(button, _systemId)
: ' ');
}
}
}
}
return sb.ToString();
}
}
}

View File

@ -17,8 +17,6 @@ namespace BizHawk.Client.Common
_source = source;
}
public string GenerateInputDisplay() => CreateLogEntry(forInputDisplay: true);
public bool IsEmpty => EmptyEntry == GenerateLogEntry();
public string EmptyEntry => CreateLogEntry(createEmpty: true);
@ -63,14 +61,11 @@ namespace BizHawk.Client.Common
return dict;
}
private string CreateLogEntry(bool createEmpty = false, bool forInputDisplay = false)
private string CreateLogEntry(bool createEmpty = false)
{
var sb = new StringBuilder();
if (!forInputDisplay)
{
sb.Append('|');
}
sb.Append('|');
foreach (var group in _source.Definition.ControlsOrdered)
{
@ -93,14 +88,7 @@ namespace BizHawk.Client.Common
val = (int)_source.AxisValue(button);
}
if (forInputDisplay && val == mid)
{
sb.Append(" ");
}
else
{
sb.Append(val.ToString().PadLeft(5, ' ')).Append(',');
}
sb.Append(val.ToString().PadLeft(5, ' ')).Append(',');
}
else if (_source.Definition.BoolButtons.Contains(button))
{
@ -112,15 +100,12 @@ namespace BizHawk.Client.Common
{
sb.Append(_source.IsPressed(button)
? Bk2MnemonicLookup.Lookup(button, _systemId)
: forInputDisplay ? ' ' : '.');
: '.');
}
}
}
if (!forInputDisplay)
{
sb.Append('|');
}
sb.Append('|');
}
}

View File

@ -25,11 +25,6 @@ namespace BizHawk.Client.Common
/// </summary>
IDictionary<string, string> Map();
/// <summary>
/// Generates a display friendly version of the input log entry
/// </summary>
string GenerateInputDisplay();
/// <summary>
/// Gets a value indicating whether or not the current controller state is "empty"
/// </summary>

View File

@ -224,7 +224,7 @@ namespace BizHawk.Client.EmuHawk
private string MakeStringFor(IController controller)
{
return Global.MovieSession.Movie.LogGeneratorInstance(controller).GenerateInputDisplay();
return new Bk2InputDisplayGenerator(Global.Emulator.SystemId, controller).Generate();
}
public string MakeIntersectImmediatePrevious()

View File

@ -0,0 +1,72 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Tests.Client.Common.Display
{
[TestClass]
public class InputDisplayTests
{
private const int MidValue = 100;
private SimpleController _boolController = null!;
private SimpleController _floatController = null!;
[TestInitialize]
public void Initializer()
{
_boolController = new SimpleController
{
Definition = new ControllerDefinition { BoolButtons = { "A" } }
};
_floatController = new SimpleController
{
Definition = new ControllerDefinition
{
AxisControls = { "StickX", "StickY" },
AxisRanges =
{
new ControllerDefinition.AxisRange(0, MidValue, 200),
new ControllerDefinition.AxisRange(0, MidValue, 200)
}
}
};
}
[TestMethod]
public void Generate_BoolPressed_GeneratesMnemonic()
{
_boolController["A"] = true;
var displayGenerator = new Bk2InputDisplayGenerator("NES", _boolController);
var actual = displayGenerator.Generate();
Assert.AreEqual("A", actual);
}
[TestMethod]
public void Generate_BoolUnPressed_GeneratesSpace()
{
_boolController["A"] = false;
var displayGenerator = new Bk2InputDisplayGenerator("NES", _boolController);
var actual = displayGenerator.Generate();
Assert.AreEqual(" ", actual);
}
[TestMethod]
public void Generate_Floats()
{
var displayGenerator = new Bk2InputDisplayGenerator("NES", _floatController);
var actual = displayGenerator.Generate();
Assert.AreEqual(" 0, 0,", actual);
}
[TestMethod]
public void Generate_MidRangeDisplaysEmpty()
{
_floatController.AcceptNewAxes(("StickX", MidValue));
var displayGenerator = new Bk2InputDisplayGenerator("NES", _floatController);
var actual = displayGenerator.Generate();
Assert.AreEqual(" 0,", actual);
}
}
}

View File

@ -9,9 +9,7 @@ namespace BizHawk.Common.Tests.Client.Common.Movie
public class LogGeneratorTests
{
private SimpleController _boolController = null!;
private Bk2LogEntryGenerator _lg = null!;
private SimpleController _floatController = null!;
private Bk2LogEntryGenerator _floatLg = null!;
[TestInitialize]
public void Initializer()
@ -33,9 +31,6 @@ namespace BizHawk.Common.Tests.Client.Common.Movie
}
}
};
_lg = new Bk2LogEntryGenerator("NES", _boolController);
_floatLg = new Bk2LogEntryGenerator("NES", _floatController);
}
[TestMethod]
@ -59,7 +54,8 @@ namespace BizHawk.Common.Tests.Client.Common.Movie
public void GenerateLogEntry_BoolPressed_GeneratesMnemonic()
{
_boolController["A"] = true;
var actual = _lg.GenerateLogEntry();
var lg = new Bk2LogEntryGenerator("NES", _boolController);
var actual = lg.GenerateLogEntry();
Assert.AreEqual("|A|", actual);
}
@ -67,38 +63,17 @@ namespace BizHawk.Common.Tests.Client.Common.Movie
public void GenerateLogEntry_BoolUnPressed_GeneratesPeriod()
{
_boolController["A"] = false;
var actual = _lg.GenerateLogEntry();
var lg = new Bk2LogEntryGenerator("NES", _boolController);
var actual = lg.GenerateLogEntry();
Assert.AreEqual("|.|", actual);
}
[TestMethod]
public void GenerateLogEntry_Floats()
{
var actual = _floatLg.GenerateLogEntry();
var lg = new Bk2LogEntryGenerator("NES", _floatController);
var actual = lg.GenerateLogEntry();
Assert.AreEqual("| 0, 0,|", actual);
}
[TestMethod]
public void GenerateInputDisplay_BoolPressed_GeneratesMnemonic()
{
_boolController["A"] = true;
var actual = _lg.GenerateInputDisplay();
Assert.AreEqual("A", actual);
}
[TestMethod]
public void GenerateInputDisplay_BoolUnPressed_GeneratesSpace()
{
_boolController["A"] = false;
var actual = _lg.GenerateInputDisplay();
Assert.AreEqual(" ", actual);
}
[TestMethod]
public void GenerateInputDisplay_Floats()
{
var actual = _floatLg.GenerateInputDisplay();
Assert.AreEqual(" 0, 0,", actual);
}
}
}