BizHawk/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/VirtualPadAnalogButton.cs

130 lines
2.2 KiB
C#

using System;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
{
public partial class VirtualPadAnalogButton : UserControl, IVirtualPadControl
{
private string _displayName = string.Empty;
private int _maxValue;
private bool _programmaticallyChangingValue;
private bool _readonly;
public VirtualPadAnalogButton()
{
InitializeComponent();
}
#region IVirtualPadControl Implementation
public void Clear()
{
// Nothing to do
}
public void Set(IController controller)
{
var newVal = (int)controller.GetFloat(Name);
var changed = AnalogTrackBar.Value != newVal;
if (changed)
{
CurrentValue = newVal;
}
}
public bool ReadOnly
{
get
{
return _readonly;
}
set
{
if (_readonly != value)
{
AnalogTrackBar.Enabled =
ValueLabel.Enabled =
!value;
_readonly = value;
Refresh();
}
}
}
#endregion
private void VirtualPadAnalogButton_Load(object sender, EventArgs e)
{
DisplayNameLabel.Text = DisplayName;
ValueLabel.Text = AnalogTrackBar.Value.ToString();
}
public string DisplayName
{
get
{
return _displayName;
}
set
{
_displayName = value ?? string.Empty;
if (DisplayNameLabel != null)
{
DisplayNameLabel.Text = _displayName;
}
}
}
public int MaxValue
{
get
{
return _maxValue;
}
set
{
_maxValue = value;
if (AnalogTrackBar != null)
{
AnalogTrackBar.Maximum = _maxValue;
AnalogTrackBar.TickFrequency = _maxValue / 10;
}
}
}
public int CurrentValue
{
get
{
return AnalogTrackBar.Value;
}
set
{
_programmaticallyChangingValue = true;
AnalogTrackBar.Value = value;
ValueLabel.Text = AnalogTrackBar.Value.ToString();
_programmaticallyChangingValue = false;
}
}
private void AnalogTrackBar_ValueChanged(object sender, EventArgs e)
{
if (!_programmaticallyChangingValue)
{
CurrentValue = AnalogTrackBar.Value;
Global.StickyXORAdapter.SetFloat(Name, AnalogTrackBar.Value);
}
}
}
}