using System; using System.Collections.Specialized; using System.Windows; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Input; using JD.Collections.Extensions; using JD.Core.Collections; using JD.Timeline.View.ClipView; using JD.Timeline.View.ClipView.Widgets; using JD.WPF.Extensions; using TeaBox; using TeaBox.ObjectModel; using JD.Serialization; using DataFormats = System.Windows.DataFormats; using DataObject = System.Windows.DataObject; using DragDropEffects = System.Windows.DragDropEffects; using MouseEventArgs = System.Windows.Input.MouseEventArgs; namespace JD.Timeline.View.Pads { /// /// Interaction logic for MoveModelPad.xaml /// public partial class MoveModelControl { #region Variables private ClipViewControl _clipViewControl; private readonly CollectionViewSource _collectionViewSource; private bool _mouseEnteredPressed; #endregion #region Constructor public MoveModelControl() { InitializeComponent(); _dataGrid.MouseEnter += _dataGrid_MouseEnter; _dataGrid.MouseMove += _dataGrid_MouseMove; _dataGrid.PreviewMouseLeftButtonDown += _dataGrid_PreviewMouseLeftButtonDown; _collectionViewSource = FindResource("cvsMoveModels") as CollectionViewSource; } #endregion #region Properties /// /// The models from the tape /// private MoveModelCollection TapeModels { get { return _clipViewControl != null && _clipViewControl.Tape != null ? _clipViewControl.Tape.MoveModels() : new MoveModelCollection(); } } /// /// The models from the wpf control /// private MoveModelCollection PadModels { get { return FindResource("models") as MoveModelCollection; } } public ClipViewControl ClipViewControl { get { return _clipViewControl; } set { _clipViewControl = value; if (TapeModels != null) { TapeModels.CollectionChanged -= TapeMoveModels_CollectionChanged; TapeModels.ItemChanged -= TapeMoveModels_ItemChanged; } SynchronizeModelList(); _collectionViewSource.Source = PadModels; if (TapeModels != null) { TapeModels.CollectionChanged += TapeMoveModels_CollectionChanged; TapeModels.ItemChanged += TapeMoveModels_ItemChanged; } } } /// /// Object to allow selection of item in PropertyBrowser /// public DataSourceContainer DataSourceContainer { get; set; } private Point MouseLeftButtonDownPosition { get; set; } #endregion #region Methods /// /// Synchronize the models from wpf controls with models from tape /// void SynchronizeModelList() { PadModels.SynchronizeWith(TapeModels, PadModels.Add, model => PadModels.Remove(model)); } void TapeMoveModels_ItemChanged(object sender, ItemChangedEventArgs args) { if (args.PropertyName == "InstancesCount" && args.Item.InstancesCount <= 1) { //We need to refresh the model list when instances count changes from 0 to 1 or from 1 to 0 SynchronizeModelList(); } } void TapeMoveModels_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { SynchronizeModelList(); } void _dataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { MouseLeftButtonDownPosition = e.GetPosition(this); } // Avoid conflicting with dragged Avalon pad private void _dataGrid_MouseEnter(object sender, MouseEventArgs e) { this._mouseEnteredPressed = e.LeftButton == MouseButtonState.Pressed; } /// /// Starts the drag & drop action /// private void _dataGrid_MouseMove(object sender, MouseEventArgs e) { var moveModel = _dataGrid.GetCurrentRowItem(e.GetPosition); if (e.LeftButton == MouseButtonState.Pressed && !_mouseEnteredPressed && moveModel != null && !(e.OriginalSource is Hyperlink)) { if (e.GetPosition(this) != MouseLeftButtonDownPosition) { TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync(() => { _dataGrid.CancelEdit(); var dragData = new MoveModelDragData { ModelName = moveModel.Name }; DragDrop.DoDragDrop(_dataGrid, new DataObject(DataFormats.Serializable, dragData), DragDropEffects.Copy); }, true); } } } /// /// Allows to change the color of the model /// private void Color_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 1 && sender is FrameworkElement) { var moveModel = ((FrameworkElement)sender).DataContext as MoveModel; if (moveModel == null) return; var dlg = new ColorDialog(); dlg.Color = moveModel.Color; if (dlg.ShowDialog() == DialogResult.OK) { moveModel.Color = dlg.Color; } } } /// /// Allows to select all instances of a model on click /// private void InstancesCount_Click(object sender, RoutedEventArgs e) { var moveModel = ((FrameworkElement)sender).DataContext as MoveModel; if (ClipViewControl.TrackView != null && moveModel != null) { TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync( () => { ClipViewControl.TrackView.SelectAll(x => x.Model.Model == moveModel); ClipViewControl.ZoomExtents(); }, forceAsync: true); } } /// /// Selects the model in PropertyBrowser /// private void _dataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { var moveModel = e.AddedItems.FirstOf(); if (moveModel != null) { DataSourceContainer.SetDataSource(moveModel, null); DataSourceContainer.DataSourceName = moveModel.Name; } } #endregion } #region Dragging [Serializable] public class MoveModelDragData { private string _modelName; public string ModelName { get { return _modelName; } set { _modelName = value; } } } #endregion }