Esempio n. 1
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            bool left   = e.Button.HasFlag(MouseButtons.Left);
            bool middle = e.Button.HasFlag(MouseButtons.Middle) || (e.Button.HasFlag(MouseButtons.Left) && ModifierKeys.HasFlag(Keys.Alt));
            bool right  = e.Button.HasFlag(MouseButtons.Right);

            var buttonIdx = GetButtonAtCoord(e.X, e.Y, out var subButtonType);

            if (buttonIdx >= 0)
            {
                var button = buttons[buttonIdx];

                if (left)
                {
                    if (button.type == ButtonType.SongHeader)
                    {
                        if (subButtonType == SubButtonType.Add)
                        {
                            App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                            App.Project.CreateSong();
                            App.UndoRedoManager.EndTransaction();
                            RefreshButtons();
                            ConditionalInvalidate();
                        }
                    }
                    else if (button.type == ButtonType.Song)
                    {
                        if (button.song != selectedSong)
                        {
                            selectedSong = button.song;
                            SongSelected?.Invoke(selectedSong);
                            ConditionalInvalidate();
                        }
                    }
                    else if (button.type == ButtonType.InstrumentHeader)
                    {
                        if (subButtonType == SubButtonType.Add)
                        {
                            App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                            App.Project.CreateInstrument();
                            App.UndoRedoManager.EndTransaction();
                            RefreshButtons();
                            ConditionalInvalidate();
                        }
                        if (subButtonType == SubButtonType.LoadInstrument)
                        {
                            var filename = PlatformDialogs.ShowOpenFileDialog("Open File", "Fami Tracker Instrument (*.fti)|*.fti");
                            if (filename != null)
                            {
                                App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                                var instrument = FamitrackerInstrumentFile.CreateFromFile(App.Project, filename);
                                if (instrument == null)
                                {
                                    App.UndoRedoManager.AbortTransaction();
                                }
                                else
                                {
                                    App.UndoRedoManager.EndTransaction();
                                }
                            }

                            RefreshButtons();
                            ConditionalInvalidate();
                        }
                    }
                    else if (button.type == ButtonType.Instrument)
                    {
                        selectedInstrument = button.instrument;

                        if (selectedInstrument != null)
                        {
                            instrumentDrag = selectedInstrument;
                            mouseDragY     = e.Y;
                        }

                        if (subButtonType == SubButtonType.Volume)
                        {
                            InstrumentEdited?.Invoke(selectedInstrument, Envelope.Volume);
                            envelopeDragIdx = Envelope.Volume;
                        }
                        else if (subButtonType == SubButtonType.Pitch)
                        {
                            InstrumentEdited?.Invoke(selectedInstrument, Envelope.Pitch);
                            envelopeDragIdx = Envelope.Pitch;
                        }
                        else if (subButtonType == SubButtonType.Arpeggio)
                        {
                            InstrumentEdited?.Invoke(selectedInstrument, Envelope.Arpeggio);
                            envelopeDragIdx = Envelope.Arpeggio;
                        }
                        else if (subButtonType == SubButtonType.DPCM)
                        {
                            InstrumentEdited?.Invoke(selectedInstrument, Envelope.Max);
                        }
                        else if (subButtonType >= SubButtonType.DutyCycle0 && subButtonType <= SubButtonType.DutyCycle3)
                        {
                            selectedInstrument.DutyCycle = (selectedInstrument.DutyCycle + 1) % 4;
                        }

                        InstrumentSelected?.Invoke(selectedInstrument);
                        ConditionalInvalidate();
                    }
                }
                else if (right)
                {
                    if (button.type == ButtonType.Song && App.Project.Songs.Count > 1)
                    {
                        var song = button.song;
                        if (PlatformDialogs.MessageBox($"Are you sure you want to delete '{song.Name}' ?", "Delete song", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            bool selectNewSong = song == selectedSong;
                            App.Stop();
                            App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                            App.Project.DeleteSong(song);
                            if (selectNewSong)
                            {
                                selectedSong = App.Project.Songs[0];
                            }
                            SongSelected?.Invoke(selectedSong);
                            App.UndoRedoManager.EndTransaction();
                            RefreshButtons();
                            ConditionalInvalidate();
                        }
                    }
                    else if (button.type == ButtonType.Instrument && button.instrument != null)
                    {
                        var instrument = button.instrument;

                        if (subButtonType == SubButtonType.Arpeggio ||
                            subButtonType == SubButtonType.Pitch ||
                            subButtonType == SubButtonType.Volume)
                        {
                            int envType = Envelope.Volume;

                            switch (subButtonType)
                            {
                            case SubButtonType.Arpeggio: envType = Envelope.Arpeggio; break;

                            case SubButtonType.Pitch: envType = Envelope.Pitch;    break;
                            }

                            App.UndoRedoManager.BeginTransaction(TransactionScope.Instrument, instrument.Id);
                            instrument.Envelopes[envType].Length = 0;
                            App.UndoRedoManager.EndTransaction();
                            ConditionalInvalidate();
                        }
                        else if (subButtonType == SubButtonType.Max)
                        {
                            if (PlatformDialogs.MessageBox($"Are you sure you want to delete '{instrument.Name}' ? All notes using this instrument will be deleted.", "Delete intrument", MessageBoxButtons.YesNo) == DialogResult.Yes)
                            {
                                bool selectNewInstrument = instrument == selectedInstrument;
                                App.StopInstrumentNoteAndWait();
                                App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                                App.Project.DeleteInstrument(instrument);
                                if (selectNewInstrument)
                                {
                                    selectedInstrument = App.Project.Instruments.Count > 0 ? App.Project.Instruments[0] : null;
                                }
                                SongSelected?.Invoke(selectedSong);
                                InstrumentDeleted?.Invoke(instrument);
                                App.UndoRedoManager.EndTransaction();
                                RefreshButtons();
                                ConditionalInvalidate();
                            }
                        }
                    }
                }
            }

            if (middle)
            {
                mouseLastY = e.Y;
            }
        }
