Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: ImRaii.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Oct 1, 2023
1 parent 703642b commit 701554b
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 268 deletions.
3 changes: 2 additions & 1 deletion Resources/RotationSolverRecord.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"ClickingCount": 11761
"ClickingCount": 11761,
"SaidUsers": []
}
12 changes: 8 additions & 4 deletions RotationSolver/UI/CollapsingHeaderGroup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dalamud.Logging;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Logging;
using ECommons.DalamudServices;

namespace RotationSolver.UI;
Expand Down Expand Up @@ -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);
Expand Down
124 changes: 74 additions & 50 deletions RotationSolver/UI/ControlWindow.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -428,6 +453,5 @@ static unsafe void DrawNextAction(float gcd, float ability, float width)
ImGui.SetCursorPosY(y);

DrawIAction(next, ability, 1);
ImGui.EndGroup();
}
}
13 changes: 7 additions & 6 deletions RotationSolver/UI/CooldownWindow.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions RotationSolver/UI/ImGuiHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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))
{
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions RotationSolver/UI/ImguiTooltips.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Dalamud.Interface.Colors;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;

namespace RotationSolver.UI;

Expand Down Expand Up @@ -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);
Expand All @@ -44,7 +46,5 @@ public static void ShowTooltip(Action act)
act();
ImGui.End();
}

ImGui.PopStyleColor();
}
}
Loading

0 comments on commit 701554b

Please sign in to comment.