diff --git a/ChatTwo/Ui/ChatLog.cs b/ChatTwo/Ui/ChatLog.cs index 44775ad..23057fb 100755 --- a/ChatTwo/Ui/ChatLog.cs +++ b/ChatTwo/Ui/ChatLog.cs @@ -38,12 +38,14 @@ internal sealed class ChatLog : IUiComponent { internal Vector2 LastWindowPos { get; private set; } = Vector2.Zero; private PayloadHandler PayloadHandler { get; } + private Lender HandlerLender { get; } private Dictionary TextCommandChannels { get; } = new(); private HashSet AllCommands { get; } = new(); internal ChatLog(PluginUi ui) { this.Ui = ui; this.PayloadHandler = new PayloadHandler(this.Ui, this); + this.HandlerLender = new Lender(() => new PayloadHandler(this.Ui, this)); this.SetUpTextCommandChannels(); this.SetUpAllCommands(); @@ -618,7 +620,7 @@ internal sealed class ChatLog : IUiComponent { this._hideState = HideState.User; } - private void DrawMessageLog(Tab tab, float childHeight, bool switchedTab) { + private void DrawMessageLog(Tab tab, PayloadHandler handler, float childHeight, bool switchedTab) { if (ImGui.BeginChild("##chat2-messages", new Vector2(-1, childHeight))) { ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); var table = tab.DisplayTimestamp && this.Ui.Plugin.Config.PrettierTimestamps; @@ -712,11 +714,11 @@ internal sealed class ChatLog : IUiComponent { var beforeDraw = ImGui.GetCursorScreenPos(); if (message.Sender.Count > 0) { - this.DrawChunks(message.Sender, true, this.PayloadHandler, lineWidth); + this.DrawChunks(message.Sender, true, handler, lineWidth); ImGui.SameLine(); } - this.DrawChunks(message.Content, true, this.PayloadHandler, lineWidth); + this.DrawChunks(message.Content, true, handler, lineWidth); var afterDraw = ImGui.GetCursorScreenPos(); message.Height = ImGui.GetCursorPosY() - lastPos; @@ -740,7 +742,7 @@ internal sealed class ChatLog : IUiComponent { ImGui.SetScrollHereY(1f); } - this.PayloadHandler.Draw(); + handler.Draw(); if (table) { ImGui.EndTable(); @@ -777,7 +779,7 @@ internal sealed class ChatLog : IUiComponent { this.LastTab = tabI; tab.Unread = 0; - this.DrawMessageLog(tab, GetRemainingHeightForMessageLog(), switchedTab); + this.DrawMessageLog(tab, this.PayloadHandler, GetRemainingHeightForMessageLog(), switchedTab); ImGui.EndTabItem(); } @@ -832,7 +834,7 @@ internal sealed class ChatLog : IUiComponent { } if (currentTab > -1) { - this.DrawMessageLog(this.Ui.Plugin.Config.Tabs[currentTab], childHeight, switchedTab); + this.DrawMessageLog(this.Ui.Plugin.Config.Tabs[currentTab], this.PayloadHandler, childHeight, switchedTab); } ImGui.EndTable(); @@ -897,6 +899,7 @@ internal sealed class ChatLog : IUiComponent { } private void DrawPopOuts() { + this.HandlerLender.ResetCounter(); foreach (var tab in this.Ui.Plugin.Config.Tabs.Where(tab => tab.PopOut)) { this.DrawPopOut(tab); } @@ -922,7 +925,8 @@ internal sealed class ChatLog : IUiComponent { ImGui.Separator(); } - this.DrawMessageLog(tab, ImGui.GetContentRegionAvail().Y, false); + var handler = this.HandlerLender.Borrow(); + this.DrawMessageLog(tab, handler, ImGui.GetContentRegionAvail().Y, false); ImGui.PopID(); diff --git a/ChatTwo/Util/Lender.cs b/ChatTwo/Util/Lender.cs new file mode 100755 index 0000000..24f85d9 --- /dev/null +++ b/ChatTwo/Util/Lender.cs @@ -0,0 +1,23 @@ +namespace ChatTwo.Util; + +internal class Lender { + private readonly Func _ctor; + private readonly List _items = new(); + private int _counter; + + internal Lender(Func ctor) { + this._ctor = ctor; + } + + internal void ResetCounter() { + this._counter = 0; + } + + internal T Borrow() { + if (this._items.Count <= this._counter) { + this._items.Add(this._ctor()); + } + + return this._items[this._counter++]; + } +}