Rip out UpdateControls() method from IController, also some code cleanup in related files

This commit is contained in:
adelikat 2013-12-07 00:53:06 +00:00
parent 38d6555763
commit 50d4a3307a
14 changed files with 314 additions and 232 deletions

View File

@ -39,7 +39,6 @@ namespace BizHawk.Client.Common
}
public float GetFloat(string name) { return FloatButtons[name]; }
public void UpdateControls(int frame) { }
//look for bindings which are activated by the supplied physical button.
public List<string> SearchBindings(string button)
@ -201,7 +200,6 @@ namespace BizHawk.Client.Common
}
public float GetFloat(string name) { throw new NotImplementedException(); }
public void UpdateControls(int frame) { }
//look for bindings which are activated by the supplied physical button.
public List<string> SearchBindings(string button)

View File

@ -59,7 +59,7 @@ namespace BizHawk.Client.Common
public static UD_LR_ControllerAdapter UD_LR_ControllerAdapter = new UD_LR_ControllerAdapter();
public static AutoFireStickyXORAdapter AutofireStickyXORAdapter = new AutoFireStickyXORAdapter();
public static AutoFireStickyXorAdapter AutofireStickyXORAdapter = new AutoFireStickyXorAdapter();
/// <summary>
/// will OR together two IControllers
@ -69,7 +69,7 @@ namespace BizHawk.Client.Common
/// <summary>
/// provides an opportunity to mutate the player's input in an autohold style
/// </summary>
public static StickyXORAdapter StickyXORAdapter = new StickyXORAdapter();
public static StickyXorAdapter StickyXORAdapter = new StickyXorAdapter();
/// <summary>
/// Forces any controller button to Off, useful for things like Joypad.Set

View File

