OrangeGuidanceTomestone/client/Ui/Viewer.cs

186 lines
5.8 KiB
C#
Raw Normal View History

2022-09-04 05:03:59 +00:00
using System.Numerics;
using Dalamud.Interface;
2023-09-29 00:53:50 +00:00
using Dalamud.Interface.Utility;
2022-09-04 05:03:59 +00:00
using ImGuiNET;
2022-09-04 05:23:44 +00:00
using OrangeGuidanceTomestone.Helpers;
2022-09-04 05:03:59 +00:00
namespace OrangeGuidanceTomestone.Ui;
internal class Viewer {
private Plugin Plugin { get; }
internal bool Visible;
private int _idx;
internal Viewer(Plugin plugin) {
this.Plugin = plugin;
}
internal void Draw() {
if (!this.Visible) {
return;
}
2022-09-07 17:25:29 +00:00
var flags = ImGuiWindowFlags.NoFocusOnAppearing;
2022-09-07 17:25:05 +00:00
flags |= this.Plugin.Config.HideTitlebar ? ImGuiWindowFlags.NoTitleBar : ImGuiWindowFlags.None;
flags |= this.Plugin.Config.LockViewer ? ImGuiWindowFlags.NoMove : ImGuiWindowFlags.None;
flags |= this.Plugin.Config.ClickThroughViewer ? ImGuiWindowFlags.NoInputs : ImGuiWindowFlags.None;
2022-09-04 07:40:34 +00:00
ImGui.SetNextWindowSize(new Vector2(350, 175), ImGuiCond.FirstUseEver);
2022-09-05 10:12:36 +00:00
ImGui.SetNextWindowBgAlpha(this.Plugin.Config.ViewerOpacity / 100.0f);
2022-09-07 09:13:50 +00:00
if (!ImGui.Begin("Messages", ref this.Visible, flags)) {
2022-09-04 05:03:59 +00:00
ImGui.End();
return;
}
if (ImGui.IsWindowAppearing()) {
this._idx = 0;
}
var nearby = this.Plugin.Messages.Nearby()
.OrderBy(msg => msg.Id)
.ToList();
if (nearby.Count == 0) {
2022-09-05 23:23:55 +00:00
if (this.Plugin.Config.AutoViewerClose) {
2022-09-05 09:05:14 +00:00
this.Visible = false;
2022-09-05 09:33:36 +00:00
} else {
ImGui.TextUnformatted("No nearby messages");
2022-09-05 09:05:14 +00:00
}
2022-09-04 05:03:59 +00:00
goto End;
}
2022-09-04 22:20:07 +00:00
if (this._idx >= nearby.Count) {
this._idx = Math.Max(0, nearby.Count - 1);
}
2022-09-04 05:03:59 +00:00
if (!ImGui.BeginTable("##viewer-table", 3)) {
goto End;
}
ImGui.TableSetupColumn("##prev-arrow", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableSetupColumn("##content", ImGuiTableColumnFlags.WidthStretch);
ImGui.TableSetupColumn("##next-arrow", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableNextRow();
if (ImGui.TableSetColumnIndex(0)) {
var height = ImGui.GetContentRegionAvail().Y;
var buttonHeight = ImGuiHelpers.GetButtonSize("<").Y;
ImGui.Dummy(new Vector2(1, height / 2 - buttonHeight / 2 - ImGui.GetStyle().ItemSpacing.Y));
if (this._idx == 0) {
ImGui.BeginDisabled();
}
2022-09-09 10:19:14 +00:00
if (ImGuiHelper.IconButton(FontAwesomeIcon.AngleLeft)) {
2022-09-04 05:03:59 +00:00
this._idx -= 1;
}
if (this._idx == 0) {
ImGui.EndDisabled();
}
}
if (ImGui.TableSetColumnIndex(1) && this._idx > -1 && this._idx < nearby.Count) {
var message = nearby[this._idx];
2022-09-04 05:28:54 +00:00
var size = ImGui.CalcTextSize(message.Text, ImGui.GetContentRegionAvail().X).Y;
size += ImGui.GetStyle().ItemSpacing.Y * 2;
size += ImGui.CalcTextSize("A").Y;
size += ImGuiHelpers.GetButtonSize("A").Y;
2022-09-04 05:03:59 +00:00
var height = ImGui.GetContentRegionAvail().Y;
2022-09-04 05:28:54 +00:00
ImGui.Dummy(new Vector2(1, height / 2 - size / 2 - ImGui.GetStyle().ItemSpacing.Y));
2022-09-04 05:03:59 +00:00
ImGui.PushTextWrapPos();
ImGui.TextUnformatted(message.Text);
ImGui.PopTextWrapPos();
2022-09-04 05:23:44 +00:00
2022-09-04 07:12:51 +00:00
var appraisals = Math.Max(0, message.PositiveVotes - message.NegativeVotes);
ImGui.TextUnformatted($"Appraisals: {appraisals:N0}");
2022-09-04 05:27:04 +00:00
2022-09-04 07:12:51 +00:00
void Vote(int way) {
2022-09-04 05:23:44 +00:00
Task.Run(async () => {
2022-09-04 07:12:51 +00:00
var resp = await ServerHelper.SendRequest(
2022-09-04 05:23:44 +00:00
this.Plugin.Config.ApiKey,
HttpMethod.Patch,
$"/messages/{message.Id}/votes",
2022-09-04 05:25:13 +00:00
"application/json",
2022-09-04 07:12:51 +00:00
new StringContent(way.ToString())
2022-09-04 05:23:44 +00:00
);
2022-09-04 07:12:51 +00:00
if (resp.IsSuccessStatusCode) {
2022-09-04 07:19:48 +00:00
var oldWay = message.UserVote;
switch (oldWay) {
case 1:
message.PositiveVotes -= 1;
break;
case -1:
message.NegativeVotes -= 1;
break;
}
switch (way) {
case 1:
message.PositiveVotes += 1;
break;
case -1:
message.NegativeVotes += 1;
break;
}
2022-09-04 07:12:51 +00:00
message.UserVote = way;
}
2022-09-04 05:23:44 +00:00
});
}
2022-09-04 05:27:04 +00:00
2022-09-04 07:12:51 +00:00
var vote = message.UserVote;
if (vote == 1) {
ImGui.BeginDisabled();
}
if (ImGui.Button("Like")) {
Vote(1);
}
if (vote == 1) {
ImGui.EndDisabled();
}
2022-09-04 05:23:44 +00:00
ImGui.SameLine();
2022-09-04 07:12:51 +00:00
if (vote == -1) {
ImGui.BeginDisabled();
}
2022-09-04 05:23:44 +00:00
if (ImGui.Button("Dislike")) {
2022-09-04 07:12:51 +00:00
Vote(-1);
}
if (vote == -1) {
ImGui.EndDisabled();
2022-09-04 05:23:44 +00:00
}
2022-09-04 05:03:59 +00:00
}
if (ImGui.TableSetColumnIndex(2)) {
var height = ImGui.GetContentRegionAvail().Y;
var buttonHeight = ImGuiHelpers.GetButtonSize(">").Y;
ImGui.Dummy(new Vector2(1, height / 2 - buttonHeight / 2 - ImGui.GetStyle().ItemSpacing.Y));
if (this._idx == nearby.Count - 1) {
ImGui.BeginDisabled();
}
2022-09-09 10:19:14 +00:00
if (ImGuiHelper.IconButton(FontAwesomeIcon.AngleRight)) {
2022-09-04 05:03:59 +00:00
this._idx += 1;
}
if (this._idx == nearby.Count - 1) {
ImGui.EndDisabled();
}
}
ImGui.EndTable();
End:
ImGui.End();
}
}