Skip to content

Commit

Permalink
preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Sep 20, 2022
1 parent 656e190 commit c98634d
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CUE4Parse
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Camera(Vector3 position, Vector3 direction, float near, float far, float
public void ModifyZoom(float zoomAmount)
{
//We don't want to be able to zoom in too close or too far away so clamp to these values
Zoom = Math.Clamp(Zoom - zoomAmount, 1.0f, 60f);
Zoom = Math.Clamp(Zoom - zoomAmount, 1.0f, 89f);
}

public void ModifyDirection(float xOffset, float yOffset)
Expand Down
7 changes: 3 additions & 4 deletions FModel/Views/Snooper/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class Model : IDisposable
public bool IsSavable;
public bool DisplayVertexColors;
public bool DisplayBones;
public int SelectedMorph;
public float MorphTime;

protected Model(UObject owner, string name, string type)
Expand Down Expand Up @@ -149,10 +148,10 @@ public void UpdateMatrix(int index)
_matrixVbo.Unbind();
}

public void UpdateMorph()
public void UpdateMorph(int index)
{
_morphVbo.Bind();
_morphVbo.Update(Morphs[SelectedMorph].Vertices);
_morphVbo.Update(Morphs[index].Vertices);
_morphVbo.Unbind();
}

Expand Down Expand Up @@ -188,7 +187,7 @@ public void Setup(GL gl)
for (uint morph = 0; morph < Morphs.Length; morph++)
{
Morphs[morph].Setup(gl);
if (morph == SelectedMorph)
if (morph == 0)
_morphVbo = new BufferObject<float>(_gl, Morphs[morph].Vertices, BufferTargetARB.ArrayBuffer);
}
_vao.Bind();
Expand Down
52 changes: 52 additions & 0 deletions FModel/Views/Snooper/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using CUE4Parse.UE4.Objects.Core.Misc;

namespace FModel.Views.Snooper;

public class Options
{
public FGuid SelectedModel { get; private set; }
public int SelectedModelInstance;

public int SelectedSection { get; private set; }
public int SelectedMorph { get; private set; }

public Options()
{
Reset();
}

public void Reset()
{
SelectedModel = Guid.Empty;
SelectedModelInstance = 0;
SelectedSection = 0;
SelectedMorph = 0;
}

public void SelectModel(FGuid guid)
{
SelectedModel = guid;
SelectedModelInstance = 0;
SelectedSection = 0;
SelectedMorph = 0;
}

public void SelectSection(int index)
{
SelectedSection = index;
}

public void SelectMorph(int index, Model model)
{
SelectedMorph = index;
model.UpdateMorph(SelectedMorph);
}

public bool TryGetSection(Model model, out Section section)
{
if (SelectedSection >= 0 && SelectedSection < model.Sections.Length)
section = model.Sections[SelectedSection]; else section = null;
return section != null;
}
}
98 changes: 45 additions & 53 deletions FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public class SnimGui : IDisposable
private readonly Vector2 _textureSize;
private readonly Vector2 _texturePosition;
private bool _viewportFocus;
private FGuid _selectedModel;
private int _selectedInstance;
private int _selectedSection;

private const ImGuiWindowFlags _noResize = ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove; // delete once we have a proper docking branch
private const ImGuiCond _firstUse = ImGuiCond.Appearing; // switch to FirstUseEver once the docking branch will not be useful anymore...
Expand All @@ -59,23 +56,18 @@ public SnimGui(GL gl, IWindow window, IInputContext input)
_viewportPosition = new Vector2(0, titleBarHeight);
_textureSize = _viewportSize with { Y = viewport.WorkSize.Y - _viewportSize.Y - titleBarHeight };
_texturePosition = new Vector2(0, _viewportPosition.Y + _viewportSize.Y);
_selectedModel = new FGuid();
_selectedInstance = 0;
_selectedSection = 0;

Theme(style);
}

public void Increment(FGuid guid) => _selectedModel = guid;

