From 19f77d2e6e600f91e2d752a8c0988148a7691b51 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 29 Mar 2021 12:33:26 -0400 Subject: [PATCH] Viewport support within Dalamud --- Dalamud/Dalamud.csproj | 5 ++- Dalamud/Game/Internal/Gui/GameGui.cs | 23 +++++++++++-- Dalamud/Interface/DalamudDataWindow.cs | 45 +++++++++++++++++--------- Dalamud/Interface/InterfaceManager.cs | 10 ++++-- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/Dalamud/Dalamud.csproj b/Dalamud/Dalamud.csproj index 55e97b46f..562b2b6db 100644 --- a/Dalamud/Dalamud.csproj +++ b/Dalamud/Dalamud.csproj @@ -10,7 +10,7 @@ false true - Portable + full $(SolutionDir)\bin\Dalamud.xml @@ -30,6 +30,9 @@ false + + DEBUG;TRACE + diff --git a/Dalamud/Game/Internal/Gui/GameGui.cs b/Dalamud/Game/Internal/Gui/GameGui.cs index b15effe7d..06b0b1677 100644 --- a/Dalamud/Game/Internal/Gui/GameGui.cs +++ b/Dalamud/Game/Internal/Gui/GameGui.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.InteropServices; using Dalamud.Game.Chat.SeStringHandling.Payloads; using Dalamud.Hooking; +using ImGuiNET; using Serilog; using SharpDX; @@ -287,6 +288,8 @@ namespace Dalamud.Game.Internal.Gui { // Read current ViewProjectionMatrix plus game window size var viewProjectionMatrix = new Matrix(); float width, height; + var windowPos = ImGui.GetMainViewport().Pos; + unsafe { var rawMatrix = (float*) (matrixSingleton + 0x1b4).ToPointer(); @@ -301,10 +304,12 @@ namespace Dalamud.Game.Internal.Gui { screenPos = new Vector2(pCoords.X / pCoords.Z, pCoords.Y / pCoords.Z); - screenPos.X = 0.5f * width * (screenPos.X + 1f); - screenPos.Y = 0.5f * height * (1f - screenPos.Y); + screenPos.X = 0.5f * width * (screenPos.X + 1f) + windowPos.X; + screenPos.Y = 0.5f * height * (1f - screenPos.Y) + windowPos.Y; - return pCoords.Z > 0; + return pCoords.Z > 0 && + screenPos.X > windowPos.X && screenPos.X < windowPos.X + width && + screenPos.Y > windowPos.Y && screenPos.Y < windowPos.Y + height; } /// @@ -316,6 +321,18 @@ namespace Dalamud.Game.Internal.Gui { /// True if successful. On false, worldPos's contents are undefined public bool ScreenToWorld(Vector2 screenPos, out Vector3 worldPos, float rayDistance = 100000.0f) { + // The game is only visible in the main viewport, so if the cursor is outside + // of the game window, do not bother calculating anything + var windowPos = ImGui.GetMainViewport().Pos; + var windowSize = ImGui.GetMainViewport().Size; + + if (screenPos.X < windowPos.X || screenPos.X > windowPos.X + windowSize.X || + screenPos.Y < windowPos.Y || screenPos.Y > windowPos.Y + windowSize.Y) + { + worldPos = new Vector3(); + return false; + } + // Get base object with matrices var matrixSingleton = this.getMatrixSingleton(); diff --git a/Dalamud/Interface/DalamudDataWindow.cs b/Dalamud/Interface/DalamudDataWindow.cs index 3899697bc..3f3727744 100644 --- a/Dalamud/Interface/DalamudDataWindow.cs +++ b/Dalamud/Interface/DalamudDataWindow.cs @@ -157,29 +157,44 @@ namespace Dalamud.Interface PrintActor(actor, i.ToString()); - if (this.drawActors && - this.dalamud.Framework.Gui.WorldToScreen(actor.Position, out var screenCoords) - ) { - ImGui.PushID("ActorWindow" + i); - ImGui.SetNextWindowPos(new Vector2(screenCoords.X, screenCoords.Y)); + if (this.drawActors && this.dalamud.Framework.Gui.WorldToScreen(actor.Position, out var screenCoords)) { + + // So, while WorldToScreen will return false if the point is off of game client screen, to + // to avoid performance issues, we have to manually determine if creating a window would + // produce a new viewport, and skip rendering it if so + var actorText = $"{actor.Address.ToInt64():X}:{actor.ActorId:X}[{i}] - {actor.ObjectKind} - {actor.Name}"; + + var screenPos = ImGui.GetMainViewport().Pos; + var screenSize = ImGui.GetMainViewport().Size; + + var windowSize = ImGui.CalcTextSize(actorText); + + // Add some extra safety padding + windowSize.X += ImGui.GetStyle().WindowPadding.X + 10; + windowSize.Y += ImGui.GetStyle().WindowPadding.Y + 10; + + if (screenCoords.X + windowSize.X > screenPos.X + screenSize.X || + screenCoords.Y + windowSize.Y > screenPos.Y + screenSize.Y) + continue; if (actor.YalmDistanceX > this.maxActorDrawDistance) continue; - ImGui.SetNextWindowBgAlpha( - Math.Max(1f - (actor.YalmDistanceX / this.maxActorDrawDistance), 0.2f)); - if (ImGui.Begin("Actor" + i, + ImGui.SetNextWindowPos(new Vector2(screenCoords.X, screenCoords.Y)); + + ImGui.SetNextWindowBgAlpha(Math.Max(1f - (actor.YalmDistanceX / this.maxActorDrawDistance), 0.2f)); + if (ImGui.Begin($"Actor{i}##ActorWindow{i}", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.AlwaysAutoResize | - ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoMove | + ImGuiWindowFlags.NoSavedSettings | + ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoMouseInputs | - ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoNav)) { - ImGui.Text( - $"{actor.Address.ToInt64():X}:{actor.ActorId:X}[{i}] - {actor.ObjectKind} - {actor.Name}"); - ImGui.End(); + ImGuiWindowFlags.NoDocking | + ImGuiWindowFlags.NoFocusOnAppearing | + ImGuiWindowFlags.NoNav)) { + ImGui.Text(actorText); } - - ImGui.PopID(); + ImGui.End(); } } } diff --git a/Dalamud/Interface/InterfaceManager.cs b/Dalamud/Interface/InterfaceManager.cs index c195e5a36..45f11c2a9 100644 --- a/Dalamud/Interface/InterfaceManager.cs +++ b/Dalamud/Interface/InterfaceManager.cs @@ -165,7 +165,7 @@ namespace Dalamud.Interface // So... not great, but much better than constantly crashing on unload this.Disable(); System.Threading.Thread.Sleep(500); - + this.scene?.Dispose(); this.presentHook.Dispose(); this.resizeBuffersHook.Dispose(); @@ -228,6 +228,7 @@ namespace Dalamud.Interface private IntPtr PresentDetour(IntPtr swapChain, uint syncInterval, uint presentFlags) { if (this.scene == null) { + this.scene = new RawDX11Scene(swapChain); this.scene.ImGuiIniPath = Path.Combine(Path.GetDirectoryName(this.dalamud.StartInfo.ConfigurationPath), "dalamudUI.ini"); @@ -353,7 +354,12 @@ namespace Dalamud.Interface private IntPtr ResizeBuffersDetour(IntPtr swapChain, uint bufferCount, uint width, uint height, uint newFormat, uint swapChainFlags) { - Log.Verbose($"Calling resizebuffers {bufferCount} {width} {height} {newFormat} {swapChainFlags}"); + Log.Verbose($"Calling resizebuffers swap@{swapChain.ToInt64():X}{bufferCount} {width} {height} {newFormat} {swapChainFlags}"); + + // We have to ensure we're working with the main swapchain, + // as viewports might be resizing as well + if (swapChain != this.scene.SwapChain.NativePointer) + return resizeBuffersHook.Original(swapChain, bufferCount, width, height, newFormat, swapChainFlags); this.scene?.OnPreResize();