From c0837cbf45b0fb5fed17727bb31c70c803c056e1 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Sat, 11 Nov 2023 17:47:35 +0900 Subject: [PATCH] (REMOVE) Add example in CorePlugin --- Dalamud.CorePlugin/PluginImpl.cs | 4 +- Dalamud.CorePlugin/PluginWindow.cs | 59 ++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/Dalamud.CorePlugin/PluginImpl.cs b/Dalamud.CorePlugin/PluginImpl.cs index ef99f6def6..f5ff92537f 100644 --- a/Dalamud.CorePlugin/PluginImpl.cs +++ b/Dalamud.CorePlugin/PluginImpl.cs @@ -56,7 +56,7 @@ public void Dispose() /// /// Dalamud plugin interface. /// Logging service. - public PluginImpl(DalamudPluginInterface pluginInterface, IPluginLog log) + public PluginImpl(DalamudPluginInterface pluginInterface, IDataManager dataManager, ITextureProvider textureProvider, IPluginLog log) { try { @@ -64,7 +64,7 @@ public PluginImpl(DalamudPluginInterface pluginInterface, IPluginLog log) this.Interface = pluginInterface; this.pluginLog = log; - this.windowSystem.AddWindow(new PluginWindow()); + this.windowSystem.AddWindow(new PluginWindow(pluginInterface.UiBuilder, dataManager, textureProvider)); this.Interface.UiBuilder.Draw += this.OnDraw; this.Interface.UiBuilder.OpenConfigUi += this.OnOpenConfigUi; diff --git a/Dalamud.CorePlugin/PluginWindow.cs b/Dalamud.CorePlugin/PluginWindow.cs index 27be82f41e..6430b04245 100644 --- a/Dalamud.CorePlugin/PluginWindow.cs +++ b/Dalamud.CorePlugin/PluginWindow.cs @@ -1,8 +1,14 @@ using System; -using System.Numerics; - +using System.IO; +using Dalamud.Interface; +using Dalamud.Interface.Internal; using Dalamud.Interface.Windowing; +using Dalamud.Plugin.Services; using ImGuiNET; +using Lumina.Data.Files; +using SharpDX; +using SharpDX.Direct3D11; +using Vector2 = System.Numerics.Vector2; namespace Dalamud.CorePlugin { @@ -11,21 +17,59 @@ namespace Dalamud.CorePlugin /// internal class PluginWindow : Window, IDisposable { + private readonly UiBuilder uiBuilder; + private readonly ITextureProvider textureProvider; + private readonly IDalamudTextureWrap tex; + private readonly IntPtr callbackId; + private readonly Device device; + private readonly DeviceContext deviceContext; + private readonly PixelShader pixelShader; + private readonly SamplerState fontSampler; + /// /// Initializes a new instance of the class. /// - public PluginWindow() + public PluginWindow(UiBuilder uiBuilder, IDataManager dataManager, ITextureProvider textureProvider) : base("CorePlugin") { + this.uiBuilder = uiBuilder; + this.textureProvider = textureProvider; this.IsOpen = true; this.Size = new Vector2(810, 520); this.SizeCondition = ImGuiCond.FirstUseEver; + + this.tex = this.textureProvider.GetTexture(dataManager.GetFile("chara/monster/m0361/obj/body/b0001/texture/v01_m0361b0001_n.tex")!); + this.callbackId = this.uiBuilder.AddImGuiDrawCmdUserCallback(this.DrawCmdUserCallback); + this.device = CppObject.FromPointer(uiBuilder.DeviceNativePointer); + this.deviceContext = CppObject.FromPointer(uiBuilder.DeviceContextNativePointer); + this.pixelShader = new PixelShader(this.device, File.ReadAllBytes(@"Z:\test.fxc")); + this.fontSampler = new SamplerState(this.device, new SamplerStateDescription + { + Filter = Filter.MinMagMipLinear, + AddressU = TextureAddressMode.Wrap, + AddressV = TextureAddressMode.Wrap, + AddressW = TextureAddressMode.Wrap, + MipLodBias = 0, + ComparisonFunction = Comparison.Always, + MinimumLod = 0, + MaximumLod = 0, + }); + } + + private void DrawCmdUserCallback(ImDrawDataPtr drawData, ImDrawCmdPtr drawCmd) + { + this.deviceContext.PixelShader.Set(this.pixelShader); + this.deviceContext.PixelShader.SetSampler(0, this.fontSampler); } /// public void Dispose() { + this.uiBuilder.RemoveImGuiDrawCmdUserCallback(this.DrawCmdUserCallback); + this.tex.Dispose(); + this.pixelShader.Dispose(); + this.fontSampler.Dispose(); } /// @@ -36,6 +80,15 @@ public override void OnOpen() /// public override void Draw() { + var drawList = ImGui.GetWindowDrawList(); + drawList.AddCallback(this.callbackId, nint.Zero); + ImGui.Image(this.tex.ImGuiHandle, new(512, 512), new(1, 0), new(2, 1)); + ImGui.SameLine(); + ImGui.Image(this.tex.ImGuiHandle, new(512, 512), new(2, 0), new(3, 1)); + ImGui.Image(this.tex.ImGuiHandle, new(512, 512), new(3, 0), new(4, 1)); + ImGui.SameLine(); + ImGui.Image(this.tex.ImGuiHandle, new(512, 512), new(4, 0), new(5, 1)); + drawList.AddCallback(this.uiBuilder.ImGuiResetDrawCmdUserCallback, nint.Zero); } } }