using System; using System.Globalization; using BizHawk.Client.Common; namespace BizHawk.Client.ApiHawk { /// /// This class holds a converter for BizHawk joypad buttons (which is a simple /// It allows you to convert it to a value and vice versa /// /// I made it this way just in case one day we need it for WPF (DependencyProperty binding). Just uncomment :IValueConverter implementation /// I didn't implemented it because of mono compatibility /// public sealed class JoypadStringToEnumConverter //:IValueConverter { /// /// Convert BizHawk button to value /// /// you want to convert /// The type of the binding target property /// The converter parameter to use; null in our case /// The culture to use in the converter /// A that is equivalent to BizHawk button /// Thrown when SystemId hasn't been found public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo) { switch (((string)value).ToUpper()) { case "A": return JoypadButton.A; case "B": return JoypadButton.B; case "B1": return JoypadButton.B1; case "B2": return JoypadButton.B2; case "C": return JoypadButton.C; case "C UP": return JoypadButton.CUp; case "C DOWN": return JoypadButton.CDown; case "C LEFT": return JoypadButton.CLeft; case "C RIGHT": return JoypadButton.CRight; case "X": return JoypadButton.X; case "Y": return JoypadButton.Y; case "Z": return JoypadButton.Z; case "START": return JoypadButton.Start; case "SELECT": return JoypadButton.Select; case "UP": case "DPAD U": return JoypadButton.Up; case "DOWN": case "DPAD D": return JoypadButton.Down; case "LEFT": case "DPAD L": return JoypadButton.Left; case "RIGHT": case "DPAD R": return JoypadButton.Right; case "L": return JoypadButton.L; case "R": return JoypadButton.R; default: throw new IndexOutOfRangeException(string.Format("{0} is missing in convert list", value)); } } /// /// Convert BizHawk button to value /// /// you want to convert /// A that is equivalent to BizHawk button /// Thrown when SystemId hasn't been found public JoypadButton Convert(string value) { return (JoypadButton)Convert(value, null, null, CultureInfo.CurrentCulture); } /// /// Convert a value to BizHawk /// /// you want to convert /// The type of the binding target property /// In our case, we pass the /// The culture to use in the converter /// A that is used by BizHawk /// Thrown when hasn't been found public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo) { switch ((JoypadButton)value) { case JoypadButton.A: return "A"; case JoypadButton.B: return "B"; case JoypadButton.B1: return "B1"; case JoypadButton.B2: return "B2"; case JoypadButton.C: return "C"; case JoypadButton.CUp: return "C Up"; case JoypadButton.CDown: return "C Down"; case JoypadButton.CLeft: return "C Left"; case JoypadButton.CRight: return "C Right"; case JoypadButton.X: return "X"; case JoypadButton.Y: return "Y"; case JoypadButton.Z: return "Z"; case JoypadButton.Start: return "Start"; case JoypadButton.Select: return "Select"; case JoypadButton.Up: if (((SystemInfo)parameter) == SystemInfo.N64) { return "Dpad U"; } else { return "Up"; } case JoypadButton.Down: if (((SystemInfo)parameter) == SystemInfo.N64) { return "Dpad D"; } else { return "Down"; } case JoypadButton.Left: if (((SystemInfo)parameter) == SystemInfo.N64) { return "Dpad L"; } else { return "Left"; } case JoypadButton.Right: if (((SystemInfo)parameter) == SystemInfo.N64) { return "Dpad R"; } else { return "Right"; } case JoypadButton.L: return "L"; case JoypadButton.R: return "R"; default: throw new IndexOutOfRangeException(string.Format("{0} is missing in convert list", value)); } } /// /// Convert a value to BizHawk /// /// you want to convert /// Current /// A that is used by BizHawk /// Thrown when hasn't been found public string ConvertBack(JoypadButton button, SystemInfo system) { return (string)ConvertBack(button, null, system, CultureInfo.CurrentCulture); } } }