From b204d65afe706cbace1d6a3b6821c21f69928f90 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 16 Aug 2020 17:03:38 +1000 Subject: [PATCH] Set properties via ctors instead of obj init syntax and cleanup --- .../tools/VirtualPads/VirtualPad.cs | 55 ++++--- .../VirtualPadAnalogButton.Designer.cs | 1 - .../controls/VirtualPadAnalogButton.cs | 139 +++++------------- .../VirtualPadAnalogStick.Designer.cs | 1 - .../controls/VirtualPadAnalogStick.cs | 26 ++-- .../controls/VirtualPadDiscManager.cs | 8 +- .../controls/VirtualPadTargetScreen.cs | 20 ++- 7 files changed, 106 insertions(+), 144 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualPad.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualPad.cs index d6b72ed4b6..bbc9c7aaa9 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualPad.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualPad.cs @@ -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() }); diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.Designer.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.Designer.cs index 8a0247d786..9bd2b27e8d 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.Designer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.Designer.cs @@ -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(); diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.cs index e783e7b3ef..036388fb14 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.cs @@ -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; diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.Designer.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.Designer.cs index 9d1c6b2015..322af246d6 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.Designer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.Designer.cs @@ -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(); diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.cs index cd6438c57d..704e4377e6 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogStick.cs @@ -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 diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadDiscManager.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadDiscManager.cs index 1609128b49..d83559bf35 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadDiscManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadDiscManager.cs @@ -11,15 +11,21 @@ namespace BizHawk.Client.EmuHawk { private readonly StickyXorAdapter _stickyXorAdapter; - public VirtualPadDiscManager(StickyXorAdapter stickyXorAdapter, IReadOnlyList buttonNames) + public VirtualPadDiscManager( + StickyXorAdapter stickyXorAdapter, + IEmulator ownerEmulator, + string name, + IReadOnlyList buttonNames) { _stickyXorAdapter = stickyXorAdapter; + Name = name; InitializeComponent(); btnOpen.Name = buttonNames[0]; btnClose.Name = buttonNames[1]; _discSelectName = buttonNames[2]; UpdateCoreAssociation(); + OwnerEmulator = ownerEmulator; } private readonly string _discSelectName; diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadTargetScreen.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadTargetScreen.cs index dc3cf1c0a8..30d31654d8 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadTargetScreen.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadTargetScreen.cs @@ -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 {