Set properties via ctors instead of obj init syntax and cleanup

This commit is contained in:
YoshiRulz 2020-08-16 17:03:38 +10:00
parent bea796fb2d
commit b204d65afe
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
7 changed files with 106 additions and 144 deletions

View File

@ -83,40 +83,49 @@ namespace BizHawk.Client.EmuHawk
PadBox.Controls.Add(controlSchema switch
{
ButtonSchema button => GenVirtualPadButton(button),
SingleAxisSchema singleAxis => new VirtualPadAnalogButton(_inputManager.StickyXorAdapter)
SingleAxisSchema singleAxis => new VirtualPadAnalogButton(
_inputManager.StickyXorAdapter,
singleAxis.Name,
singleAxis.DisplayName,
singleAxis.MinValue,
singleAxis.MaxValue,
singleAxis.Orientation
)
{
Name = singleAxis.Name,
DisplayName = singleAxis.DisplayName,
Location = UIHelper.Scale(singleAxis.Location),
Size = UIHelper.Scale(singleAxis.TargetSize),
MinValue = singleAxis.MinValue,
MaxValue = singleAxis.MaxValue,
Orientation = singleAxis.Orientation
Size = UIHelper.Scale(singleAxis.TargetSize)
},
AnalogSchema analog => new VirtualPadAnalogStick(_inputManager)
AnalogSchema analog => new VirtualPadAnalogStick(
_inputManager,
analog.Name,
analog.SecondaryName,
analog.Spec,
analog.SecondarySpec
)
{
Name = analog.Name,
SecondaryName = analog.SecondaryName,
Location = UIHelper.Scale(analog.Location),
Size = UIHelper.Scale(new Size(180 + 79, 200 + 9)),
RangeX = analog.Spec,
RangeY = analog.SecondarySpec
Size = UIHelper.Scale(new Size(180 + 79, 200 + 9))
},
TargetedPairSchema targetedPair => new VirtualPadTargetScreen(_inputManager.StickyXorAdapter)
TargetedPairSchema targetedPair => new VirtualPadTargetScreen(
_inputManager.StickyXorAdapter,
targetedPair.Name,
targetedPair.SecondaryName,
targetedPair.MaxValue,
targetedPair.MaxValue // TODO split into MaxX and MaxY, and rename VirtualPadTargetScreen.RangeX/RangeY
)
{
Name = targetedPair.Name,
Location = UIHelper.Scale(targetedPair.Location),
TargetSize = targetedPair.TargetSize,
XName = targetedPair.Name,
YName = targetedPair.SecondaryName,
RangeX = targetedPair.MaxValue,
RangeY = targetedPair.MaxValue // TODO split into MaxX and MaxY, and rename VirtualPadTargetScreen.RangeX/RangeY
},
DiscManagerSchema discManager => new VirtualPadDiscManager(_inputManager.StickyXorAdapter, discManager.SecondaryNames) {
Name = discManager.Name,
DiscManagerSchema discManager => new VirtualPadDiscManager(
_inputManager.StickyXorAdapter,
discManager.OwnerEmulator,
discManager.Name,
discManager.SecondaryNames
)
{
Location = UIHelper.Scale(discManager.Location),
Size = UIHelper.Scale(discManager.TargetSize),
OwnerEmulator = discManager.OwnerEmulator
Size = UIHelper.Scale(discManager.TargetSize)
},
_ => throw new InvalidOperationException()
});

View File

@ -67,7 +67,6 @@
this.Controls.Add(this.AnalogTrackBar);
this.Name = "VirtualPadAnalogButton";
this.Size = new System.Drawing.Size(297, 74);
this.Load += new System.EventHandler(this.VirtualPadAnalogButton_Load);
((System.ComponentModel.ISupportInitialize)(this.AnalogTrackBar)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

View File

@ -9,8 +9,6 @@ namespace BizHawk.Client.EmuHawk
public partial class VirtualPadAnalogButton : UserControl, IVirtualPadControl
{
private readonly StickyXorAdapter _stickyXorAdapter;
private string _displayName = "";
private int _maxValue, _minValue;
private bool _programmaticallyChangingValue;
private bool _readonly;
@ -25,10 +23,46 @@ namespace BizHawk.Client.EmuHawk
}
}
public VirtualPadAnalogButton(StickyXorAdapter stickyXorAdapter)
public VirtualPadAnalogButton(
StickyXorAdapter stickyXorAdapter,
string name,
string displayName,
int minValue,
int maxValue,
Orientation orientation)
{
_stickyXorAdapter = stickyXorAdapter;
Name = name;
InitializeComponent();
// AnalogTrackBar, DisplayNameLabel, and ValueLabel are now assigned
var trackbarWidth = Size.Width - 15;
int trackbarHeight;
if ((AnalogTrackBar.Orientation = orientation) == Orientation.Vertical)
{
AnalogTrackBar.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
trackbarHeight = Size.Height - 30;
ValueLabel.Top = Size.Height / 2;
}
else
{
AnalogTrackBar.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
trackbarHeight = Size.Height - 15;
}
AnalogTrackBar.Size = new Size(trackbarWidth, trackbarHeight);
AnalogTrackBar.Minimum = minValue;
AnalogTrackBar.Maximum = maxValue;
// try to base it on the width, lets make a tick every 10 pixels at the minimum
// yo none of this makes any sense --yoshi
var range = maxValue - minValue + 1;
var canDoTicks = Math.Min(Math.Max(2, trackbarWidth / 10), range);
AnalogTrackBar.TickFrequency = range / Math.Max(1, canDoTicks);
DisplayNameLabel.Text = displayName ?? string.Empty;
ValueLabel.Text = AnalogTrackBar.Value.ToString();
}
public void UpdateValues()
@ -82,105 +116,6 @@ namespace BizHawk.Client.EmuHawk
}
}
private void VirtualPadAnalogButton_Load(object sender, EventArgs e)
{
DisplayNameLabel.Text = DisplayName;
ValueLabel.Text = AnalogTrackBar.Value.ToString();
}
public string DisplayName
{
get => _displayName;
set
{
_displayName = value ?? "";
if (DisplayNameLabel != null)
{
DisplayNameLabel.Text = _displayName;
}
}
}
public int MaxValue
{
get => _maxValue;
set
{
_maxValue = value;
if (AnalogTrackBar != null)
{
AnalogTrackBar.Maximum = _maxValue;
UpdateTickFrequency();
}
}
}
public int MinValue
{
get => _minValue;
set
{
_minValue = value;
if (AnalogTrackBar != null)
{
AnalogTrackBar.Minimum = _minValue;
UpdateTickFrequency();
}
}
}
public Orientation Orientation
{
get => AnalogTrackBar.Orientation;
set
{
AnalogTrackBar.Orientation = value;
if (value == Orientation.Horizontal)
{
AnalogTrackBar.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
AnalogTrackBar.Size = new Size(Size.Width - 15, Size.Height - 15);
}
else if (value == Orientation.Vertical)
{
AnalogTrackBar.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
AnalogTrackBar.Size = new Size(Size.Width - 15, Size.Height - 30);
ValueLabel.Top = Size.Height / 2;
}
}
}
private void UpdateTickFrequency()
{
if (AnalogTrackBar == null)
{
return;
}
// try to base it on the width, lets make a tick every 10 pixels at the minimum
int canDoTicks = AnalogTrackBar.Width / 10;
if (canDoTicks < 2)
{
canDoTicks = 2;
}
int range = _maxValue - _minValue + 1;
if (range < canDoTicks)
{
canDoTicks = range;
}
if (canDoTicks <= 0)
{
canDoTicks = 1;
}
AnalogTrackBar.TickFrequency = range / canDoTicks;
}
public int CurrentValue
{
get => AnalogTrackBar.Value;

View File

@ -170,7 +170,6 @@
this.Controls.Add(this.AnalogStick);
this.Name = "VirtualPadAnalogStick";
this.Size = new System.Drawing.Size(253, 172);
this.Load += new System.EventHandler(this.VirtualPadAnalogStick_Load);
((System.ComponentModel.ISupportInitialize)(this.ManualX)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ManualY)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.MaxXNumeric)).EndInit();

