// ============================================================================ // RynthVision - RynthVisionPlugin.Ui.cs // The Vision window's side of the plugin (2026-10-27; Ui/VisionWindow.cs, // Ui/VisionManifest.cs): RynthVision draws the whole Vision window, in the // engine's Vision panel's place, on an engine with plugin API v91 (_hostsWindow, // decided once in Initialize; an older engine keeps its own panel). // // Threads: the window (AC's render thread), /rv (AC's thread) and the light's // commands queue their changes here; the tick (the plugin pump) applies them // first thing, saves rynthvision.json and publishes a fresh copy of the // settings for the window (SettingsView) whenever anything changed them (a // change here, or the engine's panels through PluginExports, on the pump too). // /rv light show asks the window for its Light tab (_tabRequest). // ============================================================================ using System; using System.Collections.Concurrent; using System.Threading; using RynthCore.Plugin.RynthVision.Light; using RynthCore.Plugin.RynthVision.Ui; namespace RynthCore.Plugin.RynthVision; public sealed partial class RynthVisionPlugin { private readonly ConcurrentQueue _uiActions = new(); private volatile SettingsView? _settingsView; private int _settingsVersion; private bool _settingsDirty = true; // pump thread private bool _hostsWindow; // Initialize: the engine hosts the Vision window private int _tabRequest = -1; // a VisionTab the window should show (-1 none) private VisionWindow? _visionWindow; // render thread /// The manifest RynthCore hosts the Vision window from (no window on an older engine). internal string SectionsJson => VisionManifest.Json(_hostsWindow); /// The settings as the tick last published them (the window's copy source). Any thread. internal SettingsView? Settings => _settingsView; /// The dungeon light's state and status line, as the tick last published them. Any thread. internal LightView LightState => _lightView; /// Tick, first thing: the queued changes, then a fresh copy for the window if anything changed. private void TickUi() { while (_uiActions.TryDequeue(out Action? act)) { try { act(); } catch (Exception ex) { Host.Log("[RynthVision] Settings change: " + ex.Message); } } if (!_settingsDirty) return; _settingsDirty = false; _settingsView = new SettingsView(++_settingsVersion, _settings.Clone()); } /// The settings changed (pump thread): republish them, and the light's view. private void MarkSettingsChanged() { _settingsDirty = true; _lightViewDirty = true; } /// Applies on the tick and saves when . Any thread. internal void QueueSettingsChange(Action change, bool save = true) => _uiActions.Enqueue(() => { change(_settings); if (save) _settings.Save(); MarkSettingsChanged(); }); /// "Log terrain types here": on the tick (it may open the terrain data). Any thread. internal void QueueInspectTerrain() => _uiActions.Enqueue(InspectTerrain); /// Asks the window to show on its next draw. Any thread. private void RequestTab(VisionTab tab) => Volatile.Write(ref _tabRequest, (int)tab); /// The tab /rv light show asked for, once. Render thread. internal bool TakeTabRequest(out VisionTab tab) { int t = Interlocked.Exchange(ref _tabRequest, -1); tab = (VisionTab)Math.Max(0, t); return t >= 0; } /// RynthPluginRenderEmbed: draws (":popout" already off). Render thread. internal bool RenderEmbed(string surface) { if (!_hostsWindow || !surface.Equals(VisionManifest.Surface, StringComparison.Ordinal)) return false; _visionWindow ??= new VisionWindow(this, Host); _visionWindow.Draw(); return true; } }