eorzea-votes/client/EorzeaVotes/Ui/Tabs/Questions.cs

181 lines
5.8 KiB
C#

using System.Numerics;
using Dalamud.Interface;
using Dalamud.Interface.Utility;
using EorzeaVotes.Model;
using EorzeaVotes.Utilities;
using ImGuiNET;
namespace EorzeaVotes.Ui.Tabs;
internal class QuestionsTab {
private Plugin Plugin { get; }
private bool _voting;
internal QuestionsTab(Plugin plugin) {
this.Plugin = plugin;
}
internal void Draw() {
if (!ImGui.BeginTabItem("Questions")) {
return;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
if (this.Plugin.Manager.Current == null && this.Plugin.Manager.Count == 0) {
ImGuiHelpers.CenteredText("Fetching questions - please wait...");
return;
}
ImGuiHelpers.CenteredText("Active question");
var active = this.Plugin.Manager.Current;
if (active == null) {
ImGui.TextUnformatted("There is no active question at the moment.");
} else {
ImGui.TextUnformatted(active.Text);
for (var i = 0; i < active.Answers.Length; i++) {
if (i != 0) {
ImGui.SameLine();
}
var label = active.Answers[i];
var buttonSize = ImGuiHelpers.GetButtonSize(label);
var xPosAfter = ImGui.GetCursorPosX()
+ buttonSize.X
+ ImGui.GetStyle().ItemSpacing.X;
if (i != 0 && xPosAfter > ImGui.GetContentRegionAvail().X) {
ImGui.Dummy(Vector2.Zero);
}
var disabled = this._voting || active is FullQuestion;
using var endDisabled = ImGuiHelper.WithDisabled(disabled);
if (!ImGui.Button(active.Answers[i])) {
continue;
}
this._voting = true;
var answer = i;
Task.Run(async () => {
try {
// first vote
await this.Plugin.Manager.Vote(active.Id, (ushort) answer);
// then get updated info
await this.Plugin.Manager.Check();
} finally {
this._voting = false;
}
});
}
if (active is FullQuestion full) {
full.DrawResponses();
this.DrawMoreDetailsButton(full);
}
DrawSuggester(active);
}
ImGui.Separator();
this.DrawInactive();
}
private static void DrawSuggester(IQuestion question) {
if (question.Suggester is not { } suggester) {
return;
}
var disabled = ImGui.GetStyle().Colors[(int) ImGuiCol.TextDisabled];
ImGui.PushStyleColor(ImGuiCol.Text, disabled);
using var popStyleColor = new OnDispose(ImGui.PopStyleColor);
var label = $"Suggested by {suggester}";
var width = ImGui.CalcTextSize(label).X;
var currentX = ImGui.GetCursorPosX();
var availX = ImGui.GetContentRegionAvail().X;
var move = availX - width;
ImGui.SetCursorPosX(currentX + move);
ImGui.TextUnformatted(label);
}
private void DrawMoreDetailsButton(FullQuestion full) {
var open = this.Plugin.Ui.IsMoreDetailsOpen(full.Id);
using var endDisabled = ImGuiHelper.WithDisabled(open);
if (ImGui.Button($"More details##{full.Id}", new Vector2(ImGui.GetContentRegionAvail().X, 0))) {
this.Plugin.Ui.OpenMoreDetails(full);
}
}
private void DrawInactive() {
ImGuiHelpers.CenteredText("Inactive questions");
var dummyHeight = 0f;
float pageButtonsWidth;
var pageLabel = $"{this.Plugin.Manager.Page:N0}";
ImGui.PushFont(UiBuilder.IconFont);
using (new OnDispose(ImGui.PopFont)) {
var prev = ImGuiHelpers.GetButtonSize(FontAwesomeIcon.ArrowLeft.ToIconString());
var label = ImGui.CalcTextSize(pageLabel);
var next = ImGuiHelpers.GetButtonSize(FontAwesomeIcon.ArrowRight.ToIconString());
dummyHeight += Math.Max(prev.Y, next.Y);
dummyHeight += ImGui.GetStyle().ItemSpacing.Y;
pageButtonsWidth = prev.X + label.X + next.X + ImGui.GetStyle().ItemSpacing.X * 2;
}
var loading = this.Plugin.Manager.Loading;
using (new OnDispose(ImGui.EndChild)) {
if (!ImGui.BeginChild("ev-inactive-questions", ImGui.GetContentRegionAvail() - new Vector2(0, dummyHeight), false)) {
return;
}
ImGui.PushTextWrapPos();
using var pop = new OnDispose(ImGui.PopTextWrapPos);
if (loading) {
ImGuiHelpers.CenteredText("Loading...");
ImGui.Spacing();
} else {
foreach (var question in this.Plugin.Manager) {
if (question.Active || question is not FullQuestion full) {
continue;
}
ImGui.TextUnformatted(full.Text);
full.DrawResponses();
this.DrawMoreDetailsButton(full);
DrawSuggester(full);
ImGui.Spacing();
}
}
}
ImGuiHelpers.CenterCursorFor(pageButtonsWidth);
using (ImGuiHelper.WithDisabled(loading || this.Plugin.Manager.Page == 1)) {
if (ImGuiHelper.IconButton(FontAwesomeIcon.ArrowLeft)) {
this.Plugin.Manager.Page -= 1;
}
}
ImGui.SameLine();
ImGui.TextUnformatted(pageLabel);
ImGui.SameLine();
using (ImGuiHelper.WithDisabled(loading || !this.Plugin.Manager.HasNext)) {
if (ImGuiHelper.IconButton(FontAwesomeIcon.ArrowRight)) {
this.Plugin.Manager.Page += 1;
}
}
}
}