public void Construct(Vector2D<int> size, FramebufferObject framebuffer, Camera camera, IMouse mouse, IDictionary<FGuid, Model> models)
public void Construct(ref Options options, Vector2D<int> size, FramebufferObject framebuffer, Camera camera, IMouse mouse, IDictionary<FGuid, Model> models)
{
DrawDockSpace(size);
DrawNavbar();

DrawOuliner(camera, models);
DrawProperties(camera, models);
DrawTextures(models);
DrawOuliner(ref options, camera, models);
DrawProperties(ref options, camera, models);
DrawTextures(ref options, models);
Draw3DViewport(framebuffer, camera, mouse);
}

Expand Down Expand Up @@ -126,7 +118,7 @@ private void DrawNavbar()
ImGui.EndMainMenuBar();
}

private void DrawOuliner(Camera camera, IDictionary<FGuid, Model> models)
private void DrawOuliner(ref Options options, Camera camera, IDictionary<FGuid, Model> models)
{
ImGui.SetNextWindowSize(_outlinerSize, _firstUse);
ImGui.SetNextWindowPos(_outlinerPosition, _firstUse);
Expand All @@ -144,20 +136,19 @@ private void DrawOuliner(Camera camera, IDictionary<FGuid, Model> models)
foreach (var (guid, model) in models)
{
ImGui.PushID(i);
model.IsSelected = _selectedModel == guid;
model.IsSelected = options.SelectedModel == guid;
if (ImGui.Selectable(model.Name, model.IsSelected))
{
_selectedModel = guid;
_selectedInstance = 0;
_selectedSection = 0;
options.SelectModel(guid);
}
if (ImGui.BeginPopupContextItem())
{
options.SelectModel(guid);
if (ImGui.Selectable("Deselect"))
_selectedModel = Guid.Empty;
options.SelectModel(Guid.Empty);
if (ImGui.Selectable("Delete"))
models.Remove(guid);
if (ImGui.Selectable("Copy to Clipboard"))
if (ImGui.Selectable("Copy Name to Clipboard"))
Application.Current.Dispatcher.Invoke(delegate
{
Clipboard.SetText(model.Name);
Expand Down Expand Up @@ -187,19 +178,19 @@ private void DrawOuliner(Camera camera, IDictionary<FGuid, Model> models)
ImGui.End();
}

private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
private void DrawProperties(ref Options options, Camera camera, IDictionary<FGuid, Model> models)
{
ImGui.SetNextWindowSize(_propertiesSize, _firstUse);
ImGui.SetNextWindowPos(_propertiesPosition, _firstUse);
ImGui.Begin("Properties", _noResize | ImGuiWindowFlags.NoCollapse);
if (!models.TryGetValue(_selectedModel, out var model))
if (!models.TryGetValue(options.SelectedModel, out var model))
return;

ImGui.Text($"Entity: ({model.Type}) {model.Name}");
ImGui.Text($"Guid: {_selectedModel.ToString(EGuidFormats.UniqueObjectGuid)}");
ImGui.Text($"Guid: {options.SelectedModel.ToString(EGuidFormats.UniqueObjectGuid)}");
PushStyleCompact();
ImGui.Columns(4, "Actions", false);
if (ImGui.Button("Go To")) camera.Position = model.Transforms[_selectedInstance].Position;
if (ImGui.Button("Go To")) camera.Position = model.Transforms[options.SelectedModelInstance].Position;
ImGui.NextColumn(); ImGui.Checkbox("Show", ref model.Show);
ImGui.NextColumn(); ImGui.BeginDisabled(!model.HasVertexColors); ImGui.Checkbox("Colors", ref model.DisplayVertexColors); ImGui.EndDisabled();
ImGui.NextColumn(); ImGui.BeginDisabled(!model.HasBones); ImGui.Checkbox("Bones", ref model.DisplayBones); ImGui.EndDisabled();
Expand All @@ -219,21 +210,21 @@ private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
PushStyleCompact();
ImGui.PushID(0); ImGui.BeginDisabled(model.TransformsCount < 2);
ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X);
ImGui.SliderInt("", ref _selectedInstance, 0, model.TransformsCount - 1, "Instance %i", ImGuiSliderFlags.AlwaysClamp);
ImGui.SliderInt("", ref options.SelectedModelInstance, 0, model.TransformsCount - 1, "Instance %i", ImGuiSliderFlags.AlwaysClamp);
ImGui.EndDisabled(); ImGui.PopID();

ImGui.SetNextItemOpen(true, ImGuiCond.Appearing);
if (ImGui.TreeNode("Location"))
{
ImGui.PushID(1);
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("X", ref model.Transforms[_selectedInstance].Position.X, speed, 0f, 0f, "%.2f m");
ImGui.DragFloat("X", ref model.Transforms[options.SelectedModelInstance].Position.X, speed, 0f, 0f, "%.2f m");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Y", ref model.Transforms[_selectedInstance].Position.Y, speed, 0f, 0f, "%.2f m");
ImGui.DragFloat("Y", ref model.Transforms[options.SelectedModelInstance].Position.Y, speed, 0f, 0f, "%.2f m");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Z", ref model.Transforms[_selectedInstance].Position.Z, speed, 0f, 0f, "%.2f m");
ImGui.DragFloat("Z", ref model.Transforms[options.SelectedModelInstance].Position.Z, speed, 0f, 0f, "%.2f m");

ImGui.PopID();
ImGui.TreePop();
Expand All @@ -244,13 +235,13 @@ private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
{
ImGui.PushID(2);
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("X", ref model.Transforms[_selectedInstance].Rotation.Pitch, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("X", ref model.Transforms[options.SelectedModelInstance].Rotation.Pitch, .5f, 0f, 0f, "%.1f°");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Y", ref model.Transforms[_selectedInstance].Rotation.Roll, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("Y", ref model.Transforms[options.SelectedModelInstance].Rotation.Roll, .5f, 0f, 0f, "%.1f°");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Z", ref model.Transforms[_selectedInstance].Rotation.Yaw, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("Z", ref model.Transforms[options.SelectedModelInstance].Rotation.Yaw, .5f, 0f, 0f, "%.1f°");

ImGui.PopID();
ImGui.TreePop();
Expand All @@ -261,19 +252,19 @@ private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
{
ImGui.PushID(3);
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("X", ref model.Transforms[_selectedInstance].Scale.X, speed, 0f, 0f, "%.3f");
ImGui.DragFloat("X", ref model.Transforms[options.SelectedModelInstance].Scale.X, speed, 0f, 0f, "%.3f");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Y", ref model.Transforms[_selectedInstance].Scale.Y, speed, 0f, 0f, "%.3f");
ImGui.DragFloat("Y", ref model.Transforms[options.SelectedModelInstance].Scale.Y, speed, 0f, 0f, "%.3f");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Z", ref model.Transforms[_selectedInstance].Scale.Z, speed, 0f, 0f, "%.3f");
ImGui.DragFloat("Z", ref model.Transforms[options.SelectedModelInstance].Scale.Z, speed, 0f, 0f, "%.3f");

ImGui.PopID();
ImGui.TreePop();
}

model.UpdateMatrix(_selectedInstance);
model.UpdateMatrix(options.SelectedModelInstance);
PopStyleCompact();
ImGui.EndTabItem();
}
Expand All @@ -296,14 +287,20 @@ private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
ImGui.TableSetBgColor(ImGuiTableBgTarget.RowBg0, ImGui.GetColorU32(new Vector4(1, 0, 0, .5f)));
ImGui.Text(section.Index.ToString("D"));
ImGui.TableNextColumn();
if (ImGui.Selectable(section.Name, _selectedSection == i, ImGuiSelectableFlags.SpanAllColumns))
_selectedSection = i;
if (ImGui.Selectable(section.Name, options.SelectedSection == i, ImGuiSelectableFlags.SpanAllColumns))
options.SelectSection(i);
if (ImGui.BeginPopupContextItem())
{
options.SelectSection(i);
if (ImGui.Selectable("Swap"))
{

}
if (ImGui.Selectable("Copy Name to Clipboard"))
Application.Current.Dispatcher.Invoke(delegate
{
Clipboard.SetText(section.Name);
});
ImGui.EndPopup();
}
ImGui.PopID();
Expand All @@ -321,11 +318,8 @@ private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
for (int i = 0; i < model.Morphs.Length; i++)
{
ImGui.PushID(i);
if (ImGui.Selectable(model.Morphs[i].Name, model.SelectedMorph == i))
{
model.SelectedMorph = i;
model.UpdateMorph();
}
if (ImGui.Selectable(model.Morphs[i].Name, options.SelectedMorph == i))
options.SelectMorph(i, model);
ImGui.PopID();
}
ImGui.EndListBox();
Expand All @@ -340,15 +334,15 @@ private void DrawProperties(Camera camera, IDictionary<FGuid, Model> models)
ImGui.End();
}

private void DrawTextures(IDictionary<FGuid, Model> models)
private void DrawTextures(ref Options options, IDictionary<FGuid, Model> models)
{
ImGui.SetNextWindowSize(_textureSize, _firstUse);
ImGui.SetNextWindowPos(_texturePosition, _firstUse);
ImGui.Begin("Textures", _noResize | ImGuiWindowFlags.NoCollapse);
if (!models.TryGetValue(_selectedModel, out var model))
if (!models.TryGetValue(options.SelectedModel, out var model) ||
!options.TryGetSection(model, out var section))
return;

var section = model.Sections[_selectedSection];
PushStyleCompact(); ImGui.BeginGroup();
ImGui.Checkbox("Show", ref section.Show);
ImGui.Checkbox("Wireframe", ref section.Wireframe);
Expand Down Expand Up @@ -405,7 +399,7 @@ private void DrawTexture(Texture texture, string label)
Application.Current.Dispatcher.Invoke(delegate
{
Clipboard.SetText(Utils.FixPath(texture.Path));
texture.Label = "(?) Copied to Clipboard";
texture.Label = "(?) Path Copied to Clipboard";
});
}

Expand Down Expand Up @@ -451,26 +445,24 @@ private void Draw3DViewport(FramebufferObject framebuffer, Camera camera, IMouse
var io = ImGui.GetIO();
if (ImGui.IsItemHovered())
{
camera.ModifyZoom(io.MouseWheel);

// if left button down while mouse is hover viewport
if (ImGui.IsMouseDown(ImGuiMouseButton.Left) && !_viewportFocus)
// if right button down while mouse is hover viewport
if (ImGui.IsMouseDown(ImGuiMouseButton.Right) && !_viewportFocus)
_viewportFocus = true;
}

// this can't be inside IsItemHovered! read it as
// if left mouse button was pressed while hovering the viewport
// move camera until left mouse button is released
// if right mouse button was pressed while hovering the viewport
// move camera until right mouse button is released
// no matter where mouse position end up
if (ImGui.IsMouseDragging(ImGuiMouseButton.Left, lookSensitivity) && _viewportFocus)
if (ImGui.IsMouseDragging(ImGuiMouseButton.Right, lookSensitivity) && _viewportFocus)
{
var delta = io.MouseDelta * lookSensitivity;
camera.ModifyDirection(delta.X, delta.Y);
mouse.Cursor.CursorMode = CursorMode.Raw;
}

// if left button up and mouse was in viewport
if (ImGui.IsMouseReleased(ImGuiMouseButton.Left) && _viewportFocus)
if (ImGui.IsMouseReleased(ImGuiMouseButton.Right) && _viewportFocus)
{
_viewportFocus = false;
mouse.Cursor.CursorMode = CursorMode.Normal;
Expand Down
Loading

0 comments on commit c98634d

Please sign in to comment.