public Events() { musicLoadEvent = new MusicLoadEvent(); musicSwitchEvent = new MusicSwitchEvent(); playbackChangeEvent = new PlaybackChangeEvent(); bpmChangeEvent = new BpmChangeEvent(); muteSectionChangeEvent = new MuteSectionChangeEvent(); }
private MainMenu CreateMainMenu(NoteView noteView) { var importPluginItems = PluginManager.ScoreBookImportPlugins.Select(p => new MenuItem(p.DisplayName, (s, e) => { if (!ConfirmDiscardChanges()) { return; } if (!TrySelectOpeningFile(p.FileFilter, out string path)) { return; } using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { var args = new ScoreBookImportPluginArgs(stream); HandleImport(p, args); } })).ToArray(); var exportPluginItems = PluginManager.ScoreBookExportPlugins.Select(p => new MenuItem(p.DisplayName, (s, e) => { ExportAs(p); })).ToArray(); var bookPropertiesMenuItem = new MenuItem(MainFormStrings.BookProperty, (s, e) => { var vm = new BookPropertiesWindowViewModel(ScoreBook, CurrentMusicSource); var window = new BookPropertiesWindow() { DataContext = vm }; window.ShowDialog(this); }); 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) { Enabled = importPluginItems.Length > 0 }, new MenuItem(MainFormStrings.Export, exportPluginItems) { Enabled = exportPluginItems.Length > 0 }, new MenuItem("-"), bookPropertiesMenuItem, new MenuItem("-"), new MenuItem(MainFormStrings.Exit + "(&X)", (s, e) => this.Close()) }; var undoItem = new MenuItem(MainFormStrings.Undo, (s, e) => OperationManager.Undo()) { Shortcut = Shortcut.CtrlZ, Enabled = false }; var redoItem = new MenuItem(MainFormStrings.Redo, (s, e) => OperationManager.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 selectAllItem = new MenuItem(MainFormStrings.SelectAll, (s, e) => noteView.SelectAll(), Shortcut.CtrlA); var selectToEndItem = new MenuItem(MainFormStrings.SelectToEnd, (s, e) => noteView.SelectToEnd()); var selectoToBeginningItem = new MenuItem(MainFormStrings.SelectToBeginning, (s, e) => noteView.SelectToBeginning()); 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); bool isContained(EventBase 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(); OperationManager.Push(new CompositeOperation("イベント削除", bpmOp.Cast <IOperation>().Concat(speedOp).Concat(signatureOp))); noteView.Invalidate(); }); var insertAirWithAirActionItem = new MenuItem(MainFormStrings.InsertAirWithAirAction, (s, e) => { var item = s as MenuItem; item.Checked = !item.Checked; NoteView.InsertAirWithAirAction = item.Checked; ApplicationSettings.Default.InsertAirWithAirAction = item.Checked; }) { Checked = ApplicationSettings.Default.InsertAirWithAirAction }; var pluginItems = PluginManager.ScorePlugins.Select(p => new MenuItem(p.DisplayName, (s, e) => { CommitChanges(); void updateScore(Score newScore) { var op = new UpdateScoreOperation(ScoreBook.Score, newScore, score => { ScoreBook.Score = score; noteView.UpdateScore(score); }); OperationManager.InvokeAndPush(op); } 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) { Enabled = pluginItems.Length > 0 }; var editMenuItems = new MenuItem[] { undoItem, redoItem, new MenuItem("-"), cutItem, copyItem, pasteItem, pasteFlippedItem, new MenuItem("-"), selectAllItem, selectToEndItem, selectoToBeginningItem, new MenuItem("-"), flipSelectedNotesItem, removeSelectedNotesItem, removeEventsItem, new MenuItem("-"), insertAirWithAirActionItem, new MenuItem("-"), 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 }; void UpdateEvent <T>(List <T> list, T item) where T : EventBase { var prev = list.SingleOrDefault(p => p.Tick == item.Tick); var insertOp = new InsertEventOperation <T>(list, item); if (prev == null) { OperationManager.InvokeAndPush(insertOp); } else { var removeOp = new RemoveEventOperation <T>(list, prev); OperationManager.InvokeAndPush(new CompositeOperation(insertOp.Description, new IOperation[] { removeOp, insertOp })); } noteView.Invalidate(); } 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 ?? 120 }; if (form.ShowDialog(this) != DialogResult.OK) { return; } var item = new BpmChangeEvent() { Tick = noteView.CurrentTick, Bpm = form.Bpm }; UpdateEvent(noteView.ScoreEvents.BpmChangeEvents, item); }); 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 item = new HighSpeedChangeEvent() { Tick = noteView.CurrentTick, SpeedRatio = form.SpeedRatio }; UpdateEvent(noteView.ScoreEvents.HighSpeedChangeEvents, item); }); var insertTimeSignatureItem = new MenuItem(MainFormStrings.TimeSignature, (s, e) => { var form = new TimeSignatureSelectionForm(); if (form.ShowDialog(this) != DialogResult.OK) { return; } var item = new TimeSignatureChangeEvent() { Tick = noteView.CurrentTick, Numerator = form.Numerator, DenominatorExponent = form.DenominatorExponent }; UpdateEvent(noteView.ScoreEvents.TimeSignatureChangeEvents, item); }); var insertMenuItems = new MenuItem[] { insertBpmItem, insertHighSpeedItem, insertTimeSignatureItem }; 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 (string.IsNullOrEmpty(CurrentMusicSource?.FilePath)) { 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; void lambda(object p, EventArgs q) { isAbortAtLastNoteItem.Enabled = true; PreviewManager.Finished -= lambda; noteView.CurrentTick = startTick; noteView.Editable = CanEdit; } try { CommitChanges(); var context = new SoundPreviewContext(ScoreBook.Score, CurrentMusicSource); if (!PreviewManager.Start(context, 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/Ched/wiki"), Shortcut.F1), new MenuItem(MainFormStrings.VersionInfo, (s, e) => new VersionInfoForm().ShowDialog(this)) }; OperationManager.OperationHistoryChanged += (s, e) => { redoItem.Enabled = OperationManager.CanRedo; undoItem.Enabled = OperationManager.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) })); }
public void ChangeBpm(double value) { CurrentBpm = value; BpmChangeEvent?.Invoke(); }