using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Mesen.GUI.Config; namespace Mesen.GUI.Forms.Config { public partial class frmInputConfig : BaseConfigForm { private bool _hasCartridgeInput = false; public frmInputConfig() { InitializeComponent(); tlpControllers.Enabled = !InteropEmu.MoviePlaying() && !InteropEmu.MovieRecording(); InteropEmu.UpdateInputDevices(); Entity = ConfigManager.Config.InputInfo; for(int i = 0; i < 4; i++) { ConfigManager.Config.InputInfo.Controllers[i].ControllerType = InteropEmu.GetControllerType(i); } ConfigManager.Config.InputInfo.ExpansionPortDevice = InteropEmu.GetExpansionDevice(); ConfigManager.Config.InputInfo.ConsoleType = InteropEmu.GetConsoleType(); ConfigManager.Config.InputInfo.UseFourScore = InteropEmu.HasFourScore(); AddBinding("ExpansionPortDevice", cboExpansionPort); AddBinding("ConsoleType", cboConsoleType); AddBinding("UseFourScore", chkFourScore); AddBinding("AutoConfigureInput", chkAutoConfigureInput); AddBinding("DisplayInputPort1", chkDisplayPort1); AddBinding("DisplayInputPort2", chkDisplayPort2); AddBinding("DisplayInputPort3", chkDisplayPort3); AddBinding("DisplayInputPort4", chkDisplayPort4); AddBinding("DisplayInputPosition", cboDisplayInputPosition); AddBinding("DisplayInputHorizontally", chkDisplayInputHorizontally); //Sort expansion port dropdown alphabetically, but keep the "None" option at the top SortDropdown(cboExpansionPort, ResourceHelper.GetEnumText(InteropEmu.ExpansionPortDevice.None)); UpdateCartridgeInputUi(); UpdateConflictWarning(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); btnSetupP1.Click += btnSetup_Click; btnSetupP2.Click += btnSetup_Click; btnSetupP3.Click += btnSetup_Click; btnSetupP4.Click += btnSetup_Click; cboConsoleType.SelectedIndexChanged += cboNesType_SelectedIndexChanged; cboExpansionPort.SelectedIndexChanged += cboExpansionPort_SelectedIndexChanged; chkFourScore.CheckedChanged += chkFourScore_CheckedChanged; btnSetupExp.Click += btnSetup_Click; btnSetupCartridge.Click += btnSetupCartridge_Click; } private void UpdateCartridgeInputUi() { ConsoleFeatures features = InteropEmu.GetAvailableFeatures(); bool hasCartridgeInput = features.HasFlag(ConsoleFeatures.BandaiMicrophone) || features.HasFlag(ConsoleFeatures.DatachBarcodeReader); _hasCartridgeInput = hasCartridgeInput; lblCartridge.Visible = hasCartridgeInput; cboCartridge.Visible = hasCartridgeInput; btnSetupCartridge.Visible = hasCartridgeInput; btnSetupCartridge.Enabled = features.HasFlag(ConsoleFeatures.BandaiMicrophone); if(hasCartridgeInput) { if(features.HasFlag(ConsoleFeatures.BandaiMicrophone)) { cboCartridge.Items.Add(ResourceHelper.GetMessage("BandaiMicrophone")); cboCartridge.SelectedIndex = 0; } else if(features.HasFlag(ConsoleFeatures.DatachBarcodeReader)) { cboCartridge.Items.Add(ResourceHelper.GetMessage("DatachBarcodeReader")); cboCartridge.SelectedIndex = 0; } this.Height += (int)(30 * _yFactor); } } private void SortDropdown(ComboBox dropdown, string optionAtTop) { dropdown.Sorted = true; dropdown.Sorted = false; int index = dropdown.FindStringExact(optionAtTop); if(index >= 0) { object topOption = dropdown.Items[index]; dropdown.Items.RemoveAt(index); dropdown.Items.Insert(0, topOption); } } protected override void OnFormClosed(FormClosedEventArgs e) { base.OnFormClosed(e); if(DialogResult == DialogResult.OK) { InputInfo.ApplyConfig(); } } protected override void AfterUpdateUI() { base.AfterUpdateUI(); this.UpdateInterface(); } private void UpdateAvailableControllerTypes() { cboPlayer1.SelectedIndexChanged -= cboPlayerController_SelectedIndexChanged; cboPlayer2.SelectedIndexChanged -= cboPlayerController_SelectedIndexChanged; cboPlayer3.SelectedIndexChanged -= cboPlayerController_SelectedIndexChanged; cboPlayer4.SelectedIndexChanged -= cboPlayerController_SelectedIndexChanged; bool isNes = ((InputInfo)Entity).ConsoleType == ConsoleType.Nes; bool isOriginalFamicom = !isNes && !ConfigManager.Config.EmulationInfo.UseNes101Hvc101Behavior; List controllerTypes = new List() { InteropEmu.ControllerType.None, InteropEmu.ControllerType.StandardController }; controllerTypes.Add(InteropEmu.ControllerType.SnesMouse); controllerTypes.Add(InteropEmu.ControllerType.SuborMouse); if(!isNes) { controllerTypes.Add(InteropEmu.ControllerType.SnesController); } SetAvailableControllerTypes(cboPlayer3, controllerTypes.ToArray()); SetAvailableControllerTypes(cboPlayer4, controllerTypes.ToArray()); if(isOriginalFamicom) { //Original famicom only allows standard controllers in port 1/2 controllerTypes = new List() { InteropEmu.ControllerType.StandardController }; } else if(isNes && !chkFourScore.Checked) { //These use more than just bit 0, and won't work on the four score controllerTypes.Add(InteropEmu.ControllerType.ArkanoidController); controllerTypes.Add(InteropEmu.ControllerType.PowerPad); controllerTypes.Add(InteropEmu.ControllerType.Zapper); controllerTypes.Add(InteropEmu.ControllerType.SnesController); } SetAvailableControllerTypes(cboPlayer1, controllerTypes.ToArray()); SetAvailableControllerTypes(cboPlayer2, controllerTypes.ToArray()); cboPlayer1.SelectedIndexChanged += cboPlayerController_SelectedIndexChanged; cboPlayer2.SelectedIndexChanged += cboPlayerController_SelectedIndexChanged; cboPlayer3.SelectedIndexChanged += cboPlayerController_SelectedIndexChanged; cboPlayer4.SelectedIndexChanged += cboPlayerController_SelectedIndexChanged; } private void SetAvailableControllerTypes(ComboBox comboBox, InteropEmu.ControllerType[] controllerTypes) { comboBox.Items.Clear(); foreach(InteropEmu.ControllerType type in controllerTypes) { comboBox.Items.Add(ResourceHelper.GetEnumText(type)); } InputInfo inputInfo = (InputInfo)Entity; string currentSelection = null; if(comboBox == cboPlayer1) { currentSelection = ResourceHelper.GetEnumText(inputInfo.Controllers[0].ControllerType); } else if(comboBox == cboPlayer2) { currentSelection = ResourceHelper.GetEnumText(inputInfo.Controllers[1].ControllerType); } else if(comboBox == cboPlayer3) { currentSelection = ResourceHelper.GetEnumText(inputInfo.Controllers[2].ControllerType); } else if(comboBox == cboPlayer4) { currentSelection = ResourceHelper.GetEnumText(inputInfo.Controllers[3].ControllerType); } SortDropdown(comboBox, ResourceHelper.GetEnumText(InteropEmu.ControllerType.None)); if(currentSelection != null && comboBox.Items.Contains(currentSelection)) { comboBox.SelectedItem = currentSelection; } else { comboBox.SelectedIndex = 0; } } protected override void UpdateConfig() { InputInfo inputInfo = (InputInfo)Entity; inputInfo.Controllers[0].ControllerType = cboPlayer1.GetEnumValue(); inputInfo.Controllers[1].ControllerType = cboPlayer2.GetEnumValue(); inputInfo.Controllers[2].ControllerType = cboPlayer3.GetEnumValue(); inputInfo.Controllers[3].ControllerType = cboPlayer4.GetEnumValue(); } private void UpdateInterface() { bool isNes = ((InputInfo)Entity).ConsoleType == ConsoleType.Nes; cboExpansionPort.Visible = !isNes; lblExpansionPort.Visible = !isNes; btnSetupExp.Visible = !isNes; chkFourScore.Visible = isNes; UpdatePlayer3And4Visibility(); UpdateAvailableControllerTypes(); UpdateSetupButtons(); if(ConfigManager.Config.PreferenceInfo.DisableGameDatabase) { //This option requires the game DB to be active chkAutoConfigureInput.Enabled = false; chkAutoConfigureInput.Checked = false; } UpdateConflictWarning(); } private bool FourScoreAttached { get { bool isNes = ((InputInfo)Entity).ConsoleType == ConsoleType.Nes; return (isNes && chkFourScore.Checked) || (!isNes && ((InputInfo)Entity).ExpansionPortDevice == InteropEmu.ExpansionPortDevice.FourPlayerAdapter); } } private void UpdatePlayer3And4Visibility() { bool visible = this.FourScoreAttached; if(lblPlayer3.Visible != visible) { lblPlayer3.Visible = visible; lblPlayer4.Visible = visible; cboPlayer3.Visible = visible; cboPlayer4.Visible = visible; btnSetupP3.Visible = visible; btnSetupP4.Visible = visible; } } private void cboNesType_SelectedIndexChanged(object sender, EventArgs e) { UpdateObject(); if(((InputInfo)Entity).ConsoleType == ConsoleType.Famicom) { ((InputInfo)Entity).Controllers[0].ControllerType = InteropEmu.ControllerType.StandardController; ((InputInfo)Entity).Controllers[1].ControllerType = InteropEmu.ControllerType.StandardController; } UpdateInterface(); } private void chkFourScore_CheckedChanged(object sender, EventArgs e) { UpdateObject(); UpdateInterface(); } private void cboExpansionPort_SelectedIndexChanged(object sender, EventArgs e) { UpdateObject(); UpdateInterface(); } private void UpdateSetupButtons() { List dropdowns = new List() { cboPlayer1, cboPlayer2, cboPlayer3, cboPlayer4 }; List