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

200 lines
5.5 KiB
C#
Raw Normal View History

2023-01-24 17:53:04 +08:00
using System;
using System.Linq;
2023-03-24 22:32:42 +08:00
using System.Windows.Input;
2023-01-22 21:46:59 +08:00
using AIStudio.Wpf.DiagramDesigner.Geometrys;
2023-01-25 11:11:27 +08:00
using AIStudio.Wpf.DiagramDesigner.Models;
2023-01-22 21:46:59 +08:00
using SvgPathProperties;
namespace AIStudio.Wpf.DiagramDesigner
2023-01-08 09:22:37 +08:00
{
2023-01-24 16:20:39 +08:00
public class ConnectorLabelModel : ConnectorPointModel, ISelectable
2023-01-08 09:22:37 +08:00
{
2023-01-27 14:54:03 +08:00
public ConnectorLabelModel(ConnectionViewModel connector, string content, double? distance = null, PointBase? offset = null) : this(null, connector, content, distance, offset)
{
}
public ConnectorLabelModel(IDiagramViewModel root, ConnectionViewModel connector, string content, double? distance = null, PointBase? offset = null) : base(root)
2023-01-08 09:22:37 +08:00
{
2023-01-24 16:20:39 +08:00
Parent = connector;
2023-01-22 21:46:59 +08:00
Text = content;
2023-01-08 09:22:37 +08:00
Distance = distance;
2023-01-27 14:54:03 +08:00
Offset = offset ?? new PointBase();
2023-01-08 09:22:37 +08:00
}
2023-01-24 17:53:04 +08:00
public ConnectorLabelModel(IDiagramViewModel root, ConnectionViewModel connector, SelectableItemBase designer) : base(root, designer)
{
Parent = connector;
}
2023-01-25 11:11:27 +08:00
public ConnectorLabelModel(IDiagramViewModel root, ConnectionViewModel connector, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
2023-01-24 17:53:04 +08:00
{
Parent = connector;
}
2023-01-25 11:11:27 +08:00
public override SelectableItemBase GetSerializableObject()
{
return new ConnectorLabelItem(this);
}
2023-01-27 14:54:03 +08:00
protected override void Init(IDiagramViewModel root)
2023-01-08 09:22:37 +08:00
{
2023-01-27 14:54:03 +08:00
base.Init(root);
2023-01-22 21:46:59 +08:00
ConnectorWidth = 30;
ConnectorHeight = 30;
2023-01-25 23:55:30 +08:00
DeleteLabelCommand = new SimpleCommand(Command_Enable, DeleteLabel);
2023-01-29 22:54:06 +08:00
EditCommand = new SimpleCommand(Command_Enable, ExecuteEditCommand);
2023-01-22 21:46:59 +08:00
}
2023-01-27 14:54:03 +08:00
protected override void LoadDesignerItemViewModel(SelectableItemBase designerbase)
2023-01-24 17:53:04 +08:00
{
2023-01-27 14:54:03 +08:00
base.LoadDesignerItemViewModel(designerbase);
2023-01-24 17:53:04 +08:00
if (designerbase is ConnectorLabelItem designer)
{
Distance = designer.Distance;
Offset = designer.Offset;
}
}
2023-01-24 16:20:39 +08:00
#region
2023-01-24 17:53:04 +08:00
public ConnectionViewModel Connector
2023-01-22 21:46:59 +08:00
{
get
{
2023-01-24 17:53:04 +08:00
return Parent as ConnectionViewModel;
2023-01-22 21:46:59 +08:00
}
2023-01-08 09:22:37 +08:00
}
/// <summary>
/// 3 types of values are possible:
/// <para>- A number between 0 and 1: Position relative to the link's length</para>
/// <para>- A positive number, greater than 1: Position away from the start</para>
/// <para>- A negative number, less than 0: Position away from the end</para>
/// </summary>
2023-01-22 21:46:59 +08:00
public double? Distance
{
get; set;
}
public PointBase Offset
{
get; set;
}
2023-03-24 22:32:42 +08:00
public ICommand DeleteLabelCommand
2023-01-22 21:46:59 +08:00
{
2023-01-23 15:43:44 +08:00
get; set;
2023-01-22 21:46:59 +08:00
}
2023-01-29 22:54:06 +08:00
2023-03-24 22:32:42 +08:00
public ICommand EditCommand
2023-01-29 22:54:06 +08:00
{
get; private set;
}
2023-01-24 16:20:39 +08:00
#endregion
2023-01-22 21:46:59 +08:00
private bool updating = false;
public void UpdatePosition(SvgPath[] paths)
{
2023-01-25 14:42:01 +08:00
if (paths == null)
return;
2023-01-22 21:46:59 +08:00
var position = FindPosition(paths);
if (position == null)
return;
updating = true;
X = position.Value.X;
Y = position.Value.Y;
updating = false;
}
public PointBase? FindPosition(SvgPath[] paths)
{
var totalLength = paths.Sum(p => p.Length);
double length;
if (Distance >= 0 && Distance <= 1)
{
length = Distance.Value * totalLength;
}
else if (Distance > 1)
{
length = Distance.Value;
}
else if (Distance < 0)
{
length = totalLength + Distance.Value;
}
else
{
2023-01-24 16:20:39 +08:00
length = totalLength * (Connector.Labels.IndexOf(this) + 1) / (Connector.Labels.Count + 1);
2023-01-22 21:46:59 +08:00
}
foreach (var path in paths)
{
var pathLength = path.Length;
if (length < pathLength)
{
var pt = path.GetPointAtLength(length);
return new PointBase(pt.X + Offset.X, pt.Y + Offset.Y);
}
length -= pathLength;
}
return null;
}
public void UpdateOffsetX(double oldvalue, double newvalue)
{
if (updating == true) return;
Offset += new VectorBase(newvalue - oldvalue, 0);
}
public void UpdateOffsetY(double oldvalue, double newvalue)
{
if (updating == true) return;
Offset += new VectorBase(0, newvalue - oldvalue);
}
2023-01-24 16:20:39 +08:00
public override void AddToSelection(bool selected)
2023-01-22 21:46:59 +08:00
{
2023-01-24 16:20:39 +08:00
foreach (var item in Connector.Labels.ToList())
2023-01-22 21:46:59 +08:00
item.IsSelected = false;
if (selected == true)
{
IsSelected = true;
}
}
2023-02-01 23:05:56 +08:00
protected override void ClearText()
{
Connector.Labels.Remove(this);
}
2023-01-23 15:43:44 +08:00
private void DeleteLabel(object parameter)
{
if (parameter is ConnectorLabelModel label)
{
2023-01-24 16:20:39 +08:00
Connector.Labels.Remove(label);
2023-01-23 15:43:44 +08:00
}
}
2023-01-29 22:54:06 +08:00
protected virtual void ExecuteEditCommand(object param)
{
if (IsReadOnly == true) return;
RaisePropertyChanged("ShowText");
}
2023-01-08 09:22:37 +08:00
}
}