104 lines
4.5 KiB
C#
104 lines
4.5 KiB
C#
// CastWarningHud.cs — the "Out of components" warning: a small window at the top of the game view
|
|
// when the server refuses one of RynthAi's spells for missing components, naming the spell and
|
|
// what RynthAi does instead (a lower level of it). OK closes it; it closes by itself after
|
|
// ShownForMs; the same spell warns again at most once every RepeatSameMs (its refusals repeat
|
|
// until the level is tried again). Settings > General > Show No Components Warning turns it off;
|
|
// the chat lines stay either way. Drawn by RynthAi itself (the layer rule: plugins own their screens).
|
|
// Spotted by SilentKelpie (rynth PR #12); re-implemented here.
|
|
// Post: the pump thread (the chat handlers). Render: the render thread (OnRender / OnRenderOverlay).
|
|
using System;
|
|
using System.Numerics;
|
|
using ImGuiNET;
|
|
using RynthCore.Plugin.Shared;
|
|
|
|
namespace RynthCore.Plugin.RynthAi.Huds;
|
|
|
|
internal sealed class CastWarningHud
|
|
{
|
|
/// <summary>How long a warning stays up without OK.</summary>
|
|
internal const long ShownForMs = 30_000;
|
|
/// <summary>The same spell warns again at most this often.</summary>
|
|
internal const long RepeatSameMs = 120_000;
|
|
|
|
private static readonly Vector4 ColTitle = new(1.00f, 0.55f, 0.35f, 1f);
|
|
private static readonly Vector4 ColBody = new(0.92f, 0.92f, 0.92f, 1f);
|
|
private static readonly Vector4 ColHint = new(0.70f, 0.75f, 0.80f, 1f);
|
|
|
|
private readonly Func<long> _nowMs;
|
|
private readonly Func<bool> _enabled;
|
|
private readonly object _gate = new();
|
|
private string _spell = "", _body = "";
|
|
private long _shownAt;
|
|
private bool _up;
|
|
private string _lastSpell = "";
|
|
private long _lastSpellAt;
|
|
|
|
public CastWarningHud(Func<bool> enabled, Func<long>? nowMs = null)
|
|
{
|
|
_enabled = enabled;
|
|
_nowMs = nowMs ?? (() => Environment.TickCount64);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows the warning for <paramref name="spellName"/>; <paramref name="whatNext"/> says what
|
|
/// RynthAi does instead. Nothing with the setting off, or for the same spell within
|
|
/// <see cref="RepeatSameMs"/>. Any thread.
|
|
/// </summary>
|
|
public void Post(string spellName, string whatNext)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(spellName) || !_enabled()) return;
|
|
long now = _nowMs();
|
|
lock (_gate)
|
|
{
|
|
if (string.Equals(spellName, _lastSpell, StringComparison.OrdinalIgnoreCase) && now - _lastSpellAt < RepeatSameMs) return;
|
|
_lastSpell = spellName;
|
|
_lastSpellAt = now;
|
|
_spell = spellName;
|
|
_body = whatNext ?? "";
|
|
_shownAt = now;
|
|
_up = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>The warning up now (spell, what RynthAi does), or null. Closes an expired one, or any with the setting off.</summary>
|
|
internal (string Spell, string Body)? Current()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (!_up) return null;
|
|
if (!_enabled() || _nowMs() - _shownAt > ShownForMs) { _up = false; return null; }
|
|
return (_spell, _body);
|
|
}
|
|
}
|
|
|
|
/// <summary>OK: closes the warning up now.</summary>
|
|
public void Dismiss()
|
|
{
|
|
lock (_gate) _up = false;
|
|
}
|
|
|
|
/// <summary>The warning window, while one is up. Render thread, RynthAi's ImGui context current.</summary>
|
|
public void Render()
|
|
{
|
|
if (Current() is not { } w) return;
|
|
|
|
// Top centre of the game view, clear of the character and the chat. It isn't moved, so it
|
|
// keeps no saved place.
|
|
ImGuiViewportPtr vp = ImGui.GetMainViewport();
|
|
ImGui.SetNextWindowPos(new Vector2(vp.WorkPos.X + vp.WorkSize.X * 0.5f, vp.WorkPos.Y + 60f), ImGuiCond.Always, new Vector2(0.5f, 0f));
|
|
ImGui.SetNextWindowBgAlpha(0.85f);
|
|
const ImGuiWindowFlags flags = ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoSavedSettings
|
|
| ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoNav | ImGuiWindowFlags.NoMove;
|
|
bool visible = ImGui.Begin("Out of components##rynthcastwarn", flags);
|
|
if (visible)
|
|
{
|
|
SafeImGui.TextColored(ColTitle, "Out of components: " + w.Spell);
|
|
if (w.Body.Length > 0) SafeImGui.TextColored(ColBody, w.Body);
|
|
SafeImGui.TextColored(ColHint, "Restock scarabs and tapers to cast it again.");
|
|
if (ImGui.Button("OK##rynthcastwarn")) Dismiss();
|
|
ImGui.SameLine();
|
|
ImGui.TextDisabled("(Settings > General turns this off)");
|
|
}
|
|
ImGui.End();
|
|
}
|
|
}
|