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

181 lines
5.8 KiB
C#
Raw Normal View History

2023-11-14 04:14:57 +00:00
using System.Numerics;
2024-01-04 04:23:09 +00:00
using Dalamud.Interface;
2023-11-14 04:14:57 +00:00
using Dalamud.Interface.Utility;
using EorzeaVotes.Model;
using EorzeaVotes.Utilities;
2023-11-14 04:14:57 +00:00
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);
2024-01-04 04:23:09 +00:00
if (this.Plugin.Manager.Current == null && this.Plugin.Manager.Count == 0) {
2023-11-25 17:33:58 +00:00
ImGuiHelpers.CenteredText("Fetching questions - please wait...");
2023-11-14 04:14:57 +00:00
return;
}
ImGuiHelpers.CenteredText("Active question");
2024-01-04 04:23:09 +00:00
var active = this.Plugin.Manager.Current;
2023-11-14 04:14:57 +00:00
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();
}
2024-04-11 01:51:32 +00:00
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);
}
2023-11-14 04:14:57 +00:00
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);
}
2024-03-14 18:59:41 +00:00
DrawSuggester(active);
2023-11-14 04:14:57 +00:00
}
ImGui.Separator();
this.DrawInactive();
}
2024-03-14 18:59:41 +00:00
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;
2024-03-14 18:59:41 +00:00
ImGui.SetCursorPosX(currentX + move);
ImGui.TextUnformatted(label);
}
2023-11-14 04:14:57 +00:00
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");
2024-01-04 04:47:42 +00:00
var dummyHeight = 0f;
float pageButtonsWidth;
2024-01-04 04:40:32 +00:00
var pageLabel = $"{this.Plugin.Manager.Page:N0}";
2024-01-04 04:47:42 +00:00
2024-01-04 04:40:32 +00:00
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());
2024-01-04 04:53:23 +00:00
dummyHeight += Math.Max(prev.Y, next.Y);
dummyHeight += ImGui.GetStyle().ItemSpacing.Y;
2024-01-04 04:47:42 +00:00
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;
2024-01-04 04:40:32 +00:00
}
2023-11-14 04:14:57 +00:00
2024-01-04 04:47:42 +00:00
ImGui.PushTextWrapPos();
using var pop = new OnDispose(ImGui.PopTextWrapPos);
if (loading) {
2024-01-04 04:53:23 +00:00
ImGuiHelpers.CenteredText("Loading...");
2024-01-04 04:47:42 +00:00
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);
2024-03-14 18:59:41 +00:00
DrawSuggester(full);
2024-01-04 04:47:42 +00:00
ImGui.Spacing();
}
}
2024-01-04 04:40:32 +00:00
}
2024-01-04 04:23:09 +00:00
2024-01-04 04:47:42 +00:00
ImGuiHelpers.CenterCursorFor(pageButtonsWidth);
2024-01-04 04:40:32 +00:00
using (ImGuiHelper.WithDisabled(loading || this.Plugin.Manager.Page == 1)) {
if (ImGuiHelper.IconButton(FontAwesomeIcon.ArrowLeft)) {
this.Plugin.Manager.Page -= 1;
2024-01-04 04:35:13 +00:00
}
2024-01-04 04:40:32 +00:00
}
2024-01-04 04:23:09 +00:00
2024-01-04 04:40:32 +00:00
ImGui.SameLine();
2024-01-04 04:23:09 +00:00
2024-01-04 04:40:32 +00:00
ImGui.TextUnformatted(pageLabel);
2024-01-04 04:35:13 +00:00
2024-01-04 04:40:32 +00:00
ImGui.SameLine();
using (ImGuiHelper.WithDisabled(loading || !this.Plugin.Manager.HasNext)) {
if (ImGuiHelper.IconButton(FontAwesomeIcon.ArrowRight)) {
this.Plugin.Manager.Page += 1;
}
2023-11-14 04:14:57 +00:00
}
}
}