2023-05-07 23:01:38 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Media.Media3D;
|
|
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
|
using System.Windows.Controls;
|
2023-05-14 00:31:25 +08:00
|
|
|
|
using AIStudio.Wpf.DiagramDesigner.Models;
|
2023-05-07 23:01:38 +08:00
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner
|
|
|
|
|
|
{
|
|
|
|
|
|
public class DrawingDesignerItemViewModelBase : DesignerItemViewModelBase
|
|
|
|
|
|
{
|
2023-05-14 00:31:25 +08:00
|
|
|
|
public DrawingDesignerItemViewModelBase() : base()
|
2023-05-07 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-14 00:31:25 +08:00
|
|
|
|
public DrawingDesignerItemViewModelBase(IDiagramViewModel root, DrawMode drawMode, Point startPoint, bool erasable) : base(root)
|
|
|
|
|
|
{
|
|
|
|
|
|
DrawMode = drawMode;
|
|
|
|
|
|
Points = new List<Point> { startPoint };
|
|
|
|
|
|
Erasable = erasable;
|
|
|
|
|
|
if (Erasable)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Root?.DrawModeViewModel != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.ColorViewModel = CopyHelper.Mapper(Root.DrawModeViewModel.DrawingColorViewModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
this.ColorViewModel = CopyHelper.Mapper(_service.DrawModeViewModel.DrawingColorViewModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DrawingDesignerItemViewModelBase(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer)
|
2023-05-07 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-14 00:31:25 +08:00
|
|
|
|
public DrawingDesignerItemViewModelBase(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
|
2023-05-07 23:01:38 +08:00
|
|
|
|
{
|
2023-05-09 19:21:48 +08:00
|
|
|
|
|
2023-05-07 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-14 00:31:25 +08:00
|
|
|
|
public override SelectableItemBase GetSerializableObject()
|
2023-05-07 23:01:38 +08:00
|
|
|
|
{
|
2023-05-14 00:31:25 +08:00
|
|
|
|
return new DrawingDesignerItemBase(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Init(IDiagramViewModel root, bool initNew)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Init(root, initNew);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void InitNew()
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearConnectors();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void LoadDesignerItemViewModel(SelectableItemBase designerbase)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.LoadDesignerItemViewModel(designerbase);
|
|
|
|
|
|
|
|
|
|
|
|
if (designerbase is DrawingDesignerItemBase designer)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Erasable = designer.Erasable;
|
|
|
|
|
|
this.Geometry = Geometry.Parse(designer.Geometry).GetFlattenedPathGeometry();
|
2023-05-14 15:06:30 +08:00
|
|
|
|
//this.Geometry.Transform = Transform.Parse(designer.Matrix.ToString());
|
2023-05-14 00:31:25 +08:00
|
|
|
|
this.Points = designer.Points;
|
|
|
|
|
|
this.DrawMode = designer.DrawMode;
|
2023-05-14 18:02:59 +08:00
|
|
|
|
this.SelectedDisable = Erasable;
|
2023-05-14 00:31:25 +08:00
|
|
|
|
}
|
2023-05-07 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsFinish)
|
|
|
|
|
|
{
|
2023-05-14 00:31:25 +08:00
|
|
|
|
if (Geometry != null)
|
2023-05-07 23:01:38 +08:00
|
|
|
|
{
|
2023-05-14 15:06:30 +08:00
|
|
|
|
if (Geometry.Transform is TransformGroup transformGroup)
|
|
|
|
|
|
{
|
|
|
|
|
|
TranslateTransform translateTransform = transformGroup.Children.OfType<TranslateTransform>().FirstOrDefault();
|
|
|
|
|
|
transformGroup.Children.Remove(translateTransform); ;
|
|
|
|
|
|
transformGroup.Children.Add(new TranslateTransform(0 - Geometry.Bounds.Left, 0 - Geometry.Bounds.Top));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2023-05-14 18:02:59 +08:00
|
|
|
|
Geometry.Transform = new TranslateTransform(0 - Geometry.Bounds.Left, 0 - Geometry.Bounds.Top);
|
2023-05-14 15:06:30 +08:00
|
|
|
|
|
2023-05-14 00:31:25 +08:00
|
|
|
|
if (Erasable)
|
|
|
|
|
|
{
|
|
|
|
|
|
var aPen = new Pen(ColorViewModel.LineColor.ToBrush(), ColorViewModel.LineWidth);
|
|
|
|
|
|
aPen.DashStyle = new DashStyle(StrokeDashArray.Dash[(int)ColorViewModel.LineDashStyle], 1);
|
|
|
|
|
|
Geometry = Geometry.GetWidenedPathGeometry(aPen); //可擦除,需要把Geometry转成几何图像,所以不能有填充色
|
2023-05-14 18:02:59 +08:00
|
|
|
|
|
2023-05-14 00:31:25 +08:00
|
|
|
|
}
|
2023-05-14 18:02:59 +08:00
|
|
|
|
UpdateLocation();
|
2023-05-07 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool Erase(Geometry erase)
|
|
|
|
|
|
{
|
2023-05-10 22:46:39 +08:00
|
|
|
|
if (Erasable && Keyboard.IsKeyDown(Key.LeftShift) == false)
|
2023-05-07 23:01:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
erase.Transform = new TranslateTransform(0 - Left, 0 - Top);
|
|
|
|
|
|
Geometry = Geometry.Combine(Geometry, erase, GeometryCombineMode.Exclude, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (Geometry.IsEmpty())
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-05-09 19:21:48 +08:00
|
|
|
|
if (this.GetBounds().IntersectsWith(erase.Bounds))//相交
|
|
|
|
|
|
{
|
2023-05-07 23:01:38 +08:00
|
|
|
|
return true;
|
2023-05-09 19:21:48 +08:00
|
|
|
|
}
|
2023-05-07 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool Erasable
|
|
|
|
|
|
{
|
2023-05-09 19:21:48 +08:00
|
|
|
|
get; set;
|
2023-05-07 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Geometry _geometry;
|
|
|
|
|
|
public Geometry Geometry
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return _geometry;
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _geometry, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsFinish
|
|
|
|
|
|
{
|
|
|
|
|
|
get; set;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<Point> _points;
|
|
|
|
|
|
public List<Point> Points
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return _points;
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetProperty(ref _points, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-05-14 00:31:25 +08:00
|
|
|
|
|
2023-05-07 23:01:38 +08:00
|
|
|
|
public DrawMode DrawMode
|
|
|
|
|
|
{
|
|
|
|
|
|
get; set;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateLocation()
|
|
|
|
|
|
{
|
2023-05-14 18:02:59 +08:00
|
|
|
|
if (Erasable)
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemWidth = Geometry.Bounds.Width - ColorViewModel.LineWidth * 0.5;
|
|
|
|
|
|
ItemHeight = Geometry.Bounds.Height - ColorViewModel.LineWidth * 0.5;
|
|
|
|
|
|
Left = Points[0].X;
|
|
|
|
|
|
Top = Points[0].Y;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemWidth = Geometry.Bounds.Width;
|
|
|
|
|
|
ItemHeight = Geometry.Bounds.Height;
|
|
|
|
|
|
Left = Points[0].X;
|
|
|
|
|
|
Top = Points[0].Y;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void RemoveFromSelection()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Erasable == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.SelectedDisable = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
base.RemoveFromSelection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void AddToSelection(bool selected, bool clearother)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (selected == true && _service.DrawModeViewModel.DrawingDrawMode == DrawMode.Select)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.SelectedDisable = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
base.AddToSelection(selected, clearother);
|
2023-05-07 23:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|