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

248 lines
8.2 KiB
C#

using System;
using System.Collections.Specialized;
using System.Linq;
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 DataGrid = System.Windows.Controls.DataGrid;
using DataObject = System.Windows.DataObject;
using DragDropEffects = System.Windows.DragDropEffects;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
namespace JD.Timeline.View.Pads
{
/// <summary>
/// Interaction logic for PictogramModelWidget.xaml
/// </summary>
public partial class PictogramModelControl
{
#region Variables
private ClipViewControl _clipViewControl;
private readonly CollectionViewSource _collectionViewSource;
private bool _mouseEnteredPressed;
#endregion
#region Constructor
public PictogramModelControl()
{
InitializeComponent();
_dataGrid.MouseEnter += _dataGrid_MouseEnter;
_dataGrid.MouseMove += _dataGrid_MouseMove;
_dataGrid.PreviewMouseLeftButtonDown += _dataGrid_PreviewMouseLeftButtonDown;
_collectionViewSource = FindResource("cvsPictoModels") as CollectionViewSource;
}
#endregion
#region Properties
/// <summary>
/// The models from the tape
/// </summary>
private PictogramModelCollection TapeModels
{
get
{
return _clipViewControl != null && _clipViewControl.Tape != null
? _clipViewControl.Tape.PictogramModels()
: new PictogramModelCollection();
}
}
/// <summary>
/// The models from the wpf control
/// </summary>
private PictogramModelCollection PadModels
{
get
{
return FindResource("models") as PictogramModelCollection;
}
}
public ClipViewControl ClipViewControl
{
get { return _clipViewControl; }
set
{
_clipViewControl = value;
if (TapeModels != null)
{
TapeModels.CollectionChanged -= TapePictoModels_CollectionChanged;
TapeModels.ItemChanged -= TapePictoModels_ItemChanged;
}
SynchronizeModelList();
_collectionViewSource.Source = PadModels;
if (TapeModels != null)
{
TapeModels.CollectionChanged += TapePictoModels_CollectionChanged;
TapeModels.ItemChanged += TapePictoModels_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>
private void SynchronizeModelList()
{
PadModels.SynchronizeWith(TapeModels, PadModels.Add, model => PadModels.Remove(model));
}
private void TapePictoModels_ItemChanged(object sender, ItemChangedEventArgs<PictogramModel> 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();
}
if (args.PropertyName == "BitmapThumbnail")
{
TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync(() =>
{
_dataGrid.Items.Refresh();
});
}
}
private void TapePictoModels_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<PictogramModel>(e.GetPosition);
if (e.LeftButton == MouseButtonState.Pressed
&& moveModel != null
&& !(e.OriginalSource is Hyperlink))
{
if (e.GetPosition(this) != MouseLeftButtonDownPosition)
{
_dataGrid.CancelEdit();
TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync(() =>
{
var dragData = new PictoModelDragData {ModelName = moveModel.Name};
try
{
DragDrop.DoDragDrop(_dataGrid, new DataObject(DataFormats.Serializable, dragData),
DragDropEffects.Copy);
}
catch (Exception)
{
MessageService.MessageService.ShowBalloon(TimeSpan.FromSeconds(10), "Warning", "An error occurred trying to Drag the Pictogram", ToolTipIcon.Warning);
}
}, true);
}
}
}
/// <summary>
/// Allow to select all instances of a model on click
/// </summary>
private void InstancesCount_Click(object sender, RoutedEventArgs e)
{
var pictoModel = ((FrameworkElement)sender).DataContext as PictogramModel;
if (ClipViewControl.TrackView != null && pictoModel != null)
{
TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync(
() =>
{
ClipViewControl.TrackView.SelectAll<PictogramClipWidget>(x => x.Model.Model == pictoModel);
ClipViewControl.ZoomExtents();
},
forceAsync: true);
}
}
/// <summary>
/// Selects the model in PropertyBrowser
/// </summary>
private void _dataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var pictoModel = e.AddedItems.Count > 0 ? e.AddedItems[0] as PictogramModel : null;
if (pictoModel != null)
{
DataSourceContainer.SetDataSource(pictoModel, null);
DataSourceContainer.DataSourceName = pictoModel.Name;
}
}
#endregion
private void _dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
var grid = (DataGrid)sender;
if ( Key.Delete == e.Key )
{
foreach (var model in grid.SelectedItems.Cast<PictogramModel>().ToArray())
{
if (model.InstancesCount == 0)
{
model.Instances.ToArray().ForEach(x => x.Tape.Clips.Remove(x));
model.Tape.PictogramModels().Remove(model);
}
}
}
}
}
#region Dragging
[Serializable]
public class PictoModelDragData
{
private string _modelName;
public string ModelName
{
get { return _modelName; }
set { _modelName = value; }
}
}
#endregion
}