using QuikDawEditor.VST; using static QuikDawEditor.EDITING.VstMethods; namespace QuikDawEditor; public partial class VstFxWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private string _TitleText = "Track -"; public string TitleText { get { return _TitleText; } set { _TitleText = value; NotifyPropertyChanged(nameof(TitleText)); } } public ObservableCollection VstFxReferences { get; set; } public VstFxWindow() { InitializeComponent(); VstFxReferences = new ObservableCollection(ScannedVstEffectReferences); VstEffectsLV.ItemsSource = VstFxReferences; VstFxFavoritesTV.ItemsSource = VstFxFavorites; } private void Window_LocationChanged(object sender, EventArgs e) { Track thisTrack = (Track)this.DataContext; thisTrack.FxWinLocation = new IntPair() { int1 = (int)this.Left, int2 = (int)this.Top }; editingProject.NeedsSaving = true; } public bool receivingAdjustment = false; private void OpenVstBut_Click(object sender, RoutedEventArgs e) { try { ActiveVstPlugin thisactivevst = (ActiveVstPlugin)((Button)e.OriginalSource).DataContext; if (thisactivevst.IsEditorOpen) thisactivevst.edHostWin.Activate(); else thisactivevst.OpenEditor(this); editingProject.NeedsSaving = true; } catch(Exception ex) { MessageBox.Show("Error opening Vst window:\n" + ex.Message); } } private void ActiveCheckBox_Checked(object sender, RoutedEventArgs e) { CheckBox thisCB = (CheckBox)sender; ActiveVstPlugin thisVSTPlugin = (ActiveVstPlugin)thisCB.DataContext; thisVSTPlugin.IsActive = true; editingProject.NeedsSaving = true; } private void ActiveCheckBox_Unchecked(object sender, RoutedEventArgs e) { CheckBox thisCB = (CheckBox)sender; ActiveVstPlugin thisVSTPlugin = (ActiveVstPlugin)thisCB.DataContext; thisVSTPlugin.IsActive = false; editingProject.NeedsSaving = true; } private void SelectedVstsLV_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (movingLVItem) return; if (SelectedVstsLV.SelectedIndex == -1) { detailsLV.ItemsSource = null; detailsLV.Visibility = Visibility.Collapsed; return; } Track thisTrack = (Track)this.DataContext; ActiveVstPlugin thisActivevst = thisTrack.TrackEffectVsts[SelectedVstsLV.SelectedIndex]; if (!thisActivevst.ReturnedWithoutError) { detailsLV.ItemsSource = null; return; } int progcount = thisActivevst.myContext.PluginInfo.ProgramCount; int paramcount = thisActivevst.myContext.PluginInfo.ParameterCount; if (paramcount == 0) { if (progcount > 0) { for (int progno = 0; progno < progcount; progno++) thisActivevst.ActiveVstPrograms.Add(new VstProgram() { ProgramName = thisActivevst.myContext.PluginCommandStub.Commands.GetProgramNameIndexed(progno) }); } detailsLV.ItemsSource = thisActivevst.ActiveVstPrograms; detailsLV.SelectedIndex = thisActivevst.SelectedVstProgramIndex; detailsLV.ScrollIntoView(thisActivevst.ActiveVstPrograms[Math.Max(0, thisActivevst.SelectedVstProgramIndex)]); } else detailsLV.ItemsSource = thisActivevst.VstParameters; } private void CloseButton_Click(object sender, RoutedEventArgs e) { this.Close(); } private void vstfxWin_PreviewKeyDown(object sender, KeyEventArgs e) { if (VstFavoriteFolderEditing) return; if (e.Key == Key.Escape) this.Close(); if (Keyboard.IsKeyDown(Key.LeftCtrl) && e.Key == Key.S) editingProject.Save(); } private void RemoveVstBut_Click(object sender, RoutedEventArgs e) { Track thisTrack = (Track)this.DataContext; Button thisBut = (Button)sender; ActiveVstPlugin thisVSTRef = (ActiveVstPlugin)thisBut.DataContext; if (thisVSTRef.isNotifying) return; thisVSTRef.Close(); thisTrack.TrackEffectVsts.Remove(thisVSTRef); thisVSTRef.IsSelected = false; editingProject.NeedsSaving = true; } private void vstfxWin_Activated(object sender, EventArgs e) { //this.Topmost = true; } TextBox editingTB = null; private void DisplayNameTextBox_PreviewKeyDown(object sender, KeyEventArgs e) { TextBox thisTB = (TextBox)sender; VstFavorite thisVF = (VstFavorite)thisTB.DataContext; if (e.Key == Key.Enter) { if (thisTB.Text == "") { MessageBox.Show("Cannot have empty folder name"); return; } thisVF.FolderName = thisTB.Text; SaveVstFxFavorites(); } if (e.Key == Key.Escape | e.Key == Key.Enter) { thisTB.BorderThickness = new Thickness(0); thisTB.Background = Brushes.Transparent; thisTB.IsReadOnly = true; VstFavoriteFolderEditing = false; thisTB.Focusable = false; } if (e.Key == Key.Escape) thisTB.Text = thisVF.FolderName; } private void TreeViewItem_Collapsed(object sender, RoutedEventArgs e) { if (VstFavoriteFolderEditing) ((VstFavorite)((TreeViewItem)e.OriginalSource).DataContext).IsExpanded = true; } private void VstTreeViewMenuItem_Click(object sender, RoutedEventArgs e) { VstFxFavorites.Add(new VstFavorite("Folder")); SaveVstFxFavorites(); } private void VstTVIAddFolderMenuItem_Click(object sender, RoutedEventArgs e) { MenuItem mitem = (MenuItem)sender; VstFavorite vstf = mitem.DataContext as VstFavorite; vstf.VstFavorites.Add(new VstFavorite("Folder")); vstf.IsExpanded = true; SaveVstFxFavorites(); } private void VstTVIRemoveItemMenuItem_Click(object sender, RoutedEventArgs e) { MenuItem mitem = (MenuItem)sender; VstFavorite vstf = mitem.DataContext as VstFavorite; GetParentFavoriteCollection(VstFxFavorites, vstf).Remove(vstf); SaveVstFxFavorites(); } private bool FolderContainsFolder(VstFavorite vf1, VstFavorite vf2) { bool contains = false; ObservableCollection vfavs = VstFxFavorites; VstFavorite vfav = vf2; while (vfav != null && !VstFxFavorites.Contains(vfav)) { vfavs = GetParentFavoriteCollection(VstFxFavorites, vfav); vfav = GetAllNestedFavorites(VstFxFavorites).FirstOrDefault(vf => vf.VstFavorites == vfavs); if (vfav == vf1) { contains = true; break; } } return contains; } private List GetAllNestedFavorites (IEnumerable vsfs) { List favorites = new List(); favorites.AddRange(vsfs); foreach (VstFavorite vf in vsfs) favorites.AddRange(GetAllNestedFavorites(vf.VstFavorites)); return favorites; } private ObservableCollection GetParentFavoriteCollection (ObservableCollection VSFs, VstFavorite vsf) { if (VSFs.Contains(vsf)) return VSFs; else { ObservableCollection foundCollection = null; foreach (VstFavorite subvsf in VSFs) { foundCollection = GetParentFavoriteCollection(subvsf.VstFavorites, vsf); if (foundCollection?.Count > 0) break; } return foundCollection; } } private void VstTVITextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e) { TextBox thisTB = (TextBox)sender; VstFavorite vstf = (VstFavorite)thisTB.DataContext; if (vstf != null) { switch (vstf.ItemType) { case "Plugin": ((MenuItem)thisTB.ContextMenu.Items[0]).Visibility = Visibility.Collapsed; ((MenuItem)thisTB.ContextMenu.Items[1]).Visibility = Visibility.Collapsed; break; case "Folder": ((MenuItem)thisTB.ContextMenu.Items[2]).Visibility = Visibility.Collapsed; ((MenuItem)thisTB.ContextMenu.Items[1]).Visibility = vstf.VstFavorites.Count == 0 ? Visibility.Visible : Visibility.Collapsed; break; } } } private void ContextMenu_Opened(object sender, RoutedEventArgs e) { if (MouseOverVstItem) { VstFxFavoritesTV.ContextMenu.IsOpen = false; } MouseOverVstItem = false; } private void vstfxWin_Loaded(object sender, RoutedEventArgs e) { Track thisTrack = (Track)this.DataContext; TitleText = "Track: " + thisTrack.TrackName + " - Effects"; } private void DetailsToggleBut_Click(object sender, RoutedEventArgs e) { detailsLV.Visibility = detailsLV.IsVisible ? Visibility.Collapsed : Visibility.Visible; } private void AddFavoritesFolderBut_Click(object sender, RoutedEventArgs e) { VstFxFavorites.Add(new VstFavorite("Folder")); SaveVstFxFavorites(); } private void SelectedVstsLV_PreviewMouseMove(object sender, MouseEventArgs e) { if (MouseDownOnLVItem) { if (Math.Abs(e.GetPosition(SelectedVstsLV).Y - MouseDownOnLV_Y) > 7) { e.Handled = true; movingLVItem = true; foreach (ActiveVstPlugin avp in ((Track)this.DataContext).TrackEffectVsts.Where(tiv => tiv != movingAVP)) avp.IsEnabled = false; double yDiff = e.GetPosition(SelectedVstsLV).Y - MouseDownOnLV_Y; //if (yDiff < 0) yDiff = Math.Max(yDiff, -MouseDownOnLV_Y + 25); movingAVP.Margin = new Thickness(0, yDiff, 0, -yDiff); } } } bool MouseDownOnLVItem = false; double MouseDownOnLV_Y; ActiveVstPlugin movingAVP; private void SelectedVstsLV_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if (e.OriginalSource.GetType() != typeof(TextBlock)) return; TextBlock thisTB = (TextBlock)e.OriginalSource; if (thisTB.DataContext.GetType() == typeof(ActiveVstPlugin)) { movingAVP = (ActiveVstPlugin)thisTB.DataContext; MouseDownOnLV_Y = e.GetPosition(SelectedVstsLV).Y; MouseDownOnLVItem = true; } } bool movingLVItem = false; private void SelectedVstsLV_PreviewMouseUp(object sender, MouseButtonEventArgs e) { MouseDownOnLVItem = false; if (movingLVItem) { if (movingAVP != null) { movingAVP.Margin = new Thickness(0); foreach (ActiveVstPlugin avp in ((Track)this.DataContext).TrackEffectVsts.Where(tiv => tiv != movingAVP)) avp.IsEnabled = true; Track thisTrack = (Track)this.DataContext; int targetTrackIndex = Math.Min(thisTrack.TrackEffectVsts.Count - 1, Math.Max(0, (int)Math.Truncate(e.GetPosition(SelectedVstsLV).Y / 24 - 1))); thisTrack.TrackEffectVsts.Move(thisTrack.TrackEffectVsts.IndexOf(movingAVP), targetTrackIndex); movingLVItem = false; SelectedVstsLV.SelectedIndex = targetTrackIndex; } } } }