AutofireController - pass in an instance of an emulator rather than use Global.Emulator

This commit is contained in:
adelikat 2015-02-22 18:02:56 +00:00
parent 7e763408e2
commit 7e92f38eb3
3 changed files with 19 additions and 16 deletions

View File

@ -217,6 +217,16 @@ namespace BizHawk.Client.Common
public class AutofireController : IController public class AutofireController : IController
{ {
public AutofireController(ControllerDefinition definition, IEmulator emulator)
{
On = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn;
Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff;
_type = definition;
_emulator = emulator;
}
private readonly IEmulator _emulator;
private readonly ControllerDefinition _type; private readonly ControllerDefinition _type;
private readonly WorkingDictionary<string, List<string>> _bindings = new WorkingDictionary<string, List<string>>(); private readonly WorkingDictionary<string, List<string>> _bindings = new WorkingDictionary<string, List<string>>();
private readonly WorkingDictionary<string, bool> _buttons = new WorkingDictionary<string, bool>(); private readonly WorkingDictionary<string, bool> _buttons = new WorkingDictionary<string, bool>();
@ -228,20 +238,13 @@ namespace BizHawk.Client.Common
public int On { get; set; } public int On { get; set; }
public int Off { get; set; } public int Off { get; set; }
public AutofireController(ControllerDefinition definition)
{
On = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn;
Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff;
_type = definition;
}
public ControllerDefinition Type { get { return _type; } } public ControllerDefinition Type { get { return _type; } }
public bool this[string button] { get { return IsPressed(button); } } public bool this[string button] { get { return IsPressed(button); } }
public bool IsPressed(string button) public bool IsPressed(string button)
{ {
if (_autofire) if (_autofire)
{ {
var a = (Global.Emulator.Frame - _buttonStarts[button]) % (On + Off); var a = (_emulator.Frame - _buttonStarts[button]) % (On + Off);
return a < On && _buttons[button]; return a < On && _buttons[button];
} }
@ -273,7 +276,7 @@ namespace BizHawk.Client.Common
{ {
if (_buttons[kvp.Key] == false && controller[bound_button]) if (_buttons[kvp.Key] == false && controller[bound_button])
{ {
_buttonStarts[kvp.Key] = Global.Emulator.Frame; _buttonStarts[kvp.Key] = _emulator.Frame;
} }
} }
} }
@ -311,7 +314,7 @@ namespace BizHawk.Client.Common
public void BindMulti(string button, string controlString) public void BindMulti(string button, string controlString)
{ {
if (!String.IsNullOrEmpty(controlString)) if (!string.IsNullOrEmpty(controlString))
{ {
var controlbindings = controlString.Split(','); var controlbindings = controlString.Split(',');
foreach (var control in controlbindings) foreach (var control in controlbindings)

View File

@ -40,12 +40,12 @@ namespace BizHawk.Client.Common
var def = Global.Emulator.ControllerDefinition; var def = Global.Emulator.ControllerDefinition;
Global.ActiveController = BindToDefinition(def, Global.Config.AllTrollers, Global.Config.AllTrollersAnalog); Global.ActiveController = BindToDefinition(def, Global.Config.AllTrollers, Global.Config.AllTrollersAnalog);
Global.AutoFireController = BindToDefinitionAF(def, Global.Config.AllTrollersAutoFire); Global.AutoFireController = BindToDefinitionAF(def, Global.Emulator, Global.Config.AllTrollersAutoFire);
// allow propogating controls that are in the current controller definition but not in the prebaked one // allow propogating controls that are in the current controller definition but not in the prebaked one
// these two lines shouldn't be required anymore under the new system? // these two lines shouldn't be required anymore under the new system?
Global.ActiveController.ForceType(new ControllerDefinition(Global.Emulator.ControllerDefinition)); Global.ActiveController.ForceType(new ControllerDefinition(def));
Global.ClickyVirtualPadController.Type = new ControllerDefinition(Global.Emulator.ControllerDefinition); Global.ClickyVirtualPadController.Type = new ControllerDefinition(def);
RewireInputChain(); RewireInputChain();
} }
@ -81,9 +81,9 @@ namespace BizHawk.Client.Common
return ret; return ret;
} }
private static AutofireController BindToDefinitionAF(ControllerDefinition def, IDictionary<string, Dictionary<string, string>> allbinds) private static AutofireController BindToDefinitionAF(ControllerDefinition def, IEmulator emulator, IDictionary<string, Dictionary<string, string>> allbinds)
{ {
var ret = new AutofireController(def); var ret = new AutofireController(def, emulator);
Dictionary<string, string> binds; Dictionary<string, string> binds;
if (allbinds.TryGetValue(def.Name, out binds)) if (allbinds.TryGetValue(def.Name, out binds))
{ {

View File

@ -1605,7 +1605,7 @@ namespace BizHawk.Client.EmuHawk
Global.ClientControls = controls; Global.ClientControls = controls;
Global.NullControls = new Controller(NullEmulator.NullController); Global.NullControls = new Controller(NullEmulator.NullController);
Global.AutofireNullControls = new AutofireController(NullEmulator.NullController); Global.AutofireNullControls = new AutofireController(NullEmulator.NullController, Global.Emulator);
} }