VPet/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs

259 lines
7.8 KiB
C#
Raw Normal View History

2024-03-07 10:41:18 +00:00
using System;
2024-01-07 14:57:27 +00:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-01-09 15:22:25 +00:00
using System.Windows;
2024-03-07 10:41:18 +00:00
using HKW.HKWUtils.Observable;
using LinePutScript;
using LinePutScript.Localization.WPF;
using Panuon.WPF;
2024-01-07 14:57:27 +00:00
using VPet.Solution.Models;
using VPet.Solution.Models.SettingEditor;
2024-01-09 15:22:25 +00:00
using VPet.Solution.Views.SettingEditor;
2024-03-09 18:05:27 +00:00
using VPet_Simulator.Windows;
2024-01-07 14:57:27 +00:00
namespace VPet.Solution.ViewModels.SettingEditor;
public class SettingWindowVM : ObservableClass<SettingWindowVM>
{
2024-03-07 10:41:18 +00:00
public static SettingWindowVM Current { get; private set; } = null!;
2024-01-09 15:22:25 +00:00
#region Properties
2024-03-07 10:41:18 +00:00
private SettingModel _currentSetting = null!;
2024-01-07 14:57:27 +00:00
public SettingModel CurrentSetting
{
get => _currentSetting;
set
{
if (_currentSetting?.IsChanged is true)
{
var result = MessageBox.Show(
"当前设置未保存 确定要保存吗".Translate(),
"",
MessageBoxButton.YesNoCancel
);
if (result is MessageBoxResult.Yes)
{
_currentSetting.Save();
}
else if (result is MessageBoxResult.No)
{
_currentSetting.IsChanged = false;
}
else if (result is MessageBoxResult.Cancel)
{
return;
}
}
2024-03-07 10:41:18 +00:00
SetProperty(ref _currentSetting!, value);
}
2024-01-07 14:57:27 +00:00
}
2024-03-07 10:41:18 +00:00
public readonly ObservableCollection<SettingModel> Settings = new();
2024-01-07 14:57:27 +00:00
2024-03-07 10:41:18 +00:00
#region ShowSettings
private IEnumerable<SettingModel> _showSettings = null!;
2024-01-07 14:57:27 +00:00
public IEnumerable<SettingModel> ShowSettings
{
get => _showSettings;
set => SetProperty(ref _showSettings, value);
}
2024-03-07 10:41:18 +00:00
#endregion
#region SearchSetting
private string _searchSetting = string.Empty;
2024-01-07 14:57:27 +00:00
public string SearchSetting
{
get => _searchSetting;
set => SetProperty(ref _searchSetting, value);
}
2024-03-07 10:41:18 +00:00
#endregion
2024-01-07 14:57:27 +00:00
2024-01-09 15:22:25 +00:00
#endregion
#region Command
2024-01-11 16:28:22 +00:00
/// <summary>
/// 打开文件
/// </summary>
public ObservableCommand<SettingModel> OpenFileCommand { get; } = new();
/// <summary>
/// 从资源管理器打开
/// </summary>
public ObservableCommand<SettingModel> OpenFileInExplorerCommand { get; } = new();
/// <summary>
/// 重置
/// </summary>
2024-01-10 13:37:17 +00:00
public ObservableCommand<SettingModel> ResetSettingCommand { get; } = new();
2024-01-11 16:28:22 +00:00
/// <summary>
/// 保存
/// </summary>
2024-01-09 15:22:25 +00:00
public ObservableCommand<SettingModel> SaveSettingCommand { get; } = new();
2024-01-11 16:28:22 +00:00
/// <summary>
/// 保存全部
/// </summary>
2024-01-09 15:22:25 +00:00
public ObservableCommand SaveAllSettingCommand { get; } = new();
/// <summary>
/// 重置全部
/// </summary>
public ObservableCommand ResetAllSettingCommand { get; } = new();
2024-01-09 15:22:25 +00:00
#endregion
2024-01-07 14:57:27 +00:00
public SettingWindowVM()
{
Current = this;
2024-01-12 14:17:50 +00:00
LoadSettings();
2024-03-07 10:41:18 +00:00
ShowSettings = Settings = new(Settings.OrderBy(m => m.Name));
2024-01-07 14:57:27 +00:00
PropertyChanged += MainWindowVM_PropertyChanged;
2024-01-11 16:28:22 +00:00
OpenFileCommand.ExecuteCommand += OpenFileCommand_ExecuteCommand;
OpenFileInExplorerCommand.ExecuteCommand += OpenFileInExplorerCommand_ExecuteCommand;
2024-01-09 15:22:25 +00:00
ResetSettingCommand.ExecuteCommand += ResetSettingCommand_ExecuteCommand;
SaveSettingCommand.ExecuteCommand += SaveSettingCommand_ExecuteCommand;
2024-01-10 13:37:17 +00:00
SaveAllSettingCommand.ExecuteCommand += SaveAllSettingCommand_ExecuteCommand;
ResetAllSettingCommand.ExecuteCommand += ResetAllSettingCommand_ExecuteCommand;
}
private void ResetAllSettingCommand_ExecuteCommand()
{
if (
MessageBox.Show(
SettingWindow.Instance,
"确定全部重置吗".Translate(),
"",
MessageBoxButton.YesNo,
MessageBoxImage.Warning
)
is not MessageBoxResult.Yes
)
return;
2024-03-07 10:41:18 +00:00
for (var i = 0; i < Settings.Count; i++)
Settings[i] = new SettingModel();
2024-01-10 13:37:17 +00:00
}
2024-01-11 16:28:22 +00:00
private void OpenFileInExplorerCommand_ExecuteCommand(SettingModel parameter)
{
HKWUtils.OpenFileInExplorer(parameter.FilePath);
2024-01-11 16:28:22 +00:00
}
private void OpenFileCommand_ExecuteCommand(SettingModel parameter)
{
HKWUtils.OpenLink(parameter.FilePath);
2024-01-11 16:28:22 +00:00
}
2024-01-10 13:37:17 +00:00
private void SaveAllSettingCommand_ExecuteCommand()
{
2024-01-11 16:28:22 +00:00
if (
MessageBox.Show(
SettingWindow.Instance,
"确定全部保存吗".Translate(),
"",
MessageBoxButton.YesNo,
MessageBoxImage.Warning
)
is not MessageBoxResult.Yes
)
return;
2024-03-07 10:41:18 +00:00
foreach (var setting in Settings)
2024-01-10 13:37:17 +00:00
setting.Save();
2024-01-09 15:22:25 +00:00
}
private void SaveSettingCommand_ExecuteCommand(SettingModel parameter)
{
parameter.Save();
}
2024-01-10 13:37:17 +00:00
private void ResetSettingCommand_ExecuteCommand(SettingModel parameter)
2024-01-09 15:22:25 +00:00
{
if (
2024-01-10 13:37:17 +00:00
MessageBox.Show(
2024-01-09 15:22:25 +00:00
SettingWindow.Instance,
2024-01-10 13:37:17 +00:00
"确定重置设置吗\n名称: {0}\n路径: {1}".Translate(parameter.Name, parameter.FilePath),
2024-01-09 15:22:25 +00:00
"",
MessageBoxButton.YesNo,
2024-01-10 13:37:17 +00:00
MessageBoxImage.Warning
2024-01-09 15:22:25 +00:00
)
is not MessageBoxResult.Yes
)
return;
2024-03-07 10:41:18 +00:00
CurrentSetting = Settings[Settings.IndexOf(CurrentSetting)] = new SettingModel()
2024-01-10 13:37:17 +00:00
{
Name = CurrentSetting.Name,
FilePath = CurrentSetting.FilePath
};
2024-01-09 15:22:25 +00:00
RefreshShowSettings(SearchSetting);
}
public void RefreshShowSettings(string name)
{
if (string.IsNullOrWhiteSpace(name))
2024-03-07 10:41:18 +00:00
ShowSettings = Settings;
2024-01-09 15:22:25 +00:00
else
2024-03-07 10:41:18 +00:00
ShowSettings = Settings.Where(s =>
s.Name.Contains(SearchSetting, StringComparison.OrdinalIgnoreCase)
2024-01-09 15:22:25 +00:00
);
2024-01-07 14:57:27 +00:00
}
2024-03-07 10:41:18 +00:00
private void MainWindowVM_PropertyChanged(object? sender, PropertyChangedEventArgs e)
2024-01-07 14:57:27 +00:00
{
if (e.PropertyName == nameof(SearchSetting))
{
2024-01-09 15:22:25 +00:00
RefreshShowSettings(SearchSetting);
2024-01-07 14:57:27 +00:00
}
}
2024-01-12 14:17:50 +00:00
private void LoadSettings()
2024-01-07 14:57:27 +00:00
{
2024-01-12 14:17:50 +00:00
foreach (var file in GetSettingFiles())
2024-01-07 14:57:27 +00:00
{
2024-01-12 14:17:50 +00:00
var fileName = Path.GetFileNameWithoutExtension(file);
try
2024-01-07 14:57:27 +00:00
{
2024-03-09 18:05:27 +00:00
var setting = new Setting(null, File.ReadAllText(file));
2024-01-12 14:17:50 +00:00
var settingModel = new SettingModel(setting) { Name = fileName, FilePath = file };
2024-03-07 10:41:18 +00:00
Settings.Add(settingModel);
2024-01-12 14:17:50 +00:00
}
catch (Exception ex)
{
if (
MessageBox.Show(
2024-01-13 14:23:34 +00:00
"设置载入失败, 是否强制载入并重置\n[是]: 载入并重置\t[否]: 取消载入\n名称: {0}\n路径: {1}\n异常: {2}".Translate(
2024-01-12 14:17:50 +00:00
fileName,
file,
ex.ToString()
),
"载入设置出错".Translate(),
MessageBoxButton.YesNo,
MessageBoxImage.Warning
)
is not MessageBoxResult.Yes
2024-01-12 14:17:50 +00:00
)
return;
var setting = new SettingModel() { Name = fileName, FilePath = file };
2024-03-07 10:41:18 +00:00
Settings.Add(setting);
setting.Save();
2024-01-12 14:17:50 +00:00
}
2024-01-07 14:57:27 +00:00
}
}
2024-01-12 14:17:50 +00:00
private static IEnumerable<string> GetSettingFiles()
{
return Directory
.EnumerateFiles(Environment.CurrentDirectory)
.Where(
(s) =>
{
if (s.EndsWith(".lps") is false)
return false;
return Path.GetFileName(s).StartsWith(nameof(Setting));
2024-01-12 14:17:50 +00:00
}
);
}
2024-01-07 14:57:27 +00:00
}