Esempio n. 2
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            bool left   = e.Button.HasFlag(MouseButtons.Left);
            bool middle = e.Button.HasFlag(MouseButtons.Middle) || (e.Button.HasFlag(MouseButtons.Left) && ModifierKeys.HasFlag(Keys.Alt));
            bool right  = e.Button.HasFlag(MouseButtons.Right);

            var buttonIndex     = (e.Y + scrollY) / ButtonSizeY;
            var songIndex       = buttonIndex - 1;
            var instrumentIndex = buttonIndex - (App.Project.Songs.Count + 2);
            var shiftY          = (buttonIndex * ButtonSizeY - scrollY);

            if (left)
            {
                if (songIndex == -1)
                {
                    if (GetButtonRect(1, shiftY).Contains(e.X, e.Y))
                    {
                        App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                        App.Project.CreateSong();
                        App.UndoRedoManager.EndTransaction();
                        ConditionalInvalidate();
                    }
                }
                else if (instrumentIndex == -1)
                {
                    if (GetButtonRect(1, shiftY).Contains(e.X, e.Y))
                    {
                        App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                        App.Project.CreateInstrument();
                        App.UndoRedoManager.EndTransaction();
                        ConditionalInvalidate();
                    }
                }
                else if (songIndex >= 0 && songIndex < App.Project.Songs.Count)
                {
                    selectedSong = App.Project.Songs[songIndex];
                    SongSelected?.Invoke(selectedSong);
                    ConditionalInvalidate();
                    return;
                }
                else if (instrumentIndex >= 0 && instrumentIndex <= App.Project.Instruments.Count)
                {
                    selectedInstrument = instrumentIndex == 0 ? null : App.Project.Instruments[instrumentIndex - 1];

                    if (selectedInstrument != null)
                    {
                        instrumentDragIdx = instrumentIndex - 1;
                        mouseDragY        = e.Y;
                    }

                    if (selectedInstrument != null && GetButtonRect(4, shiftY).Contains(e.X, e.Y))
                    {
                        InstrumentEdited?.Invoke(selectedInstrument, Envelope.Volume);
                        envelopeDragIdx = Envelope.Volume;
                    }
                    else if (selectedInstrument != null && GetButtonRect(3, shiftY).Contains(e.X, e.Y))
                    {
                        InstrumentEdited?.Invoke(selectedInstrument, Envelope.Pitch);
                        envelopeDragIdx = Envelope.Pitch;
                    }
                    else if (selectedInstrument != null && GetButtonRect(2, shiftY).Contains(e.X, e.Y))
                    {
                        InstrumentEdited?.Invoke(selectedInstrument, Envelope.Arpeggio);
                        envelopeDragIdx = Envelope.Arpeggio;
                    }
                    else if (GetButtonRect(1, shiftY).Contains(e.X, e.Y))
                    {
                        if (selectedInstrument == null)
                        {
                            InstrumentEdited?.Invoke(selectedInstrument, Envelope.Max);
                        }
                        else
                        {
                            selectedInstrument.DutyCycle = (selectedInstrument.DutyCycle + 1) % 4;
                            ConditionalInvalidate();
                        }
                    }

                    InstrumentSelected?.Invoke(selectedInstrument);
                    ConditionalInvalidate();
                }
            }
            else if (right)
            {
                instrumentIndex--;

                if (songIndex >= 0 && songIndex < App.Project.Songs.Count && App.Project.Songs.Count > 1)
                {
                    var song = App.Project.Songs[songIndex];
                    if (MessageBox.Show($"Are you sure you want to delete '{song.Name}' ?", "Delete song", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        bool selectNewSong = song == selectedSong;
                        App.Stop();
                        App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                        App.Project.DeleteSong(song);
                        App.UndoRedoManager.EndTransaction();
                        if (selectNewSong)
                        {
                            selectedSong = App.Project.Songs[0];
                        }
                        SongSelected?.Invoke(selectedSong);
                        ConditionalInvalidate();
                        return;
                    }
                }
                else if (instrumentIndex >= 0 && instrumentIndex < App.Project.Instruments.Count)
                {
                    var instrument = App.Project.Instruments[instrumentIndex];
                    if (MessageBox.Show($"Are you sure you want to delete '{instrument.Name}' ? All notes using this instrument will be deleted.", "Delete intrument", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        bool selectNewInstrument = instrument == selectedInstrument;
                        App.StopInstrumentNoteAndWait();
                        App.UndoRedoManager.BeginTransaction(TransactionScope.Project);
                        App.Project.DeleteInstrument(instrument);
                        App.UndoRedoManager.EndTransaction();
                        if (selectNewInstrument)
                        {
                            selectedInstrument = App.Project.Instruments.Count > 0 ? App.Project.Instruments[0] : null;
                        }
                        SongSelected?.Invoke(selectedSong);
                        ConditionalInvalidate();
                        return;
                    }
                }
            }

            if (middle)
            {
                mouseLastY = e.Y;
            }
        }