Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate toasts, fix drawing logic crash #556

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions RotationSolver/Commands/RSCommands_StateSpecialCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ public static partial class RSCommands

internal static string EntryString => $"{_stateString}{(DataCenter.SpecialTimeLeft < 0 ? string.Empty : $" - {_specialString}: {DataCenter.SpecialTimeLeft:F2}s")}";

private static string _lastToastMessage = string.Empty;

private static void UpdateToast()
{
if (!Service.Config.ShowInfoOnToast) return;

Svc.Toasts.ShowQuest($" {EntryString}", new Dalamud.Game.Gui.Toast.QuestToastOptions
string currentMessage = $" {EntryString}";
if (currentMessage == _lastToastMessage) return;

Svc.Toasts.ShowQuest(currentMessage, new Dalamud.Game.Gui.Toast.QuestToastOptions
{
IconId = 101,
});

_lastToastMessage = currentMessage;
}

public static unsafe void DoStateCommandType(StateCommandType stateType, int index = -1) => DoOneCommandType((type, role) => type.ToStateString(role), role =>
Expand Down Expand Up @@ -110,4 +117,4 @@ private static void DoOneCommandType<T>(Func<T, JobRole, string> sayout, Func<Jo
if (Service.Config.SayOutStateChanged) SpeechHelper.Speak(sayout(type, role));
}
}
}
}
38 changes: 21 additions & 17 deletions RotationSolver/UI/HighlightTeachingMode/DrawingHighlightHotbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ private protected override unsafe IEnumerable<IDrawing2D> To2D()
var result = new List<IDrawing2D>();

var hotBarIndex = 0;
foreach (var intPtr in GetAddons<AddonActionBar>()
.Union(GetAddons<AddonActionBarX>())
.Union(GetAddons<AddonActionCross>())
.Union(GetAddons<AddonActionDoubleCrossBase>()))
foreach (var intPtr in GetAddons<AddonActionBar>())
{
var actionBar = (AddonActionBarBase*)intPtr;
if (actionBar != null && IsVisible(actionBar->AtkUnitBase))
Expand Down Expand Up @@ -142,9 +139,16 @@ private static unsafe IEnumerable<nint> GetAddons<T>() where T : struct
{
if (typeof(T).GetCustomAttribute<AddonAttribute>() is not AddonAttribute on) return Array.Empty<nint>();

return on.AddonIdentifiers
.Select(str => Svc.GameGui.GetAddonByName(str, 1))
.Where(ptr => ptr != nint.Zero);
var result = new List<nint>();
foreach (var str in on.AddonIdentifiers)
{
var ptr = Svc.GameGui.GetAddonByName(str, 1);
if (ptr != nint.Zero)
{
result.Add(ptr);
}
}
return result;
}

private static unsafe bool IsVisible(AtkUnitBase unit)
Expand Down Expand Up @@ -239,12 +243,6 @@ public interface IDrawing2D
void Draw();
}

/// <summary> Drawing the image. </summary>
/// <remarks> </remarks>
/// <param name="texture"> </param>
/// <param name="pt1"> </param>
/// <param name="pt2"> </param>
/// <param name="col"> </param>
public readonly struct ImageDrawing(IDalamudTextureWrap texture, Vector2 pt1, Vector2 pt2, uint col = uint.MaxValue) : IDrawing2D
{
/// <summary> </summary>
Expand All @@ -262,13 +260,19 @@ public ImageDrawing(IDalamudTextureWrap texture, Vector2 pt1, Vector2 pt2,
_uv2 = uv2;
}

/// <summary> Draw on the <seealso cref="ImGui" /> </summary>
public void Draw()
public unsafe void Draw()
{
ImGui.GetWindowDrawList().AddImage(_texture.ImGuiHandle, _pt1, _pt2, _uv1, _uv2, _col);
if (_texture == null)
return;

var drawList = ImGui.GetWindowDrawList();
if (drawList.NativePtr == null)
return;

drawList.AddImage(_texture.ImGuiHandle, _pt1, _pt2, _uv1, _uv2, _col);
}

private readonly uint _col = col;
private readonly Vector2 _pt1 = pt1, _pt2 = pt2, _uv1 = default, _uv2 = Vector2.One;
private readonly IDalamudTextureWrap _texture = texture;
}
}
26 changes: 16 additions & 10 deletions RotationSolver/UI/OverlayWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Dalamud.Interface.Windowing;
using ECommons.DalamudServices;
using RotationSolver.UI.HighlightTeachingMode;
using System.Linq;
using System.Threading.Tasks;

namespace RotationSolver.UI;

Expand Down Expand Up @@ -37,7 +35,7 @@ public override void PreDraw()
base.PreDraw();
}

public override async void Draw()
public unsafe override void Draw()
{
if (!HotbarHighlightManager.Enable || Svc.ClientState == null || Svc.ClientState.LocalPlayer == null)
return;
Expand All @@ -46,10 +44,17 @@ public override async void Draw()

try
{
await UpdateDrawingElementsAsync();
UpdateDrawingElementsAsync().GetAwaiter().GetResult();

if (HotbarHighlightManager._drawingElements2D != null)
{
var drawList = ImGui.GetWindowDrawList();
if (drawList.NativePtr == null)
{
Svc.Log.Warning($"{nameof(OverlayWindow)}: Window draw list is null.");
return;
}

foreach (var item in HotbarHighlightManager._drawingElements2D.OrderBy(GetDrawingOrder))
{
item.Draw();
Expand All @@ -72,13 +77,14 @@ private async Task UpdateDrawingElementsAsync()

private int GetDrawingOrder(object drawing)
{
if (drawing is PolylineDrawing poly)
{
return poly._thickness == 0 ? 0 : 1;
}
else
switch (drawing)
{
return 2;
case PolylineDrawing poly:
return poly._thickness == 0 ? 0 : 1;
case ImageDrawing:
return 1;
default:
return 2;
}
}

Expand Down