using QuikDawEditor.EditingClasses; using QuikDawEditor.VST; using System; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using static QuikDawEditor.EDITING.StaticProperties; using static QuikDawEditor.EDITING.VstMethods; namespace QuikDawEditor; public partial class VstiWindow : Window, INotifyPropertyChanged { bool mouseDownInTitleBar = false; Point MouseDownPos; private void TitleLabel_PreviewMouseDown(object sender, MouseButtonEventArgs e) { mouseDownInTitleBar = true; MouseDownPos = e.GetPosition(this); Mouse.Capture(this.TitleLabel); } private void TitleLabel_PreviewMouseMove(object sender, MouseEventArgs e) { if (mouseDownInTitleBar) { var newPoint = e.GetPosition(this) - MouseDownPos; this.Left += newPoint.X; this.Top += newPoint.Y; } } private void TitleLabel_PreviewMouseUp(object sender, MouseButtonEventArgs e) { mouseDownInTitleBar = false; Mouse.Capture(this.TitleLabel, CaptureMode.None); } private void VstiFavoritesTV_MouseDown(object sender, MouseButtonEventArgs e) { if (VstFavoriteFolderEditing && !MouseInTextBox) { VstFavorite thisVF = (VstFavorite)editingTB.DataContext; if (editingTB.Text == "") { MessageBox.Show("Cannot have empty folder name"); return; } editingTB.BorderThickness = new Thickness(0); editingTB.Background = Brushes.Transparent; editingTB.IsReadOnly = true; editingTB.Focusable = false; VstFavoriteFolderEditing = false; thisVF.FolderName = editingTB.Text; SaveVstiFavorites(); } } bool MouseInTextBox = false; private void DisplayNameTextBox_MouseEnter(object sender, MouseEventArgs e) { TextBox thisTB = (TextBox)sender; thisTB.Background = thisTB.IsReadOnly ? Brushes.Gray : Brushes.White; MouseInTextBox = true; } private void DisplayNameTextBox_MouseLeave(object sender, MouseEventArgs e) { TextBox thisTB = (TextBox)sender; thisTB.Background = thisTB.IsReadOnly ? Brushes.Transparent : Brushes.White; MouseInTextBox = false; } bool MouseDownOnVstItem = false; Point MouseDownPoint = new Point(); Grid draggingGrid; private void VstiItemsGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { draggingGrid = (Grid)sender; //Grid thisGrid = (Grid)sender; MouseDownPoint = e.GetPosition(draggingGrid); draggingGrid.CaptureMouse(); MouseDownOnVstItem = true; } private void VstiItemsGrid_PreviewMouseUp(object sender, MouseButtonEventArgs e) { MouseDownOnVstItem = false; draggingGrid.ReleaseMouseCapture(); } private void VstiItemsGrid_PreviewMouseMove(object sender, MouseEventArgs e) { //Grid thisGrid = (Grid)sender; if (MouseDownOnVstItem) { Vector vec = Point.Subtract(e.GetPosition(draggingGrid), MouseDownPoint); bool dragme = (Math.Abs(vec.X) > 5 || Math.Abs(vec.Y) > 3); if (dragme) { MouseDownOnVstItem = false; draggingGrid.ReleaseMouseCapture(); var dataObj = new DataObject(); dataObj.SetData("vstirefdrag", (VstPluginReference)draggingGrid.DataContext); DragDrop.DoDragDrop(draggingGrid, dataObj, DragDropEffects.Copy); } } } private void SelectedVstsLV_Drop(object sender, DragEventArgs e) { Track thisTrack = (Track)this.DataContext; if (e.Data.GetDataPresent("vstirefdrag")) { VstPluginReference thisVSTRef = (VstPluginReference)e.Data.GetData("vstirefdrag"); if (thisVSTRef.isNotifying) return; AddVSTInstrument(thisTrack, thisVSTRef); editingProject.NeedsSaving = true; if (SelectedVstsLV.Items.Count > 0) SelectedVstsLV.ScrollIntoView(SelectedVstsLV.Items[SelectedVstsLV.Items.Count - 1]); } else if (e.Data.GetDataPresent("vstifavdrag")) { VstFavorite thisVSTFav = (VstFavorite)e.Data.GetData("vstifavdrag"); VstPluginReference thisVSTRef = thisVSTFav.myPluginRef; if (thisVSTRef != null) { if (thisVSTRef.isNotifying) return; AddVSTInstrument(thisTrack, thisVSTRef); editingProject.NeedsSaving = true; if (SelectedVstsLV.Items.Count > 0) SelectedVstsLV.ScrollIntoView(SelectedVstsLV.Items[SelectedVstsLV.Items.Count - 1]); } } } private void SelectedVstsLV_DragOver(object sender, DragEventArgs e) { e.Handled = true; if (e.Data.GetDataPresent("vstirefdrag") | e.Data.GetDataPresent("vstifavdrag")) { if (e.Data.GetDataPresent("vstifavdrag")) { VstFavorite thisVSTFav = (VstFavorite)e.Data.GetData("vstifavdrag"); if (thisVSTFav.ItemType == "Folder") e.Effects = DragDropEffects.None; else e.Effects = DragDropEffects.Copy; } else e.Effects = DragDropEffects.Copy; } else e.Effects = DragDropEffects.None; } private void VstTreeViewMenuItem_Click(object sender, RoutedEventArgs e) { VstiFavorites.Add(new VstFavorite("Folder")); SaveVstiFavorites(); } 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; SaveVstiFavorites(); } private void VstTVIRemoveItemMenuItem_Click(object sender, RoutedEventArgs e) { MenuItem mitem = (MenuItem)sender; VstFavorite vstf = mitem.DataContext as VstFavorite; GetParentFavoriteCollection(VstiFavorites, vstf).Remove(vstf); SaveVstiFavorites(); } bool MouseOverVstItem = false; private void VstItemStackPanel_MouseLeave(object sender, MouseEventArgs e) { }// MouseOverVstItem = false; } private void VstItemStackPanel_MouseEnter(object sender, MouseEventArgs e) { MouseOverVstItem = true; } private void VstiFavoritesTV_DragOver(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("vstirefdrag")) { e.Effects = DragDropEffects.Copy; } else if (e.Data.GetDataPresent("vstifavdrag")) { e.Effects = DragDropEffects.Move; } else e.Effects = DragDropEffects.None; } private void VstiFavoritesTV_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("vstirefdrag")) { VstPluginReference thisVSTRef = (VstPluginReference)e.Data.GetData("vstirefdrag"); VstiFavorites.Add(new VstFavorite("Plugin", thisVSTRef)); SaveVstiFavorites(); } else if (e.Data.GetDataPresent("vstifavdrag")) { VstFavorite thisVSTFav = (VstFavorite)e.Data.GetData("vstifavdrag"); GetParentFavoriteCollection(VstiFavorites, thisVSTFav).Remove(thisVSTFav); VstiFavorites.Add(thisVSTFav); SaveVstiFavorites(); } } private void TextBox_PreviewDragOver(object sender, DragEventArgs e) { e.Handled = true; StackPanel thisSP = (StackPanel)sender; VstFavorite vstf = (VstFavorite)thisSP.DataContext; if (vstf.ItemType == "Folder") { if (e.Data.GetDataPresent("vstirefdrag")) { e.Effects = DragDropEffects.Copy; } else if (e.Data.GetDataPresent("vstifavdrag")) { VstFavorite vfavsource = (VstFavorite)e.Data.GetData("vstifavdrag"); bool folderContainsFolder = vfavsource.ItemType == "Folder" && FolderContainsFolder(vfavsource, vstf); if (vstf.VstFavorites.Contains(vfavsource) || folderContainsFolder) e.Effects = DragDropEffects.None; else e.Effects = DragDropEffects.Move; } } else e.Effects = DragDropEffects.None; } private void TextBox_PreviewDrop(object sender, DragEventArgs e) { e.Handled = true; StackPanel thisSP = (StackPanel)sender; VstFavorite vstf = (VstFavorite)thisSP.DataContext; if (e.Data.GetDataPresent("vstirefdrag")) { VstPluginReference thisVSTRef = (VstPluginReference)e.Data.GetData("vstirefdrag"); vstf.VstFavorites.Insert(0, new VstFavorite("Plugin", thisVSTRef)); vstf.IsExpanded = true; SaveVstiFavorites(); } else if (e.Data.GetDataPresent("vstifavdrag")) { VstFavorite thisVSTFav = (VstFavorite)e.Data.GetData("vstifavdrag"); GetParentFavoriteCollection(VstiFavorites, thisVSTFav).Remove(thisVSTFav); vstf.VstFavorites.Insert(0, thisVSTFav); vstf.IsExpanded = true; SaveVstiFavorites(); } } bool MouseDownOnVstFavorite = false; Point MouseDownOnVstFavoritePoint = new Point(); TextBox editingTB = null; bool VstFavoriteFolderEditing = false; private void DisplayNameTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) { TextBox thisTB = (TextBox)sender; VstFavorite thisVF = (VstFavorite)thisTB.DataContext; if (e.ClickCount == 2) { if (VstFavoriteFolderEditing) return; if (thisVF.ItemType == "Folder") { editingTB = thisTB; thisTB.BorderThickness = new Thickness(1); thisTB.Background = Brushes.White; thisTB.IsReadOnly = false; VstFavoriteFolderEditing = true; thisTB.Focusable = true; thisTB.SelectAll(); } return; } MouseDownOnVstFavorite = true; MouseDownOnVstFavoritePoint = e.GetPosition(thisTB); thisTB.CaptureMouse(); } private void DisplayNameTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e) { MouseDownOnVstFavorite = false; ((TextBox)sender).ReleaseMouseCapture(); } private void DisplayNameTextBox_PreviewMouseMove(object sender, MouseEventArgs e) { if (MouseDownOnVstFavorite) { TextBox vstfTB = (TextBox)sender; VstFavorite thisVFav = (VstFavorite)vstfTB.DataContext; bool isDraggablePlugin = thisVFav.ItemType == "Plugin" && thisVFav.ExistsVsti; if (isDraggablePlugin) { Vector vec = Point.Subtract(e.GetPosition(vstfTB), MouseDownOnVstFavoritePoint); bool dragme = (Math.Abs(vec.X) > 6 || Math.Abs(vec.Y) > 6); if (dragme) { MouseDownOnVstFavorite = false; vstfTB.ReleaseMouseCapture(); var dataObj = new DataObject(); dataObj.SetData("vstifavdrag", thisVFav); DragDrop.DoDragDrop(vstfTB, dataObj, DragDropEffects.Move | DragDropEffects.Copy); } } } } bool MouseDownOnLVItem = false; double MouseDownOnLV_Y; ActiveVstPlugin movingAVP; 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).TrackInstrumentVsts.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); } } } 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).TrackInstrumentVsts.Where(tiv => tiv != movingAVP)) avp.IsEnabled = true; Track thisTrack = (Track)this.DataContext; int targetTrackIndex = Math.Min(thisTrack.TrackInstrumentVsts.Count - 1, Math.Max(0, (int)Math.Truncate(e.GetPosition(SelectedVstsLV).Y / 24 - 1))); thisTrack.TrackInstrumentVsts.Move(thisTrack.TrackInstrumentVsts.IndexOf(movingAVP), targetTrackIndex); movingLVItem = false; SelectedVstsLV.SelectedIndex = targetTrackIndex; } } } bool scrolling = false; private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { if (scrolling) return; scrolling = true; ScrollViewer sviewer = (ScrollViewer)sender; sviewer.ScrollToVerticalOffset(sviewer.VerticalOffset - e.Delta / 3); scrolling = false; } }