TheHeartOfTheParty/TheHeartOfTheParty/PluginUi.cs

158 lines
5.3 KiB
C#
Raw Permalink Normal View History

2022-04-17 05:46:22 +00:00
using System.Numerics;
using ImGuiNET;
2022-04-17 03:15:22 +00:00
using Lumina.Excel.GeneratedSheets;
2022-04-17 03:48:44 +00:00
using Lumina.Text;
2022-04-17 03:15:22 +00:00
2022-04-17 03:48:44 +00:00
namespace TheHeartOfTheParty;
2022-04-17 03:15:22 +00:00
internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
2022-04-17 03:48:44 +00:00
2022-04-17 04:46:15 +00:00
private Dictionary<uint, Achievement> Achievements { get; } = new();
2022-04-17 03:48:44 +00:00
private bool _visible;
private string _searchText = string.Empty;
2022-04-17 03:15:22 +00:00
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
2022-04-17 03:48:44 +00:00
2022-04-17 04:46:15 +00:00
foreach (var achievement in this.Plugin.DataManager.GetExcelSheet<Achievement>()!) {
if (achievement.Title.Row == 0) {
continue;
}
this.Achievements[achievement.Title.Row] = achievement;
}
2022-04-17 03:15:22 +00:00
this.Plugin.Interface.UiBuilder.Draw += this.OnDraw;
}
2022-04-17 03:48:44 +00:00
2022-04-17 03:15:22 +00:00
public void Dispose() {
this.Plugin.Interface.UiBuilder.Draw -= this.OnDraw;
}
2022-04-17 03:48:44 +00:00
internal void Toggle() {
this._visible ^= true;
}
2022-04-17 03:15:22 +00:00
private void OnDraw() {
2022-04-17 03:48:44 +00:00
if (!this._visible) {
return;
}
2022-04-17 05:46:22 +00:00
ImGui.SetNextWindowSize(new Vector2(780, 450), ImGuiCond.FirstUseEver);
2023-09-28 02:13:32 +00:00
if (!ImGui.Begin(Plugin.Name, ref this._visible)) {
2022-04-17 03:15:22 +00:00
ImGui.End();
return;
}
2022-04-17 22:18:40 +00:00
if (ImGui.IsWindowAppearing()) {
this.Plugin.Functions.RequestTitles();
}
2022-04-17 03:48:44 +00:00
var anyChanged = false;
anyChanged |= ImGui.Checkbox("Only show unlocked titles", ref this.Plugin.Config.OnlyUnlocked);
if (ImGui.BeginCombo("Sort", this.Plugin.Config.SortOrder.ToString())) {
foreach (var ordering in Enum.GetValues<SortOrder>()) {
if (ImGui.Selectable(ordering.ToString(), this.Plugin.Config.SortOrder == ordering)) {
this.Plugin.Config.SortOrder = ordering;
anyChanged = true;
}
}
ImGui.EndCombo();
2022-04-17 03:15:22 +00:00
}
2022-04-17 03:48:44 +00:00
if (anyChanged) {
this.Plugin.SaveConfig();
2022-04-17 03:15:22 +00:00
}
2022-04-17 03:48:44 +00:00
var fem = true;
if (this.Plugin.ClientState.LocalPlayer is { } player) {
fem = player.Customize[1] == 1;
2022-04-17 03:15:22 +00:00
}
2022-04-17 03:48:44 +00:00
var titles = this.GetTitles(fem).ToList();
2022-04-17 04:23:05 +00:00
var hint = titles.Count == 1 ? "Search 1 title..." : $"Search {titles.Count} titles...";
2022-04-17 03:48:44 +00:00
ImGui.SetNextItemWidth(-1);
ImGui.InputTextWithHint("##search", hint, ref this._searchText, 64);
2022-04-17 05:46:22 +00:00
if (ImGui.BeginTable("##titles-table", 3, ImGuiTableFlags.ScrollY, ImGui.GetContentRegionAvail())) {
ImGui.TableSetupColumn("Title", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableSetupColumn("Achievement", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableSetupColumn("Category", ImGuiTableColumnFlags.WidthStretch);
ImGui.TableSetupScrollFreeze(0, 1);
ImGui.TableHeadersRow();
2022-04-17 04:46:15 +00:00
2022-04-17 05:46:22 +00:00
foreach (var title in titles) {
ImGui.TableNextRow();
ImGui.TableNextColumn();
2022-04-17 04:46:15 +00:00
2022-04-17 05:46:22 +00:00
if (title.Unlocked) {
const ImGuiSelectableFlags flags = ImGuiSelectableFlags.SpanAllColumns
| ImGuiSelectableFlags.AllowItemOverlap;
// TODO: detect current title?
if (ImGui.Selectable(title.Text, false, flags)) {
this.Plugin.Functions.SetTitle(title.Row.RowId);
}
} else {
ImGui.TextDisabled(title.Text);
2022-04-17 03:15:22 +00:00
}
2022-04-17 04:46:15 +00:00
2022-04-17 05:46:22 +00:00
ImGui.TableNextColumn();
ImGui.TextUnformatted(title.Achievement?.Name?.RawString ?? "???");
ImGui.TableNextColumn();
ImGui.TextUnformatted(title.Achievement?.AchievementCategory.Value?.AchievementKind.Value?.Name?.RawString ?? "???");
2022-04-17 03:15:22 +00:00
}
2022-04-17 03:48:44 +00:00
2022-04-17 05:46:22 +00:00
ImGui.EndTable();
2022-04-17 03:15:22 +00:00
}
ImGui.End();
}
2022-04-17 03:48:44 +00:00
private IEnumerable<TitleInfo> GetTitles(bool fem) {
var titles = this.Plugin.DataManager.GetExcelSheet<Title>()!
.Where(row => row.Order != 0)
.Select(row => new TitleInfo {
Row = row,
Unlocked = this.Plugin.Functions.IsTitleUnlocked(row.RowId),
Text = fem ? row.Feminine : row.Masculine,
2022-04-17 04:46:15 +00:00
Achievement = this.Achievements.TryGetValue(row.RowId, out var achievement) ? achievement : null,
2022-04-17 03:48:44 +00:00
});
if (this.Plugin.Config.OnlyUnlocked) {
titles = titles.Where(t => t.Unlocked);
}
if (this._searchText.Length > 0) {
var search = this._searchText.ToLowerInvariant();
titles = titles.Where(t => t.Text.RawString.ToLowerInvariant().Contains(search));
}
titles = this.Plugin.Config.SortOrder switch {
SortOrder.Default => titles.OrderBy(t => t.Row.Order),
SortOrder.Alphabetical => titles.OrderBy(t => t.Text.RawString),
2022-04-17 04:46:15 +00:00
SortOrder.Achievement => titles.OrderBy(t => t.Achievement?.Name?.RawString ?? "???"),
2022-04-17 04:55:57 +00:00
SortOrder.Category => titles.OrderBy(t => t.Achievement?.AchievementCategory.Value?.AchievementKind.Value?.Name?.RawString ?? "???"),
2022-04-17 03:48:44 +00:00
_ => titles,
};
return titles;
}
}
internal class TitleInfo {
internal Title Row { get; init; } = null!;
internal bool Unlocked { get; init; }
internal SeString Text { get; init; } = null!;
2022-04-17 04:46:15 +00:00
internal Achievement? Achievement { get; init; }
2022-04-17 03:15:22 +00:00
}