Snapshot for release 2026.10.13.1 (internal d2b5fb3)
This commit is contained in:
parent
fe1eca0ca7
commit
319740ff5d
291 changed files with 58505 additions and 2107 deletions
130
Plugins/RynthCore.Plugin.RynthThreads/Ui/UiFormat.cs
Normal file
130
Plugins/RynthCore.Plugin.RynthThreads/Ui/UiFormat.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
|
||||
namespace RynthCore.Plugin.RynthThreads.Ui;
|
||||
|
||||
/// <summary>
|
||||
/// Display-list format 1 for the engine's script windows (RynthSuite
|
||||
/// Docs/RYNTHLUA_WINDOWS_DESIGN.md §4.2-4.3; the engine's DisplayListParser.cs is the source of
|
||||
/// truth). Little-endian. Only the ops these windows draw are listed.
|
||||
/// </summary>
|
||||
internal static class UiFormat
|
||||
{
|
||||
public const uint Magic = 0x31575352; // 'RSW1'
|
||||
public const ushort FormatVersion = 1;
|
||||
public const uint BodyUnchanged = 0xFFFFFFFF;
|
||||
public const int MaxString = 4096;
|
||||
public const int MaxStr8 = 128;
|
||||
public const int MaxOps = 1500; // engine limit per window
|
||||
public const int MaxBodyBytes = 64 * 1024; // engine limit per window
|
||||
public const byte OpHasBlock = 1;
|
||||
|
||||
// Ops
|
||||
public const byte OpEnd = 0x00;
|
||||
public const byte OpText = 0x01;
|
||||
public const byte OpTextColored = 0x02;
|
||||
public const byte OpTextWrapped = 0x03;
|
||||
public const byte OpTextDisabled = 0x04;
|
||||
public const byte OpBulletText = 0x05;
|
||||
public const byte OpSeparatorText = 0x06;
|
||||
public const byte OpSeparator = 0x10;
|
||||
public const byte OpSameLine = 0x11;
|
||||
public const byte OpSpacing = 0x13;
|
||||
public const byte OpDummy = 0x14;
|
||||
public const byte OpIndent = 0x15;
|
||||
public const byte OpUnindent = 0x16;
|
||||
public const byte OpButton = 0x20;
|
||||
public const byte OpCheckbox = 0x21;
|
||||
public const byte OpSliderInt = 0x22;
|
||||
public const byte OpInputText = 0x24;
|
||||
public const byte OpCombo = 0x25;
|
||||
public const byte OpSelectable = 0x26;
|
||||
public const byte OpProgressBar = 0x27;
|
||||
public const byte OpImage = 0x28; // op level 2 (engines from 2026-09-30)
|
||||
public const byte OpPushID = 0x30;
|
||||
public const byte OpPopID = 0x31;
|
||||
public const byte OpPushStyleColor = 0x32;
|
||||
public const byte OpPopStyleColor = 0x33;
|
||||
public const byte OpSetItemTooltip = 0x38;
|
||||
public const byte OpChild = 0x40;
|
||||
public const byte OpCollapsingHeader = 0x41;
|
||||
|
||||
// Window flags
|
||||
public const uint FlagVisible = 1u << 0;
|
||||
public const uint FlagShowInBar = 1u << 1;
|
||||
/// <summary>With ShowInBar: a bar button of its own instead of a Scripts menu line (engines from
|
||||
/// 2026-10-11; older ones ignore it and list the window in the Scripts menu).</summary>
|
||||
public const uint FlagBarButton = 1u << 11;
|
||||
public const uint FlagNoScrollbar = 1u << 5;
|
||||
|
||||
// Image icon kinds (op level 2)
|
||||
public const byte IconKindIcon = 0; // a 0x06 texture id
|
||||
public const byte IconKindObject = 1; // a live object's icon with underlay and overlay
|
||||
|
||||
// Selectable flags
|
||||
public const uint SelectableAllowDoubleClick = 4;
|
||||
|
||||
// Event types
|
||||
public const ushort EvClicked = 1;
|
||||
public const ushort EvBool = 2;
|
||||
public const ushort EvInt = 3;
|
||||
public const ushort EvText = 5;
|
||||
public const ushort EvOpen = 6;
|
||||
public const ushort EvVisibility = 8;
|
||||
public const ushort EvError = 10;
|
||||
|
||||
// ImGuiCol ids the engine accepts in PushStyleColor (ImGui.NET 1.91.6.1)
|
||||
public const int ColText = 0;
|
||||
public const int ColButton = 21;
|
||||
public const int ColButtonHovered = 22;
|
||||
public const int ColHeader = 24;
|
||||
|
||||
// InputText flags
|
||||
public const uint InputAutoSelectAll = 4096;
|
||||
/// <summary>The text box reports Enter (its event carries submitted = true).</summary>
|
||||
public const uint InputEnterReturnsTrue = 32;
|
||||
|
||||
// CollapsingHeader flags
|
||||
public const uint TreeDefaultOpen = 32;
|
||||
|
||||
public static uint Fnv1a(ReadOnlySpan<byte> bytes, uint hash = 2166136261)
|
||||
{
|
||||
foreach (byte b in bytes)
|
||||
{
|
||||
hash ^= b;
|
||||
hash *= 16777619;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
/// <summary>The length of <paramref name="s"/> cut to at most <paramref name="max"/> bytes without splitting a character.</summary>
|
||||
public static int CutUtf8(ReadOnlySpan<byte> s, int max)
|
||||
{
|
||||
if (s.Length <= max) return s.Length;
|
||||
int n = Math.Max(0, max);
|
||||
while (n > 0 && (s[n] & 0xC0) == 0x80) n--;
|
||||
return n;
|
||||
}
|
||||
|
||||
/// <summary>An ImGui U32 colour (0xAABBGGRR) from RGB bytes.</summary>
|
||||
public static uint Rgb(byte r, byte g, byte b, byte a = 255) =>
|
||||
((uint)a << 24) | ((uint)b << 16) | ((uint)g << 8) | r;
|
||||
}
|
||||
|
||||
/// <summary>Colours RynthThreads uses (copied from RynthSort) (0xAABBGGRR).</summary>
|
||||
internal static class UiColors
|
||||
{
|
||||
public static readonly uint Green = UiFormat.Rgb(110, 210, 110);
|
||||
public static readonly uint Red = UiFormat.Rgb(225, 95, 95);
|
||||
public static readonly uint Yellow = UiFormat.Rgb(235, 205, 90);
|
||||
public static readonly uint Grey = UiFormat.Rgb(150, 150, 150);
|
||||
public static readonly uint Gold = UiFormat.Rgb(230, 180, 60);
|
||||
/// <summary>Upstream's "destruction" highlight: deep pink brightened by half (255, 30, 220).</summary>
|
||||
public static readonly uint Destruction = UiFormat.Rgb(255, 30, 220);
|
||||
public static readonly uint TabActive = UiFormat.Rgb(60, 110, 170);
|
||||
public static readonly uint TabActiveHover = UiFormat.Rgb(75, 130, 195);
|
||||
public static readonly uint OtherServer = UiFormat.Rgb(150, 185, 235);
|
||||
public static readonly uint Stale = UiFormat.Rgb(225, 150, 80);
|
||||
public static readonly uint White = UiFormat.Rgb(255, 255, 255);
|
||||
/// <summary>The Lua script's amber (paused, full packs): 1.0, 0.75, 0.25.</summary>
|
||||
public static readonly uint Amber = UiFormat.Rgb(255, 191, 64);
|
||||
}
|
||||
202
Plugins/RynthCore.Plugin.RynthThreads/Ui/UiHost.cs
Normal file
202
Plugins/RynthCore.Plugin.RynthThreads/Ui/UiHost.cs
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using RynthCore.PluginSdk;
|
||||
|
||||
namespace RynthCore.Plugin.RynthThreads.Ui;
|
||||
|
||||
/// <summary>
|
||||
/// The plugin's side of the engine's script windows (API v71): polls the engine's events into
|
||||
/// the windows' handlers and submits the window set when something changed. Plugin pump thread
|
||||
/// only (inside the plugin's own tick), as the engine requires.
|
||||
/// </summary>
|
||||
internal sealed class UiHost
|
||||
{
|
||||
private readonly List<UiWindow> _windows = new();
|
||||
private readonly Dictionary<uint, UiWindow> _byHash = new();
|
||||
private byte[] _events = new byte[8192];
|
||||
private byte[] _submit = new byte[64 * 1024];
|
||||
private uint _lastSeq;
|
||||
private bool _submittedSomething;
|
||||
private long _retryAtMs;
|
||||
private long _lastErrorLogMs;
|
||||
|
||||
public Action<string>? Log;
|
||||
|
||||
public IReadOnlyList<UiWindow> Windows => _windows;
|
||||
|
||||
public UiWindow Add(UiWindow w)
|
||||
{
|
||||
_windows.Add(w);
|
||||
_byHash[w.Hash] = w;
|
||||
return w;
|
||||
}
|
||||
|
||||
/// <summary>Drains the engine's queued events into the windows' handlers.</summary>
|
||||
public void Poll(RynthCoreHost host)
|
||||
{
|
||||
if (!host.HasUi || !_submittedSomething) return;
|
||||
for (int round = 0; round < 8; round++)
|
||||
{
|
||||
int n = host.UiPollEvents(_events, out int remaining);
|
||||
if (n > 0) Apply(_events.AsSpan(0, n));
|
||||
if (remaining == 0) break;
|
||||
if (n == 0) Array.Resize(ref _events, _events.Length * 2); // one event didn't fit
|
||||
}
|
||||
}
|
||||
|
||||
private void Apply(ReadOnlySpan<byte> data)
|
||||
{
|
||||
int pos = 0;
|
||||
while (data.Length - pos >= 16)
|
||||
{
|
||||
ushort type = BinaryPrimitives.ReadUInt16LittleEndian(data[pos..]);
|
||||
int size = BinaryPrimitives.ReadUInt16LittleEndian(data[(pos + 2)..]);
|
||||
if (size < 16 || size > data.Length - pos) break;
|
||||
uint seq = BinaryPrimitives.ReadUInt32LittleEndian(data[(pos + 4)..]);
|
||||
uint window = BinaryPrimitives.ReadUInt32LittleEndian(data[(pos + 8)..]);
|
||||
uint key = BinaryPrimitives.ReadUInt32LittleEndian(data[(pos + 12)..]);
|
||||
ReadOnlySpan<byte> p = data.Slice(pos + 16, size - 16);
|
||||
pos += size;
|
||||
if (seq > _lastSeq) _lastSeq = seq;
|
||||
|
||||
_byHash.TryGetValue(window, out UiWindow? w);
|
||||
switch (type)
|
||||
{
|
||||
case UiFormat.EvClicked when w != null && p.Length >= 2:
|
||||
int count = BinaryPrimitives.ReadUInt16LittleEndian(p);
|
||||
for (int i = 0; i < Math.Min(count, 4); i++) w.Dispatch(key, new UiEvent(type));
|
||||
break;
|
||||
case UiFormat.EvBool when w != null && p.Length >= 1:
|
||||
w.Dispatch(key, new UiEvent(type, b: p[0] != 0));
|
||||
break;
|
||||
case UiFormat.EvInt when w != null && p.Length >= 4:
|
||||
w.Dispatch(key, new UiEvent(type, i: BinaryPrimitives.ReadInt32LittleEndian(p)));
|
||||
break;
|
||||
case UiFormat.EvText when w != null && p.Length >= 3:
|
||||
{
|
||||
int n = Math.Min(BinaryPrimitives.ReadUInt16LittleEndian(p[1..]), p.Length - 3);
|
||||
w.Dispatch(key, new UiEvent(type, text: Encoding.UTF8.GetString(p.Slice(3, n)), submitted: p[0] != 0));
|
||||
break;
|
||||
}
|
||||
case UiFormat.EvOpen when w != null && p.Length >= 1:
|
||||
w.Dispatch(key, new UiEvent(type, b: p[0] != 0));
|
||||
break;
|
||||
case UiFormat.EvVisibility when w != null && p.Length >= 1:
|
||||
// The player closed it (X) or opened it (bar menu / command): theirs wins.
|
||||
w.Visible = p[0] != 0;
|
||||
w.NeedsPass = true;
|
||||
break;
|
||||
case UiFormat.EvError when p.Length >= 2:
|
||||
{
|
||||
int n = Math.Min(BinaryPrimitives.ReadUInt16LittleEndian(p), p.Length - 2);
|
||||
Log?.Invoke("[RynthThreads] window error: " + Encoding.UTF8.GetString(p.Slice(2, n)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Sends the window set when any window's list or header changed.</summary>
|
||||
public void Submit(RynthCoreHost host, long nowMs)
|
||||
{
|
||||
if (!host.HasUi || nowMs < _retryAtMs) return;
|
||||
bool changed = !_submittedSomething && _windows.Count > 0;
|
||||
foreach (UiWindow w in _windows)
|
||||
if (w.BodyDirty || w.HeaderDirty) { changed = true; break; }
|
||||
if (!changed) return;
|
||||
|
||||
int pos = Write();
|
||||
int rc = host.UiSubmit(_submit.AsSpan(0, pos));
|
||||
if (rc == 0)
|
||||
{
|
||||
foreach (UiWindow w in _windows) w.MarkSubmitted(w.BodyDirty);
|
||||
_submittedSomething = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Nothing was applied; the engine keeps the previous lists. Try again in a second.
|
||||
_retryAtMs = nowMs + 1000;
|
||||
if (nowMs - _lastErrorLogMs > 10_000)
|
||||
{
|
||||
_lastErrorLogMs = nowMs;
|
||||
Log?.Invoke($"[RynthThreads] the engine refused the window list ({rc}).");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Removes every window from the engine (logout, shutdown). Their placement is kept by the engine.</summary>
|
||||
public void SubmitEmpty(RynthCoreHost host)
|
||||
{
|
||||
if (!host.HasUi || !_submittedSomething) return;
|
||||
Span<byte> b = stackalloc byte[16];
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b, UiFormat.Magic);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(b[4..], UiFormat.FormatVersion);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(b[6..], 0);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[8..], _lastSeq);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[12..], 16);
|
||||
if (host.UiSubmit(b) == 0)
|
||||
{
|
||||
_submittedSomething = false;
|
||||
foreach (UiWindow w in _windows) w.ForgetSubmitted();
|
||||
}
|
||||
}
|
||||
|
||||
private int Write()
|
||||
{
|
||||
int size = 16;
|
||||
foreach (UiWindow w in _windows) size += 2 * (1 + UiFormat.MaxStr8) + 4 + 32 + 8 + (w.BodyDirty ? w.Body.Length : 0);
|
||||
if (_submit.Length < size) _submit = new byte[Math.Max(size, _submit.Length * 2)];
|
||||
Span<byte> b = _submit;
|
||||
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b, UiFormat.Magic);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(b[4..], UiFormat.FormatVersion);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(b[6..], (ushort)_windows.Count);
|
||||
// Every event polled so far has been applied (handlers run as they arrive).
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[8..], _lastSeq);
|
||||
int pos = 16;
|
||||
foreach (UiWindow w in _windows)
|
||||
{
|
||||
pos = Str8(b, pos, w.KeyUtf8);
|
||||
pos = Str8(b, pos, Encoding.UTF8.GetBytes(w.Title ?? string.Empty));
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[pos..], w.Flags); pos += 4;
|
||||
pos = F32(b, pos, w.DefaultWidth);
|
||||
pos = F32(b, pos, w.DefaultHeight);
|
||||
pos = F32(b, pos, w.MinWidth);
|
||||
pos = F32(b, pos, w.MinHeight);
|
||||
pos = F32(b, pos, float.NaN); // RequestSize: none
|
||||
pos = F32(b, pos, float.NaN);
|
||||
pos = F32(b, pos, float.NaN); // RequestPos: none
|
||||
pos = F32(b, pos, float.NaN);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[pos..], w.ListSeq); pos += 4;
|
||||
if (w.BodyDirty)
|
||||
{
|
||||
ReadOnlySpan<byte> body = w.Body;
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[pos..], (uint)body.Length); pos += 4;
|
||||
body.CopyTo(b[pos..]);
|
||||
pos += body.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[pos..], UiFormat.BodyUnchanged); pos += 4;
|
||||
}
|
||||
}
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b[12..], (uint)pos);
|
||||
return pos;
|
||||
}
|
||||
|
||||
private static int Str8(Span<byte> b, int pos, ReadOnlySpan<byte> s)
|
||||
{
|
||||
int n = UiFormat.CutUtf8(s, UiFormat.MaxStr8);
|
||||
b[pos] = (byte)n;
|
||||
s[..n].CopyTo(b[(pos + 1)..]);
|
||||
return pos + 1 + n;
|
||||
}
|
||||
|
||||
private static int F32(Span<byte> b, int pos, float v)
|
||||
{
|
||||
BinaryPrimitives.WriteSingleLittleEndian(b[pos..], v);
|
||||
return pos + 4;
|
||||
}
|
||||
}
|
||||
515
Plugins/RynthCore.Plugin.RynthThreads/Ui/UiWindow.cs
Normal file
515
Plugins/RynthCore.Plugin.RynthThreads/Ui/UiWindow.cs
Normal file
|
|
@ -0,0 +1,515 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace RynthCore.Plugin.RynthThreads.Ui;
|
||||
|
||||
/// <summary>One input from the engine for a widget (a click, a new value, an open state).</summary>
|
||||
internal readonly struct UiEvent
|
||||
{
|
||||
public readonly ushort Type;
|
||||
public readonly bool Bool;
|
||||
public readonly int Int;
|
||||
public readonly string Text;
|
||||
public readonly bool Submitted;
|
||||
|
||||
public UiEvent(ushort type, bool b = false, int i = 0, string? text = null, bool submitted = false)
|
||||
{
|
||||
Type = type;
|
||||
Bool = b;
|
||||
Int = i;
|
||||
Text = text ?? string.Empty;
|
||||
Submitted = submitted;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One script window (engine API v71) and its recorder. A render pass records ImGui-style calls
|
||||
/// into a display list; every widget's handler (click, value change) is kept by its widget key
|
||||
/// until the next pass, so an event from the engine runs the code that drew that widget.
|
||||
///
|
||||
/// Widget keys are FNV-1a(id, window hash): RynthThreads names every widget itself, so it needs
|
||||
/// no ID stack. ImGui sees "label###id": its ID comes from the id alone, so labels can change.
|
||||
/// The engine echoes the key in events and keys its per-widget local values by it.
|
||||
/// </summary>
|
||||
internal sealed class UiWindow
|
||||
{
|
||||
public readonly string Key;
|
||||
public readonly byte[] KeyUtf8;
|
||||
public readonly uint Hash;
|
||||
|
||||
public string Title;
|
||||
public bool Visible;
|
||||
public bool ShowInBar = true;
|
||||
/// <summary>With ShowInBar: the window has its own button on the RynthCore bar (title as the
|
||||
/// tooltip) rather than a line in the Scripts menu.</summary>
|
||||
public bool BarButton = true;
|
||||
public float DefaultWidth, DefaultHeight, MinWidth, MinHeight;
|
||||
|
||||
/// <summary>A pass is wanted at the next chance (input arrived, it was just shown, data changed).</summary>
|
||||
public bool NeedsPass = true;
|
||||
public long NextPassMs;
|
||||
|
||||
// The list as recorded by the last pass, and what the engine last accepted.
|
||||
private byte[] _body = new byte[16 * 1024];
|
||||
private int _len;
|
||||
private int _ops;
|
||||
private bool _cut;
|
||||
private byte[] _sentBody = Array.Empty<byte>();
|
||||
private int _sentLen = -1;
|
||||
public uint ListSeq { get; private set; }
|
||||
internal bool BodyDirty;
|
||||
|
||||
// Header as last accepted by the engine.
|
||||
private string? _sentTitle;
|
||||
private uint _sentFlags;
|
||||
private float _sentDw = float.NaN, _sentDh, _sentMw, _sentMh;
|
||||
|
||||
private Dictionary<uint, Action<UiEvent>> _handlers = new();
|
||||
private Dictionary<uint, Action<UiEvent>> _next = new();
|
||||
private readonly Dictionary<uint, bool> _openStates = new();
|
||||
|
||||
// Child blocks: where each open block's u32 length sits, or -1 when the block was dropped.
|
||||
private readonly int[] _blocks = new int[16];
|
||||
private int _blockDepth;
|
||||
private byte[] _tmp = new byte[512];
|
||||
|
||||
public UiWindow(string key, string title)
|
||||
{
|
||||
Key = key;
|
||||
KeyUtf8 = Encoding.UTF8.GetBytes(key);
|
||||
Hash = UiFormat.Fnv1a(KeyUtf8);
|
||||
Title = title;
|
||||
}
|
||||
|
||||
public uint Flags => (Visible ? UiFormat.FlagVisible : 0) | (ShowInBar ? UiFormat.FlagShowInBar : 0)
|
||||
| (ShowInBar && BarButton ? UiFormat.FlagBarButton : 0);
|
||||
|
||||
public bool HeaderDirty =>
|
||||
_sentTitle != Title || _sentFlags != Flags || _sentDw != DefaultWidth || _sentDh != DefaultHeight
|
||||
|| _sentMw != MinWidth || _sentMh != MinHeight;
|
||||
|
||||
public ReadOnlySpan<byte> Body => _body.AsSpan(0, _len);
|
||||
|
||||
// ── Pass ────────────────────────────────────────────────────────────────
|
||||
|
||||
public void Begin()
|
||||
{
|
||||
_len = 0;
|
||||
_ops = 0;
|
||||
_cut = false;
|
||||
_blockDepth = 0;
|
||||
_next.Clear();
|
||||
}
|
||||
|
||||
/// <summary>Ends the pass: closes open blocks, writes End, and notes whether the list changed.</summary>
|
||||
public void End()
|
||||
{
|
||||
while (_blockDepth > 0) EndChild();
|
||||
if (_cut)
|
||||
{
|
||||
// Room was kept for this line (see Room).
|
||||
int p = BeginOp(UiFormat.OpTextDisabled);
|
||||
Str("(more than fits in one window; narrow the list)");
|
||||
EndOp(p);
|
||||
}
|
||||
Ensure(4);
|
||||
_body[_len++] = UiFormat.OpEnd;
|
||||
_body[_len++] = 0;
|
||||
_body[_len++] = 0;
|
||||
_body[_len++] = 0;
|
||||
|
||||
(_handlers, _next) = (_next, _handlers);
|
||||
bool differs = _sentLen != _len || !_body.AsSpan(0, _len).SequenceEqual(_sentBody.AsSpan(0, _sentLen));
|
||||
if (differs && !BodyDirty) ListSeq++;
|
||||
BodyDirty = differs;
|
||||
}
|
||||
|
||||
/// <summary>The engine accepted the last submit: remember what it now has.</summary>
|
||||
internal void MarkSubmitted(bool bodySent)
|
||||
{
|
||||
if (bodySent)
|
||||
{
|
||||
if (_sentBody.Length < _len) _sentBody = new byte[_body.Length];
|
||||
_body.AsSpan(0, _len).CopyTo(_sentBody);
|
||||
_sentLen = _len;
|
||||
BodyDirty = false;
|
||||
}
|
||||
_sentTitle = Title;
|
||||
_sentFlags = Flags;
|
||||
_sentDw = DefaultWidth; _sentDh = DefaultHeight; _sentMw = MinWidth; _sentMh = MinHeight;
|
||||
}
|
||||
|
||||
/// <summary>The engine dropped every window (logout, empty submit): send everything again next time.</summary>
|
||||
internal void ForgetSubmitted()
|
||||
{
|
||||
_sentLen = -1;
|
||||
_sentTitle = null;
|
||||
BodyDirty = _len > 0;
|
||||
}
|
||||
|
||||
internal void Dispatch(uint widgetKey, in UiEvent e)
|
||||
{
|
||||
if (_handlers.TryGetValue(widgetKey, out Action<UiEvent>? h))
|
||||
h(e);
|
||||
NeedsPass = true;
|
||||
}
|
||||
|
||||
// ── Widgets ─────────────────────────────────────────────────────────────
|
||||
|
||||
public uint KeyOf(string id)
|
||||
{
|
||||
int max = Encoding.UTF8.GetMaxByteCount(id.Length);
|
||||
if (_tmp.Length < max) _tmp = new byte[Math.Max(max, _tmp.Length * 2)];
|
||||
int n = Encoding.UTF8.GetBytes(id, _tmp);
|
||||
return UiFormat.Fnv1a(_tmp.AsSpan(0, n), Hash);
|
||||
}
|
||||
|
||||
public void Text(string text) => WriteText(UiFormat.OpText, text);
|
||||
public void TextDisabled(string text) => WriteText(UiFormat.OpTextDisabled, text);
|
||||
public void TextWrapped(string text) => WriteText(UiFormat.OpTextWrapped, text);
|
||||
public void BulletText(string text) => WriteText(UiFormat.OpBulletText, text);
|
||||
public void SeparatorText(string text) => WriteText(UiFormat.OpSeparatorText, text);
|
||||
|
||||
public void TextColored(uint abgr, string text)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpTextColored);
|
||||
U32(abgr);
|
||||
Str(text);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
public void Separator()
|
||||
{
|
||||
if (!Room()) return;
|
||||
EndOp(BeginOp(UiFormat.OpSeparator));
|
||||
}
|
||||
|
||||
public void Spacing()
|
||||
{
|
||||
if (!Room()) return;
|
||||
EndOp(BeginOp(UiFormat.OpSpacing));
|
||||
}
|
||||
|
||||
/// <summary>Next item on the same line, <paramref name="x"/> logical units from the content start (0 = right after).</summary>
|
||||
public void SameLine(float x = 0f, float spacing = -1f)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpSameLine);
|
||||
F32(x);
|
||||
F32(spacing);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
public void Dummy(float w, float h)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpDummy);
|
||||
F32(w);
|
||||
F32(h);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
/// <summary>Moves the following items right by <paramref name="w"/> logical units, until <see cref="Unindent"/>.</summary>
|
||||
public void Indent(float w)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpIndent);
|
||||
F32(w);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
public void Unindent(float w)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpUnindent);
|
||||
F32(w);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
public void Button(string label, string id, Action onClick, float w = 0f, float h = 0f, bool small = false)
|
||||
{
|
||||
if (!Room()) return;
|
||||
uint key = KeyOf(id);
|
||||
int p = BeginOp(UiFormat.OpButton);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
F32(w);
|
||||
F32(h);
|
||||
U8(small ? (byte)1 : (byte)0);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvClicked) onClick(); };
|
||||
}
|
||||
|
||||
public void SmallButton(string label, string id, Action onClick) => Button(label, id, onClick, small: true);
|
||||
|
||||
/// <summary>A button drawn highlighted when <paramref name="active"/> (tabs, toggles).</summary>
|
||||
public void ToggleButton(string label, string id, bool active, Action onClick, bool small = false)
|
||||
{
|
||||
if (active)
|
||||
{
|
||||
PushStyleColor(UiFormat.ColButton, UiColors.TabActive);
|
||||
PushStyleColor(UiFormat.ColButtonHovered, UiColors.TabActiveHover);
|
||||
}
|
||||
Button(label, id, onClick, small: small);
|
||||
if (active) PopStyleColor(2);
|
||||
}
|
||||
|
||||
public void Checkbox(string label, string id, bool value, Action<bool> set)
|
||||
{
|
||||
if (!Room()) return;
|
||||
uint key = KeyOf(id);
|
||||
int p = BeginOp(UiFormat.OpCheckbox);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
U8(value ? (byte)1 : (byte)0);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvBool) set(e.Bool); };
|
||||
}
|
||||
|
||||
/// <summary>An integer slider from <paramref name="min"/> to <paramref name="max"/> (clamped by the engine).</summary>
|
||||
public void SliderInt(string label, string id, int value, int min, int max, Action<int> set, string format = "%d")
|
||||
{
|
||||
if (!Room()) return;
|
||||
uint key = KeyOf(id);
|
||||
int p = BeginOp(UiFormat.OpSliderInt);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
I32(value);
|
||||
I32(min);
|
||||
I32(max);
|
||||
Str(format);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvInt) set(Math.Clamp(e.Int, min, max)); };
|
||||
}
|
||||
|
||||
/// <summary>A text box; <paramref name="set"/> gets every edit (submitted = Enter was pressed).</summary>
|
||||
public void InputText(string label, string id, string value, int maxLen, Action<string> set, uint flags = 0) =>
|
||||
InputText(label, id, value, maxLen, (text, _) => set(text), flags);
|
||||
|
||||
/// <summary>A text box; <paramref name="set"/> gets every edit and whether Enter was pressed.</summary>
|
||||
public void InputText(string label, string id, string value, int maxLen, Action<string, bool> set, uint flags = 0)
|
||||
{
|
||||
if (!Room()) return;
|
||||
maxLen = Math.Clamp(maxLen, 1, UiFormat.MaxString);
|
||||
uint key = KeyOf(id);
|
||||
int p = BeginOp(UiFormat.OpInputText);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
Str(value ?? string.Empty, maxLen);
|
||||
U16((ushort)maxLen);
|
||||
U32(flags);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvText) set(e.Text, e.Submitted); };
|
||||
}
|
||||
|
||||
public void Combo(string label, string id, int index, IReadOnlyList<string> items, Action<int> set)
|
||||
{
|
||||
if (!Room()) return;
|
||||
uint key = KeyOf(id);
|
||||
int count = Math.Min(items.Count, 512);
|
||||
int p = BeginOp(UiFormat.OpCombo);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
I32(index);
|
||||
U16((ushort)count);
|
||||
for (int i = 0; i < count; i++) Str(items[i]);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvInt) set(e.Int); };
|
||||
}
|
||||
|
||||
public void Selectable(string label, string id, bool selected, Action onClick, float w = 0f, float h = 0f)
|
||||
{
|
||||
if (!Room()) return;
|
||||
uint key = KeyOf(id);
|
||||
int p = BeginOp(UiFormat.OpSelectable);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
U8(selected ? (byte)1 : (byte)0);
|
||||
U32(0);
|
||||
F32(w);
|
||||
F32(h);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvClicked) onClick(); };
|
||||
}
|
||||
|
||||
public void ProgressBar(float fraction, float w, float h, string overlay)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpProgressBar);
|
||||
F32(float.IsFinite(fraction) ? Math.Clamp(fraction, 0f, 1f) : 0f);
|
||||
F32(w);
|
||||
F32(h);
|
||||
Str(overlay ?? string.Empty);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An icon (op level 2): <paramref name="kind"/> <see cref="UiFormat.IconKindIcon"/> = a 0x06
|
||||
/// texture id, <see cref="UiFormat.IconKindObject"/> = a live object. Engines without op level
|
||||
/// 2 skip it (the caller checks first so the layout doesn't shift).
|
||||
/// </summary>
|
||||
public void Image(byte kind, uint id, float w, float h)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpImage);
|
||||
U8(kind);
|
||||
U32(id);
|
||||
F32(w);
|
||||
F32(h);
|
||||
F32(0f); F32(0f); // uv0
|
||||
F32(1f); F32(1f); // uv1
|
||||
U32(0xFFFFFFFFu); // tint: as is
|
||||
U32(0u); // border: none
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
/// <summary>Tooltip for the item just drawn.</summary>
|
||||
public void Tooltip(string text)
|
||||
{
|
||||
if (!Room() || string.IsNullOrEmpty(text)) return;
|
||||
int p = BeginOp(UiFormat.OpSetItemTooltip);
|
||||
Str(text);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
public void PushStyleColor(int colId, uint abgr)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpPushStyleColor);
|
||||
U16((ushort)colId);
|
||||
U32(abgr);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
/// <summary>Pops are dropped when the list is cut short; the engine pops leftovers at the scope's end.</summary>
|
||||
public void PopStyleColor(int count = 1)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(UiFormat.OpPopStyleColor);
|
||||
U8((byte)Math.Clamp(count, 1, 255));
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A collapsing header; returns whether to record its contents. The open state is the
|
||||
/// player's (kept from the engine's Open events), so a header stays as they left it.
|
||||
/// </summary>
|
||||
public bool CollapsingHeader(string label, string id, bool defaultOpen = false)
|
||||
{
|
||||
uint key = KeyOf(id);
|
||||
bool open = _openStates.TryGetValue(key, out bool o) ? o : defaultOpen;
|
||||
if (!Room()) return false;
|
||||
int p = BeginOp(UiFormat.OpCollapsingHeader);
|
||||
U32(key);
|
||||
Str(label + "###" + id);
|
||||
U32(defaultOpen ? UiFormat.TreeDefaultOpen : 0);
|
||||
U8(open ? (byte)1 : (byte)0);
|
||||
EndOp(p);
|
||||
_next[key] = e => { if (e.Type == UiFormat.EvOpen) _openStates[key] = e.Bool; };
|
||||
return open;
|
||||
}
|
||||
|
||||
/// <summary>A scrolling region; h 0 = fill the rest of the window. Must be closed with <see cref="EndChild"/>.</summary>
|
||||
public void BeginChild(string id, float w = 0f, float h = 0f, bool border = true)
|
||||
{
|
||||
if (_blockDepth >= _blocks.Length) throw new InvalidOperationException("child regions nested too deep");
|
||||
if (!Room())
|
||||
{
|
||||
_blocks[_blockDepth++] = -1;
|
||||
return;
|
||||
}
|
||||
uint key = KeyOf(id);
|
||||
int p = BeginOp(UiFormat.OpChild, UiFormat.OpHasBlock);
|
||||
U32(key);
|
||||
Str(id);
|
||||
F32(w);
|
||||
F32(h);
|
||||
U8(border ? (byte)1 : (byte)0);
|
||||
U32(0);
|
||||
EndOp(p);
|
||||
_blocks[_blockDepth++] = _len;
|
||||
U32(0); // BlockLength, patched by EndChild
|
||||
}
|
||||
|
||||
public void EndChild()
|
||||
{
|
||||
if (_blockDepth == 0) return;
|
||||
int at = _blocks[--_blockDepth];
|
||||
if (at < 0) return;
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(_body.AsSpan(at), (uint)(_len - (at + 4)));
|
||||
}
|
||||
|
||||
// ── Writing ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Whether another op fits. Room is kept for the closing "list cut short" line and the End
|
||||
/// op, so the list never goes over the engine's limits (it refuses an over-limit list whole).
|
||||
/// </summary>
|
||||
private bool Room()
|
||||
{
|
||||
if (_cut) return false;
|
||||
if (_ops < UiFormat.MaxOps - 8 && _len < UiFormat.MaxBodyBytes - 4096) return true;
|
||||
_cut = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void WriteText(byte code, string text)
|
||||
{
|
||||
if (!Room()) return;
|
||||
int p = BeginOp(code);
|
||||
Str(text);
|
||||
EndOp(p);
|
||||
}
|
||||
|
||||
private int BeginOp(byte code, byte opFlags = 0)
|
||||
{
|
||||
_ops++;
|
||||
Ensure(4);
|
||||
_body[_len++] = code;
|
||||
_body[_len++] = opFlags;
|
||||
_len += 2; // PayloadLength, patched by EndOp
|
||||
return _len;
|
||||
}
|
||||
|
||||
private void EndOp(int payloadStart)
|
||||
{
|
||||
int length = _len - payloadStart;
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(_body.AsSpan(payloadStart - 2), (ushort)Math.Min(length, ushort.MaxValue));
|
||||
}
|
||||
|
||||
private void Ensure(int extra)
|
||||
{
|
||||
if (_len + extra <= _body.Length) return;
|
||||
int size = _body.Length;
|
||||
while (size < _len + extra) size *= 2;
|
||||
Array.Resize(ref _body, size);
|
||||
}
|
||||
|
||||
private void U8(byte v) { Ensure(1); _body[_len++] = v; }
|
||||
private void U16(ushort v) { Ensure(2); BinaryPrimitives.WriteUInt16LittleEndian(_body.AsSpan(_len), v); _len += 2; }
|
||||
private void U32(uint v) { Ensure(4); BinaryPrimitives.WriteUInt32LittleEndian(_body.AsSpan(_len), v); _len += 4; }
|
||||
private void I32(int v) { Ensure(4); BinaryPrimitives.WriteInt32LittleEndian(_body.AsSpan(_len), v); _len += 4; }
|
||||
|
||||
private void F32(float v)
|
||||
{
|
||||
Ensure(4);
|
||||
BinaryPrimitives.WriteSingleLittleEndian(_body.AsSpan(_len), float.IsFinite(v) ? v : 0f);
|
||||
_len += 4;
|
||||
}
|
||||
|
||||
/// <summary>str: u16 length + UTF-8, cut to <paramref name="maxBytes"/> on a character boundary.</summary>
|
||||
private void Str(string s, int maxBytes = UiFormat.MaxString)
|
||||
{
|
||||
s ??= string.Empty;
|
||||
ReadOnlySpan<char> src = s.Length > maxBytes ? s.AsSpan(0, maxBytes) : s.AsSpan();
|
||||
Ensure(2 + Encoding.UTF8.GetMaxByteCount(src.Length));
|
||||
int at = _len + 2;
|
||||
Span<byte> dst = _body.AsSpan(at);
|
||||
int n = UiFormat.CutUtf8(dst[..Encoding.UTF8.GetBytes(src, dst)], maxBytes);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(_body.AsSpan(_len), (ushort)n);
|
||||
_len = at + n;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue