Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/ViewModels/DefaultViewModel/GifImageItemViewModel.cs

164 lines
5.2 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
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 GifImageItemViewModel : DesignerItemViewModelBase
{
2023-01-24 09:02:40 +08:00
//private IDisposable propertyChangedSubscription;
//private IDisposable connectorsChangedSubscription;
2021-07-23 09:42:22 +08:00
2023-01-27 14:54:03 +08:00
public GifImageItemViewModel() : this(null)
{
2021-07-23 09:42:22 +08:00
2023-01-27 14:54:03 +08:00
}
public GifImageItemViewModel(IDiagramViewModel root) : base(root)
2021-07-23 09:42:22 +08:00
{
2023-01-27 14:54:03 +08:00
2021-07-23 09:42:22 +08:00
}
2023-01-27 14:54:03 +08:00
public GifImageItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer)
2021-07-23 09:42:22 +08:00
{
2023-01-27 14:54:03 +08:00
2021-07-23 09:42:22 +08:00
}
2023-01-25 11:11:27 +08:00
public GifImageItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
{
2023-01-27 14:54:03 +08:00
}
2023-01-24 23:10:57 +08:00
public override SelectableItemBase GetSerializableObject()
{
return new MediaDesignerItem(this);
}
2023-03-25 11:59:31 +08:00
protected override void Init(IDiagramViewModel root, bool initNew)
2023-01-31 22:45:50 +08:00
{
2023-03-25 11:59:31 +08:00
base.Init(root, initNew);
2021-07-23 09:42:22 +08:00
//propertyChangedSubscription = WhenPropertyChanged.Where(o => o.ToString() == "Left" || o.ToString() == "Top" || o.ToString() == "ItemWidth" || o.ToString() == "ItemHeight").Subscribe(ChangeImageElement);
2023-01-24 09:02:40 +08:00
//connectorsChangedSubscription = WhenConnectorsChanged.Subscribe(OnConnectorsChanged);
2023-01-31 22:45:50 +08:00
2023-01-27 14:54:03 +08:00
AddItemCommand = new SimpleCommand(Command_Enable, ExecuteAddItemCommand);
ImageSwitchCommand = new SimpleCommand(Command_Enable, ExecuteImageSwitchCommand);
2023-01-31 22:45:50 +08:00
BuildMenuOptions();
2021-07-23 09:42:22 +08:00
}
2023-03-25 11:59:31 +08:00
protected override void InitNew()
{
this.ClearConnectors();
}
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-27 14:54:03 +08:00
if (designerbase is MediaDesignerItem designer)
2021-07-23 09:42:22 +08:00
{
2023-03-25 11:59:31 +08:00
this.Icon = designer.Icon;
2021-07-23 09:42:22 +08:00
}
}
private bool _shouldInsertAnchor;
public bool ShouldInsertAnchor
{
2023-01-27 14:54:03 +08:00
get
{
return _shouldInsertAnchor;
}
2021-07-23 09:42:22 +08:00
set
{
SetProperty(ref _shouldInsertAnchor, value);
}
2023-01-27 14:54:03 +08:00
}
2023-03-24 22:32:42 +08:00
public ICommand AddItemCommand
2023-01-27 14:54:03 +08:00
{
get; private set;
}
2023-03-24 22:32:42 +08:00
public ICommand ImageSwitchCommand
2023-01-27 14:54:03 +08:00
{
get; private set;
}
2021-07-23 09:42:22 +08:00
private string dir = System.AppDomain.CurrentDomain.BaseDirectory + "Images\\Gifs";
private void BuildMenuOptions()
{
if (Directory.Exists(dir))
{
menuOptions = new ObservableCollection<CinchMenuItem>();
var equipmentImages = Directory.GetFiles(dir).Select(Path.GetFileName);
foreach (var item in equipmentImages)
{
CinchMenuItem menuItem = new CinchMenuItem();
menuItem.Text = item;
menuItem.Command = ImageSwitchCommand;
menuItem.CommandParameter = item;
menuOptions.Add(menuItem);
}
}
}
private void ExecuteAddItemCommand(object parameter)
{
2023-02-12 21:30:16 +08:00
FullyCreatedConnectorInfo connector = new FullyCreatedConnectorInfo(this, ConnectorOrientation.None, true);
2021-07-23 09:42:22 +08:00
MouseButtonEventArgs mosueArg = ((EventToCommandArgs)parameter).EventArgs as MouseButtonEventArgs;
var position = mosueArg.GetPosition(((EventToCommandArgs)parameter).Sender as IInputElement);
connector.XRatio = (position.X - connector.ConnectorWidth / 2) / connector.DataItem.ItemWidth;
connector.YRatio = (position.Y - connector.ConnectorHeight / 2) / connector.DataItem.ItemHeight;
AddConnector(connector);
}
private void ExecuteImageSwitchCommand(object parameter)
{
string image = parameter as string;
string path = dir + @"\{0}";
string filePath = string.Format(path, image);
if (File.Exists(filePath))
{
Icon = filePath;
}
}
2023-01-24 09:02:40 +08:00
//private void OnConnectorsChanged(NotifyCollectionChangedEventArgs args)
//{
// if (args.Action == NotifyCollectionChangedAction.Add)
// {
// if (args.NewItems.Count > 0)
// {
// foreach (var item in args.NewItems)
// {
// }
// }
// }
// else if (args.Action == NotifyCollectionChangedAction.Remove)
// {
// if (args.OldItems.Count > 0)
// {
// foreach (var item in args.OldItems)
// {
// }
// }
// }
// else if (args.Action == NotifyCollectionChangedAction.Reset)
// {
// }
//}
2021-07-23 09:42:22 +08:00
}
}