Skip to content

Commit

Permalink
using canvas in texture inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Feb 11, 2023
1 parent 995cd25 commit a77d76f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
4 changes: 2 additions & 2 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class Model : IDisposable
private BufferObject<Matrix4x4> _matrixVbo;
private VertexArrayObject<float, uint> _vao;

protected int VertexSize => _vertexAttributes.Where(x => x.Enabled).Sum(x => x.Size);
protected bool HasVertexColors => _vertexAttributes[(int) EAttribute.Colors].Enabled;
private readonly List<VertexAttribute> _vertexAttributes = new()
{
new VertexAttribute { Size = 1, Enabled = true }, // VertexIndex
Expand All @@ -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;
Expand Down
58 changes: 49 additions & 9 deletions FModel/Views/Snooper/Shading/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ public bool ImGuiTextures(Dictionary<string, Texture> 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))
Expand All @@ -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()
Expand Down
20 changes: 2 additions & 18 deletions FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a77d76f

Please sign in to comment.