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

205 lines
6.2 KiB
C#
Raw Normal View History

2024-01-07 14:57:27 +00:00
using HKW.HKWUtils.Observable;
using LinePutScript;
2024-01-10 13:37:17 +00:00
using LinePutScript.Localization.WPF;
2024-01-09 15:22:25 +00:00
using Panuon.WPF.UI;
2024-01-07 14:57:27 +00:00
using System;
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-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-01-07 14:57:27 +00:00
using VPet_Simulator.Windows.Interface;
namespace VPet.Solution.ViewModels.SettingEditor;
public class SettingWindowVM : ObservableClass<SettingWindowVM>
{
public static SettingWindowVM Current { get; private set; }
2024-01-09 15:22:25 +00:00
#region Properties
2024-01-07 14:57:27 +00:00
private SettingModel _currentSettings;
public SettingModel CurrentSetting
{
get => _currentSettings;
set => SetProperty(ref _currentSettings, value);
}
private readonly ObservableCollection<SettingModel> _settings = new();
private IEnumerable<SettingModel> _showSettings;
public IEnumerable<SettingModel> ShowSettings
{
get => _showSettings;
set => SetProperty(ref _showSettings, value);
}
private string _searchSetting;
public string SearchSetting
{
get => _searchSetting;
set => SetProperty(ref _searchSetting, value);
}
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();
#endregion
2024-01-07 14:57:27 +00:00
public SettingWindowVM()
{
Current = this;
ShowSettings = _settings;
2024-01-12 14:17:50 +00:00
LoadSettings();
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;
}
2024-01-11 16:28:22 +00:00
private void OpenFileInExplorerCommand_ExecuteCommand(SettingModel parameter)
{
Utils.OpenFileInExplorer(parameter.FilePath);
}
private void OpenFileCommand_ExecuteCommand(SettingModel parameter)
{
Utils.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-01-10 13:37:17 +00:00
foreach (var setting in _settings)
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-01-11 16:28:22 +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))
ShowSettings = _settings;
else
ShowSettings = _settings.Where(
s => s.Name.Contains(SearchSetting, StringComparison.OrdinalIgnoreCase)
);
2024-01-07 14:57:27 +00:00
}
private void MainWindowVM_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
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-01-12 14:17:50 +00:00
var setting = new Setting(File.ReadAllText(file));
var settingModel = new SettingModel(setting) { Name = fileName, FilePath = file };
_settings.Add(settingModel);
}
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 MessageBoxResult.Yes
)
_settings.Add(new SettingModel() { Name = fileName, FilePath = file });
}
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("Setting");
}
);
}
2024-01-07 14:57:27 +00:00
}