From a77d76ff34d1272653ada52ac0069b6a965f156d Mon Sep 17 00:00:00 2001 From: 4sval Date: Sat, 11 Feb 2023 23:57:42 +0100 Subject: [PATCH] using canvas in texture inspector --- FModel/Views/Snooper/Models/Model.cs | 4 +- FModel/Views/Snooper/Shading/Material.cs | 58 ++++++++++++++++++++---- FModel/Views/Snooper/SnimGui.cs | 20 +------- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/FModel/Views/Snooper/Models/Model.cs b/FModel/Views/Snooper/Models/Model.cs index 84df5065..55ed4f75 100644 --- a/FModel/Views/Snooper/Models/Model.cs +++ b/FModel/Views/Snooper/Models/Model.cs @@ -52,8 +52,6 @@ public class Model : IDisposable private BufferObject _matrixVbo; private VertexArrayObject _vao; - protected int VertexSize => _vertexAttributes.Where(x => x.Enabled).Sum(x => x.Size); - protected bool HasVertexColors => _vertexAttributes[(int) EAttribute.Colors].Enabled; private readonly List _vertexAttributes = new() { new VertexAttribute { Size = 1, Enabled = true }, // VertexIndex @@ -66,6 +64,8 @@ public class Model : IDisposable new VertexAttribute { Size = 4, Enabled = false }, // BoneIds new VertexAttribute { Size = 4, Enabled = false } // BoneWeights }; + public int VertexSize => _vertexAttributes.Where(x => x.Enabled).Sum(x => x.Size); + public bool HasVertexColors => _vertexAttributes[(int) EAttribute.Colors].Enabled; private const int _faceSize = 3; private readonly UObject _export; diff --git a/FModel/Views/Snooper/Shading/Material.cs b/FModel/Views/Snooper/Shading/Material.cs index e83b3d83..93778b97 100644 --- a/FModel/Views/Snooper/Shading/Material.cs +++ b/FModel/Views/Snooper/Shading/Material.cs @@ -336,7 +336,8 @@ public bool ImGuiTextures(Dictionary icons, Model model) return ImGui.IsItemHovered() && ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left); } - public Vector2[] ImGuiTextureInspector(Texture fallback) + private Vector3 _scrolling = new (0.0f, 0.0f, 1.0f); + public void ImGuiTextureInspector(Texture fallback) { var texture = GetSelectedTexture() ?? fallback; if (ImGui.BeginTable("texture_inspector", 2, ImGuiTableFlags.SizingStretchProp)) @@ -352,15 +353,54 @@ public Vector2[] ImGuiTextureInspector(Texture fallback) }); } - var largest = ImGui.GetContentRegionAvail(); - largest.X -= ImGui.GetScrollX(); - largest.Y -= ImGui.GetScrollY(); + var io = ImGui.GetIO(); + var canvasP0 = ImGui.GetCursorScreenPos(); + var canvasSize = ImGui.GetContentRegionAvail(); + if (canvasSize.X < 50.0f) canvasSize.X = 50.0f; + if (canvasSize.Y < 50.0f) canvasSize.Y = 50.0f; + var canvasP1 = new Vector2(canvasP0.X + canvasSize.X, canvasP0.Y + canvasSize.Y); + var origin = new Vector2(canvasP0.X + _scrolling.X, canvasP0.Y + _scrolling.Y); + var absoluteMiddle = canvasSize / 2.0f; + + ImGui.InvisibleButton("canvas", canvasSize, ImGuiButtonFlags.MouseButtonLeft | ImGuiButtonFlags.MouseButtonRight); + if (ImGui.IsItemActive() && ImGui.IsMouseDragging(ImGuiMouseButton.Left)) + { + _scrolling.X += io.MouseDelta.X; + _scrolling.Y += io.MouseDelta.Y; + } + else if (ImGui.IsItemHovered() && io.MouseWheel != 0.0f) + { + var zoomFactor = 1.0f + io.MouseWheel * 0.1f; + var mousePosCanvas = io.MousePos - origin; - var ratio = Math.Min(largest.X / texture.Width, largest.Y / texture.Height); - var size = new Vector2(texture.Width * ratio, texture.Height * ratio); - var pos = ImGui.GetCursorPos(); - ImGui.Image(texture.GetPointer(),size, Vector2.Zero, Vector2.One, Vector4.One, new Vector4(1.0f, 1.0f, 1.0f, 0.25f)); - return new[] { size, pos }; + _scrolling.X -= (mousePosCanvas.X - absoluteMiddle.X) * (zoomFactor - 1); + _scrolling.Y -= (mousePosCanvas.Y - absoluteMiddle.Y) * (zoomFactor - 1); + _scrolling.Z *= zoomFactor; + origin = new Vector2(canvasP0.X + _scrolling.X, canvasP0.Y + _scrolling.Y); + } + + var drawList = ImGui.GetWindowDrawList(); + drawList.AddRectFilled(canvasP0, canvasP1, 0xFF242424); + drawList.PushClipRect(canvasP0, canvasP1, true); + { + var sensitivity = _scrolling.Z * 25.0f; + for (float x = _scrolling.X % sensitivity; x < canvasSize.X; x += sensitivity) + drawList.AddLine(canvasP0 with { X = canvasP0.X + x }, canvasP1 with { X = canvasP0.X + x }, 0x28C8C8C8); + for (float y = _scrolling.Y % sensitivity; y < canvasSize.Y; y += sensitivity) + drawList.AddLine(canvasP0 with { Y = canvasP0.Y + y }, canvasP1 with { Y = canvasP0.Y + y }, 0x28C8C8C8); + } + drawList.PopClipRect(); + + drawList.PushClipRect(canvasP0, canvasP1, true); + { + var relativeMiddle = origin + absoluteMiddle; + var ratio = Math.Min(canvasSize.X / texture.Width, canvasSize.Y / texture.Height) * 0.95f * _scrolling.Z; + var size = new Vector2(texture.Width, texture.Height) * ratio / 2f; + + drawList.AddImage(texture.GetPointer(), relativeMiddle - size, relativeMiddle + size); + drawList.AddRect(relativeMiddle - size, relativeMiddle + size, 0xFFFFFFFF); + } + drawList.PopClipRect(); } private Texture GetSelectedTexture() diff --git a/FModel/Views/Snooper/SnimGui.cs b/FModel/Views/Snooper/SnimGui.cs index b64fd4f6..d3d737f1 100644 --- a/FModel/Views/Snooper/SnimGui.cs +++ b/FModel/Views/Snooper/SnimGui.cs @@ -48,7 +48,6 @@ public class SnimGui private readonly string _renderer; private readonly string _version; private bool _ti_open; - private bool _ti_overlayUv; private bool _viewportFocus; private readonly Vector4 _accentColor = new (0.125f, 0.42f, 0.831f, 1.0f); @@ -660,22 +659,7 @@ private void DrawTextureInspector(Snooper s) s.Renderer.Options.TryGetModel(out var model) && s.Renderer.Options.TryGetSection(model, out var section)) { - var vectors = model.Materials[section.MaterialIndex].ImGuiTextureInspector(s.Renderer.Options.Icons["noimage"]); - if (_ti_overlayUv) - { - var size = vectors[0]; - var drawList = ImGui.GetWindowDrawList(); - drawList.PushClipRect(size, size, true); - ImGui.SetCursorPos(vectors[1]); - ImGui.InvisibleButton("canvas", size, ImGuiButtonFlags.MouseButtonLeft | ImGuiButtonFlags.MouseButtonRight); - drawList.AddLine(new Vector2(0, 0), size, 255, 2f); - drawList.PopClipRect(); - } - Popup(() => - { - if (ImGui.MenuItem("Overlay UVs", null, _ti_overlayUv, false)) - _ti_overlayUv = !_ti_overlayUv; - }); + model.Materials[section.MaterialIndex].ImGuiTextureInspector(s.Renderer.Options.Icons["noimage"]); } ImGui.End(); // if window is collapsed } @@ -851,7 +835,7 @@ private void Theme() io.ConfigFlags |= ImGuiConfigFlags.DockingEnable; io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable; io.ConfigWindowsMoveFromTitleBarOnly = true; - io.ConfigDockingWithShift = true; + // io.ConfigDockingWithShift = true; doesn't work anymore?? style.WindowPadding = new Vector2(4f); style.FramePadding = new Vector2(3f);