using Bro.Common.ImageCanvas.Shapes;
|
using System;
|
using System.Collections.ObjectModel;
|
using System.Linq;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Windows.Media;
|
|
namespace Bro.Common.ImageCanvas
|
{
|
public class ObserableCollectionCanvas : Canvas
|
{
|
#region Visual
|
//public static readonly DependencyProperty VisualListProperty =
|
//DependencyProperty.Register("VisualList", typeof(ObservableCollection<ShapeElementBase>), typeof(ObserableCollectionCanvas), new PropertyMetadata(new ObservableCollection<ShapeElementBase>(), ObservableCollectionChangedCallback));
|
|
//private static void ObservableCollectionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
//{
|
// var rectangleObserableCollectionCanvas = (ObserableCollectionCanvas)d;
|
// rectangleObserableCollectionCanvas?.Redraw();
|
//}
|
|
private volatile ObservableCollection<ShapeElementBase> visualList = new ObservableCollection<ShapeElementBase>();
|
public ObservableCollection<ShapeElementBase> VisualList
|
{
|
//get => GetValue(VisualListProperty) as ObservableCollection<ShapeElementBase>;
|
//set => SetValue(VisualListProperty, value);
|
|
get => visualList;
|
set
|
{
|
if (visualList != value)
|
{
|
visualList = value;
|
Redraw();
|
|
visualList.CollectionChanged += VisualList_CollectionChanged;
|
}
|
}
|
}
|
|
private void VisualList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
{
|
//if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
|
//{
|
// int i = VisualChildrenCount;
|
//}
|
//else
|
//Dispatcher.Invoke(() =>
|
//{
|
if (e.OldItems != null)
|
{
|
foreach (ShapeElementBase shape in e.OldItems)
|
{
|
DeleteVisual(shape);
|
}
|
}
|
|
if (e.NewItems != null)
|
{
|
foreach (ShapeElementBase shape in e.NewItems)
|
{
|
shape.Draw();
|
AddVisual(shape);
|
}
|
}
|
//});
|
}
|
|
protected override int VisualChildrenCount => visualList.Count;
|
|
protected override Visual GetVisualChild(int index)
|
{
|
return VisualList.ElementAt(index);
|
}
|
|
public void AddVisual(ShapeElementBase visual)
|
{
|
AddVisualChild(visual);
|
AddLogicalChild(visual);
|
}
|
|
public void DeleteVisual(ShapeElementBase visual)
|
{
|
RemoveVisualChild(visual);
|
RemoveLogicalChild(visual);
|
}
|
#endregion
|
|
static ObserableCollectionCanvas()
|
{
|
IsHitTestVisibleProperty.OverrideMetadata(typeof(ObserableCollectionCanvas), new FrameworkPropertyMetadata(false));
|
BackgroundProperty.OverrideMetadata(typeof(ObserableCollectionCanvas), new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Transparent)));
|
}
|
|
public static readonly DependencyProperty ShowProperty =
|
DependencyProperty.Register("Show", typeof(bool), typeof(ObserableCollectionCanvas), new PropertyMetadata(true, PropertyChangedCallback));
|
public bool Show
|
{
|
get { return (bool)GetValue(ShowProperty); }
|
set { SetValue(ShowProperty, value); }
|
}
|
|
public static readonly DependencyProperty ScaleProperty =
|
DependencyProperty.Register("Scale", typeof(double), typeof(ObserableCollectionCanvas), new PropertyMetadata(.95, PropertyChangedCallback));
|
public double Scale { get { return (double)GetValue(ScaleProperty); } set { SetValue(ScaleProperty, value); } }
|
|
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
{
|
base.OnRenderSizeChanged(sizeInfo);
|
Redraw();
|
}
|
|
private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
|
{
|
(dependencyObject as ObserableCollectionCanvas)?.Redraw();
|
}
|
|
protected void Redraw()
|
{
|
if (!Show) return;
|
try
|
{
|
if (Math.Abs(ActualHeight) < 1 || Math.Abs(ActualWidth) < 1) return;
|
foreach (ShapeElementBase v in VisualList)
|
{
|
DeleteVisual(v);
|
v.Draw();
|
AddVisual(v);
|
}
|
}
|
catch (Exception ex) { }
|
}
|
|
public void OnMousePointChanged(Point point)
|
{
|
Dispatcher.Invoke(() =>
|
{
|
//Parallel.ForEach(VisualList, (v) =>
|
//{
|
// v.CheckMouseState(point);
|
//});
|
|
foreach (ShapeElementBase shape in VisualList)
|
{
|
shape.CheckMouseState(point);
|
}
|
});
|
}
|
}
|
}
|