Snapshot for release 2026.10.23.1 (internal 7581cff)
This commit is contained in:
parent
893973d0dd
commit
92f28bd243
73 changed files with 12203 additions and 308 deletions
|
|
@ -0,0 +1,148 @@
|
|||
// ============================================================================
|
||||
// RynthDefective - ShortcutBar/BarSnapshot.cs
|
||||
// What the shortcut bar shows, built on the plugin tick (BarSnapshotBuilder)
|
||||
// and drawn on the render thread (ShortcutBarWindow): one immutable record per
|
||||
// slot, swapped whole.
|
||||
//
|
||||
// Slots 1-18 are AC's own shortcut slots (RynthCoreHost.GetShortcuts, plugin
|
||||
// API v90): the items the retail toolbar holds, the keys AC's keymap gives
|
||||
// UseQuickSlot_1-18. Slots 19 and on are this plugin's own (DefData.Slots),
|
||||
// their keys the engine's hotkeys (/rdef use N). The player chooses how many
|
||||
// the bar shows (DefSettings.BarSlots).
|
||||
// ============================================================================
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using RynthCore.Plugin.RynthDefective.Items;
|
||||
using RynthCore.Plugin.RynthDefective.Model;
|
||||
using RynthCore.Plugin.RynthDefective.Views;
|
||||
using RynthCore.PluginSdk;
|
||||
|
||||
namespace RynthCore.Plugin.RynthDefective.ShortcutBar;
|
||||
|
||||
/// <summary>How a slot's count line reads: plain, in hand / worn, or nothing carried.</summary>
|
||||
internal enum CountTone { Plain, InHand, Missing }
|
||||
|
||||
/// <summary>One slot as drawn.</summary>
|
||||
internal sealed record BarSlot(
|
||||
int Number,
|
||||
bool Retail,
|
||||
uint ObjectId,
|
||||
uint SpellId,
|
||||
string Name,
|
||||
string Count,
|
||||
CountTone Tone,
|
||||
string Key,
|
||||
string AllKeys,
|
||||
string Tooltip)
|
||||
{
|
||||
public bool Empty => ObjectId == 0 && SpellId == 0 && Name.Length == 0;
|
||||
}
|
||||
|
||||
internal sealed record BarSnapshot(BarSlot[] Slots, bool Ready, int IconSize)
|
||||
{
|
||||
public static readonly BarSnapshot NotReady = new(Array.Empty<BarSlot>(), false, 36);
|
||||
}
|
||||
|
||||
/// <summary>Builds the snapshot on the plugin tick (reads the host and the carried items).</summary>
|
||||
internal sealed class BarSnapshotBuilder
|
||||
{
|
||||
private readonly RynthCoreHost _host;
|
||||
private readonly uint[] _ids = new uint[DefSettings.RetailSlots], _spells = new uint[DefSettings.RetailSlots];
|
||||
private readonly string[] _keys = new string[DefSettings.RetailSlots];
|
||||
private long _keysReadMs = long.MinValue / 2, _fastKeysUntilMs;
|
||||
|
||||
public BarSnapshotBuilder(RynthCoreHost host)
|
||||
{
|
||||
_host = host;
|
||||
Array.Fill(_keys, string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>A key was changed from the bar: ask again often for a few seconds (the change is made on the game's tick).</summary>
|
||||
public void InvalidateKeys()
|
||||
{
|
||||
_keysReadMs = long.MinValue / 2;
|
||||
_fastKeysUntilMs = Environment.TickCount64 + 4000;
|
||||
}
|
||||
|
||||
/// <summary>AC's keys for the 18 retail slots (re-asked every 2 s: the keymap can change, from any client).</summary>
|
||||
private void RefreshKeys(long now)
|
||||
{
|
||||
if (now - _keysReadMs < (now < _fastKeysUntilMs ? 250 : 2000)) return;
|
||||
_keysReadMs = now;
|
||||
for (int i = 0; i < _keys.Length; i++) _keys[i] = _host.GetActionKeys("UseQuickSlot_" + (i + 1));
|
||||
}
|
||||
|
||||
public BarSnapshot Build(DefSession session, IReadOnlyList<CarriedItem> items, long now)
|
||||
{
|
||||
if (!_host.HasShortcuts || _host.GetShortcuts(_ids, _spells) == 0) return BarSnapshot.NotReady;
|
||||
RefreshKeys(now);
|
||||
DefSettings st = session.Settings;
|
||||
var byId = new Dictionary<uint, CarriedItem>(items.Count);
|
||||
foreach (CarriedItem it in items) byId[it.Id] = it;
|
||||
|
||||
var slots = new BarSlot[st.BarSlots];
|
||||
for (int n = 1; n <= st.BarSlots; n++)
|
||||
slots[n - 1] = n <= DefSettings.RetailSlots ? Retail(n, byId) : Own(n, session, items, _host);
|
||||
return new BarSnapshot(slots, true, st.ButtonSize);
|
||||
}
|
||||
|
||||
private BarSlot Retail(int n, Dictionary<uint, CarriedItem> byId)
|
||||
{
|
||||
uint id = _ids[n - 1], spell = _spells[n - 1];
|
||||
string allKeys = _keys[n - 1];
|
||||
string key = FirstKey(allKeys);
|
||||
if (id == 0 && spell == 0)
|
||||
return new BarSlot(n, true, 0, 0, string.Empty, string.Empty, CountTone.Plain, key, allKeys,
|
||||
$"Slot {n} (the game's own): empty.{KeysLine(allKeys)}\nRight-click: put the selected item here, or change its keys.");
|
||||
string name;
|
||||
string count = string.Empty;
|
||||
CountTone tone = CountTone.Plain;
|
||||
if (id != 0 && byId.TryGetValue(id, out CarriedItem? it))
|
||||
{
|
||||
name = it.FullName;
|
||||
count = SlotText.Count(new SlotStatus(it, it.Stack, it.Uses, false), out uint color);
|
||||
tone = color == SlotText.InHand ? CountTone.InHand : color == SlotText.Missing ? CountTone.Missing : CountTone.Plain;
|
||||
}
|
||||
else if (id != 0)
|
||||
{
|
||||
name = _host.TryGetObjectName(id, out string n2) && n2.Length > 0 ? n2 : $"0x{id:X8}";
|
||||
count = "none";
|
||||
tone = CountTone.Missing;
|
||||
}
|
||||
else name = $"Spell {spell}";
|
||||
string tip = $"Slot {n} (the game's own): {name}{(count.Length > 0 ? " - " + count : "")}.{KeysLine(allKeys)}\n" +
|
||||
"Click: use it, as its key does. Right-click: change the slot or its keys.";
|
||||
return new BarSlot(n, true, id, id == 0 ? spell : 0, name, count, tone, key, allKeys, tip);
|
||||
}
|
||||
|
||||
private static BarSlot Own(int n, DefSession session, IReadOnlyList<CarriedItem> items, RynthCoreHost host)
|
||||
{
|
||||
int own = n - DefSettings.RetailSlots;
|
||||
DefSlot slot = own <= session.Data.Slots.Count ? session.Data.Slots[own - 1] : new DefSlot();
|
||||
// Every key of "/rdef use N" (the engine's hotkeys), else the one bound to the item by name.
|
||||
string all = host.HasKeyBinding ? host.GetActionKeys(DefCommandParser.UseCommand(own)) : string.Empty;
|
||||
string key = all.Length > 0 ? FirstKey(all) : session.KeyOfSlot(own) ?? string.Empty;
|
||||
if (all.Length == 0) all = key;
|
||||
if (slot.Item == null)
|
||||
return new BarSlot(n, false, 0, 0, string.Empty, string.Empty, CountTone.Plain, SlotText.ShortKey(key), all,
|
||||
$"Slot {n} (RynthCore's: the game holds 18): empty.{KeysLine(all)}\nRight-click: put the selected item here, or change its keys.");
|
||||
SlotStatus s = ItemMatcher.Status(slot.Item, items);
|
||||
string count = SlotText.Count(s, out uint color);
|
||||
CountTone tone = color == SlotText.InHand ? CountTone.InHand : color == SlotText.Missing ? CountTone.Missing : CountTone.Plain;
|
||||
string tip = SlotText.Tooltip(n, slot.Item, s, slot.Mode, key.Length > 0 ? key : null)
|
||||
+ "\n(RynthCore's slot: the game's toolbar holds 18.)";
|
||||
return new BarSlot(n, false, s.Item?.Id ?? 0, 0, s.Item?.FullName ?? slot.Item.FullName, count, tone,
|
||||
SlotText.ShortKey(key), all, tip);
|
||||
}
|
||||
|
||||
/// <summary>The first of "1, Ctrl+1": what fits on the slot.</summary>
|
||||
private static string FirstKey(string keys)
|
||||
{
|
||||
int comma = keys.IndexOf(',');
|
||||
return comma < 0 ? keys : keys[..comma];
|
||||
}
|
||||
|
||||
private static string KeysLine(string keys) => keys.Length > 0 ? "\nKeys: " + keys : "\nNo key yet.";
|
||||
}
|
||||
|
|
@ -0,0 +1,155 @@
|
|||
// ============================================================================
|
||||
// RynthDefective - ShortcutBar/PanelButtonsWindow.cs
|
||||
// Tiny buttons for AC's own panels, in the bottom-right corner (Tom
|
||||
// 2026-10-22: with the retail toolbar hidden, its panel buttons go with it).
|
||||
// RynthCore hosts the frameless window (sections manifest "corner":
|
||||
// "bottomRight", plugin API v90) and opens and closes the panels the way AC's
|
||||
// toolbar and keys do (RynthCoreHost.AcPanel): one main panel at a time.
|
||||
//
|
||||
// grip the dots on the left: drag the strip; Ctrl+wheel: bigger or
|
||||
// smaller; right-click: hide it (/rdef panels shows it again).
|
||||
// buttons the pack, attributes & skills, the spellbook, fellowship &
|
||||
// allegiance, the map, quests, spells on you, character info,
|
||||
// options. Lit while their panel is open. They fill the window's
|
||||
// width and wrap: drag it narrow for a column.
|
||||
//
|
||||
// AC's render thread; the panel calls are queued by the engine.
|
||||
// ============================================================================
|
||||
|
||||
using System;
|
||||
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.ShortcutBar;
|
||||
|
||||
internal sealed class PanelButtonsWindow
|
||||
{
|
||||
public const string Title = "Game Panels";
|
||||
|
||||
private readonly RynthCoreHost _host;
|
||||
private float _k = 1f, _r = 1f;
|
||||
|
||||
public PanelButtonsWindow(RynthCoreHost host) => _host = host;
|
||||
|
||||
private readonly record struct Button(RynthAcPanel Panel, string Icon, string Tip);
|
||||
|
||||
private static readonly Button[] Buttons =
|
||||
[
|
||||
new(RynthAcPanel.Inventory, RynthIcons.Backpack, "Pack"),
|
||||
new(RynthAcPanel.Character, RynthIcons.User, "Attributes and skills"),
|
||||
new(RynthAcPanel.Spellbook, RynthIcons.MagicWand, "Spellbook"),
|
||||
new(RynthAcPanel.Social, RynthIcons.UsersThree, "Fellowship, allegiance and friends"),
|
||||
new(RynthAcPanel.Map, RynthIcons.MapTrifold, "Map"),
|
||||
new(RynthAcPanel.Quests, RynthIcons.Scroll, "Contracts and journal"),
|
||||
new(RynthAcPanel.HelpfulSpells, RynthIcons.Sparkle, "Spells on you"),
|
||||
new(RynthAcPanel.CharacterInfo, RynthIcons.Info, "Character information"),
|
||||
new(RynthAcPanel.Options, RynthIcons.Gear, "Options"),
|
||||
];
|
||||
|
||||
private const float Pad = 4, Cell = 22, Gap = 2, GripW = 9, Radius = 4;
|
||||
|
||||
private float Px(float v) => v * _k;
|
||||
|
||||
/// <summary>
|
||||
/// The layout's scale and the text's against the baked font: the window's own size (Settings > UI >
|
||||
/// Toolbar, Ctrl+wheel: 30-250%, PanelScale) or, unset, the text size the engine baked for it.
|
||||
/// </summary>
|
||||
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 uint _bg, _key, _line, _text, _faint, _accent;
|
||||
private static uint A(uint c, float alpha) => WithAlpha(c, (uint)Math.Clamp(MathF.Round(alpha * 255f), 0f, 255f));
|
||||
|
||||
/// <summary>Draws the strip into the host's window; returns the height it needs (pixels). Render thread.</summary>
|
||||
public float Draw()
|
||||
{
|
||||
Scale();
|
||||
_bg = Color(RynthUiColor.Background);
|
||||
_key = Color(RynthUiColor.KeyFace);
|
||||
_line = Color(RynthUiColor.Hairline);
|
||||
_text = Color(RynthUiColor.Text);
|
||||
_faint = Color(RynthUiColor.Faint);
|
||||
_accent = Color(RynthUiColor.Accent);
|
||||
|
||||
Vector2 origin = ImGui.GetCursorScreenPos();
|
||||
origin = new Vector2(MathF.Round(origin.X), MathF.Round(origin.Y));
|
||||
float width = MathF.Floor(ImGui.GetContentRegionAvail().X);
|
||||
if (width < 20) return Px(Cell + 2 * Pad);
|
||||
ImDrawListPtr dl = ImGui.GetWindowDrawList();
|
||||
|
||||
float cell = Px(Cell), gap = Px(Gap), pad = Px(Pad), grip = Px(GripW);
|
||||
float room = width - 2 * pad - grip - gap;
|
||||
int perRow = Math.Max(1, (int)MathF.Floor((room + gap) / (cell + gap)));
|
||||
int rows = (Buttons.Length + perRow - 1) / perRow;
|
||||
float height = MathF.Ceiling(2 * pad + rows * cell + (rows - 1) * gap);
|
||||
|
||||
// The frame, as the shortcut bar's.
|
||||
Vector2 max = origin + new Vector2(width, height);
|
||||
dl.AddRectFilled(origin, max, A(_bg, 0.94f), Px(Radius));
|
||||
dl.AddRect(origin, max, _line, Px(Radius));
|
||||
|
||||
// The grip: drag, Ctrl+wheel, right-click to hide.
|
||||
Vector2 gp = origin + new Vector2(pad, pad);
|
||||
ImGui.SetCursorScreenPos(gp);
|
||||
ImGui.InvisibleButton("##gp_grip", new Vector2(grip, height - 2 * pad));
|
||||
_host.PanelDragWithLastItem();
|
||||
bool gripHot = ImGui.IsItemHovered();
|
||||
if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) _host.SetPanelOpen(Title, RynthPanelOpen.Close);
|
||||
SafeImGui.SetItemTooltip("Drag to move. Ctrl+wheel: bigger or smaller. Right-click: hide (/rdef panels shows it again).\nDrag the window's edge narrower for a column.");
|
||||
DrawIcon(dl, RynthIcons.DotsSixVertical, gp, new Vector2(grip, height - 2 * pad), Px(11), gripHot ? _text : _faint);
|
||||
|
||||
float x0 = gp.X + grip + gap;
|
||||
for (int i = 0; i < Buttons.Length; i++)
|
||||
{
|
||||
int col = i % perRow, row = i / perRow;
|
||||
Vector2 p = new(MathF.Round(x0 + col * (cell + gap)), MathF.Round(origin.Y + pad + row * (cell + gap)));
|
||||
DrawButton(dl, Buttons[i], p, cell);
|
||||
}
|
||||
|
||||
ImGui.SetCursorScreenPos(origin);
|
||||
ImGui.Dummy(new Vector2(width, height));
|
||||
return height;
|
||||
}
|
||||
|
||||
private void DrawButton(ImDrawListPtr dl, in Button b, Vector2 p, float cell)
|
||||
{
|
||||
Vector2 size = new(cell, cell);
|
||||
ImGui.SetCursorScreenPos(p);
|
||||
bool clicked = ImGui.InvisibleButton("##gp_" + (int)b.Panel, size);
|
||||
bool hot = ImGui.IsItemHovered();
|
||||
bool? open = _host.IsAcPanelOpen(b.Panel);
|
||||
SafeImGui.SetItemTooltip(open == null ? b.Tip + " (not in this client)" : b.Tip + (open == true ? " - open: click to close" : ""));
|
||||
float r = Px(3);
|
||||
if (hot || open == true) dl.AddRectFilled(p, p + size, _key, r);
|
||||
if (open == true) dl.AddLine(new Vector2(p.X + Px(4), p.Y + cell - Px(1.5f)), new Vector2(p.X + cell - Px(4), p.Y + cell - Px(1.5f)), _accent, Px(1.5f));
|
||||
uint color = open == null ? A(_faint, 0.5f) : open == true ? _accent : hot ? _text : A(_text, 0.72f);
|
||||
DrawIcon(dl, b.Icon, p, size, Px(14), color);
|
||||
if (clicked && open != null) _host.AcPanel(b.Panel, RynthAcPanelOp.Toggle);
|
||||
}
|
||||
|
||||
/// <summary>A Phosphor glyph centred in the box, <paramref name="px"/> across its em (from Ui18's bake).</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,196 @@
|
|||
// ============================================================================
|
||||
// RynthDefective - ShortcutBar/SelectionWindow.cs
|
||||
// What is selected, as the retail toolbar showed it (Tom 2026-10-22: "people
|
||||
// use it all the time to find things near them: doors, monsters"): its
|
||||
// picture and name, how far and which way it is (an arrow from where you
|
||||
// face, and the compass point), and the health bar of a creature or player.
|
||||
// USE uses it (opens a door, talks to an NPC). AC's own selection keys still
|
||||
// pick what is selected; this only shows it.
|
||||
//
|
||||
// the name row drag to move; Ctrl+wheel: bigger or smaller (30-250%);
|
||||
// right-click: hide it (Settings > UI > Toolbar, /rdef selection).
|
||||
//
|
||||
// AC's render thread; reads the view the tick built (SelectionView).
|
||||
// ============================================================================
|
||||
|
||||
using System;
|
||||
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.ShortcutBar;
|
||||
|
||||
/// <summary>The selected object as drawn (built on the plugin tick).</summary>
|
||||
internal sealed record SelectionView(
|
||||
bool Ready,
|
||||
uint Id,
|
||||
string Name,
|
||||
string Where,
|
||||
float Relative, // degrees from where the player faces (0 ahead, + to the right); NaN = unknown
|
||||
float Health, // 0-1, or -1 when it has none known
|
||||
uint Current,
|
||||
uint Max)
|
||||
{
|
||||
public static readonly SelectionView NotReady = new(false, 0, string.Empty, string.Empty, float.NaN, -1f, 0, 0);
|
||||
}
|
||||
|
||||
internal sealed class SelectionWindow
|
||||
{
|
||||
public const string Title = "Selection";
|
||||
|
||||
private readonly RynthCoreHost _host;
|
||||
private readonly Action<uint> _use;
|
||||
private float _k = 1f, _r = 1f;
|
||||
|
||||
public SelectionWindow(RynthCoreHost host, Action<uint> use)
|
||||
{
|
||||
_host = host;
|
||||
_use = use;
|
||||
}
|
||||
|
||||
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 = 6, Icon = 30, Gap = 6, Radius = 4, BarH = 6, UseW = 34, UseH = 16;
|
||||
|
||||
private uint _bg, _key, _inset, _line, _text, _dim, _faint, _accent;
|
||||
private static readonly uint HealthColor = Argb(0xFFE0564E), HealthHigh = Argb(0xFF73D885), White = Argb(0xFFFFFFFF);
|
||||
private static uint A(uint c, float alpha) => WithAlpha(c, (uint)Math.Clamp(MathF.Round(alpha * 255f), 0f, 255f));
|
||||
|
||||
/// <summary>Draws the selection into the host's window; returns the height it needs (pixels). Render thread.</summary>
|
||||
public float Draw(SelectionView v)
|
||||
{
|
||||
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);
|
||||
|
||||
Vector2 origin = ImGui.GetCursorScreenPos();
|
||||
origin = new Vector2(MathF.Round(origin.X), MathF.Round(origin.Y));
|
||||
float width = MathF.Floor(ImGui.GetContentRegionAvail().X);
|
||||
if (width < 40) return Px(40);
|
||||
ImDrawListPtr dl = ImGui.GetWindowDrawList();
|
||||
|
||||
float pad = Px(Pad), icon = Px(Icon);
|
||||
float height = MathF.Ceiling(2 * pad + icon);
|
||||
Vector2 max = origin + new Vector2(width, height);
|
||||
dl.AddRectFilled(origin, max, A(_bg, 0.94f), Px(Radius));
|
||||
dl.AddRect(origin, max, _line, Px(Radius));
|
||||
|
||||
bool hasUse = v.Ready && v.Id != 0;
|
||||
float useW = hasUse ? Px(UseW) : 0f;
|
||||
|
||||
// The drag surface: everything but USE.
|
||||
ImGui.SetCursorScreenPos(origin);
|
||||
ImGui.InvisibleButton("##sel_drag", new Vector2(Math.Max(1, width - useW - (hasUse ? pad : 0)), height));
|
||||
_host.PanelDragWithLastItem();
|
||||
if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) _host.SetPanelOpen(Title, RynthPanelOpen.Close);
|
||||
SafeImGui.SetItemTooltip(v.Id == 0
|
||||
? "Nothing selected. Select with a click or the game's selection keys (- and = go through what's near you).\nDrag to move; Ctrl+wheel: bigger or smaller; right-click: hide."
|
||||
: $"{v.Name}{(v.Where.Length > 0 ? " - " + v.Where : "")}\nDrag to move; Ctrl+wheel: bigger or smaller; right-click: hide.");
|
||||
|
||||
// The picture.
|
||||
Vector2 ip = origin + new Vector2(pad, pad);
|
||||
dl.AddRectFilled(ip, ip + new Vector2(icon, icon), v.Id == 0 ? _inset : _key, Px(3));
|
||||
IntPtr tex = v.Id != 0 ? _host.GetIconTexture(RynthIconKind.Object, v.Id) : IntPtr.Zero;
|
||||
if (tex != IntPtr.Zero)
|
||||
dl.AddImageRounded(tex, ip + new Vector2(Px(2), Px(2)), ip + new Vector2(icon - Px(2), icon - Px(2)), Vector2.Zero, Vector2.One, White, Px(2));
|
||||
dl.AddRect(ip, ip + new Vector2(icon, icon), _line, Px(3));
|
||||
|
||||
float tx = ip.X + icon + Px(Gap);
|
||||
float right = origin.X + width - pad - (hasUse ? useW + Px(Gap) : 0f);
|
||||
ImFontPtr nf = Font(RynthUiFont.NarrowBold11), sf = Font(RynthUiFont.Narrow9);
|
||||
if (!v.Ready || v.Id == 0)
|
||||
{
|
||||
Clip(dl, nf, Fs(nf), new Vector2(tx, ip.Y + Px(1)), _faint, v.Ready ? "Nothing selected" : "Selection", right - tx);
|
||||
Clip(dl, sf, Fs(sf), new Vector2(tx, ip.Y + icon - Fs(sf)), _faint, "- and = go through what's near you", right - tx);
|
||||
ImGui.SetCursorScreenPos(origin);
|
||||
ImGui.Dummy(new Vector2(width, height));
|
||||
return height;
|
||||
}
|
||||
|
||||
// Name, then the health bar (a creature or player) and where it is.
|
||||
Clip(dl, nf, Fs(nf), new Vector2(tx, ip.Y), _text, v.Name, right - tx);
|
||||
float lineY = ip.Y + icon - Fs(sf);
|
||||
float x = tx;
|
||||
if (!float.IsNaN(v.Relative))
|
||||
{
|
||||
float s = Fs(sf);
|
||||
Arrow(dl, new Vector2(x + s * 0.5f, lineY + s * 0.5f), s * 0.5f, v.Relative, _accent);
|
||||
x += s + Px(3);
|
||||
}
|
||||
if (v.Health >= 0f)
|
||||
{
|
||||
float barY = ip.Y + Fs(nf) + Px(3);
|
||||
float barW = right - tx, barH = Px(BarH);
|
||||
Vector2 b0 = new(tx, barY), b1 = new(tx + barW, barY + barH);
|
||||
dl.AddRectFilled(b0, b1, _inset, Px(2));
|
||||
float f = Math.Clamp(v.Health, 0f, 1f);
|
||||
if (f > 0f) dl.AddRectFilled(b0, new Vector2(tx + barW * f, barY + barH), f > 0.6f ? A(HealthHigh, 0.9f) : HealthColor, Px(2));
|
||||
dl.AddRect(b0, b1, _line, Px(2));
|
||||
string pct = (v.Max > 0 ? $"{v.Current}/{v.Max} " : "") + MathF.Round(f * 100f).ToString(CultureInfo.InvariantCulture) + "%";
|
||||
float pw = sf.CalcTextSizeA(Fs(sf), float.MaxValue, 0f, pct).X;
|
||||
dl.AddText(sf, Fs(sf), new Vector2(right - pw, lineY), _dim, pct);
|
||||
Clip(dl, sf, Fs(sf), new Vector2(x, lineY), _dim, v.Where, right - pw - Px(6) - x);
|
||||
}
|
||||
else Clip(dl, sf, Fs(sf), new Vector2(x, lineY), _dim, v.Where, right - x);
|
||||
|
||||
// USE.
|
||||
Vector2 up = new(origin.X + width - pad - useW, origin.Y + (height - Px(UseH)) * 0.5f);
|
||||
ImGui.SetCursorScreenPos(up);
|
||||
bool use = ImGui.InvisibleButton("##sel_use", new Vector2(useW, Px(UseH)));
|
||||
bool hot = ImGui.IsItemHovered();
|
||||
SafeImGui.SetItemTooltip("Use it, as a double-click does (open a door, talk, pick up, use an item).");
|
||||
dl.AddRectFilled(up, up + new Vector2(useW, Px(UseH)), hot ? _key : A(_key, 0.6f), Px(3));
|
||||
dl.AddRect(up, up + new Vector2(useW, Px(UseH)), hot ? _accent : _line, Px(3));
|
||||
ImFontPtr uf = Font(RynthUiFont.Dash9);
|
||||
float uw = uf.CalcTextSizeA(Fs(uf), float.MaxValue, 0f, "USE").X;
|
||||
dl.AddText(uf, Fs(uf), new Vector2(MathF.Round(up.X + (useW - uw) * 0.5f), MathF.Round(up.Y + (Px(UseH) - Fs(uf)) * 0.5f)), hot ? _text : _dim, "USE");
|
||||
if (use) _use(v.Id);
|
||||
|
||||
ImGui.SetCursorScreenPos(origin);
|
||||
ImGui.Dummy(new Vector2(width, height));
|
||||
return height;
|
||||
}
|
||||
|
||||
/// <summary>Text cut with "..." to <paramref name="room"/> pixels.</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>A small arrow pointing <paramref name="degrees"/> from straight up (where the player faces), clockwise.</summary>
|
||||
private static void Arrow(ImDrawListPtr dl, Vector2 c, float r, float degrees, uint color)
|
||||
{
|
||||
float a = degrees * MathF.PI / 180f;
|
||||
Vector2 Dir(float t) => new(MathF.Sin(t), -MathF.Cos(t));
|
||||
Vector2 tip = c + Dir(a) * r;
|
||||
Vector2 left = c + Dir(a + 2.45f) * r * 0.85f, rightP = c + Dir(a - 2.45f) * r * 0.85f;
|
||||
dl.AddTriangleFilled(tip, left, rightP, color);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,539 @@
|
|||
// ============================================================================
|
||||
// RynthDefective - ShortcutBar/ShortcutBarWindow.cs
|
||||
// The shortcut bar, drawn by RynthDefective in its own frameless window
|
||||
// (plugin API v90: RynthCore hosts it, "chrome": "none" + "fitHeight"), in
|
||||
// the instrument look of RynthAi's dashboard and the player's colour theme.
|
||||
//
|
||||
// header SHORTCUTS and the slot count (the drag handle; Ctrl+wheel: the
|
||||
// size), then Edit, Weapons (the weapon picker) and Hide.
|
||||
// slots the item's picture, its key in the top-left corner (AC's keymap
|
||||
// for slots 1-18, the engine's hotkeys after), the count or "in
|
||||
// hand" under it. They fill the window's width and wrap: drag the
|
||||
// window wider or narrower for one row or several.
|
||||
// edit the slot numbers show, and - / + at the end take a slot off or
|
||||
// add one (the player picks how many; 1-18 are the game's).
|
||||
// Drag an item from AC's pack onto a slot: it goes there, as on the retail
|
||||
// toolbar (the item stays in the pack). Click a slot: it is used as its key
|
||||
// would. Right-click: put the selected item there, or empty it, and its keys: remove one, or add one by pressing
|
||||
// it. A slot 1-18 key is AC's own (its keymap, so the retail toolbar answers
|
||||
// to it too, in every client); a key past 18 is an engine hotkey running
|
||||
// /rdef use N. A key comes off whatever had it, after the player says so.
|
||||
//
|
||||
// AC's render thread. Reads the snapshot the tick built; every click is
|
||||
// queued for the tick (IBarActions).
|
||||
// ============================================================================
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using ImGuiNET;
|
||||
using RynthCore.Plugin.RynthDefective.Model;
|
||||
using RynthCore.Plugin.Shared;
|
||||
using RynthCore.Plugin.Shared.RynthUi;
|
||||
using RynthCore.PluginSdk;
|
||||
using static RynthCore.Plugin.Shared.RynthUi.RynthUi;
|
||||
|
||||
namespace RynthCore.Plugin.RynthDefective.ShortcutBar;
|
||||
|
||||
/// <summary>What the bar asks of the plugin: everything runs on its tick.</summary>
|
||||
internal interface IBarActions
|
||||
{
|
||||
void Use(int number);
|
||||
void SetFromSelected(int number);
|
||||
void Clear(int number);
|
||||
void ChangeSlotCount(int delta);
|
||||
/// <summary>An item dragged out of AC's UI was let go over slot <paramref name="number"/>.</summary>
|
||||
void DropItem(int number, uint objectId);
|
||||
/// <summary>A key was bound or removed: the bar asks for its keys again.</summary>
|
||||
void KeysChanged();
|
||||
void ShowWeapons();
|
||||
void Hide();
|
||||
}
|
||||
|
||||
internal sealed class ShortcutBarWindow
|
||||
{
|
||||
public const string Title = "Shortcuts";
|
||||
|
||||
private readonly RynthCoreHost _host;
|
||||
private readonly IBarActions _act;
|
||||
private bool _edit;
|
||||
private float _k = 1f, _r = 1f;
|
||||
|
||||
public ShortcutBarWindow(RynthCoreHost host, IBarActions actions)
|
||||
{
|
||||
_host = host;
|
||||
_act = actions;
|
||||
}
|
||||
|
||||
private float Px(float v) => v * _k;
|
||||
|
||||
/// <summary>A font's size at this scale (its baked size times the text ratio).</summary>
|
||||
private float Fs(ImFontPtr f) => f.FontSize * _r;
|
||||
|
||||
/// <summary>
|
||||
/// The layout's scale and the text's against the baked font: the window's own size (Settings > UI >
|
||||
/// Toolbar, Ctrl+wheel: 30-250%, PanelScale) or, unset, the text size the engine baked for it.
|
||||
/// </summary>
|
||||
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 PadX = 7, PadTop = 6, PadBottom = 7, Radius = 4, HeaderH = 18, Gap = 3, ChipSize = 16;
|
||||
|
||||
private uint _bg, _key, _inset, _line, _text, _dim, _faint, _accent;
|
||||
private static readonly uint InHandColor = Argb(0xFF73D885), MissingColor = Argb(0xFFF98075),
|
||||
Black = Argb(0xFF000000), White = Argb(0xFFFFFFFF);
|
||||
|
||||
private static uint A(uint c, float alpha) => WithAlpha(c, (uint)Math.Clamp(MathF.Round(alpha * 255f), 0f, 255f));
|
||||
|
||||
/// <summary>Draws the bar into the host's window; returns the height it needs (pixels). Render thread.</summary>
|
||||
public float Draw(BarSnapshot view, bool 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);
|
||||
|
||||
Vector2 origin = ImGui.GetCursorScreenPos();
|
||||
origin = new Vector2(MathF.Round(origin.X), MathF.Round(origin.Y));
|
||||
float width = MathF.Floor(ImGui.GetContentRegionAvail().X);
|
||||
if (width < 40) return Px(60);
|
||||
ImDrawListPtr dl = ImGui.GetWindowDrawList();
|
||||
|
||||
float height;
|
||||
dl.ChannelsSplit(2);
|
||||
bool merged = false;
|
||||
try
|
||||
{
|
||||
dl.ChannelsSetCurrent(1);
|
||||
float x0 = origin.X + Px(PadX), w = width - 2 * Px(PadX);
|
||||
float y = origin.Y + Px(PadTop);
|
||||
y = Header(dl, view, x0, y, w);
|
||||
y += Px(5);
|
||||
y = Slots(dl, view, x0, y, w);
|
||||
y += Px(PadBottom);
|
||||
height = MathF.Ceiling(y - origin.Y);
|
||||
dl.ChannelsSetCurrent(0);
|
||||
Frame(dl, origin, new Vector2(width, height));
|
||||
dl.ChannelsMerge();
|
||||
merged = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!merged) dl.ChannelsMerge();
|
||||
}
|
||||
ImGui.SetCursorScreenPos(origin);
|
||||
ImGui.Dummy(new Vector2(width, height));
|
||||
SlotMenu(view);
|
||||
if (_openKeyPopup)
|
||||
{
|
||||
_openKeyPopup = false;
|
||||
ImGui.OpenPopup(KeyPopupId);
|
||||
}
|
||||
KeyPopup();
|
||||
return height;
|
||||
}
|
||||
|
||||
private void Frame(ImDrawListPtr dl, Vector2 min, Vector2 size)
|
||||
{
|
||||
Vector2 max = min + size;
|
||||
float r = Px(Radius);
|
||||
dl.AddRectFilled(min, max, A(_bg, 0.94f), r);
|
||||
dl.AddLine(new Vector2(min.X + r, min.Y + 1.5f), new Vector2(max.X - r, min.Y + 1.5f), A(White, 0.05f));
|
||||
dl.AddRect(min, max, _line, r);
|
||||
}
|
||||
|
||||
// ── Header ────────────────────────────────────────────────────────────
|
||||
|
||||
private float Header(ImDrawListPtr dl, BarSnapshot view, float x0, float y, float w)
|
||||
{
|
||||
float h = Px(HeaderH), chip = Px(ChipSize);
|
||||
const int chips = 3;
|
||||
float chipsW = chips * chip + (chips - 1) * Px(1);
|
||||
|
||||
ImGui.SetCursorScreenPos(new Vector2(x0, y));
|
||||
ImGui.InvisibleButton("##sb_drag", new Vector2(Math.Max(1, w - chipsW - Px(4)), h));
|
||||
_host.PanelDragWithLastItem();
|
||||
SafeImGui.SetItemTooltip("Drag to move the bar. Ctrl+wheel here: bigger or smaller.\nDrag the window's edge to change how many slots fit in a row.");
|
||||
|
||||
ImFontPtr tf = Font(RynthUiFont.NarrowBold11);
|
||||
float size = Fs(tf) * 10f / 11f;
|
||||
float tx = Tracked(dl, tf, size, new Vector2(x0, MathF.Round(y + (h - size) * 0.5f)), _text, "SHORTCUTS", 0.18f * size);
|
||||
ImFontPtr nf = Font(RynthUiFont.Dash9);
|
||||
string count = view.Ready ? view.Slots.Length.ToString(System.Globalization.CultureInfo.InvariantCulture) : "";
|
||||
dl.AddText(nf, Fs(nf), new Vector2(MathF.Round(tx + Px(6)), MathF.Round(y + (h - Fs(nf)) * 0.5f + Px(1))), _faint, count);
|
||||
|
||||
float cx = x0 + w - chipsW, cy = y + (h - chip) * 0.5f;
|
||||
if (Chip("##sb_edit", RynthIcons.PencilSimple, new Vector2(cx, cy), _edit ? _accent : _faint,
|
||||
_edit ? "Done editing." : "Edit: the slot numbers, and - / + to take a slot off or add one.", _edit))
|
||||
_edit = !_edit;
|
||||
cx += chip + Px(1);
|
||||
if (Chip("##sb_weapons", RynthIcons.Sword, new Vector2(cx, cy), _faint, "Weapons: every weapon and wand you carry, wield one with a click."))
|
||||
_act.ShowWeapons();
|
||||
cx += chip + Px(1);
|
||||
if (Chip("##sb_hide", RynthIcons.X, new Vector2(cx, cy), _faint, "Hide the bar (/rdef shows it again). Its keys keep working."))
|
||||
_act.Hide();
|
||||
return y + h;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// ── Slots ─────────────────────────────────────────────────────────────
|
||||
|
||||
private int _menuSlot;
|
||||
private const string MenuId = "##sb_slot_menu";
|
||||
|
||||
private float Slots(ImDrawListPtr dl, BarSnapshot view, float x0, float y, float w)
|
||||
{
|
||||
if (!view.Ready)
|
||||
{
|
||||
ImFontPtr f = Font(RynthUiFont.Narrow10);
|
||||
dl.AddText(f, Fs(f), new Vector2(x0, y), _faint, "Your shortcuts show once you're in the world.");
|
||||
return y + Fs(f) + Px(2);
|
||||
}
|
||||
float cell = Px(Math.Clamp(view.IconSize, 20, 64)), gap = Px(Gap);
|
||||
int cells = view.Slots.Length + (_edit ? 2 : 0);
|
||||
int perRow = Math.Max(1, (int)MathF.Floor((w + gap) / (cell + gap)));
|
||||
int rows = (cells + perRow - 1) / perRow;
|
||||
|
||||
// Items dragged out of AC's UI: the slots' area takes them (the engine keeps the drop from AC).
|
||||
Vector2 areaMin = new(x0, y), areaMax = new(x0 + perRow * (cell + gap), y + rows * (cell + gap));
|
||||
uint dropped = _host.HasDropTargets ? _host.DropTarget(Title, areaMin, areaMax, true, out _dragging) : 0;
|
||||
int under = SlotAt(ImGui.GetIO().MousePos, x0, y, cell, gap, perRow, view.Slots.Length);
|
||||
_dropSlot = _dragging ? under : 0;
|
||||
if (dropped != 0 && under > 0) _act.DropItem(under, dropped);
|
||||
|
||||
for (int i = 0; i < cells; i++)
|
||||
{
|
||||
int col = i % perRow, row = i / perRow;
|
||||
Vector2 p = new(MathF.Round(x0 + col * (cell + gap)), MathF.Round(y + row * (cell + gap)));
|
||||
if (i < view.Slots.Length) Slot(dl, view.Slots[i], p, cell);
|
||||
else EditButton(dl, i == view.Slots.Length ? -1 : 1, p, cell, view.Slots.Length);
|
||||
}
|
||||
return y + rows * (cell + gap) - gap;
|
||||
}
|
||||
|
||||
private bool _dragging;
|
||||
private int _dropSlot;
|
||||
|
||||
/// <summary>The slot number (1-based) under <paramref name="m"/>, or 0.</summary>
|
||||
private static int SlotAt(Vector2 m, float x0, float y0, float cell, float gap, int perRow, int count)
|
||||
{
|
||||
if (m.X < x0 || m.Y < y0) return 0;
|
||||
int col = (int)((m.X - x0) / (cell + gap)), row = (int)((m.Y - y0) / (cell + gap));
|
||||
if (col >= perRow || m.X - x0 - col * (cell + gap) > cell || m.Y - y0 - row * (cell + gap) > cell) return 0;
|
||||
int i = row * perRow + col;
|
||||
return i < count ? i + 1 : 0;
|
||||
}
|
||||
|
||||
private void Slot(ImDrawListPtr dl, BarSlot s, Vector2 p, float cell)
|
||||
{
|
||||
Vector2 size = new(cell, cell);
|
||||
ImGui.SetCursorScreenPos(p);
|
||||
bool clicked = ImGui.InvisibleButton(Id("##sb_slot", s.Number), size, ImGuiButtonFlags.MouseButtonLeft | ImGuiButtonFlags.MouseButtonRight)
|
||||
&& ImGui.IsMouseReleased(ImGuiMouseButton.Left);
|
||||
bool right = ImGui.IsItemClicked(ImGuiMouseButton.Right);
|
||||
bool hot = ImGui.IsItemHovered();
|
||||
SafeImGui.SetItemTooltip(s.Tooltip);
|
||||
float r = Px(3);
|
||||
|
||||
// The well, the picture, a hairline (the accent under the mouse; green while in hand).
|
||||
dl.AddRectFilled(p, p + size, s.Empty ? _inset : _key, r);
|
||||
if (!s.Empty)
|
||||
{
|
||||
uint id = s.ObjectId != 0 ? s.ObjectId : s.SpellId;
|
||||
IntPtr tex = id == 0 ? IntPtr.Zero : _host.GetIconTexture(s.ObjectId != 0 ? RynthIconKind.Object : RynthIconKind.Spell, id);
|
||||
float inset = Px(2);
|
||||
if (tex != IntPtr.Zero)
|
||||
dl.AddImageRounded(tex, p + new Vector2(inset, inset), p + size - new Vector2(inset, inset), Vector2.Zero, Vector2.One,
|
||||
s.Tone == CountTone.Missing ? A(White, 0.35f) : White, Px(2));
|
||||
else
|
||||
{
|
||||
ImFontPtr af = Font(RynthUiFont.Narrow9);
|
||||
string abbr = Abbrev(s.Name);
|
||||
float aw = af.CalcTextSizeA(Fs(af), float.MaxValue, 0f, abbr).X;
|
||||
dl.AddText(af, Fs(af), new Vector2(MathF.Round(p.X + (cell - aw) * 0.5f), MathF.Round(p.Y + (cell - Fs(af)) * 0.5f)), _dim, abbr);
|
||||
}
|
||||
}
|
||||
uint border = hot || _dropSlot == s.Number ? _accent : s.Tone == CountTone.InHand ? A(InHandColor, 0.8f) : _line;
|
||||
dl.AddRect(p, p + size, border, r, ImDrawFlags.None, _dropSlot == s.Number ? Px(2) : 1f);
|
||||
|
||||
// The key, top left (a dark plate under it so it reads over any picture); edit: the slot number.
|
||||
ImFontPtr kf = Font(RynthUiFont.Dash9);
|
||||
string label = _edit ? s.Number.ToString(System.Globalization.CultureInfo.InvariantCulture) : s.Key;
|
||||
if (label.Length > 0)
|
||||
{
|
||||
float lw = kf.CalcTextSizeA(Fs(kf), float.MaxValue, 0f, label).X;
|
||||
Vector2 lp = p + new Vector2(Px(2), Px(1));
|
||||
dl.AddRectFilled(lp - new Vector2(Px(1), 0), lp + new Vector2(lw + Px(1), Fs(kf)), A(Black, 0.55f), Px(2));
|
||||
dl.AddText(kf, Fs(kf), lp, _edit ? _accent : s.Retail ? _text : _accent, label);
|
||||
}
|
||||
// The count, bottom right.
|
||||
if (!_edit && s.Count.Length > 0)
|
||||
{
|
||||
string c = s.Count == "in hand" ? "hand" : s.Count;
|
||||
float cw = kf.CalcTextSizeA(Fs(kf), float.MaxValue, 0f, c).X;
|
||||
Vector2 cp = p + new Vector2(cell - cw - Px(2), cell - Fs(kf) - Px(1));
|
||||
dl.AddRectFilled(cp - new Vector2(Px(1), 0), cp + new Vector2(cw + Px(1), Fs(kf)), A(Black, 0.55f), Px(2));
|
||||
uint cc = s.Tone == CountTone.InHand ? InHandColor : s.Tone == CountTone.Missing ? MissingColor : _text;
|
||||
dl.AddText(kf, Fs(kf), cp, cc, c);
|
||||
}
|
||||
|
||||
if (clicked && !s.Empty) _act.Use(s.Number);
|
||||
if (right)
|
||||
{
|
||||
_menuSlot = s.Number;
|
||||
ImGui.OpenPopup(MenuId);
|
||||
}
|
||||
}
|
||||
|
||||
private void EditButton(ImDrawListPtr dl, int delta, Vector2 p, float cell, int count)
|
||||
{
|
||||
Vector2 size = new(cell, cell);
|
||||
bool can = delta < 0 ? count > DefSettings.BarSlotsMin : count < DefSettings.BarSlotsMax;
|
||||
ImGui.SetCursorScreenPos(p);
|
||||
bool clicked = ImGui.InvisibleButton(delta < 0 ? "##sb_less" : "##sb_more", size);
|
||||
bool hot = can && ImGui.IsItemHovered();
|
||||
SafeImGui.SetItemTooltip(delta < 0
|
||||
? "One slot fewer (the last one's item stays: add the slot back and it is there)."
|
||||
: count < DefSettings.RetailSlots ? "One more slot (the game's own, up to 18)."
|
||||
: "One more slot (RynthCore's: the game's toolbar holds 18).");
|
||||
float r = Px(3);
|
||||
dl.AddRect(p, p + size, hot ? _accent : A(_line, can ? 1f : 0.4f), r);
|
||||
DrawIcon(dl, delta < 0 ? RynthIcons.Minus : RynthIcons.Plus, p, size, Px(12), can ? (hot ? _text : _dim) : A(_faint, 0.5f));
|
||||
if (clicked && can) _act.ChangeSlotCount(delta);
|
||||
}
|
||||
|
||||
private void SlotMenu(BarSnapshot view)
|
||||
{
|
||||
if (!ImGui.IsPopupOpen(MenuId)) return;
|
||||
ImGui.PushStyleColor(ImGuiCol.PopupBg, WithAlpha(_bg, 0xFA));
|
||||
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
|
||||
{
|
||||
BarSlot? s = _menuSlot >= 1 && _menuSlot <= view.Slots.Length ? view.Slots[_menuSlot - 1] : null;
|
||||
if (s == null) { ImGui.CloseCurrentPopup(); return; }
|
||||
ImGui.TextDisabled($"Slot {s.Number}{(s.Retail ? " (the game's own)" : " (RynthCore's)")}");
|
||||
if (s.Name.Length > 0) SafeImGui.Text(s.Name);
|
||||
ImGui.Separator();
|
||||
bool hasSelection = _host.HasGetSelectedItemId && _host.GetSelectedItemId() != 0;
|
||||
if (ImGui.MenuItem("Put the selected item here", null, false, hasSelection)) _act.SetFromSelected(s.Number);
|
||||
if (ImGui.MenuItem("Empty this slot", null, false, !s.Empty)) _act.Clear(s.Number);
|
||||
ImGui.Separator();
|
||||
if (!_host.HasKeyBinding)
|
||||
{
|
||||
ImGui.TextDisabled(s.AllKeys.Length > 0 ? "Keys: " + s.AllKeys
|
||||
: s.Retail ? "No key: bind UseQuickSlot_" + s.Number + " in Configure Keyboard." : "No key: /rdef bind " + (s.Number - DefSettings.RetailSlots) + " F9");
|
||||
return;
|
||||
}
|
||||
string[] keys = SplitKeys(s.AllKeys);
|
||||
ImGui.TextDisabled(keys.Length == 0 ? "No key yet" : keys.Length == 1 ? "Key" : "Keys");
|
||||
foreach (string key in keys)
|
||||
{
|
||||
if (!ImGui.MenuItem("Remove " + key)) continue;
|
||||
if (_host.UnbindActionKey(KeyAction(s.Number), key, out string why)) _act.KeysChanged();
|
||||
else ShowKeyMessage(s.Number, why);
|
||||
}
|
||||
if (ImGui.MenuItem(keys.Length == 0 ? "Add a key..." : "Add another key..."))
|
||||
StartKeyCapture(s.Number);
|
||||
SafeImGui.SetItemTooltip(s.Retail
|
||||
? "Press the key: it becomes this slot's in the game's own keymap, so the retail toolbar and every client use it too."
|
||||
: "Press the key: a RynthCore hotkey for this slot (the game's toolbar holds 18).");
|
||||
}
|
||||
finally { ImGui.EndPopup(); }
|
||||
}
|
||||
|
||||
// ── Keys: "press a key" ───────────────────────────────────────────────
|
||||
|
||||
private enum KeyStep { Waiting, Confirm, Message }
|
||||
|
||||
private const string KeyPopupId = "##sb_key_popup";
|
||||
private bool _openKeyPopup;
|
||||
private int _keySlot;
|
||||
private KeyStep _keyStep;
|
||||
private string _keyName = string.Empty, _keyText = string.Empty;
|
||||
|
||||
/// <summary>The action a slot's keys run: AC's UseQuickSlot_N for 1-18, the engine command for the rest.</summary>
|
||||
public static string KeyAction(int number) =>
|
||||
number <= DefSettings.RetailSlots ? "UseQuickSlot_" + number : DefCommandParser.UseCommand(number - DefSettings.RetailSlots);
|
||||
|
||||
private static string[] SplitKeys(string keys) =>
|
||||
keys.Length == 0 ? Array.Empty<string>() : keys.Split(", ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
private void StartKeyCapture(int number)
|
||||
{
|
||||
_keySlot = number;
|
||||
_keyStep = KeyStep.Waiting;
|
||||
_keyName = _keyText = string.Empty;
|
||||
_host.BeginKeyCapture();
|
||||
_openKeyPopup = true;
|
||||
}
|
||||
|
||||
private void ShowKeyMessage(int number, string text)
|
||||
{
|
||||
_keySlot = number;
|
||||
_keyStep = KeyStep.Message;
|
||||
_keyText = text;
|
||||
_openKeyPopup = true;
|
||||
}
|
||||
|
||||
private void KeyPopup()
|
||||
{
|
||||
if (!ImGui.IsPopupOpen(KeyPopupId))
|
||||
{
|
||||
if (_keyStep == KeyStep.Waiting && _keySlot != 0) { _host.CancelKeyCapture(); _keySlot = 0; } // clicked away
|
||||
return;
|
||||
}
|
||||
ImGui.PushStyleColor(ImGuiCol.PopupBg, WithAlpha(_bg, 0xFA));
|
||||
ImGui.PushStyleColor(ImGuiCol.Border, _line);
|
||||
ImGui.PushStyleVar(ImGuiStyleVar.PopupRounding, Px(4));
|
||||
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(Px(8), Px(7)));
|
||||
bool open = ImGui.BeginPopup(KeyPopupId);
|
||||
ImGui.PopStyleVar(2);
|
||||
ImGui.PopStyleColor(2);
|
||||
if (!open) return;
|
||||
try
|
||||
{
|
||||
ImGui.PushTextWrapPos(ImGui.GetCursorPosX() + Px(230));
|
||||
ImGui.TextDisabled($"Slot {_keySlot}{(_keySlot <= DefSettings.RetailSlots ? " (the game's own)" : " (RynthCore's)")}");
|
||||
switch (_keyStep)
|
||||
{
|
||||
case KeyStep.Waiting:
|
||||
SafeImGui.Text("Press the key for this slot.");
|
||||
ImGui.TextDisabled("Hold Ctrl, Alt or Shift with it if you like. Esc cancels.");
|
||||
RynthKeyCapture got = _host.PollKeyCapture(out string text);
|
||||
if (got == RynthKeyCapture.Waiting)
|
||||
{
|
||||
if (text.Length > 0) ImGui.TextColored(new Vector4(0.98f, 0.5f, 0.46f, 1f), text);
|
||||
break;
|
||||
}
|
||||
if (got == RynthKeyCapture.Cancelled) { _keySlot = 0; ImGui.CloseCurrentPopup(); break; }
|
||||
_keyName = text;
|
||||
RynthKeyCheck check = _host.CheckActionKey(KeyAction(_keySlot), _keyName, out string msg);
|
||||
if (check == RynthKeyCheck.Free) Bind();
|
||||
else
|
||||
{
|
||||
_keyStep = check == RynthKeyCheck.Taken ? KeyStep.Confirm : KeyStep.Message;
|
||||
_keyText = msg;
|
||||
}
|
||||
break;
|
||||
case KeyStep.Confirm:
|
||||
SafeImGui.Text(_keyText);
|
||||
ImGui.TextDisabled($"Use it for slot {_keySlot} instead? It comes off those.");
|
||||
if (ImGui.Button($"Use {_keyName} here")) Bind();
|
||||
ImGui.SameLine();
|
||||
if (ImGui.Button("Another key")) StartKeyCapture(_keySlot);
|
||||
ImGui.SameLine();
|
||||
if (ImGui.Button("Cancel")) { _keySlot = 0; ImGui.CloseCurrentPopup(); }
|
||||
break;
|
||||
case KeyStep.Message:
|
||||
SafeImGui.Text(_keyText);
|
||||
if (ImGui.Button("Another key")) StartKeyCapture(_keySlot);
|
||||
ImGui.SameLine();
|
||||
if (ImGui.Button("Close")) { _keySlot = 0; ImGui.CloseCurrentPopup(); }
|
||||
break;
|
||||
}
|
||||
ImGui.PopTextWrapPos();
|
||||
}
|
||||
finally { ImGui.EndPopup(); }
|
||||
}
|
||||
|
||||
private void Bind()
|
||||
{
|
||||
if (_host.BindActionKey(KeyAction(_keySlot), _keyName, out string why))
|
||||
{
|
||||
_act.KeysChanged();
|
||||
_keySlot = 0;
|
||||
ImGui.CloseCurrentPopup();
|
||||
return;
|
||||
}
|
||||
_keyStep = KeyStep.Message;
|
||||
_keyText = why;
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
private static readonly System.Collections.Generic.Dictionary<(string, int), string> Ids = new();
|
||||
|
||||
private static string Id(string prefix, int n)
|
||||
{
|
||||
if (!Ids.TryGetValue((prefix, n), out string? id)) Ids[(prefix, n)] = id = prefix + n;
|
||||
return id;
|
||||
}
|
||||
|
||||
private static string Abbrev(string name)
|
||||
{
|
||||
if (name.Length == 0) return "?";
|
||||
string[] words = name.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (words.Length >= 2) return string.Concat(char.ToUpperInvariant(words[0][0]), char.ToUpperInvariant(words[^1][0]));
|
||||
return name.Length <= 3 ? name : name[..3];
|
||||
}
|
||||
|
||||
private static readonly string[] Chars = BuildChars();
|
||||
|
||||
private static string[] BuildChars()
|
||||
{
|
||||
var a = new string[128];
|
||||
for (int i = 32; i < 127; i++) a[i] = ((char)i).ToString();
|
||||
return a;
|
||||
}
|
||||
|
||||
/// <summary>Text with <paramref name="spacing"/> pixels between its letters. Returns the x after it.</summary>
|
||||
private static float Tracked(ImDrawListPtr dl, ImFontPtr font, float size, Vector2 pos, uint color, string text, float spacing)
|
||||
{
|
||||
unsafe { if (font.NativePtr == null || text.Length == 0) return pos.X; }
|
||||
float scale = size / font.FontSize;
|
||||
float x = pos.X;
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
char c = text[i];
|
||||
string s = c < 128 && Chars[c] != null ? Chars[c] : c.ToString();
|
||||
dl.AddText(font, size, new Vector2(MathF.Round(x), pos.Y), color, s);
|
||||
x += font.GetCharAdvance(c) * scale;
|
||||
if (i < text.Length - 1) x += spacing;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/// <summary>A Phosphor glyph centred in the box, <paramref name="px"/> across its em (from Ui18's bake).</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue