コード例 #1
0
        public MainForm()
        {
            InitializeComponent();
            Size = new Size(480, 700);
            Icon = Resources.MainIcon;

            ToolStripManager.RenderMode = ToolStripManagerRenderMode.System;

            OperationManager = new OperationManager();
            OperationManager.OperationHistoryChanged += (s, e) => SetText(ScoreBook.Path);
            OperationManager.ChangesCommited         += (s, e) => SetText(ScoreBook.Path);

            NoteView = new NoteView(OperationManager)
            {
                Dock           = DockStyle.Fill,
                UnitBeatHeight = ApplicationSettings.Default.UnitBeatHeight,
                UnitLaneWidth  = ApplicationSettings.Default.UnitLaneWidth,
            };

            PreviewManager = new SoundPreviewManager(NoteView);
            PreviewManager.IsStopAtLastNote = ApplicationSettings.Default.IsPreviewAbortAtLastNote;
            PreviewManager.TickUpdated     += (s, e) => NoteView.CurrentTick = e.Tick;
            PreviewManager.ExceptionThrown += (s, e) => MessageBox.Show(this, ErrorStrings.PreviewException, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);

            NoteViewScrollBar = new VScrollBar()
            {
                Dock        = DockStyle.Right,
                Minimum     = -NoteView.UnitBeatTick * 4 * 20,
                SmallChange = NoteView.UnitBeatTick
            };

            Action <ScrollBar> processScrollBarRangeExtension = s =>
            {
                if (NoteViewScrollBar.Value < NoteViewScrollBar.Minimum * 0.9f)
                {
                    NoteViewScrollBar.Minimum = (int)(NoteViewScrollBar.Minimum * 1.2);
                }
            };

            NoteView.Resize += (s, e) => UpdateThumbHeight();

            NoteView.MouseWheel += (s, e) =>
            {
                int value = NoteViewScrollBar.Value - e.Delta / 120 * NoteViewScrollBar.SmallChange;
                NoteViewScrollBar.Value = Math.Min(Math.Max(value, NoteViewScrollBar.Minimum), NoteViewScrollBar.GetMaximumValue());
                processScrollBarRangeExtension(NoteViewScrollBar);
            };

            NoteView.DragScroll += (s, e) =>
            {
                NoteViewScrollBar.Value = Math.Max(-NoteView.HeadTick, NoteViewScrollBar.Minimum);
                processScrollBarRangeExtension(NoteViewScrollBar);
            };

            NoteViewScrollBar.ValueChanged += (s, e) =>
            {
                NoteView.HeadTick = -NoteViewScrollBar.Value / 60 * 60; // 60の倍数できれいに表示されるので…
                NoteView.Invalidate();
            };

            NoteViewScrollBar.Scroll += (s, e) =>
            {
                if (e.Type == ScrollEventType.EndScroll)
                {
                    processScrollBarRangeExtension(NoteViewScrollBar);
                }
            };

            NoteView.NewNoteTypeChanged += (s, e) => NoteView.EditMode = EditMode.Edit;

            AllowDrop  = true;
            DragEnter += (s, e) =>
            {
                e.Effect = DragDropEffects.None;
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    var items = (string[])e.Data.GetData(DataFormats.FileDrop);
                    if (items.Length == 1 && items.All(p => Path.GetExtension(p) == FileExtension && File.Exists(p)))
                    {
                        e.Effect = DragDropEffects.Copy;
                    }
                }
            };
            DragDrop += (s, e) =>
            {
                string path = ((string[])e.Data.GetData(DataFormats.FileDrop)).Single();
                if (OperationManager.IsChanged && !this.ConfirmDiscardChanges())
                {
                    return;
                }
                LoadFile(path);
            };

            FormClosing += (s, e) =>
            {
                if (OperationManager.IsChanged && !this.ConfirmDiscardChanges())
                {
                    e.Cancel = true;
                    return;
                }

                ApplicationSettings.Default.Save();
            };

            using (var manager = this.WorkWithLayout())
            {
                this.Menu = CreateMainMenu(NoteView);
                this.Controls.Add(NoteView);
                this.Controls.Add(NoteViewScrollBar);
                this.Controls.Add(CreateNewNoteTypeToolStrip(NoteView));
                this.Controls.Add(CreateMainToolStrip(NoteView));
            }

            NoteView.NewNoteType = NoteType.Tap;
            NoteView.EditMode    = EditMode.Edit;

            LoadEmptyBook();
            SetText();

            if (!PreviewManager.IsSupported)
            {
                MessageBox.Show(this, ErrorStrings.PreviewNotSupported, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (PluginManager.FailedFiles.Count > 0)
            {
                MessageBox.Show(this, string.Join("\n", new[] { ErrorStrings.PluginLoadError }.Concat(PluginManager.FailedFiles)), Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
コード例 #2
0
        private MainMenu CreateMainMenu(NoteView noteView)
        {
            var importPluginItems = PluginManager.ScoreBookImportPlugins.Select(p => new MenuItem(p.DisplayName, (s, e) =>
            {
                OpenFile(p.FileFilter, q =>
                {
                    try
                    {
                        using (var reader = new StreamReader(q))
                            LoadBook(p.Import(reader));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ErrorStrings.ImportFailed, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Program.DumpExceptionTo(ex, "import_exception.json");
                        LoadEmptyBook();
                    }
                });
            })).ToArray();

            var bookPropertiesMenuItem = new MenuItem(MainFormStrings.bookProperty, (s, e) =>
            {
                var form = new BookPropertiesForm(ScoreBook, CurrentMusicSource);
                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    CurrentMusicSource = form.MusicSource;
                    if (string.IsNullOrEmpty(ScoreBook.Path))
                    {
                        return;
                    }
                    SoundSettings.Default.ScoreSound[ScoreBook.Path] = CurrentMusicSource;
                    SoundSettings.Default.Save();
                }
            });

            var fileMenuItems = new MenuItem[]
            {
                new MenuItem(MainFormStrings.NewFile + "(&N)", (s, e) => ClearFile())
                {
                    Shortcut = Shortcut.CtrlN
                },
                new MenuItem(MainFormStrings.OpenFile + "(&O)", (s, e) => OpenFile())
                {
                    Shortcut = Shortcut.CtrlO
                },
                new MenuItem(MainFormStrings.SaveFile + "(&S)", (s, e) => SaveFile())
                {
                    Shortcut = Shortcut.CtrlS
                },
                new MenuItem(MainFormStrings.SaveAs + "(&A)", (s, e) => SaveAs())
                {
                    Shortcut = Shortcut.CtrlShiftS
                },
                new MenuItem("-"),
                new MenuItem(MainFormStrings.Import, importPluginItems),
                new MenuItem(MainFormStrings.Export, (s, e) => ExportFile()),
                new MenuItem("-"),
                bookPropertiesMenuItem,
                new MenuItem("-"),
                new MenuItem(MainFormStrings.Exit + "(&X)", (s, e) => this.Close())
            };

            var undoItem = new MenuItem(MainFormStrings.Undo, (s, e) => noteView.Undo())
            {
                Shortcut = Shortcut.CtrlZ,
                Enabled  = false
            };
            var redoItem = new MenuItem(MainFormStrings.Redo, (s, e) => noteView.Redo())
            {
                Shortcut = Shortcut.CtrlY,
                Enabled  = false
            };

            var cutItem          = new MenuItem(MainFormStrings.Cut, (s, e) => noteView.CutSelectedNotes(), Shortcut.CtrlX);
            var copyItem         = new MenuItem(MainFormStrings.Copy, (s, e) => noteView.CopySelectedNotes(), Shortcut.CtrlC);
            var pasteItem        = new MenuItem(MainFormStrings.Paste, (s, e) => noteView.PasteNotes(), Shortcut.CtrlV);
            var pasteFlippedItem = new MenuItem(MainFormStrings.PasteFlipped, (s, e) => noteView.PasteFlippedNotes(), Shortcut.CtrlShiftV);

            var flipSelectedNotesItem   = new MenuItem(MainFormStrings.FlipSelectedNotes, (s, e) => noteView.FlipSelectedNotes());
            var removeSelectedNotesItem = new MenuItem(MainFormStrings.RemoveSelectedNotes, (s, e) => noteView.RemoveSelectedNotes(), Shortcut.Del);


            var removeEventsItem = new MenuItem(MainFormStrings.RemoveEvents, (s, e) =>
            {
                int minTick = noteView.SelectedRange.StartTick + (noteView.SelectedRange.Duration < 0 ? noteView.SelectedRange.Duration : 0);
                int maxTick = noteView.SelectedRange.StartTick + (noteView.SelectedRange.Duration < 0 ? 0 : noteView.SelectedRange.Duration);
                Func <EventBase, bool> isContained = p => p.Tick != 0 && minTick <= p.Tick && maxTick >= p.Tick;
                var events = ScoreBook.Score.Events;

                var bpmOp = events.BPMChangeEvents.Where(p => isContained(p)).ToList().Select(p =>
                {
                    ScoreBook.Score.Events.BPMChangeEvents.Remove(p);
                    return(new RemoveEventOperation <BPMChangeEvent>(events.BPMChangeEvents, p));
                }).ToList();

                var speedOp = events.HighSpeedChangeEvents.Where(p => isContained(p)).ToList().Select(p =>
                {
                    ScoreBook.Score.Events.HighSpeedChangeEvents.Remove(p);
                    return(new RemoveEventOperation <HighSpeedChangeEvent>(events.HighSpeedChangeEvents, p));
                }).ToList();

                var signatureOp = events.TimeSignatureChangeEvents.Where(p => isContained(p)).ToList().Select(p =>
                {
                    ScoreBook.Score.Events.TimeSignatureChangeEvents.Remove(p);
                    return(new RemoveEventOperation <TimeSignatureChangeEvent>(events.TimeSignatureChangeEvents, p));
                }).ToList();

                var splitOp = events.SplitLaneEvents.Where(p => isContained(p)).ToList().Select(p =>
                {
                    ScoreBook.Score.Events.SplitLaneEvents.Remove(p);
                    return(new RemoveEventOperation <SplitLaneEvent>(events.SplitLaneEvents, p));
                }).ToList();

                OperationManager.Push(new CompositeOperation("イベント削除", bpmOp.Cast <IOperation>().Concat(speedOp).Concat(signatureOp)));
                noteView.Invalidate();
            });



            var pluginItems = PluginManager.ScorePlugins.Select(p => new MenuItem(p.DisplayName, (s, e) =>
            {
                CommitChanges();
                Action <Score> updateScore = newScore =>
                {
                    var op = new UpdateScoreOperation(ScoreBook.Score, newScore, score =>
                    {
                        ScoreBook.Score = score;
                        noteView.UpdateScore(score);
                    });
                    OperationManager.Push(op);
                    op.Redo();
                };

                try
                {
                    p.Run(new ScorePluginArgs(() => ScoreBook.Score.Clone(), noteView.SelectedRange, updateScore));
                }
                catch (Exception ex)
                {
                    Program.DumpExceptionTo(ex, "plugin_exception.json");
                    MessageBox.Show(this, ErrorStrings.PluginException, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            })).ToArray();

            var pluginItem = new MenuItem(MainFormStrings.Plugin, pluginItems);

            var RefrashItem = new MenuItem(MainFormStrings.Refresh, (s, e) =>
            {
                SaveFile();
                LoadFile(ScoreBook.Path);
            }, Shortcut.CtrlR);

            var editMenuItems = new MenuItem[]
            {
                undoItem, redoItem, new MenuItem("-"),
                cutItem, copyItem, pasteItem, pasteFlippedItem, new MenuItem("-"),
                flipSelectedNotesItem, removeSelectedNotesItem, removeEventsItem, new MenuItem("-"),
                RefrashItem,
                pluginItem
            };

            var viewModeItem = new MenuItem(MainFormStrings.ScorePreview, (s, e) =>
            {
                IsPreviewMode         = !IsPreviewMode;
                ((MenuItem)s).Checked = IsPreviewMode;
            }, Shortcut.CtrlP);

            WidenLaneWidthMenuItem  = new MenuItem(MainFormStrings.WidenLaneWidth);
            NarrowLaneWidthMenuItem = new MenuItem(MainFormStrings.NarrowLaneWidth);

            WidenLaneWidthMenuItem.Click += (s, e) =>
            {
                noteView.UnitLaneWidth += 4;
                ApplicationSettings.Default.UnitLaneWidth = noteView.UnitLaneWidth;
                WidenLaneWidthMenuItem.Enabled            = CanWidenLaneWidth;
                NarrowLaneWidthMenuItem.Enabled           = CanNarrowLaneWidth;
            };

            NarrowLaneWidthMenuItem.Click += (s, e) =>
            {
                noteView.UnitLaneWidth -= 4;
                ApplicationSettings.Default.UnitLaneWidth = noteView.UnitLaneWidth;
                WidenLaneWidthMenuItem.Enabled            = CanWidenLaneWidth;
                NarrowLaneWidthMenuItem.Enabled           = CanNarrowLaneWidth;
            };

            var viewMenuItems = new MenuItem[] {
                viewModeItem,
                new MenuItem("-"),
                WidenLaneWidthMenuItem, NarrowLaneWidthMenuItem
            };

            var insertBPMItem = new MenuItem("BPM", (s, e) =>
            {
                var form = new BPMSelectionForm()
                {
                    BPM = noteView.ScoreEvents.BPMChangeEvents.OrderBy(p => p.Tick).LastOrDefault(p => p.Tick <= noteView.CurrentTick)?.BPM ?? 120m
                };
                if (form.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                var prev = noteView.ScoreEvents.BPMChangeEvents.SingleOrDefault(p => p.Tick == noteView.SelectedRange.StartTick);
                var item = new BPMChangeEvent()
                {
                    Tick = noteView.SelectedRange.StartTick,
                    BPM  = form.BPM
                };

                var insertOp = new InsertEventOperation <BPMChangeEvent>(noteView.ScoreEvents.BPMChangeEvents, item);
                if (prev == null)
                {
                    OperationManager.Push(insertOp);
                }
                else
                {
                    var removeOp = new RemoveEventOperation <BPMChangeEvent>(noteView.ScoreEvents.BPMChangeEvents, prev);
                    noteView.ScoreEvents.BPMChangeEvents.Remove(prev);
                    OperationManager.Push(new CompositeOperation(insertOp.Description, new IOperation[] { removeOp, insertOp }));
                }

                noteView.ScoreEvents.BPMChangeEvents.Add(item);
                noteView.Invalidate();
            });

            var insertHighSpeedItem = new MenuItem(MainFormStrings.HighSpeed, (s, e) =>
            {
                var form = new HighSpeedSelectionForm()
                {
                    SpeedRatio = noteView.ScoreEvents.HighSpeedChangeEvents.OrderBy(p => p.Tick).LastOrDefault(p => p.Tick <= noteView.CurrentTick)?.SpeedRatio ?? 1.0m
                };
                if (form.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                var prev = noteView.ScoreEvents.HighSpeedChangeEvents.SingleOrDefault(p => p.Tick == noteView.SelectedRange.StartTick);
                var item = new HighSpeedChangeEvent()
                {
                    Tick       = noteView.SelectedRange.StartTick,
                    SpeedRatio = form.SpeedRatio
                };

                var insertOp = new InsertEventOperation <HighSpeedChangeEvent>(noteView.ScoreEvents.HighSpeedChangeEvents, item);
                if (prev == null)
                {
                    OperationManager.Push(insertOp);
                }
                else
                {
                    var removeOp = new RemoveEventOperation <HighSpeedChangeEvent>(noteView.ScoreEvents.HighSpeedChangeEvents, prev);
                    noteView.ScoreEvents.HighSpeedChangeEvents.Remove(prev);
                    OperationManager.Push(new CompositeOperation(insertOp.Description, new IOperation[] { removeOp, insertOp }));
                }

                noteView.ScoreEvents.HighSpeedChangeEvents.Add(item);
                noteView.Invalidate();
            });


            var insertTimeSignatureItem = new MenuItem(MainFormStrings.TimeSignature, (s, e) =>
            {
                var form = new TimeSignatureSelectionForm();
                if (form.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                var prev = noteView.ScoreEvents.TimeSignatureChangeEvents.SingleOrDefault(p => p.Tick == noteView.SelectedRange.StartTick);
                var item = new TimeSignatureChangeEvent()
                {
                    Tick                = noteView.SelectedRange.StartTick,
                    Numerator           = form.Numerator,
                    DenominatorExponent = form.DenominatorExponent
                };

                var insertOp = new InsertEventOperation <TimeSignatureChangeEvent>(noteView.ScoreEvents.TimeSignatureChangeEvents, item);
                if (prev != null)
                {
                    noteView.ScoreEvents.TimeSignatureChangeEvents.Remove(prev);
                    var removeOp = new RemoveEventOperation <TimeSignatureChangeEvent>(noteView.ScoreEvents.TimeSignatureChangeEvents, prev);
                    OperationManager.Push(new CompositeOperation(insertOp.Description, new IOperation[] { removeOp, insertOp }));
                }
                else
                {
                    OperationManager.Push(insertOp);
                }

                noteView.ScoreEvents.TimeSignatureChangeEvents.Add(item);
                noteView.Invalidate();
            });

            var insertSplitLaneItem = new MenuItem(MainFormStrings.SplitLane, (s, e) =>
            {
                var form = new SplitLaneForm();
                if (form.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                var prev = noteView.ScoreEvents.SplitLaneEvents.SingleOrDefault(p => p.Tick == noteView.SelectedRange.StartTick);
                var item = new SplitLaneEvent()
                {
                    Tick = noteView.SelectedRange.StartTick,
                    Lane = form.Lane
                };

                var insertOp = new InsertEventOperation <SplitLaneEvent>(noteView.ScoreEvents.SplitLaneEvents, item);
                if (prev != null)
                {
                    noteView.ScoreEvents.SplitLaneEvents.Remove(prev);
                    var removeOp = new RemoveEventOperation <SplitLaneEvent>(noteView.ScoreEvents.SplitLaneEvents, prev);
                    OperationManager.Push(new CompositeOperation(insertOp.Description, new IOperation[] { removeOp, insertOp }));
                }
                else
                {
                    OperationManager.Push(insertOp);
                }

                noteView.ScoreEvents.SplitLaneEvents.Add(item);
                noteView.Invalidate();
            });

            var insertMenuItems = new MenuItem[] { insertBPMItem, insertHighSpeedItem, insertTimeSignatureItem, insertSplitLaneItem };

            var isAbortAtLastNoteItem = new MenuItem(MainFormStrings.AbortAtLastNote, (s, e) =>
            {
                var item     = s as MenuItem;
                item.Checked = !item.Checked;
                PreviewManager.IsStopAtLastNote = item.Checked;
                ApplicationSettings.Default.IsPreviewAbortAtLastNote = item.Checked;
            })
            {
                Checked = ApplicationSettings.Default.IsPreviewAbortAtLastNote
            };

            var playItem = new MenuItem(MainFormStrings.Play, (s, e) =>
            {
                if (CurrentMusicSource == null)
                {
                    MessageBox.Show(this, ErrorStrings.MusicSourceNull, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (!File.Exists(CurrentMusicSource.FilePath))
                {
                    MessageBox.Show(this, ErrorStrings.SourceFileNotFound, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (PreviewManager.Playing)
                {
                    PreviewManager.Stop();
                    return;
                }

                int startTick       = noteView.CurrentTick;
                EventHandler lambda = null;
                lambda = (p, q) =>
                {
                    isAbortAtLastNoteItem.Enabled = true;
                    PreviewManager.Finished      -= lambda;
                    noteView.CurrentTick          = startTick;
                    noteView.Editable             = CanEdit;
                };

                try
                {
                    if (!PreviewManager.Start(CurrentMusicSource, startTick))
                    {
                        return;
                    }
                    isAbortAtLastNoteItem.Enabled = false;
                    PreviewManager.Finished      += lambda;
                    noteView.Editable             = CanEdit;
                }
                catch (Exception ex)
                {
                    Program.DumpExceptionTo(ex, "sound_exception.json");
                }
            }, (Shortcut)Keys.Space);

            var stopItem = new MenuItem(MainFormStrings.Stop, (s, e) =>
            {
                PreviewManager.Stop();
            });

            var playMenuItems = new MenuItem[]
            {
                playItem, stopItem, new MenuItem("-"),
                isAbortAtLastNoteItem
            };

            var helpMenuItems = new MenuItem[]
            {
                new MenuItem(MainFormStrings.Help, (s, e) => System.Diagnostics.Process.Start("https://github.com/paralleltree/HexaBeatChartEditer/wiki"), Shortcut.F1),
                new MenuItem(MainFormStrings.VersionInfo, (s, e) => new VersionInfoForm().ShowDialog(this))
            };

            OperationManager.OperationHistoryChanged += (s, e) =>
            {
                redoItem.Enabled = noteView.CanRedo;
                undoItem.Enabled = noteView.CanUndo;
            };

            return(new MainMenu(new MenuItem[]
            {
                new MenuItem(MainFormStrings.FileMenu, fileMenuItems),
                new MenuItem(MainFormStrings.EditMenu, editMenuItems),
                new MenuItem(MainFormStrings.ViewMenu, viewMenuItems),
                new MenuItem(MainFormStrings.InsertMenu, insertMenuItems),
                // PreviewManager初期化後じゃないといけないのダメ設計でしょ
                new MenuItem(MainFormStrings.PlayMenu, playMenuItems)
                {
                    Enabled = PreviewManager.IsSupported
                },
                new MenuItem(MainFormStrings.HelpMenu, helpMenuItems)
            }));
        }