DP-VoidBox/VoidBox/Windows/ConfigWindow.cs

202 lines
5.7 KiB
C#
Raw Normal View History

2024-03-02 01:31:18 +01:00
using Dalamud.Interface.Windowing;
using ImGuiNET;
using Ktisis.Structs.Env.Weather;
2024-03-02 01:31:18 +01:00
using System;
using System.Numerics;
namespace VoidBox.Windows;
public class ConfigWindow : Window, IDisposable
{
private string? _renamingName;
private SavedEnv? _renaming;
2024-03-02 01:31:18 +01:00
public ConfigWindow() : base(
"VoidBox",
ImGuiWindowFlags.NoScrollbar |
2024-03-02 01:31:18 +01:00
ImGuiWindowFlags.NoScrollWithMouse)
{
SizeConstraints = new()
{
MinimumSize = new Vector2(430, 184)
};
2024-03-02 01:31:18 +01:00
Size = SizeConstraints.Value.MinimumSize;
SizeCondition = ImGuiCond.FirstUseEver;
2024-03-02 01:31:18 +01:00
}
public void Dispose() {
}
public override void Draw()
2024-03-02 01:31:18 +01:00
{
var config = Service.Config;
if (ImGui.Checkbox("Enabled", ref config.IsEnabled))
{
config.Save();
}
2024-03-02 01:31:18 +01:00
ImGui.Columns(2, "", false);
if (ImGui.BeginListBox("", new Vector2(-1f, ImGui.GetContentRegionAvail().Y - ImGui.CalcTextSize("").Y - ImGui.GetStyle().ItemSpacing.Y * 2f)))
{
foreach (var entry in Service.Envs.Copy())
{
AddMainEntry(entry);
}
2024-03-02 01:31:18 +01:00
ImGui.EndListBox();
}
2024-03-02 01:31:18 +01:00
if (ImGui.Button("Add current"))
{
config.Envs.Add(new SavedEnv($"Saved #{config.Envs.Count + 1}", Service.Plugin.Current));
config.Save();
}
2024-03-02 01:31:18 +01:00
ImGui.NextColumn();
if (ImGui.BeginCombo("Login", (config.LoginEnv ?? SavedEnv.Unchanged).Name))
{
var env = config.LoginEnv;
foreach (var entry in Service.Envs)
{
if (AddComboEntry(entry, ref env))
{
config.LoginEnv = env;
config.Save();
}
}
ImGui.EndCombo();
}
2024-03-02 01:31:18 +01:00
if (ImGui.BeginCombo("Housing", (config.HousingEnv ?? SavedEnv.Unchanged).Name))
{
var env = config.HousingEnv;
foreach (var entry in Service.Envs)
{
if (AddComboEntry(entry, ref env))
{
config.HousingEnv = env;
config.Save();
}
}
ImGui.EndCombo();
}
2024-03-02 01:31:18 +01:00
void AddMainEntry(EnvCollectionEntry entry)
2024-03-02 01:31:18 +01:00
{
(SavedEnv env, bool isBuiltin) = entry;
bool isPreview = Service.Plugin.Preview == env;
bool isDefault = env.Env == null;
bool isRenaming = _renaming == env;
bool isStyled = false;
if (isPreview)
{
ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.8f, 0.3f, 0.9f, 1f));
isStyled = true;
}
else if (isDefault)
{
ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.8f, 0.3f, 0.3f, 1f));
isStyled = true;
}
else if (isBuiltin)
{
ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.6f, 0.7f, 0.8f, 1f));
isStyled = true;
}
if (_renaming == env)
{
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X);
if (ImGui.InputText("", ref _renamingName, 128, ImGuiInputTextFlags.EnterReturnsTrue))
{
_renaming.Name = _renamingName;
_renaming = null;
config.Save();
}
ImGui.PopItemWidth();
}
else
{
ImGui.Selectable(env.Name, false, ImGuiSelectableFlags.None);
}
if (isStyled)
{
ImGui.PopStyleColor();
}
bool isHovered = ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled);
bool isLMB = isHovered && ImGui.IsMouseClicked(ImGuiMouseButton.Left);
bool isRMB = isHovered && ImGui.IsMouseClicked(ImGuiMouseButton.Right);
if (isHovered)
{
if (isDefault)
{
ImGui.SetTooltip("LMB to disable preview.");
}
else if (isBuiltin)
{
ImGui.SetTooltip("LMB to toggle preview. Built-in preset.");
}
else
{
ImGui.SetTooltip("LMB to toggle preview, RMB to rename, CTRL-RMB to remove.");
}
}
if (isLMB && !isRenaming)
{
Service.Plugin.Preview = isPreview ? null : isDefault ? null : env;
}
if (!isBuiltin && isRMB)
{
if (ImGui.IsKeyDown(ImGuiKey.ModCtrl))
{
if (isRenaming)
{
_renaming = null;
}
else
{
if (isPreview)
{
Service.Plugin.Preview = null;
}
config.Envs.Remove(env);
config.Save();
}
}
else
{
_renamingName = env.Name;
_renaming = env;
}
}
2024-03-02 01:31:18 +01:00
}
bool AddComboEntry(EnvCollectionEntry entry, ref SavedEnv? curr)
2024-03-02 01:31:18 +01:00
{
(SavedEnv env, bool isBuiltin) = entry;
SavedEnv? set = env.Env == null ? null : env;
if (ImGui.Selectable(env.Name, set == curr, ImGuiSelectableFlags.None))
{
curr = set;
return true;
}
return false;
2024-03-02 01:31:18 +01:00
}
}
}