Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramApp/ViewModels/MainWindowViewModel.cs

855 lines
27 KiB
C#
Raw Normal View History

using System;
2021-07-23 09:42:22 +08:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using AIStudio.Wpf.DiagramApp.Views;
2022-10-28 22:45:39 +08:00
using AIStudio.Wpf.DiagramDesigner;
2023-01-25 15:58:05 +08:00
using AIStudio.Wpf.DiagramDesigner.Additionals;
using AIStudio.Wpf.DiagramDesigner.Additionals.Commands;
2023-03-18 21:44:58 +08:00
using AIStudio.Wpf.DiagramDesigner.ViewModels;
using AIStudio.Wpf.DiagramDesigner.ViewModels.BaseViewModel;
2023-04-02 12:01:46 +08:00
using AIStudio.Wpf.Mind.Models;
using ControlzEx.Theming;
using Dragablz;
using Fluent;
using Newtonsoft.Json;
2021-07-23 09:42:22 +08:00
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramApp.ViewModels
2021-07-23 09:42:22 +08:00
{
public class MainWindowViewModel : BindableBase
{
2023-03-20 20:23:20 +08:00
private IDiagramServiceProvider _service
{
get
{
return DiagramServicesProvider.Instance.Provider;
}
}
2021-07-23 09:42:22 +08:00
private string _history = System.AppDomain.CurrentDomain.BaseDirectory + "history.json";
public MainWindowViewModel()
{
ToolBoxViewModel = new ToolBoxViewModel(this);
2021-07-23 09:42:22 +08:00
2023-05-27 12:35:44 +08:00
PageViewModels = new ObservableCollection<PageViewModel>();
PageViewModels.Add(new PageViewModel("新建-1", "*", DiagramType.Normal));
PageViewModel = PageViewModels.FirstOrDefault();
2021-07-23 09:42:22 +08:00
StandardColor = GenerateStandardGradients();
if (File.Exists(_history))
{
HistoryList = JsonConvert.DeserializeObject<ObservableCollection<string>>(File.ReadAllText(_history));
}
else
{
HistoryList = new ObservableCollection<string>();
}
_service.PropertyChanged += Provider_PropertyChanged;
}
#region
2023-03-20 20:23:20 +08:00
public ToolBoxViewModel ToolBoxViewModel
{
get; private set;
}
2021-07-23 09:42:22 +08:00
2023-05-27 12:35:44 +08:00
private ObservableCollection<PageViewModel> _pageViewModels;
public ObservableCollection<PageViewModel> PageViewModels
2021-07-23 09:42:22 +08:00
{
get
{
2023-05-27 12:35:44 +08:00
return _pageViewModels;
2021-07-23 09:42:22 +08:00
}
set
{
2023-05-27 12:35:44 +08:00
SetProperty(ref _pageViewModels, value);
2021-07-23 09:42:22 +08:00
}
}
2023-03-11 22:27:23 +08:00
private PageViewModel _pageViewModel;
2023-02-12 21:30:16 +08:00
public PageViewModel PageViewModel
2021-07-23 09:42:22 +08:00
{
get
{
2023-03-11 22:27:23 +08:00
return _pageViewModel;
2021-07-23 09:42:22 +08:00
}
set
{
2023-04-15 21:55:27 +08:00
if (SetProperty(ref _pageViewModel, value))
{
}
2021-07-23 09:42:22 +08:00
}
}
2023-05-09 23:22:17 +08:00
private Models.ColorType _colorType;
2021-07-29 13:55:18 +08:00
public Models.ColorType ColorType
2021-07-23 09:42:22 +08:00
{
get
{
2023-05-09 23:22:17 +08:00
return _colorType;
2021-07-23 09:42:22 +08:00
}
set
{
2023-05-09 23:22:17 +08:00
SetProperty(ref _colorType, value);
2021-07-23 09:42:22 +08:00
}
}
private bool _isOpenBackstage;
public bool IsOpenBackstage
{
get
{
return _isOpenBackstage;
}
set
{
SetProperty(ref _isOpenBackstage, value);
}
}
private ObservableCollection<string> _historyList;
public ObservableCollection<string> HistoryList
{
get
{
return _historyList;
}
set
{
SetProperty(ref _historyList, value);
}
}
2023-03-20 20:23:20 +08:00
public Color[] StandardColor
{
get; set;
}
2021-07-23 09:42:22 +08:00
public IDrawModeViewModel DrawModeViewModel
{
get
{
return _service.DrawModeViewModel;
}
}
public IFontViewModel FontViewModel
{
get
{
return _service.FontViewModel;
}
}
public IColorViewModel ColorViewModel
{
get
{
return _service.ColorViewModel;
}
}
public IShapeViewModel ShapeViewModel
{
get
{
return _service.ShapeViewModel;
}
}
2023-04-29 15:29:22 +08:00
public IAnimationViewModel AnimationViewModel
{
get
{
return _service.AnimationViewModel;
}
}
2021-07-23 09:42:22 +08:00
public IQuickThemeViewModel QuickThemeViewModel
{
get
{
return _service.QuickThemeViewModel;
}
}
public ILockObjectViewModel LockObjectViewModel
{
get
{
return _service.LockObjectViewModel;
}
}
public SelectableDesignerItemViewModelBase SelectedItemViewModel
2021-07-23 09:42:22 +08:00
{
get
{
return _service.SelectedItemViewModel;
2021-07-23 09:42:22 +08:00
}
}
public Color ThemeColor
{
get => ((SolidColorBrush)Application.Current.Resources["Fluent.Ribbon.Brushes.AccentBaseColorBrush"])?.Color ?? Colors.Pink;
set
{
var solidColorBrush = new SolidColorBrush(value);
solidColorBrush.Freeze();
Application.Current.Resources["Fluent.Ribbon.Brushes.AccentBaseColorBrush"] = solidColorBrush;
}
}
public string CurrentBaseColor
{
get => this.CurrentTheme.BaseColorScheme;
set
{
if (value is null)
{
return;
}
ThemeManager.Current.ChangeThemeBaseColor(Application.Current, value);
RaisePropertyChanged(nameof(this.CurrentTheme));
}
}
public Theme CurrentTheme
{
get => ThemeManager.Current.DetectTheme(Application.Current);
set
{
if (value is null)
{
return;
}
ThemeManager.Current.ChangeTheme(Application.Current, value);
RaisePropertyChanged(nameof(this.CurrentBaseColor));
}
}
#endregion
2023-02-12 21:30:16 +08:00
public Func<PageViewModel> NewItemFactory
2021-07-23 09:42:22 +08:00
{
get
{
return
2023-03-20 20:23:20 +08:00
() => {
2023-05-27 12:35:44 +08:00
return new PageViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", DiagramType.Normal);
2021-07-23 09:42:22 +08:00
};
}
}
#region
private ICommand _newCommand;
public ICommand NewCommand
{
get
{
return this._newCommand ?? (this._newCommand = new DelegateCommand<string>(para => this.New_Executed(para)));
}
}
2023-03-20 20:23:20 +08:00
private ICommand _newMindCommand;
public ICommand NewMindCommand
{
get
{
return this._newMindCommand ?? (this._newMindCommand = new DelegateCommand<string>(para => this.NewMind_Executed(para)));
}
}
2021-07-23 09:42:22 +08:00
private ICommand _openCommand;
public ICommand OpenCommand
{
get
{
return this._openCommand ?? (this._openCommand = new DelegateCommand<string>(para => this.OpenExecuted(para)));
}
}
private ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
return this._saveCommand ?? (this._saveCommand = new CanExecuteDelegateCommand(() => this.SaveExecuted(), () => this.Save_Enable()));
}
}
private ICommand _saveAsCommand;
public ICommand SaveAsCommand
{
get
{
return this._saveAsCommand ?? (this._saveAsCommand = new CanExecuteDelegateCommand(() => this.SaveAsExecuted(), () => this.Save_Enable()));
}
}
private ICommand _pasteCommand;
public ICommand PasteCommand
{
get
{
return this._pasteCommand ?? (this._pasteCommand = new CanExecuteDelegateCommand(() => this.PasteExecuted(), () => this.Paste_Enabled()));
}
}
private ICommand _cutCommand;
public ICommand CutCommand
{
get
{
return this._cutCommand ?? (this._cutCommand = new CanExecuteDelegateCommand(() => this.CutExecuted(), () => this.Cut_Enabled()));
}
}
private ICommand _copyCommand;
public ICommand CopyCommand
{
get
{
return this._copyCommand ?? (this._copyCommand = new CanExecuteDelegateCommand(() => this.CopyExecuted(), () => Copy_Enabled()));
}
}
private ICommand _exitCommand;
public ICommand ExitCommand
{
get
{
return this._exitCommand ?? (this._exitCommand = new CanExecuteDelegateCommand(() => this.ExitExecuted()));
}
}
private ICommand _formatCommand;
public ICommand FormatCommand
{
get
{
return this._formatCommand ?? (this._formatCommand = new CanExecuteDelegateCommand(() => this.FormatExecuted(), () => Format_Enabled()));
}
}
private ICommand _deleteCommand;
public ICommand DeleteCommand
{
get
{
return this._deleteCommand ?? (this._deleteCommand = new CanExecuteDelegateCommand(() => this.DeleteExecuted(), () => Delete_Enabled()));
}
2023-04-08 18:08:00 +08:00
}
2021-07-23 09:42:22 +08:00
private ICommand _lockCommand;
public ICommand LockCommand
{
get
{
return this._lockCommand ?? (this._lockCommand = new DelegateCommand<object>(para => this.LockExecuted(para)));
}
}
private ICommand _unlockCommand;
public ICommand UnlockCommand
{
get
{
return this._unlockCommand ?? (this._unlockCommand = new DelegateCommand<object>(para => this.UnlockExecuted(para)));
}
}
private ICommand _selectedColorCommand;
public ICommand SelectedColorCommand
{
get
{
return this._selectedColorCommand ?? (this._selectedColorCommand = new DelegateCommand<object>(para => this.SelectedColorExecuted(para)));
}
}
2021-07-23 09:42:22 +08:00
private ICommand _aboutCommand;
public ICommand AboutCommand
{
get
{
return this._aboutCommand ?? (this._aboutCommand = new DelegateCommand(() => this.AboutExecuted()));
}
}
2023-04-09 21:48:26 +08:00
2023-05-14 00:31:25 +08:00
private ICommand _colorPickerCommand;
public ICommand ColorPickerCommand
{
get
{
return this._colorPickerCommand ?? (this._colorPickerCommand = new DelegateCommand(() => this.ColorPickerExecuted()));
}
}
2023-04-09 21:48:26 +08:00
private ICommand _screenshotCommand;
public ICommand ScreenshotCommand
{
get
{
return this._screenshotCommand ?? (this._screenshotCommand = new DelegateCommand(() => this.ScreenshotExecuted()));
}
}
2021-07-23 09:42:22 +08:00
#endregion
public ItemActionCallback ClosingTabItemHandler
{
2023-03-20 20:23:20 +08:00
get
{
return ClosingTabItemHandlerImpl;
}
2021-07-23 09:42:22 +08:00
}
/// <summary>
/// Callback to handle tab closing.
/// </summary>
private void ClosingTabItemHandlerImpl(ItemActionCallbackArgs<TabablzControl> args)
{
//here's how you can cancel stuff:
//args.Cancel();
2021-08-02 18:08:43 +08:00
2023-02-12 21:30:16 +08:00
if (args.DragablzItem.DataContext is PageViewModel viewModel)
2021-08-02 18:08:43 +08:00
{
viewModel.Dispose();
}
2021-07-23 09:42:22 +08:00
}
private void Provider_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(DrawModeViewModel)
|| e.PropertyName == nameof(FontViewModel)
|| e.PropertyName == nameof(ColorViewModel)
|| e.PropertyName == nameof(ShapeViewModel)
2023-04-29 15:29:22 +08:00
|| e.PropertyName == nameof(AnimationViewModel)
2021-07-23 09:42:22 +08:00
|| e.PropertyName == nameof(QuickThemeViewModel)
|| e.PropertyName == nameof(LockObjectViewModel)
|| e.PropertyName == nameof(SelectedItemViewModel))
2021-07-23 09:42:22 +08:00
{
RaisePropertyChanged(e.PropertyName);
}
if (PageViewModel == null || PageViewModel.DiagramViewModel == null) return;
2021-07-23 09:42:22 +08:00
if (sender is IFontViewModel)
{
PageViewModel.DiagramViewModel.SetFont(sender as IFontViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
}
else if (sender is IColorViewModel)
{
PageViewModel.DiagramViewModel.SetColor(sender as IColorViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
}
else if (sender is IShapeViewModel)
{
PageViewModel.DiagramViewModel.SetSharp(sender as IShapeViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
}
2023-04-29 18:36:50 +08:00
else if (sender is IAnimationViewModel)
{
PageViewModel.DiagramViewModel.SetAnimation(sender as IAnimationViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
}
else if (sender is IQuickThemeViewModel)
{
PageViewModel.DiagramViewModel.SetQuickItem(sender as IQuickThemeViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
}
else if (sender is LockObject)
{
PageViewModel.DiagramViewModel.LockAction(sender as LockObject, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
}
else if (sender is SelectableDesignerItemViewModelBase designer)
{
PageViewModel.DiagramViewModel.SetPropertyValue(designer, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems);
2021-07-23 09:42:22 +08:00
}
}
2023-02-04 20:59:01 +08:00
public bool KeyExecuted(KeyEventArgs e)
2021-07-23 09:42:22 +08:00
{
2023-02-04 20:59:01 +08:00
var para = e.KeyboardDevice.Modifiers == ModifierKeys.None ? e.Key.ToString() : e.KeyboardDevice.Modifiers.ToString() + "+" + e.Key.ToString();
bool executed = true;
2023-03-20 20:23:20 +08:00
switch (para)
2021-07-23 09:42:22 +08:00
{
case "Control+O": OpenExecuted(); break;
case "Control+N": New_Executed(); break;
2023-03-20 20:23:20 +08:00
case "Control+S": SaveExecuted(); break;
default: executed = false; break;
2021-07-23 09:42:22 +08:00
}
return executed;
}
2021-07-23 09:42:22 +08:00
private void OpenExecuted(string para = null)
{
string filename = string.Empty;
if (string.IsNullOrEmpty(para))
{
Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();
openFile.Filter = "Designer Files (*.xml;*.json)|*.xml;*.json|All Files (*.*)|*.*";
if (openFile.ShowDialog() == false)
{
return;
}
filename = openFile.FileName;
}
else
{
filename = para;
}
2023-05-27 12:35:44 +08:00
var viewmodel = PageViewModels.FirstOrDefault(p => p.FileName == filename);
2021-07-23 09:42:22 +08:00
if (viewmodel != null)
{
2023-02-12 21:30:16 +08:00
PageViewModel = viewmodel;
2021-07-23 09:42:22 +08:00
MessageBox.Show("文档已经打开");
return;
}
2023-02-12 21:30:16 +08:00
var diagram = PageViewModel.OpenFile(filename, Path.GetExtension(filename));
PageViewModel flow;
2021-08-03 18:19:47 +08:00
if (diagram.DiagramType == DiagramType.FlowChart)
{
flow = new FlowchartViewModel(filename, diagram);
}
else if (diagram.DiagramType == DiagramType.Logical)
{
flow = new LogicalViewModel(filename, diagram);
}
else if (diagram.DiagramType == DiagramType.SFC)
{
flow = new SFCViewModel(filename, diagram);
}
2023-03-08 19:45:07 +08:00
else if (diagram.DiagramType == DiagramType.Mind)
{
flow = new MindViewModel(filename, diagram);
}
else if (diagram.DiagramType == DiagramType.Drawing)
{
flow = new DrawingViewModel(filename, diagram);
}
2021-08-03 18:19:47 +08:00
else
{
2023-02-12 21:30:16 +08:00
flow = new PageViewModel(filename, diagram);
2021-08-03 18:19:47 +08:00
}
2023-05-27 12:35:44 +08:00
PageViewModels.Add(flow);
2023-02-12 21:30:16 +08:00
PageViewModel = flow;
2021-07-23 09:42:22 +08:00
if (string.IsNullOrEmpty(para))
{
2023-02-12 21:30:16 +08:00
SaveHistory(PageViewModel);
2021-07-23 09:42:22 +08:00
}
else
{
IsOpenBackstage = false;
}
}
private void SaveExecuted()
{
2023-02-12 21:30:16 +08:00
if (PageViewModel == null) return;
2021-07-23 09:42:22 +08:00
2023-02-12 21:30:16 +08:00
if (PageViewModel.SaveFile())
2021-07-23 09:42:22 +08:00
{
2023-02-12 21:30:16 +08:00
SaveHistory(PageViewModel);
2021-07-23 09:42:22 +08:00
}
}
private void SaveAsExecuted()
{
2023-02-12 21:30:16 +08:00
if (PageViewModel == null) return;
2021-07-23 09:42:22 +08:00
2023-02-12 21:30:16 +08:00
if (PageViewModel.SaveFile(true))
2021-07-23 09:42:22 +08:00
{
2023-02-12 21:30:16 +08:00
SaveHistory(PageViewModel);
2021-07-23 09:42:22 +08:00
}
}
2023-02-12 21:30:16 +08:00
private void SaveHistory(PageViewModel diagramsViewModel)
2021-07-23 09:42:22 +08:00
{
2023-02-12 21:30:16 +08:00
HistoryList.Remove(PageViewModel.FileName);
HistoryList.Insert(0, PageViewModel.FileName);
2021-07-23 09:42:22 +08:00
File.WriteAllText(_history, JsonConvert.SerializeObject(HistoryList));
}
private bool Save_Enable()
{
2023-02-12 21:30:16 +08:00
return PageViewModel != null;
2021-07-23 09:42:22 +08:00
}
private void PasteExecuted()
{
2023-02-12 21:30:16 +08:00
PageViewModel?.DiagramViewModel?.PasteCommand.Execute(null);
2021-07-23 09:42:22 +08:00
}
private bool Paste_Enabled()
{
return Clipboard.ContainsData(DataFormats.Serializable);
2021-07-23 09:42:22 +08:00
}
private void CopyExecuted()
{
2023-02-12 21:30:16 +08:00
PageViewModel?.DiagramViewModel?.CopyCommand.Execute(null);
2021-07-23 09:42:22 +08:00
}
private bool Copy_Enabled()
{
2023-02-12 21:30:16 +08:00
return PageViewModel != null && PageViewModel.DiagramViewModel != null && PageViewModel.DiagramViewModel.SelectedItems.Count() > 0;
2021-07-23 09:42:22 +08:00
}
private void DeleteExecuted()
{
2023-02-12 21:30:16 +08:00
PageViewModel?.DiagramViewModel?.DeleteCommand.Execute(null);
2021-07-23 09:42:22 +08:00
}
private bool Delete_Enabled()
{
2023-02-12 21:30:16 +08:00
return PageViewModel != null && PageViewModel.DiagramViewModel != null && PageViewModel.DiagramViewModel.SelectedItems.Count() > 0;
2021-07-23 09:42:22 +08:00
}
private void CutExecuted()
{
2023-02-12 21:30:16 +08:00
PageViewModel?.DiagramViewModel?.CutCommand.Execute(null);
2021-07-23 09:42:22 +08:00
}
private bool Cut_Enabled()
{
2023-02-12 21:30:16 +08:00
return PageViewModel != null && PageViewModel.DiagramViewModel != null && PageViewModel.DiagramViewModel.SelectedItems.Count() > 0;
2021-07-23 09:42:22 +08:00
}
private void FormatExecuted()
{
_service.DrawModeViewModel.CursorMode = CursorMode.Format;
}
private bool Format_Enabled()
{
2023-02-12 21:30:16 +08:00
return PageViewModel != null && PageViewModel.DiagramViewModel != null && PageViewModel.DiagramViewModel.SelectedItems.Count() == 1;
2021-07-23 09:42:22 +08:00
}
private void New_Executed(string type = "Normal")
{
IsOpenBackstage = false;
if (type == DiagramType.FlowChart.ToString())
{
2023-05-27 12:35:44 +08:00
PageViewModel = new FlowchartViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", (DiagramType)Enum.Parse(typeof(DiagramType), type));
2021-07-23 09:42:22 +08:00
}
else if (type == DiagramType.Logical.ToString())
{
2023-05-27 12:35:44 +08:00
PageViewModel = new LogicalViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", (DiagramType)Enum.Parse(typeof(DiagramType), type));
2021-07-23 09:42:22 +08:00
}
else if (type == DiagramType.SFC.ToString())
{
2023-05-27 12:35:44 +08:00
PageViewModel = new SFCViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", (DiagramType)Enum.Parse(typeof(DiagramType), type));
2023-04-16 23:32:55 +08:00
}
else if (type == DiagramType.Drawing.ToString())
{
2023-05-27 12:35:44 +08:00
PageViewModel = new DrawingViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", (DiagramType)Enum.Parse(typeof(DiagramType), type));
}
2021-07-23 09:42:22 +08:00
else
{
2023-05-27 12:35:44 +08:00
PageViewModel = new PageViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", (DiagramType)Enum.Parse(typeof(DiagramType), type));
2021-07-23 09:42:22 +08:00
}
2023-05-27 12:35:44 +08:00
PageViewModels.Add(PageViewModel);
2021-07-23 09:42:22 +08:00
}
2023-03-20 20:23:20 +08:00
private void NewMind_Executed(string mindtype = "Mind")
{
IsOpenBackstage = false;
2023-05-27 12:35:44 +08:00
PageViewModel = new MindViewModel(NewNameHelper.GetNewName(PageViewModels.Select(p => p.Title), "新建-"), "*", DiagramType.Mind, mindtype.ToEnum<MindType>());
2023-03-20 20:23:20 +08:00
2023-05-27 12:35:44 +08:00
PageViewModels.Add(PageViewModel);
2023-03-20 20:23:20 +08:00
}
2021-07-23 09:42:22 +08:00
private void ExitExecuted()
{
2023-04-08 18:08:00 +08:00
}
2021-07-23 09:42:22 +08:00
private void LockExecuted(object para)
{
LockObjectViewModel.LockObject[0].IsChecked = true;
}
private void UnlockExecuted(object para)
{
LockObjectViewModel.LockObject.ForEach(p => p.IsChecked = false);
}
private void SelectedColorExecuted(object para)
{
2023-01-22 21:46:59 +08:00
if (para == null) return;
2021-07-23 09:42:22 +08:00
switch (ColorType)
{
2023-10-06 22:22:33 +08:00
case Models.ColorType.Text: PageViewModel?.DiagramViewModel?.SetFont(new FontViewModel() { FontColor = (Color)para }, "FontColor", PageViewModel.DiagramViewModel.SelectedItems); break;
case Models.ColorType.Fill: PageViewModel?.DiagramViewModel?.SetColor(new ColorViewModel() { FillColor = new ColorObject() { Color = (Color)para } }, "FillColor", PageViewModel.DiagramViewModel.SelectedItems); break;
case Models.ColorType.Line: PageViewModel?.DiagramViewModel?.SetColor(new ColorViewModel() { LineColor = new ColorObject() { Color = (Color)para } }, "LineColor", PageViewModel.DiagramViewModel.SelectedItems); break;
2021-07-23 09:42:22 +08:00
}
}
private void AboutExecuted()
{
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();
}
2023-04-09 21:48:26 +08:00
private void ScreenshotExecuted()
{
AIStudio.Wpf.ComeCapture.MainWindow window = new AIStudio.Wpf.ComeCapture.MainWindow();
window.Show();
}
2023-05-14 00:31:25 +08:00
private void ColorPickerExecuted()
{
AIStudio.Wpf.ColorPicker.MainWindow window = new AIStudio.Wpf.ColorPicker.MainWindow();
window.Show();
2023-05-14 00:31:25 +08:00
}
2021-07-23 09:42:22 +08:00
#region
private Color[] GenerateStandardGradients()
{
var count = ColorGallery.StandardThemeColors.Length;
List<Color> result = new List<Color>();
for (var i = 0; i < count; i++)
{
2023-05-09 23:22:17 +08:00
var colors = GetGradient(ColorGallery.StandardThemeColors[i], 10);
for (var j = 9; j >= 0; j--)
{
result.Add(colors[j]);
}
}
{
2023-05-09 23:22:17 +08:00
var colors = GetGradient(Colors.Black, 10);
for (var j = 9; j >= 0; j--)
{
result.Add(colors[j]);
}
2021-07-23 09:42:22 +08:00
}
return result.ToArray();
}
2023-05-09 23:22:17 +08:00
#endregion
#region Gradient Generation
/// <summary>
/// Returns brightness of the given color from 0..1
/// </summary>
/// <param name="color">Color</param>
/// <returns>Brightness of the given color from 0..1</returns>
private static double GetBrightness(Color color)
{
var summ = (double)color.R + color.G + color.B;
return summ / (255.0 * 3.0);
}
// Makes the given color lighter
private static Color Lighter(Color color, double power)
{
var totalAvailability = (255.0 * 3.0) - color.R + color.G + color.B;
double redAvailability;
double greenAvailability;
double blueAvailability;
double needToBeAdded;
if (color.R + color.G + color.B == 0)
{
redAvailability = 1.0 / 3.0;
greenAvailability = 1.0 / 3.0;
blueAvailability = 1.0 / 3.0;
needToBeAdded = power * 255.0 * 3.0;
}
else
{
redAvailability = (255.0 - color.R) / totalAvailability;
greenAvailability = (255.0 - color.G) / totalAvailability;
blueAvailability = (255.0 - color.B) / totalAvailability;
needToBeAdded = ((double)color.R + color.G + color.B) * (power - 1);
}
var result = Color.FromRgb(
(byte)(color.R + (byte)(redAvailability * needToBeAdded)),
(byte)(color.G + (byte)(greenAvailability * needToBeAdded)),
(byte)(color.B + (byte)(blueAvailability * needToBeAdded)));
return result;
}
// Makes the given color darker
private static Color Darker(Color color, double power)
{
var totalAvailability = (double)color.R + color.G + color.B;
var redAvailability = color.R / totalAvailability;
var greenAvailability = color.G / totalAvailability;
var blueAvailability = color.B / totalAvailability;
var needToBeAdded = (double)color.R + color.G + color.B;
needToBeAdded = needToBeAdded - (needToBeAdded * power);
var result = Color.FromRgb(
(byte)(color.R - (byte)(redAvailability * needToBeAdded)),
(byte)(color.G - (byte)(greenAvailability * needToBeAdded)),
(byte)(color.B - (byte)(blueAvailability * needToBeAdded)));
return result;
}
// Makes a new color from the given with new brightness
private static Color Rebright(Color color, double newBrightness)
{
var currentBrightness = GetBrightness(color);
var power = DoubleUtil.AreClose(currentBrightness, 0.0) == false
? newBrightness / currentBrightness
: 1.0 + newBrightness;
// TODO: round power to make nice numbers
// ...
if (power > 1.0)
{
return Lighter(color, power);
}
return Darker(color, power);
}
/// <summary>
/// Makes gradient colors from lighter to darker
/// </summary>
/// <param name="color">Base color</param>
/// <param name="count">Count of items in the gradient</param>
/// <returns>Colors from lighter to darker</returns>
public static Color[] GetGradient(Color color, int count)
{
const double lowBrightness = 0.15;
const double highBrightness = 0.85;
var result = new Color[count];
for (var i = 0; i < count; i++)
{
var brightness = lowBrightness + (i * (highBrightness - lowBrightness) / count);
result[count - i - 1] = Rebright(color, brightness);
}
return result;
}
2021-07-23 09:42:22 +08:00
#endregion
}
}