Screenie/Ui/Tabs/SettingsTab.cs

194 lines
6.4 KiB
C#

using System.Diagnostics;
using System.Numerics;
using System.Text;
using Dalamud.Game.ClientState.GamePad;
using Dalamud.Interface;
using Dalamud.Interface.Components;
using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Utility;
using ImGuiNET;
using Screenie.Util;
namespace Screenie.Ui.Tabs;
internal class SettingsTab : ITab {
public string Label => "Settings";
private Plugin Plugin { get; }
private FileDialogManager FileDialogManager { get; }
private ScreenshotMetadata Metadata { get; set; }
private const int RefreshSeconds = 5;
private readonly Stopwatch _metaUpdate = Stopwatch.StartNew();
internal SettingsTab(Plugin plugin) {
this.Plugin = plugin;
this.FileDialogManager = new FileDialogManager();
this.Metadata = ScreenshotMetadata.Capture(this.Plugin);
}
public void Dispose() {
}
public void Draw() {
try {
this.FileDialogManager.Draw();
} catch (Exception e) {
Plugin.Log.Error(e, "Error in FileDialogManager.Draw");
}
if (this._metaUpdate.Elapsed >= TimeSpan.FromSeconds(RefreshSeconds)) {
this.Metadata = ScreenshotMetadata.Capture(this.Plugin);
this._metaUpdate.Restart();
}
var anyChanged = false;
anyChanged |= this.DrawScreenshotsFolderInput();
ImGui.TextUnformatted("Save format");
ImGui.SetNextItemWidth(-1);
if (ImGui.BeginCombo("##file-format", this.Plugin.Config.SaveFormat.Name())) {
using var endCombo = new OnDispose(ImGui.EndCombo);
foreach (var format in Enum.GetValues<Format>()) {
if (ImGui.Selectable(format.Name(), this.Plugin.Config.SaveFormat == format)) {
this.Plugin.Config.SaveFormat = format;
anyChanged = true;
}
}
}
var label = this.Plugin.Config.SaveFormat switch {
Format.Jpg => "Quality",
Format.WebpLossless => null,
Format.WebpLossy => "Quality",
Format.Png => "Compression level",
_ => "Unknown",
};
if (label != null) {
ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1);
anyChanged |= ImGui.SliderInt("##format-data", ref this.Plugin.Config.SaveFormatData, 0, 100);
}
ImGui.TextUnformatted("Filename format");
ImGui.SetNextItemWidth(-1);
ImGui.PushFont(UiBuilder.MonoFont);
using (new OnDispose(ImGui.PopFont)) {
anyChanged |= ImGui.InputTextMultiline(
"##filename-format",
ref this.Plugin.Config.SaveFileNameFormat,
2048,
new Vector2(-1, 150)
);
}
var template = this.Plugin.Config.SaveFileNameTemplate;
if (template.HasErrors) {
ImGui.TextUnformatted("Invalid template");
foreach (var error in template.Messages) {
ImGui.Bullet();
ImGui.SameLine();
ImGui.TextUnformatted(error.Message);
}
} else {
try {
ImGui.TextUnformatted(this.Plugin.Config.SaveFileNameTemplate.Render(this.Metadata).ReplaceLineEndings(" "));
} catch (Exception ex) {
ImGui.TextUnformatted($"Failed to evaluate: {ex.Message}");
}
}
ImGui.ProgressBar(
(float) (TimeSpan.FromSeconds(RefreshSeconds) - this._metaUpdate.Elapsed).TotalSeconds / RefreshSeconds,
new Vector2(-1, 1)
);
this.DrawGamepadKeybind();
this.DrawKeyboardKeybind();
anyChanged |= ImGui.Checkbox("Disable vanilla in-game screenshots", ref this.Plugin.Config.DisableNativeScreenshots);
if (anyChanged) {
this.Plugin.SaveConfig();
}
}
private void DrawKeyboardKeybind() {
var currentKeybind = new StringBuilder();
foreach (var button in this.Plugin.Config.KeyboardKeybind) {
if (currentKeybind.Length > 0) {
currentKeybind.Append(" + ");
}
currentKeybind.Append(Enum.GetName(button));
}
ImGui.TextUnformatted($"Keyboard: {currentKeybind}");
using (ImGuiHelper.WithDisabled(this.Plugin.KeyboardKeybindTimer.IsRunning)) {
if (ImGui.Button("Change##kb-keybind")) {
this.Plugin.KeyboardKeybindTimer.Restart();
}
}
}
private void DrawGamepadKeybind() {
var currentKeybind = new StringBuilder();
foreach (var button in Enum.GetValues<GamepadButtons>()) {
if ((this.Plugin.Config.GamepadKeybind & button) > 0) {
if (currentKeybind.Length > 0) {
currentKeybind.Append(" + ");
}
currentKeybind.Append(Enum.GetName(button));
}
}
ImGui.TextUnformatted($"Gamepad: {currentKeybind}");
using (ImGuiHelper.WithDisabled(this.Plugin.GamepadKeybindTimer.IsRunning)) {
if (ImGui.Button("Change##gp-keybind")) {
this.Plugin.GamepadKeybindTimer.Restart();
}
}
}
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("browse-save-dir", FontAwesomeIcon.Folder)) {
this.FileDialogManager.OpenFolderDialog(
"Choose screenshots folder",
(b, s) => {
if (!b) {
return;
}
this.Plugin.Config.SaveDirectory = s;
this.Plugin.SaveConfig();
},
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
);
}
return changed;
}
}