using System; using System.IO; using System.Text; using System.Windows.Forms; using GrasscutterTools.Properties; using GrasscutterTools.Utils; namespace GrasscutterTools.Pages { internal partial class PageCustomCommands : BasePage { public PageCustomCommands() { InitializeComponent(); } /// /// 自定义命令保存位置 /// private readonly string CustomCommandsFilePath = Path.Combine(Application.LocalUserAppDataPath, "CustomCommands.txt"); /// /// 自定义命令是否存在更改 /// private bool CustomCommandsChanged; /// /// 加载自定义命令 /// public override void OnLoad() { if (File.Exists(CustomCommandsFilePath)) LoadCustomCommandControls(File.ReadAllText(CustomCommandsFilePath)); else LoadCustomCommandControls(Resources.CustomCommands); CustomCommandsChanged = false; } /// /// 加载自定义命令控件列表 /// /// 命令集(示例:"标签1\n命令1\n标签2\n命令2") private void LoadCustomCommandControls(string commands) { FLPCustomCommands.Controls.Clear(); var lines = commands.Split('\n'); for (int i = 0; i < lines.Length - 1; i += 2) AddCustomCommand(lines[i].Trim(), lines[i + 1].Trim()); } /// /// 保存自定义命令 /// public override void OnClosed() { if (CustomCommandsChanged) File.WriteAllText(CustomCommandsFilePath, SaveCustomCommandControls()); } /// /// 保存自定义命令控件列表 /// /// 命令集(示例:"标签1\n命令1\n标签2\n命令2") private string SaveCustomCommandControls() { StringBuilder builder = new StringBuilder(); foreach (LinkLabel lnk in FLPCustomCommands.Controls) { builder.AppendLine(lnk.Text); builder.AppendLine(lnk.Tag as string); } return builder.ToString(); } /// /// 自定义命令点击时触发 /// private void CustomCommand_Click(object sender, LinkLabelLinkClickedEventArgs e) { if (sender is LinkLabel lnk && lnk.Tag is string command) { TxtCustomName.Text = lnk.Text; SetCommand(command); } } /// /// 自定义命令文本框回车时触发 /// private void TxtCustomName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) BtnSaveCustomCommand_Click(BtnSaveCustomCommand, e); } /// /// 点击保存自定义命令列表时触发 /// /// /// private async void BtnSaveCustomCommand_Click(object sender, EventArgs e) { var name = TxtCustomName.Text.Trim(); if (string.IsNullOrEmpty(name)) { MessageBox.Show(Resources.CommandTagCannotBeEmpty, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var command = GetCommand().Trim(); if (string.IsNullOrEmpty(command)) { MessageBox.Show(Resources.CommandContentCannotBeEmpty, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (LinkLabel lnk in FLPCustomCommands.Controls) { if (lnk.Text == name) { lnk.Tag = command; CustomCommandsChanged = true; await UIUtil.ButtonComplete(BtnSaveCustomCommand); return; } } CustomCommandsChanged = true; AddCustomCommand(name, command); await UIUtil.ButtonComplete(BtnSaveCustomCommand); } /// /// 添加自定义命令 /// /// 标签 /// 命令 private void AddCustomCommand(string name, string command) { var lnk = new LinkLabel { Text = name, Tag = command, AutoSize = true, }; lnk.LinkClicked += CustomCommand_Click; FLPCustomCommands.Controls.Add(lnk); } /// /// 点击移除自定义命令按钮时触发 /// private async void BtnRemoveCustomCommand_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(TxtCustomName.Text)) { MessageBox.Show(Resources.CommandTagCannotBeEmpty, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var name = TxtCustomName.Text.Trim(); foreach (LinkLabel lnk in FLPCustomCommands.Controls) { if (lnk.Text == name && MessageBox.Show(Resources.AskConfirmDeletion, Resources.Tips, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { FLPCustomCommands.Controls.Remove(lnk); CustomCommandsChanged = true; //TxtCustomName.Text = ""; //TxtCommand.Text = ""; await UIUtil.ButtonComplete(BtnRemoveCustomCommand); return; } } MessageBox.Show(Resources.CommandNotFound, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } /// /// 点击导入自定义命令时触发 /// private void BtnImport_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog { Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" }; if (dialog.ShowDialog() == DialogResult.OK) { using (var stream = dialog.OpenFile()) using (var reader = new StreamReader(stream)) { LoadCustomCommandControls(reader.ReadToEnd()); CustomCommandsChanged = true; } } } /// /// 点击导出自定义命令时触发 /// /// /// private void BtnExport_Click(object sender, EventArgs e) { var dialog = new SaveFileDialog { FileName = "Commands.txt", Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" }; if (dialog.ShowDialog() == DialogResult.OK) { using (var stream = dialog.OpenFile()) using (var writer = new StreamWriter(stream)) { writer.Write(SaveCustomCommandControls()); } } } /// /// 点击重置链接按钮时触发 /// private void LnkResetCustomCommands_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (MessageBox.Show(Resources.RestoreCustomCommands, Resources.Tips, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { if (File.Exists(CustomCommandsFilePath)) File.Delete(CustomCommandsFilePath); LoadCustomCommandControls(Resources.CustomCommands); } } } }