init
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DatabaseImport
|
||||
{
|
||||
public class RegionAdorner : Adorner, INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected bool SetProperty<T>(ref T storage, T value, string propertyName = null)
|
||||
{
|
||||
if (Equals(storage, value)) return false;
|
||||
|
||||
storage = value;
|
||||
OnPropertyChanged(propertyName);
|
||||
return true;
|
||||
}
|
||||
protected void OnPropertyChanged(string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
public Point GetUpperLeft()
|
||||
{
|
||||
var p = SelectRect.TopLeft;
|
||||
|
||||
var image = AdornedElement as Image;
|
||||
if (null == image) { return p; }
|
||||
|
||||
var s = new Size(image.Source.Width, image.Source.Height);
|
||||
var difX = s.Width - _MeasuredSize.Width;
|
||||
var difY = s.Height - _MeasuredSize.Height;
|
||||
var scaleX = difX / _MeasuredSize.Width;
|
||||
var scaleY = difY / _MeasuredSize.Height;
|
||||
p = new Point(p.X + p.X * scaleX, p.Y + p.Y * scaleY);
|
||||
|
||||
return p;
|
||||
}
|
||||
public Point GetLowerRight()
|
||||
{
|
||||
var p = SelectRect.BottomRight;
|
||||
|
||||
if (!(AdornedElement is Image image)) { return p; }
|
||||
if (null == image.Source) { return new Point(0, 0); }
|
||||
var s = new Size(image.Source.Width, image.Source.Height);
|
||||
var difX = (s.Width - _MeasuredSize.Width);
|
||||
var difY = s.Height - _MeasuredSize.Height;
|
||||
var scaleX = difX / _MeasuredSize.Width;
|
||||
var scaleY = difY / _MeasuredSize.Height;
|
||||
|
||||
p = new Point(p.X + p.X * scaleX, p.Y + p.Y * scaleY);
|
||||
return p;
|
||||
}
|
||||
public delegate void RegionSelectedHandler(RegionAdorner r, MouseButtonEventArgs e);
|
||||
public event RegionSelectedHandler OnRegionSelected;
|
||||
|
||||
private bool _bNew = true;
|
||||
public bool IsNew
|
||||
{
|
||||
get => _bNew;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _bNew, value, "IsNew");
|
||||
if (_bNew)
|
||||
{
|
||||
MyRegion.RegionAddVisibility = Visibility.Visible;
|
||||
MyRegion.RegionDeleteVisibility = Visibility.Hidden;
|
||||
}
|
||||
else
|
||||
{
|
||||
MyRegion.RegionDeleteVisibility = Visibility.Visible;
|
||||
MyRegion.RegionAddVisibility = Visibility.Hidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Point _anchorPoint;
|
||||
public Point AnchorPoint
|
||||
{
|
||||
get => _anchorPoint;
|
||||
set => SetProperty(ref _anchorPoint, value, "AnchorPoint");
|
||||
}
|
||||
private readonly UIElement _adornedElement;
|
||||
private readonly Path _path;
|
||||
private Rect _selectRect;
|
||||
public Rect SelectRect
|
||||
{
|
||||
get => _selectRect;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _selectRect, value, "SelectRect");
|
||||
MyRegion.RegionUpperLeft = GetUpperLeft();
|
||||
MyRegion.RegionBottomRight = GetLowerRight();
|
||||
}
|
||||
}
|
||||
private readonly RectangleGeometry _geometry;
|
||||
|
||||
private Region _region;
|
||||
public Region MyRegion
|
||||
{
|
||||
get => _region;
|
||||
set => SetProperty(ref _region, value, "MyRegion");
|
||||
}
|
||||
public RegionAdorner(UIElement adornedElement, TestObjectTemplate template, Contexts context)
|
||||
: base(adornedElement)
|
||||
{
|
||||
_region = new Region(this, template);
|
||||
_adornedElement = adornedElement;
|
||||
SelectRect = new Rect();
|
||||
_geometry = new RectangleGeometry();
|
||||
_path = new Path();
|
||||
_path.Data = _geometry;
|
||||
_path.StrokeThickness = 5;
|
||||
_path.Stroke = Brushes.AliceBlue;
|
||||
_path.Opacity = .6;
|
||||
_path.Visibility = Visibility.Hidden;
|
||||
MouseLeftButtonUp += Region_MouseLeftButtonUp;
|
||||
MouseMove += Region_MouseMove;
|
||||
MouseLeftButtonDown += Region_MouseLeftButtonDown;
|
||||
_MeasuredSize = adornedElement.RenderSize;
|
||||
//_region.PropertyChanged += _region_PropertyChanged;
|
||||
Context = context;
|
||||
}
|
||||
private void Region_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
EndSelection(sender, e);
|
||||
}
|
||||
|
||||
public enum Contexts { EditTestObject, EditTestObjectTemplate };
|
||||
private Contexts _context;
|
||||
public Contexts Context
|
||||
{
|
||||
get => _context;
|
||||
set => SetProperty(ref _context, value, "Context");
|
||||
}
|
||||
|
||||
void Region_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
OnRegionSelected?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private Size _MeasuredSize { get; set; }
|
||||
|
||||
private Point p1;
|
||||
|
||||
public void Region_MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton != MouseButtonState.Pressed) return;
|
||||
if (IsNew) { DrawSelection(sender, e, _adornedElement); }
|
||||
else { MoveSelection(sender, e, _adornedElement); }
|
||||
|
||||
var layer = AdornerLayer.GetAdornerLayer(_adornedElement);
|
||||
InvalidateVisual();
|
||||
layer.InvalidateVisual();
|
||||
}
|
||||
public void DrawSelection(object sender, MouseEventArgs e, UIElement adornedElement)
|
||||
{
|
||||
if (e.LeftButton != MouseButtonState.Pressed) return;
|
||||
var mousePosition = e.GetPosition(adornedElement);
|
||||
var r = SelectRect;
|
||||
r.X = mousePosition.X < AnchorPoint.X ? mousePosition.X : AnchorPoint.X;
|
||||
r.Y = mousePosition.Y < AnchorPoint.Y ? mousePosition.Y : AnchorPoint.Y;
|
||||
r.Width = Math.Abs(mousePosition.X - AnchorPoint.X);
|
||||
r.Height = Math.Abs(mousePosition.Y - AnchorPoint.Y);
|
||||
SelectRect = r;
|
||||
_geometry.Rect = SelectRect;
|
||||
}
|
||||
public void MoveSelection(object sender, MouseEventArgs e, UIElement adornedElement)
|
||||
{
|
||||
if (e.LeftButton != MouseButtonState.Pressed) return;
|
||||
var mousePosition = e.GetPosition(adornedElement);
|
||||
var r = SelectRect;
|
||||
r.X -= p1.X - mousePosition.X;
|
||||
r.Y -= p1.Y - mousePosition.Y;
|
||||
SelectRect = r;
|
||||
p1 = mousePosition;
|
||||
_geometry.Rect = SelectRect;
|
||||
}
|
||||
public delegate void EndSelectionHandler(RegionAdorner r);
|
||||
public event EndSelectionHandler OnEndSelection;
|
||||
|
||||
public void EndSelection(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ReleaseMouseCapture();
|
||||
OnEndSelection?.Invoke(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user