@ -6,6 +6,8 @@ using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
using System.Linq;
/// <summary>
/// will hold buttons for 1 frame and then release them. (Calling Click() from your button click is what you want to do)
/// TODO - should the duration be controllable?
@ -13,19 +15,29 @@ namespace BizHawk.Client.Common
public class ClickyVirtualPadController : IController
{
public ControllerDefinition Type { get; set; }
public bool this[string button] { get { return IsPressed(button); } }
public float GetFloat(string name) { return 0.0f; } //TODO
public void UpdateControls(int frame) { }
public bool this[string button]
{
get { return IsPressed(button); }
}
public float GetFloat(string name)
{
return 0.0f;
}
// TODO
public bool IsPressed(string button)
{
return Pressed.Contains(button);
return _pressed.Contains(button);
}
/// <summary>
/// call this once per frame to do the timekeeping for the hold and release
/// </summary>
public void FrameTick()
{
Pressed.Clear();
_pressed.Clear();
}
/// <summary>
@ -33,40 +45,52 @@ namespace BizHawk.Client.Common
/// </summary>
public void Click(string button)
{
Pressed.Add(button);
_pressed.Add(button);
}
public void Unclick(string button)
{
Pressed.Remove(button);
_pressed.Remove(button);
}
public void Toggle(string button)
{
if (IsPressed(button))
{
Pressed.Remove(button);
_pressed.Remove(button);
}
else
{
Pressed.Add(button);
_pressed.Add(button);
}
}
readonly HashSet<string> Pressed = new HashSet<string>();
private readonly HashSet<string> _pressed = new HashSet<string>();
}
//filters input for things called Up and Down while considering the client's AllowUD_LR option.
//this is a bit gross but it is unclear how to do it more nicely
/// <summary>
/// Filters input for things called Up and Down while considering the client's AllowUD_LR option.
/// This is a bit gross but it is unclear how to do it more nicely
/// </summary>
public class UD_LR_ControllerAdapter : IController
{
public ControllerDefinition Type { get { return Source.Type; } }
public IController Source;
public ControllerDefinition Type
{
get { return Source.Type; }
}
public bool this[string button] { get { return IsPressed(button); } }
// the float format implies no U+D and no L+R no matter what, so just passthru
public float GetFloat(string name) { return Source.GetFloat(name); }
public void UpdateControls(int frame) { }
public bool this[string button]
{
get { return IsPressed(button); }
}
public IController Source { get; set; }
// The float format implies no U+D and no L+R no matter what, so just passthru
public float GetFloat(string name)
{
return Source.GetFloat(name);
}
public bool IsPressed(string button)
{
@ -85,6 +109,7 @@ namespace BizHawk.Client.Common
return false;
}
}
if (button.Contains("Right") && !button.Contains(" C "))
{
prefix = button.GetPrecedingString("Right");
@ -104,10 +129,21 @@ namespace BizHawk.Client.Common
protected WorkingDictionary<string, bool> Buttons = new WorkingDictionary<string, bool>();
protected WorkingDictionary<string, float> Floats = new WorkingDictionary<string, float>();
public virtual bool this[string button] { get { return Buttons[button]; } set { Buttons[button] = value; } }
public virtual bool IsPressed(string button) { return this[button]; }
public float GetFloat(string name) { return Floats[name]; }
public void UpdateControls(int frame) { }
public virtual bool this[string button]
{
get { return Buttons[button]; } set { Buttons[button] = value; }
}
public virtual bool IsPressed(string button)
{
return this[button];
}
public float GetFloat(string name)
{
return Floats[name];
}
public IEnumerable<KeyValuePair<string, bool>> BoolButtons()
{
@ -116,39 +152,46 @@ namespace BizHawk.Client.Common
public virtual void LatchFrom(IController source)
{
foreach (string button in source.Type.BoolButtons)
foreach (var button in source.Type.BoolButtons)
{
Buttons[button] = source[button];
}
}
public void AcceptNewFloats(IEnumerable<Tuple<string, float>> NewValues)
public void AcceptNewFloats(IEnumerable<Tuple<string, float>> newValues)
{
foreach (var sv in NewValues)
foreach (var sv in newValues)
{
Floats[sv.Item1] = sv.Item2;
}
}
}
public class ORAdapter : IController
{
public bool IsPressed(string button) { return this[button]; }
public bool IsPressed(string button)
{
return this[button];
}
// pass floats solely from the original source
// this works in the code because SourceOr is the autofire controller
public float GetFloat(string name) { return Source.GetFloat(name); }
public void UpdateControls(int frame) { }
public IController Source;
public IController SourceOr;
public IController Source { get; set; }
public IController SourceOr { get; set; }
public ControllerDefinition Type { get { return Source.Type; } set { throw new InvalidOperationException(); } }
public bool this[string button]
{
get
{
bool source = Source[button] | SourceOr[button];
return source;
return Source[button] | SourceOr[button];
}
set
{
throw new InvalidOperationException();
}
set { throw new InvalidOperationException(); }
}
}
@ -156,89 +199,114 @@ namespace BizHawk.Client.Common
public class ForceOffAdaptor : IController
{
public bool IsPressed(string button) { return this[button]; }
// what exactly would we want to do here with floats?
// ForceOffAdaptor is only used by lua, and the code there looks like a big mess...
public float GetFloat(string name) { return Source.GetFloat(name); }
public void UpdateControls(int frame) { }
protected HashSet<string> stickySet = new HashSet<string>();
public IController Source;
public IController SourceOr;
public ControllerDefinition Type { get { return Source.Type; } set { throw new InvalidOperationException(); } }
protected HashSet<string> StickySet = new HashSet<string>();
public IController Source { get; set; }
public IController SourceOr { get; set; }
public ControllerDefinition Type
{
get { return Source.Type; }
set { throw new InvalidOperationException(); }
}
public bool this[string button]
{
get
{
if (stickySet.Contains(button))
{
return false;
}
else
{
return Source[button];
}
return !StickySet.Contains(button) && Source[button];
}
set
{
throw new InvalidOperationException();
}
set { throw new InvalidOperationException(); }
}
public void SetSticky(string button, bool isSticky)
{
if (isSticky)
stickySet.Add(button);
else stickySet.Remove(button);
{
this.StickySet.Add(button);
}
else
{
this.StickySet.Remove(button);
}
}
}
public class StickyXORAdapter : IController
public class StickyXorAdapter : IController
{
protected HashSet<string> stickySet = new HashSet<string>();
public IController Source;
public IController Source { get; set; }
public ControllerDefinition Type { get { return Source.Type; } set { throw new InvalidOperationException(); } }
public bool Locked = false; //Pretty much a hack,
public ControllerDefinition Type
{
get { return Source.Type; }
set { throw new InvalidOperationException(); }
}
public bool Locked { get; set; } // Pretty much a hack,
public bool IsPressed(string button) { return this[button]; }
// if SetFloat() is called (typically virtual pads), then that float will entirely override the Source input
// otherwise, the source is passed thru.
readonly WorkingDictionary<string,float?> FloatSet = new WorkingDictionary<string,float?>();
private readonly WorkingDictionary<string,float?> _floatSet = new WorkingDictionary<string,float?>();
public void SetFloat(string name, float? value)
{
if (value.HasValue)
FloatSet[name] = value;
else FloatSet.Remove(name);
{
_floatSet[name] = value;
}
else
{
_floatSet.Remove(name);
}
}
public float GetFloat(string name)
{
return FloatSet[name] ?? Source.GetFloat(name);
return _floatSet[name] ?? Source.GetFloat(name);
}
public void ClearStickyFloats()
{
FloatSet.Clear();
_floatSet.Clear();
}
public void UpdateControls(int frame) { }
public bool this[string button] {
public bool this[string button]
{
get
{
bool source = Source[button];
if (source)
{
}
var source = Source[button];
source ^= stickySet.Contains(button);
return source;
}
set { throw new InvalidOperationException(); }
set
{
throw new InvalidOperationException();
}
}
public void SetSticky(string button, bool isSticky)
{
if(isSticky)
if (isSticky)
{
stickySet.Add(button);
else stickySet.Remove(button);
}
else
{
stickySet.Remove(button);
}
}
public bool IsSticky(string button)
@ -261,35 +329,33 @@ namespace BizHawk.Client.Common
public void MassToggleStickyState(List<string> buttons)
{
foreach (string button in buttons)
foreach (var button in buttons.Where(button => !_justPressed.Contains(button)))
{
if (!JustPressed.Contains(button))
if (stickySet.Contains(button))
{
if (stickySet.Contains(button))
{
stickySet.Remove(button);
}
else
{
stickySet.Add(button);
}
stickySet.Remove(button);
}
else
{
stickySet.Add(button);
}
}
JustPressed = buttons;
_justPressed = buttons;
}
private List<string> JustPressed = new List<string>();
private List<string> _justPressed = new List<string>();
}
public class AutoFireStickyXORAdapter : IController
public class AutoFireStickyXorAdapter : IController
{
public int On { get; set; }
public int Off { get; set; }
public WorkingDictionary<string, int> buttonStarts = new WorkingDictionary<string, int>();
private readonly HashSet<string> stickySet = new HashSet<string>();
private readonly HashSet<string> _stickySet = new HashSet<string>();
public IController Source;
public IController Source { get; set; }
public void SetOnOffPatternFromConfig()
{
@ -297,23 +363,27 @@ namespace BizHawk.Client.Common
Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff;
}
public AutoFireStickyXORAdapter()
public AutoFireStickyXorAdapter()
{
//On = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn;
//Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff;
// On = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn;
// Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff;
On = 1;
Off = 1;
}
public bool IsPressed(string button)
{
if (stickySet.Contains(button))
if (_stickySet.Contains(button))
{
int a = (Global.Emulator.Frame - buttonStarts[button]) % (On + Off);
var a = (Global.Emulator.Frame - buttonStarts[button]) % (On + Off);
if (a < On)
{
return this[button];
}
else
{
return false;
}
}
else
{
@ -325,15 +395,11 @@ namespace BizHawk.Client.Common
{
get
{
bool source = Source[button];
if (source)
{
}
if (stickySet.Contains(button))
{
var source = Source[button];
int a = (Global.Emulator.Frame - buttonStarts[button]) % (On + Off);
if (_stickySet.Contains(button))
{
var a = (Global.Emulator.Frame - buttonStarts[button]) % (On + Off);
if (a < On)
{
source ^= true;
@ -346,103 +412,134 @@ namespace BizHawk.Client.Common
return source;
}
set { throw new InvalidOperationException(); }
set
{
throw new InvalidOperationException();
}
}
public ControllerDefinition Type { get { return Source.Type; } set { throw new InvalidOperationException(); } }
public bool Locked = false; //Pretty much a hack,
public bool Locked { get; set; } // Pretty much a hack,
// dumb passthrough for floats, because autofire doesn't care about them
public float GetFloat(string name) { return Source.GetFloat(name); }
public void UpdateControls(int frame) { }
public float GetFloat(string name)
{
return Source.GetFloat(name);
}
public void SetSticky(string button, bool isSticky)
{
if (isSticky)
stickySet.Add(button);
else stickySet.Remove(button);
{
this._stickySet.Add(button);
}
else
{
this._stickySet.Remove(button);
}
}
public bool IsSticky(string button)
{
return stickySet.Contains(button);
return this._stickySet.Contains(button);
}
public HashSet<string> CurrentStickies
{
get
{
return stickySet;
return this._stickySet;
}
}
public void ClearStickies()
{
stickySet.Clear();
this._stickySet.Clear();
}
public void MassToggleStickyState(List<string> buttons)
{
foreach (string button in buttons)
foreach (var button in buttons.Where(button => !_justPressed.Contains(button)))
{
if (!JustPressed.Contains(button))
if (_stickySet.Contains(button))
{
if (stickySet.Contains(button))
{
stickySet.Remove(button);
}
else
{
stickySet.Add(button);
}
_stickySet.Remove(button);
}
else
{
_stickySet.Add(button);
}
}
JustPressed = buttons;
_justPressed = buttons;
}
private List<string> JustPressed = new List<string>();
private List<string> _justPressed = new List<string>();
}
/// <summary>
/// just copies source to sink, or returns whatever a NullController would if it is disconnected. useful for immovable hardpoints.
/// Just copies source to sink, or returns whatever a NullController would if it is disconnected. useful for immovable hardpoints.
/// </summary>
public class CopyControllerAdapter : IController
{
public IController Source;
public IController Source { get; set; }
private readonly NullController _null = new NullController();
IController Curr
private IController Curr
{
get
{
if (Source == null) return _null;
else return Source;
if (Source == null)
{
return _null;
}
else
{
return Source;
}
}
}
public ControllerDefinition Type { get { return Curr.Type; } }
public bool this[string button] { get { return Curr[button]; } }
public bool IsPressed(string button) { return Curr.IsPressed(button); }
public float GetFloat(string name) { return Curr.GetFloat(name); }
public void UpdateControls(int frame) { Curr.UpdateControls(frame); }
}
class ButtonNameParser
{
ButtonNameParser()
public ControllerDefinition Type
{
get { return Curr.Type; }
}
public bool this[string button]
{
get { return Curr[button]; }
}
public bool IsPressed(string button)
{
return Curr.IsPressed(button);
}
public float GetFloat(string name)
{
return Curr.GetFloat(name);
}
}
public class ButtonNameParser
{
public static ButtonNameParser Parse(string button)
{
//see if we're being asked for a button that we know how to rewire
string[] parts = button.Split(' ');
if (parts.Length < 2) return null;
if (parts[0][0] != 'P') return null;
// See if we're being asked for a button that we know how to rewire
var parts = button.Split(' ');
if (parts.Length < 2)
{
return null;
}
if (parts[0][0] != 'P')
{
return null;
}
int player;
if (!int.TryParse(parts[0].Substring(1), out player))
{
@ -454,8 +551,8 @@ namespace BizHawk.Client.Common
}
}
public int PlayerNum;
public string ButtonPart;
public int PlayerNum { get; set; }
public string ButtonPart { get; private set; }
public override string ToString()
{
@ -468,30 +565,41 @@ namespace BizHawk.Client.Common
/// </summary>
public class MultitrackRewiringControllerAdapter : IController
{
public IController Source;
public IController Source { get; set; }
public int PlayerSource = 1;
public int PlayerTargetMask = 0;
public ControllerDefinition Type { get { return Source.Type; } }
public bool this[string button] { get { return IsPressed(button); } }
// floats can be player number remapped just like boolbuttons
public float GetFloat(string name) { return Source.GetFloat(RemapButtonName(name)); }
public void UpdateControls(int frame) { Source.UpdateControls(frame); }
string RemapButtonName(string button)
private string RemapButtonName(string button)
{
//do we even have a source?
if (PlayerSource == -1) return button;
// Do we even have a source?
if (PlayerSource == -1)
{
return button;
}
//see if we're being asked for a button that we know how to rewire
ButtonNameParser bnp = ButtonNameParser.Parse(button);
if (bnp == null) return button;
// See if we're being asked for a button that we know how to rewire
var bnp = ButtonNameParser.Parse(button);
if (bnp == null)
{
return button;
}
//ok, this looks like a normal `P1 Button` type thing. we can handle it
//were we supposed to replace this one?
// Ok, this looks like a normal `P1 Button` type thing. we can handle it
// Were we supposed to replace this one?
int foundPlayerMask = (1 << bnp.PlayerNum);
if ((PlayerTargetMask & foundPlayerMask) == 0) return button;
//ok, we were. swap out the source player and then grab his button
if ((PlayerTargetMask & foundPlayerMask) == 0)
{
return button;
}
// Ok, we were. swap out the source player and then grab his button
bnp.PlayerNum = PlayerSource;
return bnp.ToString();
}
@ -501,43 +609,4 @@ namespace BizHawk.Client.Common
return Source.IsPressed(RemapButtonName(button));
}
}
//not being used..
///// <summary>
///// adapts an IController to force some buttons to a different state.
///// unforced button states will flow through to the adaptee
///// </summary>
//public class ForceControllerAdapter : IController
//{
// public IController Controller;
// public Dictionary<string, bool> Forces = new Dictionary<string, bool>();
// public void Clear()
// {
// Forces.Clear();
// }
// public ControllerDefinition Type { get { return Controller.Type; } }
// public bool this[string button] { get { return IsPressed(button); } }
// public bool IsPressed(string button)
// {
// if (Forces.ContainsKey(button))
// return Forces[button];
// else return Controller.IsPressed(button);
// }
// public float GetFloat(string name)
// {
// return Controller.GetFloat(name); //TODO!
// }
// public void UpdateControls(int frame)
// {
// Controller.UpdateControls(frame);
// }
//}
}

View File

@ -654,7 +654,6 @@ namespace BizHawk.Client.Common
public bool this[string button] { get { return MyBoolButtons[button]; } }
public bool IsPressed(string button) { return MyBoolButtons[button]; }
public float GetFloat(string name) { return MyFloatControls[name]; }
public void UpdateControls(int frame) { }
//--------
private readonly WorkingDictionary<string, bool> MyBoolButtons = new WorkingDictionary<string, bool>();

View File

@ -6,7 +6,6 @@
public bool this[string button] { get { return false; } }
public bool IsPressed(string button) { return false; }
public float GetFloat(string name) { return 0f; }
public void UpdateControls(int frame) { }
public void UnpressButton(string button) { }
public void ForceButton(string button) { }

View File

@ -21,56 +21,71 @@ namespace BizHawk.Emulation.Common
{
public readonly float Min;
public readonly float Max;
/// <summary>
/// default position
/// </summary>
public readonly float Mid;
public FloatRange(float Min, float Mid, float Max)
public FloatRange(float min, float mid, float max)
{
this.Min = Min;
this.Mid = Mid;
this.Max = Max;
Min = min;
Mid = mid;
Max = max;
}
// for terse construction
public static implicit operator FloatRange(float[] f)
{
if (f.Length != 3)
{
throw new ArgumentException();
}
return new FloatRange(f[0], f[1], f[2]);
}
}
public string Name;
public List<string> BoolButtons = new List<string>();
public List<string> FloatControls = new List<string>();
public List<FloatRange> FloatRanges = new List<FloatRange>();
/// <summary>
/// copy
/// </summary>
/// <param name="source"></param>
public string Name { get; set; }
public List<string> BoolButtons { get; set; }
public List<string> FloatControls { get; private set; }
public List<FloatRange> FloatRanges { get; private set; }
public ControllerDefinition(ControllerDefinition source)
: this()
{
Name = source.Name;
foreach (var s in source.BoolButtons)
{
BoolButtons.Add(s);
}
foreach (var s in source.FloatControls)
{
FloatControls.Add(s);
}
}
public ControllerDefinition()
{
BoolButtons = new List<string>();
FloatControls = new List<string>();
FloatRanges = new List<FloatRange>();
}
public ControllerDefinition() { }
}
public interface IController
{
ControllerDefinition Type { get; }
//TODO - it is obnoxious for this to be here. must be removed.
// TODO - it is obnoxious for this to be here. must be removed.
bool this[string button] { get; }
//TODO - this can stay but it needs to be changed to go through the float
// TODO - this can stay but it needs to be changed to go through the float
bool IsPressed(string button);
float GetFloat(string name);
//TODO - why does this have a frame argument. must be removed.
void UpdateControls(int frame);
}
}

View File

@ -492,14 +492,17 @@ namespace BizHawk.Emulation.Cores.Calculators
cpu.ExecuteCycles(10000);
cpu.Interrupt = true;
}
Controller.UpdateControls(Frame++);
Frame++;
if (lagged)
{
_lagcount++;
islag = true;
}
else
{
islag = false;
}
}
public void HardReset()

View File

@ -59,7 +59,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public void FrameAdvance(bool render, bool rendersound = true)
{
Controller.UpdateControls(Frame++);
Frame++;
IsLagFrame = true;
if (Controller["Power"])

View File

@ -153,7 +153,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
internal void FrameAdvancePrep()
{
Controller.UpdateControls(Frame++);
Frame++;
// update our local copy of the controller data
CurrentButtons = 0;

View File

@ -243,7 +243,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
HardReset();
}
Controller.UpdateControls(Frame++);
Frame++;
//if (resetSignal)
//Controller.UnpressButton("Reset"); TODO fix this
resetSignal = Controller["Reset"];

View File

@ -772,11 +772,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
{
return buttons[name];
}
public void UpdateControls(int frame)
{
//throw new NotImplementedException();
}
}

View File

@ -250,7 +250,7 @@ namespace BizHawk.Emulation.Cores.PCEngine
{
lagged = true;
CoreComm.DriveLED = false;
Controller.UpdateControls(Frame++);
Frame++;
PSG.BeginFrame(Cpu.TotalExecutedCycles);
Cpu.Debug = CoreComm.Tracer.Enabled;

View File

@ -147,8 +147,7 @@ namespace BizHawk.Emulation.Cores.Sega.Genesis
public void FrameAdvance(bool render, bool rendersound)
{
lagged = true;
Controller.UpdateControls(Frame++);
Frame++;
PSG.BeginFrame(SoundCPU.TotalExecutedCycles);
YM2612.BeginFrame(SoundCPU.TotalExecutedCycles);

View File

@ -240,14 +240,18 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
public void FrameAdvance(bool render, bool rendersound)
{
lagged = true;
Controller.UpdateControls(Frame++);
Frame++;
PSG.BeginFrame(Cpu.TotalExecutedCycles);
Cpu.Debug = CoreComm.Tracer.Enabled;
if (Cpu.Debug && Cpu.Logger == null) // TODO, lets not do this on each frame. But lets refactor CoreComm/CoreComm first
Cpu.Logger = (s) => CoreComm.Tracer.Put(s);
Cpu.Debug = CoreComm.Tracer.Enabled;
if (Cpu.Debug && Cpu.Logger == null) // TODO, lets not do this on each frame. But lets refactor CoreComm/CoreComm first
{
Cpu.Logger = (s) => CoreComm.Tracer.Put(s);
}
if (IsGameGear == false)
{
Cpu.NonMaskableInterrupt = Controller["Pause"];
}
Vdp.ExecFrame(render);
PSG.EndFrame(Cpu.TotalExecutedCycles);