Better input preview handling

This commit is contained in:
Infi
2024-05-19 01:51:36 +02:00
parent 31897251f7
commit 23122e924b
4 changed files with 119 additions and 85 deletions
+65 -12
View File
@@ -1,8 +1,11 @@
using System.Numerics;
using System.Text;
using ChatTwo.Code;
using ChatTwo.Resources;
using ChatTwo.Util;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.Windowing;
using ImGuiNET;
@@ -12,15 +15,17 @@ public class InputPreview : Window
{
private ChatLogWindow LogWindow { get; }
private float Height;
internal float PreviewHeight;
private int LastLength;
private Message? PreviewMessage;
internal InputPreview(ChatLogWindow logWindow) : base("##chat2-inputpreview")
{
LogWindow = logWindow;
Flags = ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoMove |
ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoScrollbar |
ImGuiWindowFlags.NoInputs;
ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoScrollbar;
RespectCloseHotkey = false;
DisableWindowSounds = true;
@@ -32,11 +37,11 @@ public class InputPreview : Window
var pos = LogWindow.LastWindowPos;
var size = LogWindow.LastWindowSize;
Size = size with { Y = Height };
Size = size with { Y = PreviewHeight };
var y = Plugin.Config.PreviewPosition switch
{
PreviewPosition.Top => pos.Y - Height,
PreviewPosition.Top => pos.Y - PreviewHeight,
PreviewPosition.Bottom => pos.Y + size.Y,
_ => throw new ArgumentOutOfRangeException(nameof(Plugin.Config.PreviewPosition), Plugin.Config.PreviewPosition, null)
};
@@ -52,15 +57,63 @@ public class InputPreview : Window
public override void Draw()
{
var bytes = Encoding.UTF8.GetBytes(LogWindow.Chat.Trim());
AutoTranslate.ReplaceWithPayload(Plugin.DataManager, ref bytes);
CalculatePreview();
DrawPreview();
}
var chunks = ChunkUtil.ToChunks(SeString.Parse(bytes), ChunkSource.Content, ChatType.Say).ToList();
var encodedChunks = Message.FakeMessage(chunks, new ChatCode((ushort) XivChatType.Say));
internal void CalculatePreview()
{
// We Pre-draw this once to get the actual height :HideThePain:
PreviewHeight = 0;
if (!string.IsNullOrEmpty(LogWindow.Chat))
{
if (PreviewMessage == null || LastLength != LogWindow.Chat.Length)
{
LastLength = LogWindow.Chat.Length;
LogWindow.DrawChunks(encodedChunks.Content);
var bytes = Encoding.UTF8.GetBytes(LogWindow.Chat.Trim());
AutoTranslate.ReplaceWithPayload(ref bytes);
// WindowPadding applies to top and bottom, so we take it 2 times
Height = ImGui.GetCursorPosY() + ImGui.GetStyle().WindowPadding.Y * 2;
var chunks = ChunkUtil.ToChunks(SeString.Parse(bytes), ChunkSource.Content, ChatType.Say).ToList();
PreviewMessage = Message.FakeMessage(chunks, new ChatCode((ushort)XivChatType.Say));
PreviewMessage.DecodeTextParam();
}
var pos = ImGui.GetCursorPos();
ImGui.SetCursorPos(new Vector2(-500, -500));
var before = ImGui.GetCursorPosY();
using (ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero))
{
ImGui.TextUnformatted(Language.Options_Preview_Header);
LogWindow.DrawChunks(PreviewMessage.Content);
}
var after = ImGui.GetCursorPosY();
ImGui.SetCursorPos(pos);
PreviewHeight = after - before;
PreviewHeight += Plugin.Config.PreviewPosition is not PreviewPosition.Inside
? ImGui.GetStyle().WindowPadding.Y * 2
: 0;
}
else
{
LastLength = 0;
PreviewMessage = null;
}
}
internal void DrawPreview()
{
if (PreviewMessage == null)
return;
using (ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero))
{
ImGui.TextUnformatted(Language.Options_Preview_Header);
var handler = LogWindow.HandlerLender.Borrow();
LogWindow.DrawChunks(PreviewMessage.Content, true, handler);
handler.Draw();
}
}
}