OrangeGuidanceTomestone/client/PluginUi.cs

408 lines
12 KiB
C#
Raw Normal View History

2022-09-03 13:55:36 +00:00
using System.Net.Http.Headers;
2022-09-04 02:23:35 +00:00
using System.Numerics;
2022-09-03 02:59:45 +00:00
using System.Text;
2022-09-04 02:52:18 +00:00
using Dalamud.Interface;
2022-09-03 02:59:45 +00:00
using ImGuiNET;
2022-09-03 13:55:36 +00:00
using Newtonsoft.Json;
2022-09-03 02:59:45 +00:00
namespace OrangeGuidanceTomestone;
public class PluginUi : IDisposable {
private Plugin Plugin { get; }
2022-09-04 02:52:18 +00:00
internal bool WriterVisible;
internal bool ViewerVisible;
2022-09-03 02:59:45 +00:00
private int _pack;
private int _part1 = -1;
private (int, int) _word1 = (-1, -1);
private int _part2 = -1;
private (int, int) _word2 = (-1, -1);
private int _conj = -1;
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
}
public void Dispose() {
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
}
private void Draw() {
2022-09-04 02:52:18 +00:00
this.DrawWriter();
this.DrawViewerButton();
this.DrawViewer();
}
private void DrawWriter() {
if (!this.WriterVisible) {
return;
}
if (!ImGui.Begin(this.Plugin.Name, ref this.WriterVisible)) {
2022-09-03 02:59:45 +00:00
ImGui.End();
return;
}
2022-09-03 12:59:06 +00:00
var packPrev = Pack.All.Value[this._pack].Name;
if (ImGui.BeginCombo("Pack", packPrev)) {
for (var i = 0; i < Pack.All.Value.Length; i++) {
var selPack = Pack.All.Value[i];
if (!ImGui.Selectable(selPack.Name)) {
continue;
}
this._pack = i;
this._part1 = -1;
this._word1 = (-1, -1);
this._conj = -1;
this._part2 = -1;
this._word2 = (-1, -1);
}
ImGui.EndCombo();
}
const string placeholder = "****";
2022-09-03 02:59:45 +00:00
void DrawPicker(string id, IReadOnlyList<string> items, ref int x) {
2022-09-03 12:59:06 +00:00
var preview = x == -1 ? "" : items[x].Replace("{0}", placeholder);
2022-09-03 02:59:45 +00:00
if (!ImGui.BeginCombo(id, preview)) {
return;
}
2022-09-03 12:59:06 +00:00
if (ImGui.Selectable("<none>")) {
x = -1;
}
2022-09-03 03:24:01 +00:00
for (var i = 0; i < items.Count; i++) {
2022-09-03 12:59:06 +00:00
var template = items[i].Replace("{0}", placeholder);
2022-09-03 02:59:45 +00:00
if (ImGui.Selectable(template, i == x)) {
x = i;
}
}
ImGui.EndCombo();
}
void DrawWordPicker(string id, IReadOnlyList<WordList> words, ref (int, int) x) {
2022-09-03 03:24:01 +00:00
var preview = x == (-1, -1) ? "" : words[x.Item1].Words[x.Item2];
2022-09-03 02:59:45 +00:00
if (!ImGui.BeginCombo(id, preview)) {
return;
}
for (var listIdx = 0; listIdx < words.Count; listIdx++) {
var list = words[listIdx];
2022-09-03 03:24:01 +00:00
if (!ImGui.BeginMenu(list.Name)) {
continue;
}
2022-09-03 02:59:45 +00:00
2022-09-03 03:24:01 +00:00
for (var wordIdx = 0; wordIdx < list.Words.Length; wordIdx++) {
if (ImGui.MenuItem(list.Words[wordIdx])) {
x = (listIdx, wordIdx);
}
2022-09-03 02:59:45 +00:00
}
2022-09-03 03:24:01 +00:00
ImGui.EndMenu();
2022-09-03 02:59:45 +00:00
}
ImGui.EndCombo();
}
2022-09-03 12:59:06 +00:00
var pack = Pack.All.Value[this._pack];
2022-09-03 02:59:45 +00:00
2022-09-04 02:52:18 +00:00
var actualText = string.Empty;
2022-09-03 02:59:45 +00:00
if (this._part1 == -1) {
2022-09-03 12:59:06 +00:00
ImGui.TextUnformatted(placeholder);
2022-09-03 02:59:45 +00:00
} else {
var preview = new StringBuilder();
2022-09-03 03:24:01 +00:00
2022-09-03 02:59:45 +00:00
var template1 = pack.Templates[this._part1];
2022-09-03 12:59:06 +00:00
var word1 = this._word1 == (-1, -1) ? placeholder : pack.Words[this._word1.Item1].Words[this._word1.Item2];
2022-09-03 02:59:45 +00:00
preview.Append(string.Format(template1, word1));
if (this._conj != -1) {
var conj = pack.Conjunctions[this._conj];
2022-09-03 03:24:01 +00:00
if (conj.Length != 1 || !char.IsPunctuation(conj[0])) {
2022-09-03 02:59:45 +00:00
preview.Append('\n');
}
2022-09-03 03:24:01 +00:00
preview.Append(conj);
preview.Append(' ');
if (this._part2 != -1) {
var template2 = pack.Templates[this._part2];
2022-09-03 12:59:06 +00:00
var word2 = this._word2 == (-1, -1) ? placeholder : pack.Words[this._word2.Item1].Words[this._word2.Item2];
2022-09-03 03:24:01 +00:00
preview.Append(string.Format(template2, word2));
}
2022-09-03 02:59:45 +00:00
}
2022-09-03 03:24:01 +00:00
2022-09-04 02:52:18 +00:00
actualText = preview.ToString();
ImGui.TextUnformatted(actualText);
2022-09-03 02:59:45 +00:00
}
2022-09-03 03:24:01 +00:00
2022-09-03 02:59:45 +00:00
ImGui.Separator();
DrawPicker("Template##part-1", pack.Templates, ref this._part1);
2022-09-03 12:59:06 +00:00
if (this._part1 > -1 && pack.Templates[this._part1].Contains("{0}")) {
DrawWordPicker("Word##word-1", pack.Words, ref this._word1);
}
DrawPicker("Conjunction##conj", pack.Conjunctions, ref this._conj);
if (this._conj != -1) {
DrawPicker("Template##part-2", pack.Templates, ref this._part2);
if (this._part2 > -1 && pack.Templates[this._part2].Contains("{0}")) {
DrawWordPicker("Word##word-2", pack.Words, ref this._word2);
}
}
this.ClearIfNecessary();
2022-09-03 02:59:45 +00:00
2022-09-03 12:59:06 +00:00
var valid = this.ValidSetup();
2022-09-03 22:44:43 +00:00
if (!valid) {
2022-09-03 12:59:06 +00:00
ImGui.BeginDisabled();
}
2022-09-03 13:55:36 +00:00
if (ImGui.Button("Write") && valid && this.Plugin.ClientState.LocalPlayer is { } player) {
var req = new MessageRequest {
Territory = this.Plugin.ClientState.TerritoryType,
X = player.Position.X,
Y = player.Position.Y,
Z = player.Position.Z,
PackId = pack.Id,
Template1 = this._part1,
Word1List = this._word1.Item1 == -1 ? null : this._word1.Item1,
Word1Word = this._word1.Item2 == -1 ? null : this._word1.Item2,
Conjunction = this._conj == -1 ? null : this._conj,
2022-09-03 22:44:43 +00:00
Template2 = this._part2 == -1 ? null : this._part2,
2022-09-03 13:55:36 +00:00
Word2List = this._word2.Item1 == -1 ? null : this._word2.Item1,
Word2Word = this._word2.Item2 == -1 ? null : this._word2.Item2,
};
var json = JsonConvert.SerializeObject(req);
Task.Run(async () => {
var content = new StringContent(json) {
Headers = {
ContentType = new MediaTypeHeaderValue("application/json"),
},
};
content.Headers.Add("X-Api-Key", this.Plugin.Config.ApiKey);
2022-09-04 02:52:18 +00:00
var resp = await new HttpClient().PostAsync("https://tryfingerbuthole.anna.lgbt/messages", content);
var id = await resp.Content.ReadAsStringAsync();
if (resp.IsSuccessStatusCode) {
var newMsg = new Message {
Id = Guid.Parse(id),
X = player.Position.X,
Y = player.Position.Y,
Z = player.Position.Z,
Text = actualText,
NegativeVotes = 0,
PositiveVotes = 0,
};
this.Plugin.Messages.Add(newMsg);
2022-09-04 03:23:19 +00:00
this.ResetWriter();
this.WriterVisible = false;
2022-09-04 02:52:18 +00:00
}
2022-09-03 13:55:36 +00:00
});
2022-09-03 12:59:06 +00:00
}
2022-09-03 22:44:43 +00:00
if (!valid) {
2022-09-03 12:59:06 +00:00
ImGui.EndDisabled();
}
2022-09-03 02:59:45 +00:00
2022-09-04 02:23:35 +00:00
if (this.Plugin.ClientState.LocalPlayer is { } player2) {
foreach (var msg in this.Plugin.Messages.Nearby()) {
2022-09-04 03:32:38 +00:00
ImGui.TextUnformatted($"{msg.Text}: {Vector3.Distance(msg.Position, player2.Position):N2}");
2022-09-04 02:23:35 +00:00
}
}
2022-09-03 02:59:45 +00:00
ImGui.End();
}
2022-09-03 12:59:06 +00:00
2022-09-04 03:23:19 +00:00
private void ResetWriter() {
this._part1 = this._part2 = this._conj = -1;
this._word1 = (-1, -1);
this._word2 = (-1, -1);
}
2022-09-03 12:59:06 +00:00
private void ClearIfNecessary() {
if (this._pack == -1) {
this._part1 = -1;
}
var pack = Pack.All.Value[this._pack];
if (this._part1 == -1 || !pack.Templates[this._part1].Contains("{0}")) {
this._word1 = (-1, -1);
}
if (this._conj == -1) {
this._part2 = -1;
}
if (this._part2 == -1 || !pack.Templates[this._part2].Contains("{0}")) {
this._word2 = (-1, -1);
}
}
private bool ValidSetup() {
if (this._pack == -1 || this._part1 == -1) {
return false;
}
var pack = Pack.All.Value[this._pack];
var template1 = pack.Templates[this._part1];
var temp1Variable = template1.Contains("{0}");
switch (temp1Variable) {
case true when this._word1 == (-1, -1):
case false when this._word1 != (-1, -1):
return false;
}
if (this._conj == -1 && (this._part2 != -1 || this._word2 != (-1, -1))) {
return false;
}
if (this._conj != -1) {
if (this._part2 == -1) {
return false;
}
var template2 = pack.Templates[this._part2];
var temp2Variable = template2.Contains("{0}");
switch (temp2Variable) {
case true when this._word2 == (-1, -1):
case false when this._word2 != (-1, -1):
return false;
}
}
return true;
}
2022-09-04 02:52:18 +00:00
private void DrawViewerButton() {
if (this.ViewerVisible) {
return;
}
var nearby = this.Plugin.Messages.Nearby().ToList();
if (nearby.Count == 0) {
return;
}
ImGui.SetNextWindowBgAlpha(0.5f);
if (!ImGui.Begin("##ogt-viewer-button", ImGuiWindowFlags.NoTitleBar)) {
ImGui.End();
return;
}
var label = "View message";
if (nearby.Count > 1) {
label += "s";
}
if (ImGui.Button(label)) {
this.ViewerVisible = true;
}
ImGui.End();
}
private int _viewerIdx;
private void DrawViewer() {
if (!this.ViewerVisible) {
return;
}
if (!ImGui.Begin("Messages", ref this.ViewerVisible)) {
ImGui.End();
return;
}
if (ImGui.IsWindowAppearing()) {
this._viewerIdx = 0;
}
var nearby = this.Plugin.Messages.Nearby()
.OrderBy(msg => msg.Id)
.ToList();
if (nearby.Count == 0) {
ImGui.TextUnformatted("No nearby messages");
goto End;
}
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;
2022-09-04 02:53:37 +00:00
ImGui.Dummy(new Vector2(1, height / 2 - buttonHeight / 2 - ImGui.GetStyle().ItemSpacing.Y));
2022-09-04 02:52:18 +00:00
if (this._viewerIdx == 0) {
ImGui.BeginDisabled();
}
if (ImGui.Button("<")) {
this._viewerIdx -= 1;
}
if (this._viewerIdx == 0) {
ImGui.EndDisabled();
}
}
2022-09-04 02:53:37 +00:00
if (ImGui.TableSetColumnIndex(1) && this._viewerIdx > -1 && this._viewerIdx < nearby.Count) {
2022-09-04 02:52:18 +00:00
var message = nearby[this._viewerIdx];
2022-09-04 03:49:46 +00:00
var size = ImGui.CalcTextSize(message.Text, ImGui.GetContentRegionAvail().X);
var height = ImGui.GetContentRegionAvail().Y;
ImGui.Dummy(new Vector2(1, height / 2 - size.Y / 2 - ImGui.GetStyle().ItemSpacing.Y));
ImGui.PushTextWrapPos();
2022-09-04 02:52:18 +00:00
ImGui.TextUnformatted(message.Text);
2022-09-04 03:49:46 +00:00
ImGui.PopTextWrapPos();
2022-09-04 02:52:18 +00:00
}
if (ImGui.TableSetColumnIndex(2)) {
var height = ImGui.GetContentRegionAvail().Y;
var buttonHeight = ImGuiHelpers.GetButtonSize(">").Y;
2022-09-04 02:53:37 +00:00
ImGui.Dummy(new Vector2(1, height / 2 - buttonHeight / 2 - ImGui.GetStyle().ItemSpacing.Y));
2022-09-04 02:52:18 +00:00
if (this._viewerIdx == nearby.Count - 1) {
ImGui.BeginDisabled();
}
if (ImGui.Button(">")) {
this._viewerIdx += 1;
}
if (this._viewerIdx == nearby.Count - 1) {
ImGui.EndDisabled();
}
}
ImGui.EndTable();
End:
ImGui.End();
}
2022-09-03 02:59:45 +00:00
}