This commit is contained in:
Anna 2022-09-03 22:52:18 -04:00
parent 7956a7b420
commit d6214bc893
2 changed files with 161 additions and 11 deletions

View File

@ -10,9 +10,8 @@ internal class Messages : IDisposable {
private Plugin Plugin { get; }
private SemaphoreSlim CurrentMutex { get; } = new(1, 1);
private Message[] Current { get; set; } = Array.Empty<Message>();
private Dictionary<Guid, Message> Current { get; } = new();
private Queue<Message> SpawnQueue { get; } = new();
private Queue<Message> RemoveQueue { get; } = new();
internal Messages(Plugin plugin) {
this.Plugin = plugin;
@ -39,7 +38,9 @@ internal class Messages : IDisposable {
return;
}
this.Plugin.Vfx.SpawnStatic(VfxPath, message.Position);
if (this.Plugin.Vfx.SpawnStatic(VfxPath, message.Position) == null) {
this.SpawnQueue.Enqueue(message);
}
}
private void SpawnVfx(object? sender, EventArgs e) {
@ -64,12 +65,14 @@ internal class Messages : IDisposable {
var messages = JsonConvert.DeserializeObject<Message[]>(json)!;
await this.CurrentMutex.WaitAsync();
this.Current = messages;
this.CurrentMutex.Release();
this.Current.Clear();
foreach (var message in messages) {
this.Current[message.Id] = message;
this.SpawnQueue.Enqueue(message);
}
this.CurrentMutex.Release();
});
}
@ -77,16 +80,26 @@ internal class Messages : IDisposable {
this.Plugin.Vfx.RemoveAll();
}
internal Message[] Nearby() {
internal IEnumerable<Message> Nearby() {
if (this.Plugin.ClientState.LocalPlayer is not { } player) {
return Array.Empty<Message>();
}
var position = player.Position;
return this.Current
this.CurrentMutex.Wait();
var nearby = this.Current
.Values
.Where(msg => Math.Abs(msg.Position.Y - position.Y) < 1f)
.Where(msg => Vector3.Distance(msg.Position, position) < 5f)
.ToArray();
.ToList();
this.CurrentMutex.Release();
return nearby;
}
internal void Add(Message message) {
this.Current[message.Id] = message;
this.SpawnQueue.Enqueue(message);
}
}

View File

@ -1,6 +1,7 @@
using System.Net.Http.Headers;
using System.Numerics;
using System.Text;
using Dalamud.Interface;
using Dalamud.Logging;
using ImGuiNET;
using Newtonsoft.Json;
@ -10,6 +11,9 @@ namespace OrangeGuidanceTomestone;
public class PluginUi : IDisposable {
private Plugin Plugin { get; }
internal bool WriterVisible;
internal bool ViewerVisible;
private int _pack;
private int _part1 = -1;
@ -31,7 +35,17 @@ public class PluginUi : IDisposable {
}
private void Draw() {
if (!ImGui.Begin("Orange Guidance Tomestone")) {
this.DrawWriter();
this.DrawViewerButton();
this.DrawViewer();
}
private void DrawWriter() {
if (!this.WriterVisible) {
return;
}
if (!ImGui.Begin(this.Plugin.Name, ref this.WriterVisible)) {
ImGui.End();
return;
}
@ -104,6 +118,7 @@ public class PluginUi : IDisposable {
var pack = Pack.All.Value[this._pack];
var actualText = string.Empty;
if (this._part1 == -1) {
ImGui.TextUnformatted(placeholder);
} else {
@ -129,7 +144,8 @@ public class PluginUi : IDisposable {
}
}
ImGui.TextUnformatted(preview.ToString());
actualText = preview.ToString();
ImGui.TextUnformatted(actualText);
}
ImGui.Separator();
@ -181,7 +197,21 @@ public class PluginUi : IDisposable {
content.Headers.Add("X-Api-Key", this.Plugin.Config.ApiKey);
await new HttpClient().PostAsync("https://tryfingerbuthole.anna.lgbt/messages", content);
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);
}
});
}
@ -254,4 +284,111 @@ public class PluginUi : IDisposable {
return true;
}
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;
ImGui.Dummy(new Vector2(1, height / 2 - buttonHeight / 2));
if (this._viewerIdx == 0) {
ImGui.BeginDisabled();
}
if (ImGui.Button("<")) {
this._viewerIdx -= 1;
}
if (this._viewerIdx == 0) {
ImGui.EndDisabled();
}
}
if (ImGui.TableSetColumnIndex(1) && this._viewerIdx > 0 && this._viewerIdx < nearby.Count) {
var message = nearby[this._viewerIdx];
ImGui.TextUnformatted(message.Text);
}
if (ImGui.TableSetColumnIndex(2)) {
var height = ImGui.GetContentRegionAvail().Y;
var buttonHeight = ImGuiHelpers.GetButtonSize(">").Y;
ImGui.Dummy(new Vector2(1, height / 2 - buttonHeight / 2));
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();
}
}