SoundFilter/SoundFilter/Ui/Settings.cs

159 lines
5.3 KiB
C#
Raw Normal View History

2021-05-08 17:34:13 +00:00
using System;
2021-05-08 19:03:55 +00:00
using System.Numerics;
2021-05-08 17:34:13 +00:00
using Dalamud.Interface;
using ImGuiNET;
using SoundFilter.Resources;
namespace SoundFilter.Ui {
public class Settings : IDisposable {
private SoundFilterPlugin Plugin { get; }
private AddFilter AddFilter { get; }
private AddFilter? EditFilter { get; set; }
2021-05-08 17:34:13 +00:00
private bool _showWindow;
private int _dragging = -1;
2021-05-08 17:34:13 +00:00
internal Settings(SoundFilterPlugin plugin) {
this.Plugin = plugin;
this.AddFilter = new AddFilter(plugin);
2021-05-08 17:34:13 +00:00
2021-08-24 18:18:09 +00:00
this.Plugin.Interface.UiBuilder.OpenConfigUi += this.Toggle;
2021-05-08 17:34:13 +00:00
}
public void Dispose() {
2021-08-24 18:18:09 +00:00
this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.Toggle;
2021-05-08 17:34:13 +00:00
}
2021-08-24 18:18:09 +00:00
internal void Toggle() {
2021-05-08 17:34:13 +00:00
this._showWindow = !this._showWindow;
}
internal void Draw() {
if (!this._showWindow) {
return;
}
if (this.EditFilter != null) {
ImGui.SetNextWindowSize(new Vector2(ImGui.GetWindowSize().X, -1));
if (ImGui.BeginPopupModal($"{Language.SettingsEditFilter}###edit-filter-modal")) {
if (this.EditFilter.Draw()) {
this.EditFilter = null;
}
ImGui.EndPopup();
}
ImGui.OpenPopup("###edit-filter-modal");
}
2021-05-08 19:03:55 +00:00
ImGui.SetNextWindowSize(new Vector2(500, 450), ImGuiCond.FirstUseEver);
2021-08-24 18:18:09 +00:00
var windowTitle = string.Format(Language.SettingsWindowTitle, this.Plugin.Name);
2021-05-08 17:34:13 +00:00
if (!ImGui.Begin($"{windowTitle}###soundfilter-settings", ref this._showWindow)) {
ImGui.End();
return;
}
var shouldSave = false;
if (ImGui.Checkbox(Language.SettingsEnableSoundFilter, ref this.Plugin.Config.Enabled)) {
if (this.Plugin.Config.Enabled) {
this.Plugin.Filter.Enable();
} else {
this.Plugin.Filter.Disable();
}
shouldSave = true;
}
shouldSave |= ImGui.Checkbox(Language.SettingsShowSoundLogWindow, ref this.Plugin.Config.ShowLog);
ImGui.Separator();
if (ImGui.CollapsingHeader(Language.SettingsAddFilter)) {
this.AddFilter.Draw();
2021-05-08 17:34:13 +00:00
}
2021-05-08 18:51:53 +00:00
ImGui.Separator();
2021-05-08 17:34:13 +00:00
if (ImGui.BeginChild("filtered-sounds")) {
int? toRemove = null;
(int src, int dst)? drag = null;
for (var i = 0; i < this.Plugin.Config.Filters.Count; i++) {
var filter = this.Plugin.Config.Filters[i];
2021-05-08 17:34:13 +00:00
if (Util.IconButton(FontAwesomeIcon.Trash, $"delete-filter-{i}")) {
toRemove = i;
2021-05-08 17:34:13 +00:00
shouldSave = true;
}
ImGui.SameLine();
if (Util.IconButton(FontAwesomeIcon.PencilAlt, $"edit-filter-{i}")) {
this.EditFilter = new AddFilter(this.Plugin, filter);
}
2021-05-08 17:34:13 +00:00
ImGui.SameLine();
if (Util.IconButton(FontAwesomeIcon.Copy, $"copy-filter-{i}")) {
ImGui.SetClipboardText(string.Join("\n", filter.Globs));
2021-05-08 18:56:08 +00:00
}
ImGui.SameLine();
shouldSave |= ImGui.Checkbox($"{filter.Name}##{i}", ref filter.Enabled);
if (ImGui.IsItemActive() || this._dragging == i) {
this._dragging = i;
var step = 0;
if (ImGui.GetIO().MouseDelta.Y < 0 && ImGui.GetMousePos().Y < ImGui.GetItemRectMin().Y) {
step = -1;
}
if (ImGui.GetIO().MouseDelta.Y > 0 && ImGui.GetMousePos().Y > ImGui.GetItemRectMax().Y) {
step = 1;
}
if (step != 0) {
drag = (i, i + step);
}
}
if (!ImGui.IsItemHovered()) {
continue;
}
ImGui.BeginTooltip();
foreach (var glob in filter.Globs) {
2021-05-08 18:51:53 +00:00
ImGui.TextUnformatted(glob);
2021-05-08 17:34:13 +00:00
}
2021-05-08 20:31:50 +00:00
ImGui.EndTooltip();
}
if (!ImGui.IsMouseDown(ImGuiMouseButton.Left) && this._dragging != -1) {
this._dragging = -1;
this.Plugin.Config.Save();
}
if (drag != null && drag.Value.dst < this.Plugin.Config.Filters.Count && drag.Value.dst >= 0) {
this._dragging = drag.Value.dst;
var temp = this.Plugin.Config.Filters[drag.Value.src];
this.Plugin.Config.Filters[drag.Value.src] = this.Plugin.Config.Filters[drag.Value.dst];
this.Plugin.Config.Filters[drag.Value.dst] = temp;
}
if (toRemove != null) {
this.Plugin.Config.Filters.RemoveAt(toRemove.Value);
2021-05-08 17:34:13 +00:00
}
ImGui.EndChild();
}
if (shouldSave) {
this.Plugin.Config.Save();
}
ImGui.End();
}
}
}