/**
* Grasscutter Tools
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
**/
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using GrasscutterTools.Forms;
using GrasscutterTools.Properties;
using GrasscutterTools.Utils;
namespace GrasscutterTools.Pages
{
internal partial class PageCustomCommands : BasePage
{
public override string Text => Resources.PageCustomCommandsTitle;
public PageCustomCommands()
{
InitializeComponent();
}
///
/// 自定义命令保存位置
///
private readonly string CustomCommandsFilePath = Common.GetAppDataFile("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);
}
}
///
/// 点击添加快捷键时触发
///
private void BtnAddHotKey_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;
}
// 跳转到快捷键界面
FormMain.Instance.NavigateTo()?
.AddNewHotKey(name); // 设置标签
}
///
/// 标签栏文本改变时触发
///
private void TxtCustomName_TextChanged(object sender, EventArgs e)
{
LblClearFilter.Visible = TxtCustomName.Text.Length > 0;
}
///
/// 点击清空标签栏标签时触发
///
private void LblClearFilter_Click(object sender, EventArgs e)
{
TxtCustomName.Clear();
}
}
}