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
+12 -54
View File
@@ -9,7 +9,6 @@ using ChatTwo.Util;
using Dalamud.Game.Addon.Lifecycle;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Keys;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Interface;
@@ -67,10 +66,6 @@ public sealed class ChatLogWindow : Window
private int AutoCompleteSelection;
private bool AutoCompleteShouldScroll;
private int LastLength;
private float PreviewHeight;
private Message? PreviewMessage;
public int CursorPos;
public Vector2 LastWindowPos { get; private set; } = Vector2.Zero;
@@ -327,7 +322,9 @@ public sealed class ChatLogWindow : Window
var lineHeight = ImGui.CalcTextSize("A").Y;
var height = ImGui.GetContentRegionAvail().Y - lineHeight * 2 - ImGui.GetStyle().ItemSpacing.Y - ImGui.GetStyle().FramePadding.Y * 2;
height -= PreviewHeight;
if (Plugin.Config.PreviewPosition is PreviewPosition.Inside)
height -= Plugin.InputPreview.PreviewHeight;
return height;
}
@@ -519,40 +516,9 @@ public sealed class ChatLogWindow : Window
LastViewport = ImGui.GetWindowViewport().NativePtr;
WasDocked = ImGui.IsWindowDocked();
// We Predraw this once to get the actual height :HideThePain:
PreviewHeight = 0;
if (Plugin.Config.PreviewPosition is PreviewPosition.Inside && !string.IsNullOrEmpty(Chat))
{
if (PreviewMessage == null || LastLength != Chat.Length)
{
LastLength = Chat.Length;
var bytes = Encoding.UTF8.GetBytes(Chat.Trim());
AutoTranslate.ReplaceWithPayload(Plugin.DataManager, ref bytes);
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);
DrawChunks(PreviewMessage.Content);
}
var after = ImGui.GetCursorPosY();
ImGui.SetCursorPos(pos);
PreviewHeight = after - before;
}
else
{
LastLength = 0;
PreviewMessage = null;
}
var drawPreview = Plugin.Config.PreviewPosition is PreviewPosition.Inside;
if (drawPreview)
Plugin.InputPreview.CalculatePreview();
var currentTab = Plugin.Config.SidebarTabView ? DrawTabSidebar() : DrawTabBar();
@@ -560,16 +526,8 @@ public sealed class ChatLogWindow : Window
if (currentTab > -1 && currentTab < Plugin.Config.Tabs.Count)
activeTab = Plugin.Config.Tabs[currentTab];
if (PreviewMessage != null)
{
using (ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero))
{
ImGui.TextUnformatted("Text Preview:");
var handler = HandlerLender.Borrow();
DrawChunks(PreviewMessage.Content, true, handler);
handler.Draw();
}
}
if (drawPreview)
Plugin.InputPreview.DrawPreview();
using (ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero))
{
@@ -861,7 +819,7 @@ public sealed class ChatLogWindow : Window
reason = TellReason.Friend;
var tellBytes = Encoding.UTF8.GetBytes(trimmed);
AutoTranslate.ReplaceWithPayload(Plugin.DataManager, ref tellBytes);
AutoTranslate.ReplaceWithPayload(ref tellBytes);
Plugin.Functions.Chat.SendTell(reason, target.ContentId, target.Name, (ushort) world.RowId, tellBytes);
}
@@ -881,7 +839,7 @@ public sealed class ChatLogWindow : Window
}
var bytes = Encoding.UTF8.GetBytes(trimmed);
AutoTranslate.ReplaceWithPayload(Plugin.DataManager, ref bytes);
AutoTranslate.ReplaceWithPayload(ref bytes);
Plugin.Common.Functions.Chat.SendMessageUnsafe(bytes);
}
@@ -1286,7 +1244,7 @@ public sealed class ChatLogWindow : Window
if (AutoCompleteInfo == null)
return;
AutoCompleteList ??= AutoTranslate.Matching(Plugin.DataManager, AutoCompleteInfo.ToComplete, Plugin.Config.SortAutoTranslate);
AutoCompleteList ??= AutoTranslate.Matching(AutoCompleteInfo.ToComplete, Plugin.Config.SortAutoTranslate);
if (AutoCompleteOpen)
{
ImGui.OpenPopup(AutoCompleteId);
@@ -1309,7 +1267,7 @@ public sealed class ChatLogWindow : Window
ImGui.SetNextItemWidth(-1);
if (ImGui.InputTextWithHint("##auto-complete-filter", Language.AutoTranslate_Search_Hint, ref AutoCompleteInfo.ToComplete, 256, ImGuiInputTextFlags.CallbackAlways | ImGuiInputTextFlags.CallbackHistory, AutoCompleteCallback))
{
AutoCompleteList = AutoTranslate.Matching(Plugin.DataManager, AutoCompleteInfo.ToComplete, Plugin.Config.SortAutoTranslate);
AutoCompleteList = AutoTranslate.Matching(AutoCompleteInfo.ToComplete, Plugin.Config.SortAutoTranslate);
AutoCompleteSelection = 0;
AutoCompleteShouldScroll = true;
}
+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();
}
}
}