From 701554b0fef29ada670004225848dc4f7fdd909f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E6=B0=B4?= <1123993881@qq.com> Date: Sun, 1 Oct 2023 10:07:14 +0800 Subject: [PATCH] fix: ImRaii. --- Resources/RotationSolverRecord.json | 3 +- RotationSolver/UI/CollapsingHeaderGroup.cs | 12 +- RotationSolver/UI/ControlWindow.cs | 124 ++++--- RotationSolver/UI/CooldownWindow.cs | 13 +- RotationSolver/UI/ImGuiHelper.cs | 6 +- RotationSolver/UI/ImguiTooltips.cs | 6 +- RotationSolver/UI/RotationConfigWindow.cs | 396 ++++++++++----------- 7 files changed, 292 insertions(+), 268 deletions(-) diff --git a/Resources/RotationSolverRecord.json b/Resources/RotationSolverRecord.json index 477d4a431..bf129bfe2 100644 --- a/Resources/RotationSolverRecord.json +++ b/Resources/RotationSolverRecord.json @@ -1,3 +1,4 @@ { - "ClickingCount": 11761 + "ClickingCount": 11761, + "SaidUsers": [] } \ No newline at end of file diff --git a/RotationSolver/UI/CollapsingHeaderGroup.cs b/RotationSolver/UI/CollapsingHeaderGroup.cs index a350c34a7..ba701dd9f 100644 --- a/RotationSolver/UI/CollapsingHeaderGroup.cs +++ b/RotationSolver/UI/CollapsingHeaderGroup.cs @@ -1,4 +1,5 @@ -using Dalamud.Logging; +using Dalamud.Interface.Utility.Raii; +using Dalamud.Logging; using ECommons.DalamudServices; namespace RotationSolver.UI; @@ -53,9 +54,12 @@ public void Draw() ImGui.Spacing(); ImGui.Separator(); var selected = index == _openedIndex; - ImGui.PushFont(ImGuiHelper.GetFont(HeaderSize)); - var changed = ImGui.Selectable(name, selected, ImGuiSelectableFlags.DontClosePopups); - ImGui.PopFont(); + var changed = false; + using (var font = ImRaii.PushFont(ImGuiHelper.GetFont(HeaderSize))) + { + changed = ImGui.Selectable(name, selected, ImGuiSelectableFlags.DontClosePopups); + } + if (ImGui.IsItemHovered()) { ImGui.SetMouseCursor(ImGuiMouseCursor.Hand); diff --git a/RotationSolver/UI/ControlWindow.cs b/RotationSolver/UI/ControlWindow.cs index 2d40411f2..f7e296edc 100644 --- a/RotationSolver/UI/ControlWindow.cs +++ b/RotationSolver/UI/ControlWindow.cs @@ -1,5 +1,6 @@ using Dalamud.Interface.Colors; using Dalamud.Interface.Internal; +using Dalamud.Interface.Utility.Raii; using ECommons.DalamudServices; using ImGuiScene; using RotationSolver.Basic.Configuration; @@ -180,17 +181,21 @@ private static void DrawSpecials() ImGui.SameLine(); - ImGui.BeginGroup(); - ImGui.Text(DataCenter.RightNowTargetToHostileType switch + using (var group = ImRaii.Group()) { - TargetHostileType.AllTargetsCanAttack => LocalizationManager.RightLang.ConfigWindow_Param_TargetToHostileType1, - TargetHostileType.TargetsHaveTargetOrAllTargetsCanAttack => LocalizationManager.RightLang.ConfigWindow_Param_TargetToHostileType2, - TargetHostileType.TargetsHaveTarget => LocalizationManager.RightLang.ConfigWindow_Param_TargetToHostileType3, - _ => string.Empty, - }); + if (group) + { + ImGui.Text(DataCenter.RightNowTargetToHostileType switch + { + TargetHostileType.AllTargetsCanAttack => LocalizationManager.RightLang.ConfigWindow_Param_TargetToHostileType1, + TargetHostileType.TargetsHaveTargetOrAllTargetsCanAttack => LocalizationManager.RightLang.ConfigWindow_Param_TargetToHostileType2, + TargetHostileType.TargetsHaveTarget => LocalizationManager.RightLang.ConfigWindow_Param_TargetToHostileType3, + _ => string.Empty, + }); - ImGui.Text("Auto: " + DataCenter.AutoStatus.ToString()); - ImGui.EndGroup(); + ImGui.Text("Auto: " + DataCenter.AutoStatus.ToString()); + } + } if (Service.Config.GetValue(PluginConfigFloat.MistakeRatio) > 0) { @@ -212,30 +217,37 @@ static void DrawCommandAction(IAction gcd, IAction ability, SpecialCommandType c var strWidth = ImGui.CalcTextSize(str).X; var pos = ImGui.GetCursorPos(); - ImGui.BeginGroup(); - ImGui.BeginGroup(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, width / 2 - strWidth / 2)); - ImGui.TextColored(color, str); - - var help = command.ToHelp(); - string baseId = "ImgButton" + command.ToString(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, strWidth / 2 - width / 2)); + using var group = ImRaii.Group(); + if (!group) return; - if(IconSet.GetTexture(gcd, out var texture)) + using (var subGroup = ImRaii.Group()) { - var y = ImGui.GetCursorPosY(); - - DrawIAction(texture.ImGuiHandle, baseId + nameof(gcd), gcdW, command, help); - if (IconSet.GetTexture(ability, out texture)) + if (subGroup) { - ImGui.SameLine(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, width / 2 - strWidth / 2)); + ImGui.TextColored(color, str); + + var help = command.ToHelp(); + string baseId = "ImgButton" + command.ToString(); - ImGui.SetCursorPosY(y); - DrawIAction(texture.ImGuiHandle, baseId + nameof(ability), abilityW, command, help); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, strWidth / 2 - width / 2)); + + if (IconSet.GetTexture(gcd, out var texture)) + { + var y = ImGui.GetCursorPosY(); + + DrawIAction(texture.ImGuiHandle, baseId + nameof(gcd), gcdW, command, help); + if (IconSet.GetTexture(ability, out texture)) + { + ImGui.SameLine(); + + ImGui.SetCursorPosY(y); + DrawIAction(texture.ImGuiHandle, baseId + nameof(ability), abilityW, command, help); + } + } } } - ImGui.EndGroup(); if (DataCenter.SpecialType == command) { @@ -251,7 +263,6 @@ static void DrawCommandAction(IAction gcd, IAction ability, SpecialCommandType c CooldownWindow.TextShade(winPos + pos + size - strSize, time); } } - ImGui.EndGroup(); } public static void HighLight(Vector2 pt, Vector2 size, float thickness = 2f) @@ -279,17 +290,25 @@ static void DrawCommandAction(IDalamudTextureWrap texture, SpecialCommandType co var strWidth = ImGui.CalcTextSize(str).X; var pos = ImGui.GetCursorPos(); - ImGui.BeginGroup(); - ImGui.BeginGroup(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, width / 2 - strWidth / 2)); - ImGui.TextColored(color, str); - var help = command.ToHelp(); - string baseId = "ImgButton" + command.ToString(); + using var group = ImRaii.Group(); + if (!group) return; - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, strWidth / 2 - width / 2)); - if(texture != null) DrawIAction(texture.ImGuiHandle, baseId, abilityW, command, help); - ImGui.EndGroup(); + using (var subGroup = ImRaii.Group()) + { + if (subGroup) + { + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, width / 2 - strWidth / 2)); + ImGui.TextColored(color, str); + + var help = command.ToHelp(); + string baseId = "ImgButton" + command.ToString(); + + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, strWidth / 2 - width / 2)); + if (texture != null) DrawIAction(texture.ImGuiHandle, baseId, abilityW, command, help); + + } + } if (DataCenter.SpecialType == command) { @@ -305,7 +324,6 @@ static void DrawCommandAction(IDalamudTextureWrap texture, SpecialCommandType co CooldownWindow.TextShade(winPos + pos + size - strSize, time); } } - ImGui.EndGroup(); } static void DrawCommandAction(uint iconId, StateCommandType command, Vector4 color) @@ -316,20 +334,25 @@ static void DrawCommandAction(uint iconId, StateCommandType command, Vector4 col var strWidth = ImGui.CalcTextSize(str).X; var pos = ImGui.GetCursorPos(); - ImGui.BeginGroup(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, width / 2 - strWidth / 2)); - ImGui.TextColored(color, str); - - var help = command.ToHelp(); - string baseId = "ImgButton" + command.ToString(); - if(IconSet.GetTexture(iconId, out var texture)) + using (var group = ImRaii.Group()) { - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, strWidth / 2 - width / 2)); - DrawIAction(texture.ImGuiHandle, baseId, abilityW, command, help); - } + if (group) + { + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, width / 2 - strWidth / 2)); + ImGui.TextColored(color, str); - ImGui.EndGroup(); + var help = command.ToHelp(); + string baseId = "ImgButton" + command.ToString(); + + if (IconSet.GetTexture(iconId, out var texture)) + { + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + Math.Max(0, strWidth / 2 - width / 2)); + DrawIAction(texture.ImGuiHandle, baseId, abilityW, command, help); + } + + } + } bool isMatch = false; switch (command) @@ -410,7 +433,9 @@ internal static (Vector2, Vector2) DrawIAction(IAction action, float width, floa static unsafe void DrawNextAction(float gcd, float ability, float width) { - ImGui.BeginGroup(); + using var group = ImRaii.Group(); + if (!group) return; + var str = "Next Action"; ImGui.SetCursorPosX(ImGui.GetCursorPosX() + width / 2 - ImGui.CalcTextSize(str).X / 2); ImGui.TextColored(ImGuiColors.DalamudYellow, str); @@ -428,6 +453,5 @@ static unsafe void DrawNextAction(float gcd, float ability, float width) ImGui.SetCursorPosY(y); DrawIAction(next, ability, 1); - ImGui.EndGroup(); } } diff --git a/RotationSolver/UI/CooldownWindow.cs b/RotationSolver/UI/CooldownWindow.cs index abbf3cb26..c5b8f1b84 100644 --- a/RotationSolver/UI/CooldownWindow.cs +++ b/RotationSolver/UI/CooldownWindow.cs @@ -1,4 +1,6 @@ -using RotationSolver.Basic.Configuration; +using Dalamud.Interface.Utility.Raii; +using ECommons.DalamudServices; +using RotationSolver.Basic.Configuration; using RotationSolver.Localization; using RotationSolver.Updaters; @@ -50,7 +52,9 @@ private static void DrawActionCooldown(IAction act, float width) var elapsed = act.RecastTimeElapsedRaw; var shouldSkip = recast < 3 && act is IBaseAction a && !a.IsRealGCD; - ImGui.BeginGroup(); + using var group = ImRaii.Group(); + if (!group) return; + var winPos = ImGui.GetWindowPos(); var r = -1f; @@ -82,13 +86,12 @@ private static void DrawActionCooldown(IAction act, float width) ImGui.GetWindowDrawList().AddLine(startPos, startPos + new Vector2(0, size.Y), black); } - ImGui.PushFont(ImGuiHelper.GetFont(Service.Config.GetValue(PluginConfigFloat.CooldownFontSize))); + using var font = ImRaii.PushFont(ImGuiHelper.GetFont(Service.Config.GetValue(PluginConfigFloat.CooldownFontSize))); string time = recast == 0 ? "0" : ((int)(recast - elapsed % recast) + 1).ToString(); var strSize = ImGui.CalcTextSize(time); var fontPos = new Vector2(pos.X + size.X / 2 - strSize.X / 2, pos.Y + size.Y / 2 - strSize.Y / 2) + winPos; TextShade(fontPos, time); - ImGui.PopFont(); } if (act.EnoughLevel && act is IBaseAction bAct && bAct.MaxCharges > 1) @@ -98,8 +101,6 @@ private static void DrawActionCooldown(IAction act, float width) ImGui.GetWindowDrawList().AddCircleFilled(winPos + pos + (i + 0.5f) * new Vector2(width / 5, 0), width / 12, white); } } - - ImGui.EndGroup(); } static readonly uint black = ImGui.ColorConvertFloat4ToU32(new Vector4(0, 0, 0, 1)); diff --git a/RotationSolver/UI/ImGuiHelper.cs b/RotationSolver/UI/ImGuiHelper.cs index c2a4147a5..63996a9cf 100644 --- a/RotationSolver/UI/ImGuiHelper.cs +++ b/RotationSolver/UI/ImGuiHelper.cs @@ -1,6 +1,7 @@ using Dalamud.Game.ClientState.Keys; using Dalamud.Interface.Internal; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; using ECommons.DalamudServices; using ECommons.ImGuiMethods; using ImGuiScene; @@ -297,7 +298,8 @@ internal static void DrawActionOverlay(Vector2 cursor, float width, float percen #region PopUp public static void DrawHotKeysPopup(string key, string command, params (string name, Action action, string[] keys)[] pairs) { - if (ImGui.BeginPopup(key)) + using var popup = ImRaii.Popup(key); + if (popup) { if (ImGui.BeginTable(key, 2, ImGuiTableFlags.BordersOuter)) { @@ -314,8 +316,6 @@ public static void DrawHotKeysPopup(string key, string command, params (string n } ImGui.EndTable(); } - - ImGui.EndPopup(); } } public static void PrepareGroup(string key, string command, Action reset) diff --git a/RotationSolver/UI/ImguiTooltips.cs b/RotationSolver/UI/ImguiTooltips.cs index fe59633c0..2e29fc0de 100644 --- a/RotationSolver/UI/ImguiTooltips.cs +++ b/RotationSolver/UI/ImguiTooltips.cs @@ -1,5 +1,6 @@ using Dalamud.Interface.Colors; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; namespace RotationSolver.UI; @@ -34,7 +35,8 @@ public static void ShowTooltip(Action act) if (!Service.Config.GetValue(Basic.Configuration.PluginConfigBool.ShowTooltips)) return; ImGui.SetNextWindowBgAlpha(1); - ImGui.PushStyleColor(ImGuiCol.BorderShadow, ImGuiColors.DalamudWhite); + + using var color = ImRaii.PushColor(ImGuiCol.BorderShadow, ImGuiColors.DalamudWhite); ImGui.SetNextWindowSizeConstraints(new Vector2(150, 0) * ImGuiHelpers.GlobalScale, new Vector2(1200, 1500) * ImGuiHelpers.GlobalScale); ImGui.SetWindowPos(TOOLTIP_ID, ImGui.GetIO().MousePos); @@ -44,7 +46,5 @@ public static void ShowTooltip(Action act) act(); ImGui.End(); } - - ImGui.PopStyleColor(); } } diff --git a/RotationSolver/UI/RotationConfigWindow.cs b/RotationSolver/UI/RotationConfigWindow.cs index 13269abee..226700155 100644 --- a/RotationSolver/UI/RotationConfigWindow.cs +++ b/RotationSolver/UI/RotationConfigWindow.cs @@ -1,6 +1,7 @@ using Dalamud.Interface.Colors; using Dalamud.Interface.Internal; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; using Dalamud.Interface.Windowing; using Dalamud.Logging; using Dalamud.Utility; @@ -48,18 +49,6 @@ public RotationConfigWindow() RespectCloseHotkey = true; } - public override void PreDraw() - { - ImGui.PushStyleVar(ImGuiStyleVar.SelectableTextAlign, new Vector2(0.5f, 0.5f)); - base.PreDraw(); - } - - public override void PostDraw() - { - ImGui.PopStyleVar(); - base.PostDraw(); - } - public override void OnClose() { Service.Config.Save(); @@ -69,6 +58,7 @@ public override void OnClose() public override void Draw() { + using var style = ImRaii.PushStyle(ImGuiStyleVar.SelectableTextAlign, new Vector2(0.5f, 0.5f)); try { var leftTop = ImGui.GetWindowPos() + ImGui.GetCursorPos(); @@ -83,38 +73,38 @@ public override void Draw() str += "Move away! Don't crash! "; } - ImGui.PushFont(ImGuiHelper.GetFont(24)); - ImGui.PushStyleColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudYellow)); + using var font = ImRaii.PushFont(ImGuiHelper.GetFont(24)); + using var color = ImRaii.PushColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudYellow)); ImGui.TextWrapped(str); - ImGui.PopStyleColor(); - ImGui.PopFont(); } - else if (ImGui.BeginTable("Rotation Config Table", 2, ImGuiTableFlags.Resizable)) + else { - ImGui.TableSetupColumn("Rotation Config Side Bar", ImGuiTableColumnFlags.WidthFixed, 100 * _scale); - ImGui.TableNextColumn(); - - try - { - DrawSideBar(); - } - catch (Exception ex) + using var table = ImRaii.Table("Rotation Config Table", 2, ImGuiTableFlags.Resizable); + if (table) { - Svc.Log.Warning(ex, "Something wrong with sideBar"); - } + ImGui.TableSetupColumn("Rotation Config Side Bar", ImGuiTableColumnFlags.WidthFixed, 100 * _scale); + ImGui.TableNextColumn(); - ImGui.TableNextColumn(); + try + { + DrawSideBar(); + } + catch (Exception ex) + { + Svc.Log.Warning(ex, "Something wrong with sideBar"); + } - try - { - DrawBody(); - } - catch (Exception ex) - { - Svc.Log.Warning(ex, "Something wrong with body"); - } + ImGui.TableNextColumn(); - ImGui.EndTable(); + try + { + DrawBody(); + } + catch (Exception ex) + { + Svc.Log.Warning(ex, "Something wrong with body"); + } + } } } catch(Exception ex) @@ -125,7 +115,8 @@ public override void Draw() private void DrawSideBar() { - if (BeginChild("Rotation Solver Side bar", -Vector2.One, false, ImGuiWindowFlags.NoScrollbar)) + using var child = ImRaii.Child("Rotation Solver Side bar", -Vector2.One, false, ImGuiWindowFlags.NoScrollbar); + if (child) { var wholeWidth = ImGui.GetWindowSize().X; @@ -150,7 +141,8 @@ private void DrawSideBar() { ImGuiHelper.DrawItemMiddle(() => { - if (ImGui.BeginPopup("Searching Popup")) + using var popup = ImRaii.Popup("Searching Popup"); + if (popup) { ImGui.SetNextItemWidth(200 * _scale); SearchingBox(); @@ -158,7 +150,6 @@ private void DrawSideBar() { ImGui.CloseCurrentPopup(); } - ImGui.EndPopup(); } var cursor = ImGui.GetCursorPos(); @@ -230,8 +221,6 @@ private void DrawSideBar() Util.OpenLink("https://ko-fi.com/B0B0IN5DX"); } } - - ImGui.EndChild(); } } @@ -330,7 +319,7 @@ private void DrawHeader(float wholeWidth) ImGui.SameLine(); - ImGui.BeginGroup(); + using var group = ImRaii.Group(); DrawRotationCombo(comboSize, rotations, rotation, gameVersion); ImGui.TextDisabled(slash); @@ -342,7 +331,7 @@ private void DrawHeader(float wholeWidth) ImGui.SameLine(); } ImGui.Text(rotation.GameVersion); - ImGui.EndGroup(); + }, wholeWidth, horizonalWholeWidth); } } @@ -364,28 +353,32 @@ private void DrawRotationIcon(ICustomRotation rotation, float iconSize) private static void DrawRotationCombo(float comboSize, ICustomRotation[] rotations, ICustomRotation rotation, string gameVersion) { ImGui.SetNextItemWidth(comboSize); - ImGui.PushStyleColor(ImGuiCol.Text, rotation.GetColor()); const string popUp = "Rotation Solver Select Rotation"; - if (ImGui.Selectable(rotation.RotationName + "##RotationName:" + rotation.Name)) + + using (var color = ImRaii.PushColor(ImGuiCol.Text, rotation.GetColor())) { - if (!ImGui.IsPopupOpen(popUp)) ImGui.OpenPopup(popUp); + if (ImGui.Selectable(rotation.RotationName + "##RotationName:" + rotation.Name)) + { + if (!ImGui.IsPopupOpen(popUp)) ImGui.OpenPopup(popUp); + } } - ImGui.PopStyleColor(); - if (ImGui.BeginPopup(popUp)) + using (var popup = ImRaii.Popup(popUp)) { - foreach (var r in rotations) + if (popup) { - ImGui.PushStyleColor(ImGuiCol.Text, r.GetColor()); - if (ImGui.Selectable(r.RotationName)) + foreach (var r in rotations) { - Service.Config.GetJobConfig(Job).RotationChoice = r.GetType().FullName; - Service.Config.Save(); + ImGui.PushStyleColor(ImGuiCol.Text, r.GetColor()); + if (ImGui.Selectable(r.RotationName)) + { + Service.Config.GetJobConfig(Job).RotationChoice = r.GetType().FullName; + Service.Config.Save(); + } + ImguiTooltips.HoveredTooltip(r.Description); + ImGui.PopStyleColor(); } - ImguiTooltips.HoveredTooltip(r.Description); - ImGui.PopStyleColor(); } - ImGui.EndPopup(); } var warning = gameVersion + rotation.GameVersion; @@ -401,15 +394,17 @@ private static void DrawRotationCombo(float comboSize, ICustomRotation[] rotatio private void DrawBody() { ImGui.SetCursorPos(ImGui.GetCursorPos() + Vector2.One * 8 * _scale); - if (BeginChild("Rotation Solver Body", -Vector2.One)) + using var child = ImRaii.Child("Rotation Solver Body", -Vector2.One); + if (child) { if (_searchResults != null && _searchResults.Any()) { - ImGui.PushFont(ImGuiHelper.GetFont(18)); - ImGui.PushStyleColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudYellow)); - ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_Search_Result); - ImGui.PopStyleColor(); - ImGui.PopFont(); + using(var font = ImRaii.PushFont(ImGuiHelper.GetFont(18))) + { + using var color = ImRaii.PushColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudYellow)); + ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_Search_Result); + } + ImGui.Spacing(); foreach (var searchable in _searchResults) @@ -466,8 +461,6 @@ private void DrawBody() break; } } - - ImGui.EndChild(); } } @@ -481,19 +474,21 @@ private void DrawBody() private static void DrawAbout() { - ImGui.PushFont(ImGuiHelper.GetFont(18)); - ImGui.PushStyleColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudYellow)); - ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_About_Punchline); - ImGui.PopStyleColor(); - ImGui.PopFont(); + using (var font = ImRaii.PushFont(ImGuiHelper.GetFont(18))) + { + using var color = ImRaii.PushColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudYellow)); + ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_About_Punchline); + } + ImGui.Spacing(); ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_About_Description); ImGui.Spacing(); - ImGui.PushStyleColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudOrange)); - ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_About_Warning); - ImGui.PopStyleColor(); + using (var color = ImRaii.PushColor(ImGuiCol.Text, ImGui.ColorConvertFloat4ToU32(ImGuiColors.DalamudOrange))) + { + ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_About_Warning); + } var width = ImGui.GetWindowWidth(); if (IconSet.GetTexture("https://discordapp.com/api/guilds/1064448004498653245/embed.png?style=banner2", out var icon) && ImGuiHelper.TextureButton(icon, width, width)) @@ -504,7 +499,7 @@ private static void DrawAbout() var clickingCount = OtherConfiguration.RotationSolverRecord.ClickingCount; if (clickingCount > 0) { - ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.2f, 0.6f, 0.95f, 1)); + using var color = ImRaii.PushColor(ImGuiCol.Text, new Vector4(0.2f, 0.6f, 0.95f, 1)); var countStr = string.Format(LocalizationManager.RightLang.ConfigWindow_About_ClickingCount, clickingCount); ImGuiHelper.DrawItemMiddle(() => @@ -524,8 +519,6 @@ private static void DrawAbout() break; } } - - ImGui.PopStyleColor(); } _aboutHeaders.Draw(); @@ -540,7 +533,7 @@ private static void DrawAbout() private static void DrawAboutMacros() { - ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0f, 5f)); + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(0f, 5f)); StateCommandType.Auto.DisplayCommandHelp(getHelp: EnumTranslations.ToHelp); @@ -575,8 +568,6 @@ private static void DrawAboutMacros() SpecialCommandType.AntiKnockback.DisplayCommandHelp(getHelp: EnumTranslations.ToHelp); SpecialCommandType.Burst.DisplayCommandHelp(getHelp: EnumTranslations.ToHelp); - - ImGui.PopStyleVar(); } private static void DrawAboutCompatibility() @@ -589,9 +580,10 @@ private static void DrawAboutCompatibility() var iconSize = 40 * _scale; - if (ImGui.BeginTable("Incompatible plugin", 4, ImGuiTableFlags.BordersInner + using var table = ImRaii.Table("Incompatible plugin", 4, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable - | ImGuiTableFlags.SizingStretchProp)) + | ImGuiTableFlags.SizingStretchProp); + if (table) { ImGui.TableSetupScrollFreeze(0, 1); ImGui.TableNextRow(ImGuiTableRowFlags.Headers); @@ -649,9 +641,7 @@ private static void DrawAboutCompatibility() ImguiTooltips.HoveredTooltip(LocalizationManager.RightLang.ConfigWindow_About_Compatibility_Crash); } } - ImGui.EndTable(); } - } private static void DrawAboutLinks() @@ -677,7 +667,6 @@ private static void DrawAboutLinks() Process.Start("explorer.exe", Svc.PluginInterface.ConfigDirectory.FullName); } }, width, textWidth); - } #endregion @@ -690,9 +679,8 @@ private static void DrawRotation() var desc = rotation.Description; if (!string.IsNullOrEmpty(desc)) { - ImGui.PushFont(ImGuiHelper.GetFont(15)); + using var font = ImRaii.PushFont(ImGuiHelper.GetFont(15)); ImGuiEx.TextWrappedCopy(desc); - ImGui.PopFont(); } var wholeWidth = ImGui.GetWindowWidth(); @@ -703,9 +691,9 @@ private static void DrawRotation() { var author = info.Author; if (string.IsNullOrEmpty(author)) author = "Author"; - ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DPSRed); + + using var color = ImRaii.PushColor(ImGuiCol.Text, ImGuiColors.DPSRed); ImGui.TextWrapped(string.Format(rotation.WhyNotValid, author)); - ImGui.PopStyleColor(); } if (!string.IsNullOrEmpty(info.DonateLink)) @@ -745,23 +733,28 @@ private static bool DrawRating(float value1, int value2, float max) var spacing = ImGui.GetStyle().ItemSpacing; ImGui.GetStyle().ItemSpacing = Vector2.Zero; - ImGui.PushFont(ImGuiHelper.GetFont(16)); - ImGui.PushStyleColor(ImGuiCol.Text, RatingColors[(int)(ratio1 * count)]); - ImGui.Text($"{value1:F2}"); - var size = ImGui.GetItemRectSize(); - ImGui.PopStyleColor(); + var size = Vector2.Zero; + using (var font = ImRaii.PushFont(ImGuiHelper.GetFont(16))) + { + using (var color = ImRaii.PushColor(ImGuiCol.Text, RatingColors[(int)(ratio1 * count)])) + { + ImGui.Text($"{value1:F2}"); + } + size += ImGui.GetItemRectSize(); + + ImGui.SameLine(); + ImGui.Text("/"); + size.X += ImGui.GetItemRectSize().X; - ImGui.SameLine(); - ImGui.Text("/"); - size.X += ImGui.GetItemRectSize().X; + ImGui.SameLine(); - ImGui.SameLine(); - ImGui.PushStyleColor(ImGuiCol.Text, RatingColors[(int)(ratio2 * count)]); - ImGui.Text($"{value2} "); - size.X += ImGui.GetItemRectSize().X; - ImGui.PopStyleColor(); + using (var color = ImRaii.PushColor(ImGuiCol.Text, RatingColors[(int)(ratio2 * count)])) + { + ImGui.Text($"{value2} "); + } - ImGui.PopFont(); + size.X += ImGui.GetItemRectSize().X; + } ImGui.GetStyle().ItemSpacing = spacing; var radius = size.Y * 0.2f; @@ -845,65 +838,67 @@ private static void DrawRotationDescription() attrs.Add(RotationDescAttribute.MergeToOne(m.GetCustomAttributes())); } - if (ImGui.BeginTable("Rotation Description", 2, ImGuiTableFlags.Borders + using (var table = ImRaii.Table("Rotation Description", 2, ImGuiTableFlags.Borders | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchProp)) { - foreach (var a in RotationDescAttribute.Merge(attrs)) + if (table) { - var attr = RotationDescAttribute.MergeToOne(a); - if (attr == null) continue; - - var allActions = attr.Actions.Select(i => rotation.AllBaseActions - .FirstOrDefault(a => a.ID == (uint)i)) - .Where(i => i != null); - - bool hasDesc = !string.IsNullOrEmpty(attr.Description); + foreach (var a in RotationDescAttribute.Merge(attrs)) + { + var attr = RotationDescAttribute.MergeToOne(a); + if (attr == null) continue; - if (!hasDesc && !allActions.Any()) continue; + var allActions = attr.Actions.Select(i => rotation.AllBaseActions + .FirstOrDefault(a => a.ID == (uint)i)) + .Where(i => i != null); - ImGui.TableNextRow(); - ImGui.TableNextColumn(); + bool hasDesc = !string.IsNullOrEmpty(attr.Description); - if(IconSet.GetTexture(attr.IconID, out var image)) ImGui.Image(image.ImGuiHandle, Vector2.One * DESC_SIZE * _scale); + if (!hasDesc && !allActions.Any()) continue; - ImGui.SameLine(); - var isOnCommand = attr.IsOnCommand; - if (isOnCommand) ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudYellow); - ImGui.Text(" " + attr.Type.ToName()); - if (isOnCommand) ImGui.PopStyleColor(); + ImGui.TableNextRow(); + ImGui.TableNextColumn(); - ImGui.TableNextColumn(); + if (IconSet.GetTexture(attr.IconID, out var image)) ImGui.Image(image.ImGuiHandle, Vector2.One * DESC_SIZE * _scale); - if (hasDesc) - { - ImGui.Text(attr.Description); - } + ImGui.SameLine(); + var isOnCommand = attr.IsOnCommand; + if (isOnCommand) ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudYellow); + ImGui.Text(" " + attr.Type.ToName()); + if (isOnCommand) ImGui.PopStyleColor(); - bool notStart = false; - var size = DESC_SIZE * _scale; - var y = ImGui.GetCursorPosY() + size * 4 / 82; - foreach (var item in allActions) - { - if (item == null) continue; + ImGui.TableNextColumn(); - if (notStart) + if (hasDesc) { - ImGui.SameLine(); + ImGui.Text(attr.Description); } - if (item.GetTexture(out var texture)) + bool notStart = false; + var size = DESC_SIZE * _scale; + var y = ImGui.GetCursorPosY() + size * 4 / 82; + foreach (var item in allActions) { - ImGui.SetCursorPosY(y); - var cursor = ImGui.GetCursorPos(); - ImGuiHelper.NoPaddingNoColorImageButton(texture.ImGuiHandle, Vector2.One * size); - ImGuiHelper.DrawActionOverlay(cursor, size, 1); - ImguiTooltips.HoveredTooltip(item.Name); + if (item == null) continue; + + if (notStart) + { + ImGui.SameLine(); + } + + if (item.GetTexture(out var texture)) + { + ImGui.SetCursorPosY(y); + var cursor = ImGui.GetCursorPos(); + ImGuiHelper.NoPaddingNoColorImageButton(texture.ImGuiHandle, Vector2.One * size); + ImGuiHelper.DrawActionOverlay(cursor, size, 1); + ImguiTooltips.HoveredTooltip(item.Name); + } + notStart = true; } - notStart = true; } } - ImGui.EndTable(); } var links = type.GetCustomAttributes(); @@ -1080,19 +1075,22 @@ private static void DrawRotationInformation() ImGuiHelper.DrawItemMiddle(() => { - ImGui.BeginGroup(); - if (ImGui.Button(info.Name)) + using var group = ImRaii.Group(); + if (group) { - Process.Start("explorer.exe", "/select, \"" + info.FilePath + "\""); - } + if (ImGui.Button(info.Name)) + { + Process.Start("explorer.exe", "/select, \"" + info.FilePath + "\""); + } - var version = assembly.GetName().Version; - if (version != null) - { - ImGui.Text(" v " + version.ToString()); + var version = assembly.GetName().Version; + if (version != null) + { + ImGui.Text(" v " + version.ToString()); + } + ImGui.Text(" - " + info.Author); } - ImGui.Text(" - " + info.Author); - ImGui.EndGroup(); + }, wholeWidth, _groupWidth); _groupWidth = ImGui.GetItemRectSize().X; @@ -1107,7 +1105,9 @@ private static unsafe void DrawActions() { ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_Actions_Description); - if (ImGui.BeginTable("Rotation Solver Actions", 2, ImGuiTableFlags.Resizable)) + using var table = ImRaii.Table("Rotation Solver Actions", 2, ImGuiTableFlags.Resizable); + + if (table) { ImGui.TableSetupColumn("Action Column", ImGuiTableColumnFlags.WidthFixed, ImGui.GetWindowWidth() / 2); ImGui.TableNextColumn(); @@ -1285,8 +1285,6 @@ private static unsafe void DrawActions() { _sequencerList.Draw(); } - - ImGui.EndTable(); } } @@ -1365,10 +1363,11 @@ private static void DrawRotationsLoaded() .SelectMany(g => g.Rotations) .GroupBy(r => r.GetType().Assembly); - - if (ImGui.BeginTable("Rotation Solver AssemblyTable", 3, ImGuiTableFlags.BordersInner + using var table = ImRaii.Table("Rotation Solver AssemblyTable", 3, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable - | ImGuiTableFlags.SizingStretchProp)) + | ImGuiTableFlags.SizingStretchProp); + + if (table) { ImGui.TableSetupScrollFreeze(0, 1); ImGui.TableNextRow(ImGuiTableRowFlags.Headers); @@ -1430,7 +1429,6 @@ private static void DrawRotationsLoaded() Util.OpenLink(info.DonateLink); } } - ImGui.EndTable(); } } @@ -1657,7 +1655,8 @@ private static void DrawActionsStatuses() ImGui.SetNextItemWidth(ImGui.GetWindowWidth()); ImGui.InputTextWithHint("##Searching the action", LocalizationManager.RightLang.ConfigWindow_List_StatusNameOrId, ref _statusSearching, 128); - if (ImGui.BeginTable("Rotation Solver List Statuses", 2, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame)) + using var table = ImRaii.Table("Rotation Solver List Statuses", 2, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame); + if (table) { ImGui.TableSetupScrollFreeze(0, 1); ImGui.TableNextRow(ImGuiTableRowFlags.Headers); @@ -1680,8 +1679,6 @@ private static void DrawActionsStatuses() ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_List_DangerousStatusDesc); DrawStatusList(nameof(OtherConfiguration.DangerousStatus), OtherConfiguration.DangerousStatus, AllDispelStatus); - - ImGui.EndTable(); } } @@ -1747,14 +1744,16 @@ private static void DrawStatusList(string name, HashSet statuses, Status[] internal static void StatusPopUp(string popupId, Status[] allStatus, ref string searching, Action clicked, uint notLoadId = 10100, float size = 32) { - if (ImGui.BeginPopup(popupId)) + using var popup = ImRaii.Popup(popupId); + if (popup) { ImGui.SetNextItemWidth(200 * _scale); ImGui.InputTextWithHint("##Searching the status", LocalizationManager.RightLang.ConfigWindow_List_StatusNameOrId, ref searching, 128); ImGui.Spacing(); - if (ImGui.BeginChild("Rotation Solver Add Status", new Vector2(-1, 400 * _scale))) + using var child = ImRaii.Child("Rotation Solver Add Status", new Vector2(-1, 400 * _scale)); + if (child) { var count = Math.Max(1, (int)MathF.Floor(ImGui.GetWindowWidth() / (size * 3 / 4 * _scale + ImGui.GetStyle().ItemSpacing.X))); var index = 0; @@ -1776,10 +1775,7 @@ internal static void StatusPopUp(string popupId, Status[] allStatus, ref string ImguiTooltips.HoveredTooltip($"{status.Name} ({status.RowId})"); } } - ImGui.EndChild(); } - - ImGui.EndPopup(); } } @@ -1787,12 +1783,14 @@ internal static void StatusPopUp(string popupId, Status[] allStatus, ref string { new CheckBoxSearchPlugin(PluginConfigBool.RecordCastingArea), }; + private static void DrawListActions() { ImGui.SetNextItemWidth(ImGui.GetWindowWidth()); ImGui.InputTextWithHint("##Searching the action", LocalizationManager.RightLang.ConfigWindow_List_ActionNameOrId, ref _actionSearching, 128); - if (ImGui.BeginTable("Rotation Solver List Actions", 2, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame)) + using var table = ImRaii.Table("Rotation Solver List Actions", 2, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame); + if (table) { ImGui.TableSetupScrollFreeze(0, 1); ImGui.TableNextRow(ImGuiTableRowFlags.Headers); @@ -1817,8 +1815,6 @@ private static void DrawListActions() ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_List_HostileCastingAreaDesc); DrawActionsList(nameof(OtherConfiguration.HostileCastingArea), OtherConfiguration.HostileCastingArea); - - ImGui.EndTable(); } } @@ -1829,14 +1825,16 @@ private static void DrawActionsList(string name, HashSet actions) var popupId = "Rotation Solver Action Popup" + name; - if (ImGui.BeginPopup(popupId)) + using var popup = ImRaii.Popup(popupId); + if (popup) { ImGui.SetNextItemWidth(200 * _scale); ImGui.InputTextWithHint("##Searching the action pop up", LocalizationManager.RightLang.ConfigWindow_List_ActionNameOrId, ref _actionSearching, 128); ImGui.Spacing(); - if (ImGui.BeginChild("Rotation Solver Add action", new Vector2(-1, 400 * _scale))) + using var child = ImRaii.Child("Rotation Solver Add action", new Vector2(-1, 400 * _scale)); + if (child) { foreach (var action in AllActions.OrderByDescending(s =>Similarity(s.Name + " " + s.RowId.ToString(), _actionSearching))) { @@ -1852,10 +1850,7 @@ private static void DrawActionsList(string name, HashSet actions) } } } - ImGui.EndChild(); } - - ImGui.EndPopup(); } @@ -1896,34 +1891,35 @@ private static void DrawListTerritories() var territoryId = Svc.ClientState.TerritoryType; - ImGui.PushFont(ImGuiHelper.GetFont(21)); - ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudYellow); - const int iconSize = 32; - var territory = Service.GetSheet().GetRow(territoryId); - if (territory == null) return; - var contentFinder = territory?.ContentFinderCondition?.Value; - var territoryName = territory?.PlaceName?.Value?.Name ?? string.Empty; - if(contentFinder != null && !string.IsNullOrEmpty(contentFinder.Name)) - { - territoryName += $" ({contentFinder.Name})"; - } - var icon = territory?.ContentFinderCondition?.Value?.ContentType?.Value?.Icon ?? 23; - if (icon == 0) icon = 23; - var getIcon = IconSet.GetTexture(icon, out var texture); - ImGuiHelper.DrawItemMiddle(() => + using (var font = ImRaii.PushFont(ImGuiHelper.GetFont(21))) { - if (getIcon) + using var color = ImRaii.PushColor(ImGuiCol.Text, ImGuiColors.DalamudYellow); + + const int iconSize = 32; + var territory = Service.GetSheet().GetRow(territoryId); + if (territory == null) return; + var contentFinder = territory?.ContentFinderCondition?.Value; + var territoryName = territory?.PlaceName?.Value?.Name ?? string.Empty; + if (contentFinder != null && !string.IsNullOrEmpty(contentFinder.Name)) { - ImGui.Image(texture.ImGuiHandle, Vector2.One * 28 * _scale); - ImGui.SameLine(); + territoryName += $" ({contentFinder.Name})"; } - ImGui.Text(territoryName); - }, ImGui.GetWindowWidth(), ImGui.CalcTextSize(territoryName).X + ImGui.GetStyle().ItemSpacing.X + iconSize); - - ImGui.PopStyleColor(); - ImGui.PopFont(); + var icon = territory?.ContentFinderCondition?.Value?.ContentType?.Value?.Icon ?? 23; + if (icon == 0) icon = 23; + var getIcon = IconSet.GetTexture(icon, out var texture); + ImGuiHelper.DrawItemMiddle(() => + { + if (getIcon) + { + ImGui.Image(texture.ImGuiHandle, Vector2.One * 28 * _scale); + ImGui.SameLine(); + } + ImGui.Text(territoryName); + }, ImGui.GetWindowWidth(), ImGui.CalcTextSize(territoryName).X + ImGui.GetStyle().ItemSpacing.X + iconSize); + } - if (ImGui.BeginTable("Rotation Solver List Territories", 3, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame)) + using var table = ImRaii.Table("Rotation Solver List Territories", 3, ImGuiTableFlags.BordersInner | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame); + if (table) { ImGui.TableSetupScrollFreeze(0, 1); ImGui.TableNextRow(ImGuiTableRowFlags.Headers); @@ -2064,8 +2060,6 @@ private static void DrawListTerritories() OtherConfiguration.BeneficialPositions[territoryId] = list.ToArray(); OtherConfiguration.SaveBeneficialPositions(); } - - ImGui.EndTable(); } } #endregion