Screenie/Ui/PluginUi.cs

102 lines
3.1 KiB
C#

using System.Numerics;
using Dalamud.Interface;
using Dalamud.Interface.Components;
using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Utility;
using ImGuiNET;
using Screenie.Util;
namespace Screenie.Ui;
internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
private FileDialogManager FileDialogManager { get; }
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.FileDialogManager = new FileDialogManager();
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
}
public void Dispose() {
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
}
private void Draw() {
try {
this.FileDialogManager.Draw();
} catch (Exception e) {
Plugin.Log.Error(e, "Error in FileDialogManager.Draw");
}
using var end = new OnDispose(ImGui.End);
ImGui.SetNextWindowSize(new Vector2(350, 500), ImGuiCond.FirstUseEver);
if (!ImGui.Begin(Plugin.Name)) {
return;
}
var anyChanged = false;
anyChanged |= this.DrawScreenshotsFolderInput();
ImGui.SetNextItemWidth(-1);
if (ImGui.BeginCombo("##file-format", Enum.GetName(this.Plugin.Config.SaveFormat))) {
using var endCombo = new OnDispose(ImGui.EndCombo);
foreach (var format in Enum.GetValues<Format>()) {
if (ImGui.Selectable(Enum.GetName(format), this.Plugin.Config.SaveFormat == format)) {
this.Plugin.Config.SaveFormat = format;
anyChanged = true;
}
}
}
var label = this.Plugin.Config.SaveFormat switch {
Format.Jpg => "Quality",
Format.Webp => "Quality",
Format.Png => "Compression level",
_ => "Unknown",
};
ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1);
anyChanged |= ImGui.SliderInt("##format-data", ref this.Plugin.Config.SaveFormatData, 0, 100);
if (anyChanged) {
this.Plugin.SaveConfig();
}
}
private bool DrawScreenshotsFolderInput() {
ImGui.TextUnformatted("Screenshots folder");
var changed = false;
ImGui.PushFont(UiBuilder.IconFont);
Vector2 size;
using (new OnDispose(ImGui.PopFont)) {
size = ImGuiHelpers.GetButtonSize(FontAwesomeIcon.Folder.ToIconString());
}
var availWidth = ImGui.GetContentRegionAvail().X;
ImGui.SetNextItemWidth(availWidth - size.X - ImGui.GetStyle().ItemSpacing.X);
if (ImGui.InputText("##save-directory", ref this.Plugin.Config.SaveDirectory, 1024, ImGuiInputTextFlags.EnterReturnsTrue)) {
changed = true;
}
ImGui.SameLine();
if (ImGuiComponents.IconButton(FontAwesomeIcon.Folder)) {
this.FileDialogManager.OpenFolderDialog("Choose screenshots folder", (b, s) => {
if (!b) {
return;
}
this.Plugin.Config.SaveDirectory = s;
this.Plugin.SaveConfig();
});
}
return changed;
}
}