NoSoliciting/NoSoliciting/Interface/Settings.cs

369 lines
13 KiB
C#
Raw Normal View History

2021-03-04 04:34:52 +00:00
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
2021-03-04 04:34:52 +00:00
using Dalamud.Interface;
using ImGuiNET;
2020-12-21 02:49:10 +00:00
using NoSoliciting.Ml;
2021-04-28 03:36:57 +00:00
using NoSoliciting.Resources;
2020-08-04 22:22:57 +00:00
2021-03-04 04:34:52 +00:00
namespace NoSoliciting.Interface {
public class Settings : IDisposable {
private Plugin Plugin { get; }
2021-03-04 04:34:52 +00:00
private PluginUi Ui { get; }
2020-08-04 22:22:57 +00:00
private bool _showSettings;
private bool ShowSettings {
get => this._showSettings;
set => this._showSettings = value;
}
2020-08-04 22:22:57 +00:00
2021-03-04 04:34:52 +00:00
public Settings(Plugin plugin, PluginUi ui) {
this.Plugin = plugin;
2021-03-04 04:34:52 +00:00
this.Ui = ui;
2021-08-22 22:07:28 +00:00
this.Plugin.Interface.UiBuilder.OpenConfigUi += this.Open;
}
public void Dispose() {
2021-08-22 22:07:28 +00:00
this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.Open;
2020-08-04 22:22:57 +00:00
}
2021-08-22 22:07:28 +00:00
private void Open() {
2020-08-04 22:22:57 +00:00
this.ShowSettings = true;
}
2021-03-04 04:34:52 +00:00
public void Toggle() {
this.ShowSettings = !this.ShowSettings;
}
public void Show() {
this.ShowSettings = true;
}
2021-03-04 04:34:52 +00:00
public void Draw() {
2023-09-29 00:59:42 +00:00
var windowTitle = string.Format(Language.Settings, Plugin.Name);
if (!this.ShowSettings || !ImGui.Begin($"{windowTitle}###NoSoliciting settings", ref this._showSettings)) {
return;
}
2020-08-04 22:22:57 +00:00
var advanced = this.Plugin.Config.AdvancedMode;
2021-04-28 03:36:57 +00:00
if (ImGui.Checkbox(Language.AdvancedMode, ref advanced)) {
this.Plugin.Config.AdvancedMode = advanced;
this.Plugin.Config.Save();
}
2020-12-21 02:49:10 +00:00
ImGui.Spacing();
if (!ImGui.BeginTabBar("##nosoliciting-tabs")) {
return;
}
this.DrawMachineLearningConfig();
2020-12-21 02:49:10 +00:00
this.DrawOtherFilters();
this.DrawOtherTab();
2021-01-13 20:38:52 +00:00
2020-12-21 02:49:10 +00:00
ImGui.EndTabBar();
ImGui.Separator();
2021-04-28 03:36:57 +00:00
if (ImGui.Button(Language.ShowReportingWindow)) {
2021-03-04 04:34:52 +00:00
this.Ui.Report.Open();
2020-08-04 22:22:57 +00:00
}
ImGui.End();
2020-08-04 22:22:57 +00:00
}
2021-03-04 04:34:52 +00:00
#region ML config
2020-12-21 02:49:10 +00:00
private void DrawMachineLearningConfig() {
if (this.Plugin.Config.AdvancedMode) {
this.DrawAdvancedMachineLearningConfig();
} else {
this.DrawBasicMachineLearningConfig();
}
if (!ImGui.BeginTabItem($"{Language.ModelTab}###model-tab")) {
2021-02-25 02:02:41 +00:00
return;
}
2020-12-21 02:49:10 +00:00
2021-04-28 03:36:57 +00:00
ImGui.TextUnformatted(string.Format(Language.ModelTabVersion, this.Plugin.MlFilter?.Version));
ImGui.TextUnformatted(string.Format(Language.ModelTabStatus, this.Plugin.MlStatus.Description()));
2021-02-25 02:02:41 +00:00
var lastError = MlFilter.LastError;
if (lastError != null) {
2021-04-28 03:36:57 +00:00
ImGui.TextUnformatted(string.Format(Language.ModelTabError, lastError));
2021-02-25 02:02:41 +00:00
}
2020-12-21 02:49:10 +00:00
2021-04-28 03:36:57 +00:00
if (ImGui.Button(Language.UpdateModel)) {
2021-02-25 02:02:41 +00:00
// prevent issues when people spam the button
if (ImGui.GetIO().KeyCtrl || this.Plugin.MlStatus is MlFilterStatus.Uninitialised or MlFilterStatus.Initialised) {
2021-02-25 02:02:41 +00:00
this.Plugin.MlFilter?.Dispose();
this.Plugin.MlFilter = null;
this.Plugin.MlStatus = MlFilterStatus.Uninitialised;
2021-03-06 04:00:52 +00:00
this.Plugin.InitialiseMachineLearning(ImGui.GetIO().KeyAlt);
2021-02-25 02:02:41 +00:00
}
2020-12-21 02:49:10 +00:00
}
2021-02-25 02:02:41 +00:00
ImGui.EndTabItem();
2020-12-21 02:49:10 +00:00
}
private void DrawBasicMachineLearningConfig() {
if (!ImGui.BeginTabItem($"{Language.FiltersTab}###filters-tab")) {
2020-12-21 02:49:10 +00:00
return;
}
2021-04-01 07:08:34 +00:00
foreach (var category in MessageCategoryExt.UiOrder) {
2020-12-21 02:49:10 +00:00
var check = this.Plugin.Config.BasicMlFilters.Contains(category);
if (ImGui.Checkbox(category.Name(), ref check)) {
if (check) {
this.Plugin.Config.BasicMlFilters.Add(category);
} else {
this.Plugin.Config.BasicMlFilters.Remove(category);
}
this.Plugin.Config.Save();
}
if (!ImGui.IsItemHovered()) {
continue;
}
ImGui.BeginTooltip();
ImGui.PushTextWrapPos(ImGui.GetFontSize() * 24);
ImGui.TextUnformatted(category.Description());
ImGui.PopTextWrapPos();
ImGui.EndTooltip();
2020-12-21 02:49:10 +00:00
}
ImGui.EndTabItem();
}
private void DrawAdvancedMachineLearningConfig() {
if (!ImGui.BeginTabItem($"{Language.FiltersTab}###filters-tab")) {
2020-12-21 02:49:10 +00:00
return;
}
2020-12-26 01:24:43 +00:00
ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(255f, 204f, 0f, 1f));
2021-04-28 03:36:57 +00:00
ImGui.TextUnformatted(Language.AdvancedWarning1);
ImGui.TextUnformatted(Language.AdvancedWarning2);
2020-12-26 01:24:43 +00:00
ImGui.PopStyleColor();
2021-04-01 07:08:34 +00:00
foreach (var category in MessageCategoryExt.UiOrder) {
2020-12-21 02:49:10 +00:00
if (!ImGui.CollapsingHeader(category.Name())) {
continue;
}
if (!this.Plugin.Config.MlFilters.ContainsKey(category)) {
this.Plugin.Config.MlFilters[category] = new HashSet<ChatType>();
}
var types = this.Plugin.Config.MlFilters[category];
void DrawTypes(ChatType type, string id) {
2021-08-22 22:07:28 +00:00
var name = type.Name(this.Plugin.DataManager);
2020-12-21 02:49:10 +00:00
var check = types.Contains(type);
if (!ImGui.Checkbox($"{name}##{id}", ref check)) {
2020-12-21 02:49:10 +00:00
return;
}
if (check) {
types.Add(type);
} else {
types.Remove(type);
}
this.Plugin.Config.Save();
}
DrawTypes(ChatType.None, category.ToString());
2020-12-21 02:49:10 +00:00
foreach (var type in Filter.FilteredChatTypes) {
DrawTypes(type, category.ToString());
2020-12-21 02:49:10 +00:00
}
}
ImGui.EndTabItem();
}
2021-03-04 04:34:52 +00:00
#endregion
#region Other config
internal bool ShowOtherFilters;
private static unsafe bool BeginTabItem(string label, ImGuiTabItemFlags flags) {
var unterminatedLabelBytes = Encoding.UTF8.GetBytes(label);
var labelBytes = stackalloc byte[unterminatedLabelBytes.Length + 1];
fixed (byte* unterminatedPtr = unterminatedLabelBytes) {
Buffer.MemoryCopy(unterminatedPtr, labelBytes, unterminatedLabelBytes.Length + 1, unterminatedLabelBytes.Length);
}
labelBytes[unterminatedLabelBytes.Length] = 0;
var num2 = (int) ImGuiNative.igBeginTabItem(labelBytes, null, flags);
return (uint) num2 > 0U;
}
2021-03-04 04:34:52 +00:00
private void DrawOtherFilters() {
var flags = this.ShowOtherFilters ? ImGuiTabItemFlags.SetSelected : ImGuiTabItemFlags.None;
this.ShowOtherFilters = false;
if (!BeginTabItem($"{Language.OtherFiltersTab}###other-filters-tab", flags)) {
2021-03-04 04:34:52 +00:00
return;
}
2021-04-28 03:36:57 +00:00
if (ImGui.CollapsingHeader(Language.ChatFilters)) {
2021-03-04 04:34:52 +00:00
var customChat = this.Plugin.Config.CustomChatFilter;
2021-04-28 03:36:57 +00:00
if (ImGui.Checkbox(Language.EnableCustomChatFilters, ref customChat)) {
2021-03-04 04:34:52 +00:00
this.Plugin.Config.CustomChatFilter = customChat;
this.Plugin.Config.Save();
}
if (this.Plugin.Config.CustomChatFilter) {
var substrings = this.Plugin.Config.ChatSubstrings;
var regexes = this.Plugin.Config.ChatRegexes;
this.DrawCustom("chat", ref substrings, ref regexes);
}
}
2021-04-28 03:36:57 +00:00
if (ImGui.CollapsingHeader(Language.PartyFinderFilters)) {
2021-03-04 04:34:52 +00:00
var filterHugeItemLevelPFs = this.Plugin.Config.FilterHugeItemLevelPFs;
// ReSharper disable once InvertIf
2021-04-28 03:36:57 +00:00
if (ImGui.Checkbox(Language.FilterIlvlPfs, ref filterHugeItemLevelPFs)) {
2021-03-04 04:34:52 +00:00
this.Plugin.Config.FilterHugeItemLevelPFs = filterHugeItemLevelPFs;
this.Plugin.Config.Save();
}
var considerPrivate = this.Plugin.Config.ConsiderPrivatePfs;
2021-04-28 03:36:57 +00:00
if (ImGui.Checkbox(Language.FilterPrivatePfs, ref considerPrivate)) {
2021-03-04 04:34:52 +00:00
this.Plugin.Config.ConsiderPrivatePfs = considerPrivate;
this.Plugin.Config.Save();
}
var customPf = this.Plugin.Config.CustomPFFilter;
2021-04-28 03:36:57 +00:00
if (ImGui.Checkbox(Language.EnableCustomPartyFinderFilters, ref customPf)) {
2021-03-04 04:34:52 +00:00
this.Plugin.Config.CustomPFFilter = customPf;
this.Plugin.Config.Save();
}
if (this.Plugin.Config.CustomPFFilter) {
var substrings = this.Plugin.Config.PFSubstrings;
var regexes = this.Plugin.Config.PFRegexes;
this.DrawCustom("pf", ref substrings, ref regexes);
}
}
ImGui.EndTabItem();
}
private void DrawCustom(string name, ref List<string> substrings, ref List<string> regexes) {
ImGui.Columns(2);
2021-04-28 03:36:57 +00:00
ImGui.TextUnformatted(Language.SubstringsToFilter);
if (ImGui.BeginChild($"##{name}-substrings", new Vector2(0, 175))) {
for (var i = 0; i < substrings.Count; i++) {
var input = substrings[i];
if (ImGui.InputText($"##{name}-substring-{i}", ref input, 1_000)) {
substrings[i] = input;
}
2020-11-23 18:22:19 +00:00
ImGui.SameLine();
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button($"{FontAwesomeIcon.Trash.ToIconString()}##{name}-substring-{i}-remove")) {
substrings.RemoveAt(i);
}
2020-11-23 18:22:19 +00:00
ImGui.PopFont();
}
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button($"{FontAwesomeIcon.Plus.ToIconString()}##{name}-substring-add")) {
substrings.Add("");
}
2020-11-23 18:22:19 +00:00
ImGui.PopFont();
ImGui.EndChild();
}
ImGui.NextColumn();
2021-04-28 03:36:57 +00:00
ImGui.TextUnformatted(Language.RegularExpressionsToFilter);
if (ImGui.BeginChild($"##{name}-regexes", new Vector2(0, 175))) {
for (var i = 0; i < regexes.Count; i++) {
var input = regexes[i];
if (ImGui.InputText($"##{name}-regex-{i}", ref input, 1_000)) {
try {
_ = new Regex(input);
// update if valid
regexes[i] = input;
} catch (ArgumentException) {
// ignore
}
}
2020-11-23 18:22:19 +00:00
ImGui.SameLine();
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button($"{FontAwesomeIcon.Trash.ToIconString()}##{name}-regex-{i}-remove")) {
regexes.RemoveAt(i);
}
2020-11-23 18:22:19 +00:00
ImGui.PopFont();
}
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.Button($"{FontAwesomeIcon.Plus.ToIconString()}##{name}-regex-add")) {
regexes.Add("");
}
2020-11-23 18:22:19 +00:00
ImGui.PopFont();
ImGui.EndChild();
}
ImGui.Columns(1);
// ReSharper disable once InvertIf
2021-04-28 03:36:57 +00:00
var saveLoc = Language.SaveFilters;
if (ImGui.Button($"{saveLoc}##{name}-save")) {
this.Plugin.Config.Save();
this.Plugin.Config.CompileRegexes();
}
}
2021-03-04 04:34:52 +00:00
#endregion
private void DrawOtherTab() {
if (!ImGui.BeginTabItem($"{Language.OtherTab}###other-tab")) {
return;
}
var useGameLanguage = this.Plugin.Config.FollowGameLanguage;
if (ImGui.Checkbox(Language.OtherGameLanguage, ref useGameLanguage)) {
this.Plugin.Config.FollowGameLanguage = useGameLanguage;
this.Plugin.Config.Save();
this.Plugin.ConfigureLanguage();
}
var logFilteredPfs = this.Plugin.Config.LogFilteredPfs;
if (ImGui.Checkbox(Language.LogFilteredPfs, ref logFilteredPfs)) {
this.Plugin.Config.LogFilteredPfs = logFilteredPfs;
this.Plugin.Config.Save();
}
var logFilteredMessages = this.Plugin.Config.LogFilteredChat;
if (ImGui.Checkbox(Language.LogFilteredMessages, ref logFilteredMessages)) {
this.Plugin.Config.LogFilteredChat = logFilteredMessages;
this.Plugin.Config.Save();
}
ImGui.EndTabItem();
}
2020-08-04 22:22:57 +00:00
}
}