RynthSuite/Plugins/RynthCore.Plugin.RynthDefective/Appraisal/AppraisalWindow.cs

441 lines
22 KiB
C#

// ============================================================================
// RynthDefective - Appraisal/AppraisalWindow.cs
// The Appraisal window (2026-11-28): it stays when the bot selects another
// item and follows only the player's own clicks. Retail's examine panel, roughly: the picture, the name, what
// it is and its value and burden, the bars (a creature's health, stamina and
// mana; an item's mana), a creature's attributes, then the appraisal's lines
// under small headings, the description, and every property the engine holds
// ("All properties", folded).
//
// It shows what YOU picked (RynthDefectivePlugin.Appraisal.cs): a middle-click
// on something in the world (your selection doesn't change), or something you
// select yourself with a click. The bot's selections never change it.
//
// the title row drag to move; Ctrl+wheel: bigger or smaller (30-250%);
// right-click: the menu (copy, appraise again, select it,
// pin, all properties, hide).
// the pin pinned: your own clicks don't change it, only a middle-click.
// the square pop it out of the game window into its own window (another
// screen), or dock it back (2026-11-28); /rdef appraisal popout | dock.
// the arrows appraise it again (ask the server).
//
// AC's render thread; reads the view the tick built (AppraisalView).
// ============================================================================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using ImGuiNET;
using RynthCore.Plugin.Shared;
using RynthCore.Plugin.Shared.RynthUi;
using RynthCore.PluginSdk;
using static RynthCore.Plugin.Shared.RynthUi.RynthUi;
namespace RynthCore.Plugin.RynthDefective.Appraisal;
/// <summary>What the window draws (built on the plugin tick).</summary>
internal sealed record AppraisalView(bool Ready, AppraisalDoc? Doc, string Source, bool Pinned, bool Gone)
{
public static readonly AppraisalView NotReady = new(false, null, string.Empty, false, false);
}
internal sealed class AppraisalWindow
{
public const string Title = "Appraisal";
private readonly RynthCoreHost _host;
private float _k = 1f, _r = 1f;
private bool _rawOpen, _popped;
private float _bodyContentH;
/// <summary>The menu's and chips' actions (the plugin runs them on its tick).</summary>
public Action? Refresh { get; set; }
public Action? TogglePin { get; set; }
public Action<uint>? Select { get; set; }
public AppraisalWindow(RynthCoreHost host) { _host = host; }
private float Px(float v) => v * _k;
private float Fs(ImFontPtr f) => f.FontSize * _r;
private void Scale()
{
float fontK = Math.Max(0.5f, Font(RynthUiFont.Ui10).FontSize / 10f);
int pct = _host.PanelScale(Title);
_k = pct > 0 ? pct / 100f : fontK;
_r = _k / fontK;
}
private const float Pad = 7, Icon = 40, Gap = 7, Radius = 4, ChipSize = 16, BarH = 9, MaxBodyH = 430;
private uint _bg, _key, _inset, _line, _text, _dim, _faint, _accent, _good, _bad, _gold;
private static readonly uint HealthColor = Argb(0xFFE0564E), StaminaColor = Argb(0xFFE0B54E), ManaColor = Argb(0xFF4F8DF0),
MagicColor = Argb(0xFF9DB8F5), White = Argb(0xFFFFFFFF);
private static uint A(uint c, float alpha) => WithAlpha(c, (uint)Math.Clamp(MathF.Round(alpha * 255f), 0f, 255f));
/// <summary>Draws into the host's window (<paramref name="popped"/>: its own OS window); returns the height it needs (pixels). Render thread.</summary>
public float Draw(AppraisalView v, bool popped = false)
{
_popped = popped;
Scale();
_bg = Color(RynthUiColor.Background);
_key = Color(RynthUiColor.KeyFace);
_inset = Color(RynthUiColor.Inset);
_line = Color(RynthUiColor.Hairline);
_text = Color(RynthUiColor.Text);
_dim = Color(RynthUiColor.Muted);
_faint = Color(RynthUiColor.Faint);
_accent = Color(RynthUiColor.Accent);
_good = Color(RynthUiColor.Green);
_bad = Color(RynthUiColor.Red);
_gold = Color(RynthUiColor.Highlight);
Vector2 origin = ImGui.GetCursorScreenPos();
origin = new Vector2(MathF.Round(origin.X), MathF.Round(origin.Y));
float width = MathF.Floor(ImGui.GetContentRegionAvail().X);
if (width < 80) return Px(40);
ImDrawListPtr dl = ImGui.GetWindowDrawList();
AppraisalDoc? doc = v.Ready ? v.Doc : null;
float pad = Px(Pad), icon = Px(Icon);
ImFontPtr nf = Font(RynthUiFont.NarrowBold13), kf = Font(RynthUiFont.Narrow10), sf = Font(RynthUiFont.Narrow9);
// ── The header: picture, name, what it is, the facts ──
float headH = MathF.Ceiling(2 * pad + icon);
float barsH = doc == null ? 0 : doc.Bars.Count * (Px(BarH) + Px(4));
float attrH = doc == null || doc.Attributes.Count == 0 ? 0 : 2 * (Fs(kf) + Px(3)) + Px(4);
float topH = headH + barsH + attrH;
float footH = Fs(sf) + Px(6);
float bodyWant = doc == null ? 0 : _bodyContentH;
float bodyH = MathF.Min(bodyWant, Px(MaxBodyH));
float total = topH + (bodyH > 0 ? bodyH + Px(2) : 0) + footH;
Vector2 max = origin + new Vector2(width, total);
dl.AddRectFilled(origin, max, A(_bg, 0.95f), Px(Radius));
dl.AddRect(origin, max, _line, Px(Radius));
float chipsW = 3 * Px(ChipSize) + 2 * Px(4);
ImGui.SetCursorScreenPos(origin);
ImGui.InvisibleButton("##appr_drag", new Vector2(Math.Max(1, width - chipsW - pad), headH));
_host.PanelDragWithLastItem();
if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) ImGui.OpenPopup(MenuId);
SafeImGui.SetItemTooltip(doc == null
? "Middle-click anything in the world to appraise it here (your selection doesn't change), or select something yourself.\nThe bot's selections never change this window.\nDrag to move; Ctrl+wheel: bigger or smaller; right-click: the menu."
: $"{doc.Name}\nMiddle-click something else to appraise it; {(v.Pinned ? "pinned: your own clicks don't change it" : "your own clicks change it too")}.\nDrag to move; Ctrl+wheel: bigger or smaller; right-click: the menu.");
Menu(v);
Vector2 ip = origin + new Vector2(pad, pad);
dl.AddRectFilled(ip, ip + new Vector2(icon, icon), doc == null ? _inset : _key, Px(3));
IntPtr tex = doc != null ? _host.GetIconTexture(RynthIconKind.Object, doc.Id) : IntPtr.Zero;
if (tex != IntPtr.Zero)
dl.AddImageRounded(tex, ip + new Vector2(Px(3), Px(3)), ip + new Vector2(icon - Px(3), icon - Px(3)), Vector2.Zero, Vector2.One, White, Px(2));
else if (doc == null)
DrawIcon(dl, RynthIcons.MagnifyingGlass, ip, new Vector2(icon, icon), Px(18), _faint);
dl.AddRect(ip, ip + new Vector2(icon, icon), _line, Px(3));
float tx = ip.X + icon + Px(Gap);
float right = origin.X + width - pad;
// The chips: pin, pop out / dock, appraise again.
Vector2 chip = new(right - Px(ChipSize), ip.Y);
if (Chip("##appr_refresh", RynthIcons.ArrowsClockwise, chip, _faint, doc == null ? "Nothing to appraise yet." : "Appraise it again (ask the server).") && doc != null)
Refresh?.Invoke();
chip.X -= Px(ChipSize) + Px(4);
if (Chip("##appr_popout", popped ? RynthIcons.ArrowSquareIn : RynthIcons.ArrowSquareOut, chip, _faint,
popped ? "Dock it back into the game window (/rdef appraisal dock)."
: "Pop it out of the game window into its own window: put it on another screen (/rdef appraisal popout)."))
_host.SetPanelPopOut(Title, RynthPanelPopOut.Toggle);
chip.X -= Px(ChipSize) + Px(4);
if (Chip("##appr_pin", RynthIcons.PushPin, chip, v.Pinned ? _accent : _faint,
v.Pinned ? "Pinned: only a middle-click changes this window. Click: follow your own clicks too."
: "Follows your own clicks and middle-clicks. Click: pin it (only a middle-click changes it).", v.Pinned))
TogglePin?.Invoke();
float textRight = chip.X - Px(6);
if (doc == null)
{
Clip(dl, nf, Fs(nf), new Vector2(tx, ip.Y), _faint, "Appraisal", textRight - tx);
Clip(dl, kf, Fs(kf), new Vector2(tx, ip.Y + Fs(nf) + Px(2)), _faint, "Middle-click something in the world", right - tx);
Clip(dl, sf, Fs(sf), new Vector2(tx, ip.Y + icon - Fs(sf)), _faint, "or select it yourself: the bot's picks don't count", right - tx);
ImGui.SetCursorScreenPos(origin);
ImGui.Dummy(new Vector2(width, total));
return total;
}
Clip(dl, nf, Fs(nf), new Vector2(tx, ip.Y), v.Gone ? _faint : _text, doc.Name, textRight - tx);
float y2 = ip.Y + Fs(nf) + Px(2);
if (doc.Kind.Length > 0) Clip(dl, kf, Fs(kf), new Vector2(tx, y2), _accent, doc.Kind, right - tx);
string facts = Facts(doc);
if (facts.Length > 0) Clip(dl, sf, Fs(sf), new Vector2(tx, ip.Y + icon - Fs(sf)), _dim, facts, right - tx);
// ── The bars ──
float y = origin.Y + headH;
foreach (AppraiseBar b in doc.Bars)
{
float lw = Px(48);
Clip(dl, sf, Fs(sf), new Vector2(origin.X + pad, y + (Px(BarH) - Fs(sf)) * 0.5f), _dim, b.Label, lw);
Vector2 b0 = new(origin.X + pad + lw, y), b1 = new(right, y + Px(BarH));
dl.AddRectFilled(b0, b1, _inset, Px(2));
float f = Math.Clamp(b.Ratio, 0f, 1f);
uint fill = b.Kind switch { AppraiseBarKind.Health => HealthColor, AppraiseBarKind.Stamina => StaminaColor, _ => ManaColor };
if (f > 0f) dl.AddRectFilled(b0, new Vector2(b0.X + (b1.X - b0.X) * f, b1.Y), fill, Px(2));
dl.AddRect(b0, b1, _line, Px(2));
string label = (b.Max > 0 ? $"{b.Current.ToString("N0", CultureInfo.InvariantCulture)} / {b.Max.ToString("N0", CultureInfo.InvariantCulture)} " : "")
+ MathF.Round(f * 100f).ToString(CultureInfo.InvariantCulture) + "%";
float w = sf.CalcTextSizeA(Fs(sf), float.MaxValue, 0f, label).X;
if (w + Px(8) < b1.X - b0.X)
dl.AddText(sf, Fs(sf), new Vector2(MathF.Round(b1.X - w - Px(4)), MathF.Round(y + (Px(BarH) - Fs(sf)) * 0.5f)), White, label);
y += Px(BarH) + Px(4);
}
// ── A creature's attributes: three columns, two rows ──
if (doc.Attributes.Count > 0)
{
float colW = (right - origin.X - pad) / 3f;
for (int i = 0; i < doc.Attributes.Count; i++)
{
var (label, value, buffed, up) = doc.Attributes[i];
float cx = origin.X + pad + (i % 3) * colW, cy = y + (i / 3) * (Fs(kf) + Px(3));
dl.AddText(kf, Fs(kf), new Vector2(cx, cy), _dim, Short(label));
string num = value.ToString(CultureInfo.InvariantCulture);
uint c = !buffed ? _text : up ? _good : _bad;
dl.AddText(kf, Fs(kf), new Vector2(cx + Px(34), cy), c, num);
}
y += attrH;
}
// ── The body: sections, description, every property (scrolls past MaxBodyH) ──
if (bodyWant <= 0) bodyH = Px(1);
ImGui.SetCursorScreenPos(new Vector2(origin.X + Px(1), y));
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, Vector2.Zero);
ImGui.PushStyleColor(ImGuiCol.ScrollbarBg, 0u);
bool child = ImGui.BeginChild("##appr_body", new Vector2(width - Px(2), Math.Max(Px(1), bodyH)), ImGuiChildFlags.None,
ImGuiWindowFlags.NoBackground | (bodyWant > Px(MaxBodyH) ? ImGuiWindowFlags.None : ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse));
ImGui.PopStyleColor();
ImGui.PopStyleVar();
if (child)
{
try { _bodyContentH = Body(doc, width - Px(2) - (bodyWant > Px(MaxBodyH) ? ImGui.GetStyle().ScrollbarSize : 0)); }
finally { ImGui.EndChild(); }
}
else ImGui.EndChild();
y += bodyH > Px(1) ? bodyH + Px(2) : 0;
// ── The footer: how it was picked, and whether it is appraised ──
string foot = v.Source + (v.Gone ? " · no longer in view" : doc.Identified ? "" : " · not appraised yet");
Clip(dl, sf, Fs(sf), new Vector2(origin.X + pad, origin.Y + total - footH + Px(2)), _faint, foot, width - 2 * pad);
ImGui.SetCursorScreenPos(origin);
ImGui.Dummy(new Vector2(width, total));
return total;
}
/// <summary>The body's lines in the child window; returns their height. Render thread.</summary>
private float Body(AppraisalDoc doc, float width)
{
ImDrawListPtr dl = ImGui.GetWindowDrawList();
Vector2 o = ImGui.GetCursorScreenPos();
float pad = Px(Pad), x = o.X + pad - Px(1), wrapW = width - 2 * pad;
float y = o.Y;
ImFontPtr hf = Font(RynthUiFont.Narrow9), lf = Font(RynthUiFont.Ui10), mf = Font(RynthUiFont.Dash9);
foreach (AppraiseSection s in doc.Sections)
{
if (s.Title.Length > 0)
{
y += Px(3);
string t = s.Title.ToUpperInvariant();
dl.AddText(hf, Fs(hf), new Vector2(x, y), _faint, t);
float tw = hf.CalcTextSizeA(Fs(hf), float.MaxValue, 0f, t).X;
float ly = MathF.Round(y + Fs(hf) * 0.55f);
dl.AddLine(new Vector2(x + tw + Px(6), ly), new Vector2(x + wrapW, ly), _line);
y += Fs(hf) + Px(2);
}
foreach (AppraiseLine l in s.Lines)
y = Wrapped(dl, lf, new Vector2(x, y), wrapW, l.Text, Tone(l.Tone)) + Px(1);
}
if (doc.Description.Length > 0)
{
y += Px(4);
y = Wrapped(dl, lf, new Vector2(x, y), wrapW, doc.Description, _dim) + Px(2);
}
// Every property, folded.
if (doc.Raw.Count > 0)
{
y += Px(4);
string head = (_rawOpen ? "v " : "> ") + "All properties (" + doc.Raw.Count.ToString(CultureInfo.InvariantCulture) + ")";
ImGui.SetCursorScreenPos(new Vector2(x, y));
if (ImGui.InvisibleButton("##appr_raw", new Vector2(wrapW, Fs(hf) + Px(2)))) _rawOpen = !_rawOpen;
bool hot = ImGui.IsItemHovered();
SafeImGui.SetItemTooltip("Every property the engine holds for it, by ACE's names (what /ub propertydump lists).");
dl.AddText(hf, Fs(hf), new Vector2(x, y), hot ? _text : _faint, head);
y += Fs(hf) + Px(3);
if (_rawOpen)
{
float kindW = mf.CalcTextSizeA(Fs(mf), float.MaxValue, 0f, "InstanceId 9999 ").X;
foreach (AppraiseRaw r in doc.Raw)
{
string k = r.Kind + " " + r.Key.ToString(CultureInfo.InvariantCulture);
dl.AddText(mf, Fs(mf), new Vector2(x, y), _faint, k);
float nx = x + kindW;
string nv = r.Name + " = " + r.Value;
y = Wrapped(dl, mf, new Vector2(nx, y), wrapW - kindW, nv, r.Name == "?" ? _faint : _dim) + Px(1);
}
}
}
y += Px(4);
float h = y - o.Y;
ImGui.SetCursorScreenPos(o);
ImGui.Dummy(new Vector2(Math.Max(1, width), Math.Max(1, h)));
return h;
}
private uint Tone(AppraiseTone t) => t switch
{
AppraiseTone.Dim => _dim,
AppraiseTone.Good => _good,
AppraiseTone.Bad => _bad,
AppraiseTone.Magic => MagicColor,
AppraiseTone.Gold => _gold,
_ => _text,
};
private static string Facts(AppraisalDoc doc)
{
var parts = new List<string>(doc.Facts.Count);
foreach (var (label, value) in doc.Facts) parts.Add(label + " " + value);
return string.Join(" · ", parts);
}
private static string Short(string attribute) => attribute switch
{
"Strength" => "Str", "Endurance" => "End", "Quickness" => "Quick", "Coordination" => "Coord", "Focus" => "Focus", "Self" => "Self", _ => attribute,
};
// ── The menu ─────────────────────────────────────────────────────────────
private const string MenuId = "##appr_menu";
private void Menu(AppraisalView v)
{
if (!ImGui.IsPopupOpen(MenuId)) return;
ImGui.PushStyleColor(ImGuiCol.PopupBg, A(_bg, 0.98f));
ImGui.PushStyleColor(ImGuiCol.Border, _line);
ImGui.PushStyleVar(ImGuiStyleVar.PopupRounding, Px(4));
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(Px(6), Px(5)));
bool open = ImGui.BeginPopup(MenuId);
ImGui.PopStyleVar(2);
ImGui.PopStyleColor(2);
if (!open) return;
try
{
AppraisalDoc? doc = v.Ready ? v.Doc : null;
if (doc != null) SafeImGui.TextDisabled(doc.Name);
else ImGui.TextDisabled("Nothing appraised yet");
ImGui.Separator();
if (ImGui.MenuItem("Copy as text", null, false, doc != null) && doc != null) ImGui.SetClipboardText(doc.ToText());
SafeImGui.SetItemTooltip("The whole appraisal, to paste in chat or a note.");
if (ImGui.MenuItem("Appraise again", null, false, doc != null && !v.Gone)) Refresh?.Invoke();
if (ImGui.MenuItem("Select it", null, false, doc != null && !v.Gone) && doc != null) Select?.Invoke(doc.Id);
SafeImGui.SetItemTooltip("Make it the game's selection (as a click on it does).");
if (ImGui.MenuItem("Pinned (only a middle-click changes it)", null, v.Pinned)) TogglePin?.Invoke();
if (ImGui.MenuItem("Show every property", null, _rawOpen, doc != null)) _rawOpen = !_rawOpen;
if (ImGui.MenuItem(_popped ? "Dock back into the game window" : "Pop out of the game window"))
_host.SetPanelPopOut(Title, RynthPanelPopOut.Toggle);
SafeImGui.SetItemTooltip(_popped ? "Back inside the game window, where it was." : "Its own window, which you can put on another screen (/rdef appraisal popout).");
ImGui.Separator();
if (ImGui.MenuItem("Hide the Appraisal window")) _host.SetPanelOpen(Title, RynthPanelOpen.Close);
SafeImGui.SetItemTooltip("/rdef appraisal shows it again; a middle-click on something does too.");
}
finally { ImGui.EndPopup(); }
}
// ── Drawing helpers ──────────────────────────────────────────────────────
private bool Chip(string id, string icon, Vector2 pos, uint color, string tip, bool held = false)
{
Vector2 size = new(Px(ChipSize), Px(ChipSize));
ImGui.SetCursorScreenPos(pos);
bool clicked = ImGui.InvisibleButton(id, size);
bool hot = ImGui.IsItemHovered();
SafeImGui.SetItemTooltip(tip);
ImDrawListPtr dl = ImGui.GetWindowDrawList();
if (hot || held) dl.AddRectFilled(pos, pos + size, _key, Px(3));
DrawIcon(dl, icon, pos, size, Px(11), hot ? _text : color);
return clicked;
}
/// <summary>Word-wrapped text from <paramref name="pos"/> within <paramref name="width"/>; returns the y under it.</summary>
private float Wrapped(ImDrawListPtr dl, ImFontPtr f, Vector2 pos, float width, string text, uint color)
{
float size = Fs(f);
float y = pos.Y;
foreach (string line in WrapLines(f, size, text, Math.Max(Px(20), width)))
{
dl.AddText(f, size, new Vector2(MathF.Round(pos.X), MathF.Round(y)), color, line);
y += size + Px(1);
}
return y;
}
private static IEnumerable<string> WrapLines(ImFontPtr f, float size, string text, float width)
{
foreach (string para in text.Replace("\r", "").Split('\n'))
{
if (para.Length == 0) { yield return ""; continue; }
int start = 0;
while (start < para.Length)
{
int end = para.Length;
if (f.CalcTextSizeA(size, float.MaxValue, 0f, para[start..end]).X > width)
{
// The longest run of whole words that fits, else as many characters as fit.
int lastSpace = -1, i = start + 1;
for (; i <= para.Length; i++)
{
if (f.CalcTextSizeA(size, float.MaxValue, 0f, para[start..i]).X > width) break;
if (i < para.Length && para[i] == ' ') lastSpace = i;
}
end = lastSpace > start ? lastSpace : Math.Max(start + 1, i - 1);
}
yield return para[start..end].TrimEnd();
start = end;
while (start < para.Length && para[start] == ' ') start++;
}
}
}
private static void Clip(ImDrawListPtr dl, ImFontPtr f, float size, Vector2 pos, uint color, string text, float room)
{
if (text.Length == 0 || room <= 4) return;
float w = f.CalcTextSizeA(size, float.MaxValue, 0f, text).X;
if (w > room)
{
int n = text.Length;
while (n > 1 && f.CalcTextSizeA(size, float.MaxValue, 0f, text[..n] + "...").X > room) n--;
text = text[..n] + "...";
}
dl.AddText(f, size, new Vector2(MathF.Round(pos.X), MathF.Round(pos.Y)), color, text);
}
private static unsafe void DrawIcon(ImDrawListPtr dl, string glyph, Vector2 min, Vector2 size, float px, uint color)
{
if (string.IsNullOrEmpty(glyph)) return;
ImFontPtr font = Font(RynthUiFont.Ui18);
if (font.NativePtr == null) return;
byte* g = (byte*)ImGuiNative.ImFont_FindGlyphNoFallback(font.NativePtr, glyph[0]);
if (g == null) return;
float advance = *(float*)(g + 4);
if (advance <= 0) return;
float* box = (float*)(g + 8);
float scale = px / advance;
Vector2 centre = new((box[0] + box[2]) * 0.5f * scale, (box[1] + box[3]) * 0.5f * scale);
Vector2 at = min + size * 0.5f - centre;
dl.AddText(font, font.FontSize * scale, new Vector2(MathF.Round(at.X), MathF.Round(at.Y)), color, glyph);
}
}