private void OnControllerStateUpdate(object sender, EventArgs e) { if (IsHandleCreated && !IsDisposed && !_busyRendering) { try { _busyRendering = true; BeginInvoke((Action) delegate { try { // update mapping if (_seqMapPos >= 0) { SequentialMappingUpdate(_realController.GetState()); } else // simply show mapped controller state { skinPreview.RenderSkin(_mappedController.GetState()); } } finally { _busyRendering = false; } }); } catch (InvalidOperationException) { } } }
public ControllerMapperEditor(GenericController realController, ControllerType target, Skin skin, ControllerMapping mapping) : this() { System.Diagnostics.Debug.Assert(mapping.AppliesTo(realController)); System.Diagnostics.Debug.Assert(skin.Controllers.Contains(target)); _realController = realController; _skin = skin = Skin.Clone(skin); // do not let main skin be affected _mapping = mapping; IController iController = realController.RequiresPolling ? new PollingController(realController, 1000 / 16) : (IController)realController; iController.StateUpdated += OnControllerStateUpdate; gamepadViewer.StartTesting(iController); skinPreview.Skin = skin; _mappedController = new MappedController(mapping, iController); // determine number of buttons/axes on controller and number of buttons/axes in skin var state = realController.GetState(); int numControllerButtons = state.Buttons.Count; int numControllerAxes = state.Axes.Count; skin.GetNumberOfElements(out int numSkinButtons, out int numSkinAxes); List <ControllerMapping.Button> sourceButtons = new List <ControllerMapping.Button>(); List <ControllerMapping.Axis> sourceAxes = new List <ControllerMapping.Axis>(); // ensure mapping contains entries for all buttons and axes on controller for (int i = 0; i < numControllerButtons; i++) { if (_mapping.ButtonMaps.All(b => b.Source != (ControllerMapping.Button)i)) { _mapping.ButtonMaps.Add(new ControllerMapping.ButtonMap { Source = (ControllerMapping.Button)i, Target = ControllerMapping.Button.Unmapped, }); } sourceButtons.Add((ControllerMapping.Button)i); } for (int i = 0; i < numControllerAxes; i++) { if (_mapping.AxisMaps.All(b => b.Source != (ControllerMapping.Axis)i)) { // default 1:1 map _mapping.AxisMaps.Add(new ControllerMapping.AxisMap { Source = (ControllerMapping.Axis)i, Target = (ControllerMapping.Axis)i, IsTrigger = realController.IsAxisTrigger(i) }); } sourceAxes.Add((ControllerMapping.Axis)i); } // remove invalid entries _mapping.ButtonMaps.RemoveAll(b => (int)b.Source >= numControllerButtons || (int)b.Target >= numSkinButtons); _mapping.AxisMaps.RemoveAll(a => (int)a.Source >= numControllerAxes || (int)a.Target >= numSkinAxes); // make sure they appear in logical order _mapping.ButtonMaps.Sort((self, other) => self.Source.CompareTo(other.Source)); _mapping.AxisMaps.Sort((self, other) => self.Source.CompareTo(other.Source)); dgvcSourceButton.DataSource = sourceButtons; cbButtonToAxisSource.DataSource = sourceButtons; cbAxisToButtonTarget.DataSource = sourceButtons; dgvcSourceAxis.DataSource = sourceAxes; cbAxisToButtonSource.DataSource = sourceAxes; cbButtonToAxisTarget.DataSource = sourceAxes; List <ControllerMapping.Button> targetButtons = new List <ControllerMapping.Button> { ControllerMapping.Button.Unmapped }; for (int i = 0; i < numSkinButtons; i++) { targetButtons.Add((ControllerMapping.Button)i); } List <ControllerMapping.Axis> targetAxes = new List <ControllerMapping.Axis> { ControllerMapping.Axis.Unmapped }; for (int i = 0; i < numSkinAxes; i++) { targetAxes.Add((ControllerMapping.Axis)i); } // enable dataGridViews only if there are actual elements in both controller and axis if (numSkinButtons > 0 && numControllerButtons > 0) { dgvcTargetButton.DataSource = targetButtons; bsButtons.DataSource = new BindingList <ControllerMapping.ButtonMap>(_mapping.ButtonMaps); dgvButtons.DataSource = bsButtons; } else { tpButtons.Visible = dgvButtons.Visible = lblButtons.Visible = false; } tpAxesToButtons.Visible = numControllerAxes > 0; tpButtonsToAxes.Visible = numControllerButtons > 0; bsAxesToButtons.DataSource = new BindingList <ControllerMapping.AxisToButtonMap>(_mapping.AxisToButtonMaps); bsButtonsToAxes.DataSource = new BindingList <ControllerMapping.ButtonToAxisMap>(_mapping.ButtonToAxisMaps); if (numSkinAxes > 0 && numControllerAxes > 0) { dgvcTargetAxis.DataSource = targetAxes; bsAxes.DataSource = new BindingList <ControllerMapping.AxisMap>(_mapping.AxisMaps); dgvAxes.DataSource = bsAxes; } else { tpAxes.Visible = false; tpAxesToButtons.Visible = false; } }