Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Connector/FullyCreatedConnectorInfo.cs

320 lines
9.0 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
2023-03-24 22:32:42 +08:00
using System.Windows.Input;
2023-01-12 23:02:53 +08:00
using AIStudio.Wpf.DiagramDesigner.Geometrys;
2023-01-25 11:11:27 +08:00
using AIStudio.Wpf.DiagramDesigner.Models;
2021-07-23 09:42:22 +08:00
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner
2021-07-23 09:42:22 +08:00
{
public class FullyCreatedConnectorInfo : ConnectorInfoBase
{
2023-02-01 23:05:56 +08:00
public FullyCreatedConnectorInfo(DesignerItemViewModelBase dataItem, ConnectorOrientation orientation, bool isInnerPoint = false, bool isPortless = false)
: this(null, dataItem, orientation, isInnerPoint, isPortless)
2023-01-27 14:54:03 +08:00
{
}
2023-02-01 23:05:56 +08:00
public FullyCreatedConnectorInfo(IDiagramViewModel root, DesignerItemViewModelBase dataItem, ConnectorOrientation orientation, bool isInnerPoint = false, bool isPortless = false)
2023-01-27 14:54:03 +08:00
: base(root, orientation)
2023-01-08 09:22:37 +08:00
{
2023-01-26 20:05:21 +08:00
this.Parent = dataItem;
2023-01-24 17:53:04 +08:00
this.IsInnerPoint = isInnerPoint;
2023-01-27 20:43:41 +08:00
this.IsPortless = IsPortless;
2023-01-31 22:45:50 +08:00
2023-01-24 17:53:04 +08:00
if (IsInnerPoint == true)
2023-01-08 09:22:37 +08:00
{
2023-01-24 17:53:04 +08:00
BuildMenuOptions();
2023-01-08 09:22:37 +08:00
}
}
2022-12-06 21:28:42 +08:00
2023-01-24 17:53:04 +08:00
public FullyCreatedConnectorInfo(IDiagramViewModel root, DesignerItemViewModelBase dataItem, SelectableItemBase designer) : base(root, designer)
2021-07-23 09:42:22 +08:00
{
2023-01-26 20:05:21 +08:00
this.Parent = dataItem;
2023-01-24 17:53:04 +08:00
if (IsInnerPoint == true)
{
BuildMenuOptions();
}
}
2021-07-23 09:42:22 +08:00
2023-01-25 11:11:27 +08:00
public FullyCreatedConnectorInfo(IDiagramViewModel root, DesignerItemViewModelBase dataItem, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
2023-01-24 17:53:04 +08:00
{
2023-01-26 20:05:21 +08:00
this.Parent = dataItem;
2021-07-23 09:42:22 +08:00
if (IsInnerPoint == true)
{
BuildMenuOptions();
}
}
2023-01-25 11:11:27 +08:00
public override SelectableItemBase GetSerializableObject()
{
return new FullyCreatedConnectorInfoItem(this);
}
2023-01-27 14:54:03 +08:00
protected override void Init(IDiagramViewModel root)
2021-07-23 09:42:22 +08:00
{
2023-01-27 14:54:03 +08:00
base.Init(root);
2023-01-24 17:53:04 +08:00
menuOptions = new List<CinchMenuItem>();
2023-01-25 23:55:30 +08:00
MenuItemCommand = new SimpleCommand(Command_Enable, ExecuteMenuItemCommand);
DeleteCommand = new SimpleCommand(Command_Enable, ExecuteDeleteCommand);
2023-01-24 17:53:04 +08:00
2021-07-23 09:42:22 +08:00
}
2023-01-27 14:54:03 +08:00
protected override void LoadDesignerItemViewModel(SelectableItemBase designerbase)
2021-07-23 09:42:22 +08:00
{
2023-01-27 14:54:03 +08:00
base.LoadDesignerItemViewModel(designerbase);
2021-07-23 09:42:22 +08:00
2023-01-24 17:53:04 +08:00
if (designerbase is FullyCreatedConnectorInfoItem designer)
{
XRatio = designer.XRatio;
YRatio = designer.YRatio;
IsInnerPoint = designer.IsInnerPoint;
2023-01-27 20:43:41 +08:00
IsPortless = designer.IsPortless;
2023-01-24 17:53:04 +08:00
}
2021-07-23 09:42:22 +08:00
}
2023-01-24 17:53:04 +08:00
#region
public override PointBase Position
{
get
{
return PointHelper.GetPointForConnector(this);
}
}
2023-02-11 10:03:06 +08:00
public override PointBase MiddlePosition
{
get
{
return PointHelper.GetPointForConnector(this, true);
}
}
2023-01-24 17:53:04 +08:00
private List<CinchMenuItem> menuOptions;
public IEnumerable<CinchMenuItem> MenuOptions
{
get
{
return menuOptions;
}
2021-07-23 09:42:22 +08:00
}
2022-12-06 21:28:42 +08:00
public DesignerItemViewModelBase DataItem
{
2023-01-26 20:05:21 +08:00
get
{
return Parent as DesignerItemViewModelBase;
}
2022-12-06 21:28:42 +08:00
}
2021-07-23 09:42:22 +08:00
private bool _showConnectors = false;
public bool ShowConnectors
{
get
{
return _showConnectors;
}
set
{
2022-12-06 21:28:42 +08:00
SetProperty(ref _showConnectors, value);
2021-07-23 09:42:22 +08:00
}
}
private double _xRatio;
public double XRatio
{
2022-12-06 21:28:42 +08:00
get
{
return _xRatio;
}
2021-07-23 09:42:22 +08:00
set
{
SetProperty(ref _xRatio, value);
}
}
private double _yRatio;
public double YRatio
{
2022-12-06 21:28:42 +08:00
get
{
return _yRatio;
}
2021-07-23 09:42:22 +08:00
set
{
SetProperty(ref _yRatio, value);
}
}
2023-03-10 12:09:13 +08:00
public PointBase Ratio
{
get
{
return new PointBase(XRatio, YRatio);
}
set
{
XRatio = value.X;
YRatio = value.Y;
}
}
2022-12-06 21:28:42 +08:00
public bool IsInnerPoint
{
get; set;
}
2021-07-23 09:42:22 +08:00
2023-01-27 20:10:17 +08:00
public bool IsPortless
{
get; set;
}
2023-01-23 15:43:44 +08:00
private Style _style;
public Style Style
{
get
{
return _style;
}
set
{
SetProperty(ref _style, value);
}
}
2023-01-24 17:53:04 +08:00
#endregion
2023-01-23 15:43:44 +08:00
2023-01-24 17:53:04 +08:00
#region
2023-03-24 22:32:42 +08:00
public ICommand DeleteCommand
2022-12-06 21:28:42 +08:00
{
get; private set;
}
2023-03-24 22:32:42 +08:00
public ICommand MenuItemCommand
2022-12-06 21:28:42 +08:00
{
get; private set;
}
2023-01-24 17:53:04 +08:00
#endregion
2021-07-23 09:42:22 +08:00
2023-01-24 17:53:04 +08:00
public void ExecuteMenuItemCommand(object arg)
2022-12-06 21:28:42 +08:00
{
2023-01-24 17:53:04 +08:00
Orientation = (ConnectorOrientation)arg;
2023-01-31 22:45:50 +08:00
DataItem.Left += 0.1;
DataItem.Left -= 0.1;
2023-01-24 17:53:04 +08:00
}
public void ExecuteDeleteCommand(object arg)
{
DataItem.RemoveConnector(this);
}
private void BuildMenuOptions()
{
menuOptions.Clear();
var orientation = new CinchMenuItem("方向");
2023-02-12 21:30:16 +08:00
var none = new CinchMenuItem("无");
none.Command = MenuItemCommand;
none.CommandParameter = ConnectorOrientation.None;
2023-01-24 17:53:04 +08:00
var top = new CinchMenuItem("上");
top.Command = MenuItemCommand;
top.CommandParameter = ConnectorOrientation.Top;
var bottom = new CinchMenuItem("下");
bottom.Command = MenuItemCommand;
bottom.CommandParameter = ConnectorOrientation.Bottom;
var left = new CinchMenuItem("左");
left.Command = MenuItemCommand;
left.CommandParameter = ConnectorOrientation.Left;
var right = new CinchMenuItem("右");
right.Command = MenuItemCommand;
right.CommandParameter = ConnectorOrientation.Right;
2023-02-12 21:30:16 +08:00
orientation.Children.Add(none);
2023-01-24 17:53:04 +08:00
orientation.Children.Add(top);
orientation.Children.Add(bottom);
orientation.Children.Add(left);
orientation.Children.Add(right);
var delete = new CinchMenuItem("删除");
delete.Command = DeleteCommand;
menuOptions.Add(orientation);
menuOptions.Add(delete);
2022-12-06 21:28:42 +08:00
}
2023-01-12 23:02:53 +08:00
public double GetXRatioFromConnector()
{
if (IsInnerPoint)
{
return XRatio;
}
2023-01-27 20:43:41 +08:00
else if (IsPortless)
{
return 0.5;
}
2023-01-12 23:02:53 +08:00
else
{
switch (Orientation)
{
case ConnectorOrientation.Left:
return 0;
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.TopLeft:
return 0;
case ConnectorOrientation.Top:
2023-01-12 23:02:53 +08:00
return 0.5;
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.TopRight:
return 1;
2023-01-12 23:02:53 +08:00
case ConnectorOrientation.Right:
return 1;
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.BottomRight:
return 1;
case ConnectorOrientation.Bottom:
return 0.5;
case ConnectorOrientation.BottomLeft:
return 0;
2023-01-12 23:02:53 +08:00
default: return XRatio;
}
}
}
public double GetYRatioFromConnector()
{
if (IsInnerPoint)
{
return YRatio;
}
2023-01-27 20:43:41 +08:00
else if (IsPortless)
{
return 0.5;
}
2023-01-12 23:02:53 +08:00
else
{
switch (Orientation)
{
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.Left:
return 0.5;
case ConnectorOrientation.TopLeft:
return 0;
2023-01-12 23:02:53 +08:00
case ConnectorOrientation.Top:
return 0;
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.TopRight:
return 0;
case ConnectorOrientation.Right:
2023-01-12 23:02:53 +08:00
return 0.5;
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.BottomRight:
return 1;
2023-01-12 23:02:53 +08:00
case ConnectorOrientation.Bottom:
return 1;
2023-01-27 20:43:41 +08:00
case ConnectorOrientation.BottomLeft:
return 1;
2023-01-12 23:02:53 +08:00
default: return YRatio;
}
}
}
2023-01-26 20:05:21 +08:00
2023-01-29 22:54:06 +08:00
public virtual bool CanAttachTo(FullyCreatedConnectorInfo port)
=> port != this && !port.IsReadOnly && DataItem != port.DataItem;
2021-07-23 09:42:22 +08:00
}
}