View File

@ -20,9 +20,18 @@ namespace BizHawk.Client.EmuHawk
private bool _updatingFromXY;
public VirtualPadAnalogStick(InputManager inputManager)
public VirtualPadAnalogStick(
InputManager inputManager,
string name,
string secondaryName,
AxisSpec rangeX,
AxisSpec rangeY)
{
_inputManager = inputManager;
Name = name;
RangeX = rangeX;
RangeY = rangeY;
InitializeComponent();
AnalogStick.ClearCallback = ClearCallback;
@ -30,21 +39,12 @@ namespace BizHawk.Client.EmuHawk
ManualY.ValueChanged += ManualXY_ValueChanged;
manualR.ValueChanged += PolarNumeric_Changed;
manualTheta.ValueChanged += PolarNumeric_Changed;
}
public AxisSpec RangeX { get; set; }
public AxisSpec RangeY { get; set; }
public string? SecondaryName { get; set; }
private void VirtualPadAnalogStick_Load(object sender, EventArgs e)
{
AnalogStick.Init(
_inputManager.StickyXorAdapter,
Name,
RangeX,
!string.IsNullOrEmpty(SecondaryName) ? SecondaryName : Name.Replace("X", "Y"),
!string.IsNullOrEmpty(secondaryName) ? secondaryName : Name.Replace("X", "Y"),
RangeY
);
@ -62,6 +62,10 @@ namespace BizHawk.Client.EmuHawk
MaxYNumeric.Value = 100;
}
private readonly AxisSpec RangeX;
private readonly AxisSpec RangeY;
public void UpdateValues()
{
// Nothing to do

View File

@ -11,15 +11,21 @@ namespace BizHawk.Client.EmuHawk
{
private readonly StickyXorAdapter _stickyXorAdapter;
public VirtualPadDiscManager(StickyXorAdapter stickyXorAdapter, IReadOnlyList<string> buttonNames)
public VirtualPadDiscManager(
StickyXorAdapter stickyXorAdapter,
IEmulator ownerEmulator,
string name,
IReadOnlyList<string> buttonNames)
{
_stickyXorAdapter = stickyXorAdapter;
Name = name;
InitializeComponent();
btnOpen.Name = buttonNames[0];
btnClose.Name = buttonNames[1];
_discSelectName = buttonNames[2];
UpdateCoreAssociation();
OwnerEmulator = ownerEmulator;
}
private readonly string _discSelectName;

View File

@ -21,9 +21,19 @@ namespace BizHawk.Client.EmuHawk
private int? _overrideX;
private int? _overrideY;
public VirtualPadTargetScreen(StickyXorAdapter stickyXorAdapter)
public VirtualPadTargetScreen(
StickyXorAdapter stickyXorAdapter,
string nameX,
string nameY,
int rangeX,
int rangeY)
{
_stickyXorAdapter = stickyXorAdapter;
Name = XName = nameX;
YName = nameY;
RangeX = rangeX;
RangeY = rangeY;
InitializeComponent();
}
@ -112,8 +122,8 @@ namespace BizHawk.Client.EmuHawk
}
// These are the value that a maximum x or y actually represent, used to translate from control X,Y to values the core expects
public int RangeX { get; set; }
public int RangeY { get; set; }
private readonly int RangeX;
private readonly int RangeY;
public float MultiplierX
{
@ -156,8 +166,8 @@ namespace BizHawk.Client.EmuHawk
Refresh();
}
public string XName { get; set; }
public string YName { get; set; }
private readonly string XName;
private readonly string YName;
public int X
{