コード例 #1
0
ファイル: MainForm.cs プロジェクト: ROMaster2/munia
        private void HoverSkinPreview(object sender, Skin skin)
        {
            if (skin == null || skin.Path == _activeSkinPreview?.Skin?.Path || _busyRendering)
            {
                return;
            }

            try {
                // create a clone of requested skin
                var clonedSkin = Skin.Clone(skin);
                if (clonedSkin.LoadResult != SkinLoadResult.Ok)
                {
                    return;
                }

                _activeSkinPreview?.Skin?.Deactivate();
                _activeSkinPreview?.Hide();

                Point loc = this.PointToClient(Cursor.Position);
                if (sender is ToolStripMenuItem tsmi)
                {
                    // make sure preview is at least to the right of the toolstrip item
                    int           r    = menu.Left;
                    int           t    = menu.Bottom;
                    ToolStripItem item = tsmi;
                    while (item != null)
                    {
                        if (item.Owner != menu)
                        {
                            r += item.Bounds.Width;
                        }

                        // add vertical offset
                        var parent = item.OwnerItem as ToolStripMenuItem;
                        int i      = 0;
                        while (parent != null && i < parent.DropDownItems.Count)
                        {
                            if (parent.DropDownItems[i] == item)
                            {
                                break;
                            }
                            t += parent.DropDownItems[i].Height;
                            i++;
                        }
                        item = parent;
                    }
                    loc = new Point(r, t);
                    loc.Offset(5, 5);                     // small margin
                }

                if (_activeSkinPreview == null)
                {
                    _activeSkinPreview          = new SkinPreviewWindow(clonedSkin);
                    _activeSkinPreview.Location = loc;
                    Controls.Add(_activeSkinPreview);
                    _activeSkinPreview.BringToFront();
                }
                else
                {
                    _activeSkinPreview.Location = loc;
                    _activeSkinPreview.ChangeSkin(clonedSkin);
                }

                ControllerState state = new ControllerState();
                skin.GetNumberOfElements(out int numButtons, out int numAxes);
                state.Buttons.EnsureSize(numButtons);
                state.Axes.EnsureSize(numAxes);
                _activeSkinPreview.RenderSkin(state);

                _activeSkinPreview.Show();
            }
            finally {
                _busyRendering = false;
            }
        }
コード例 #2
0
        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;
            }
        }