JD2022-TU1/main/tools/framework/JD.Timeline/Dance/MoveModelControl.xaml.cs

233 lines
7.5 KiB
C#

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
{
/// <summary>
/// Interaction logic for MoveModelPad.xaml
/// </summary>
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
/// <summary>
/// The models from the tape
/// </summary>
private MoveModelCollection TapeModels
{
get
{
return _clipViewControl != null && _clipViewControl.Tape != null
? _clipViewControl.Tape.MoveModels()
: new MoveModelCollection();
}
}
/// <summary>
/// The models from the wpf control
/// </summary>
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;
}
}
}
/// <summary>
/// Object to allow selection of item in PropertyBrowser
/// </summary>
public DataSourceContainer DataSourceContainer { get; set; }
private Point MouseLeftButtonDownPosition { get; set; }
#endregion
#region Methods
/// <summary>
/// Synchronize the models from wpf controls with models from tape
/// </summary>
void SynchronizeModelList()
{
PadModels.SynchronizeWith(TapeModels, PadModels.Add, model => PadModels.Remove(model));
}
void TapeMoveModels_ItemChanged(object sender, ItemChangedEventArgs<MoveModel> 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;
}
/// <summary>
/// Starts the drag & drop action
/// </summary>
private void _dataGrid_MouseMove(object sender, MouseEventArgs e)
{
var moveModel = _dataGrid.GetCurrentRowItem<MoveModel>(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);
}
}
}
/// <summary>
/// Allows to change the color of the model
/// </summary>
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;
}
}
}
/// <summary>
/// Allows to select all instances of a model on click
/// </summary>
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<MotionClipWidget>(x => x.Model.Model == moveModel);
ClipViewControl.ZoomExtents();
},
forceAsync: true);
}
}
/// <summary>
/// Selects the model in PropertyBrowser
/// </summary>
private void _dataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var moveModel = e.AddedItems.FirstOf<MoveModel>();
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
}