2D Polygon Collider editor with Gizmos

+ Added Handles class
This commit is contained in:
Simon Lübeß
2023-08-21 13:18:51 +02:00
parent 377997f903
commit d3c19dd8da
3 changed files with 80 additions and 3 deletions
@@ -77,6 +77,37 @@ namespace GlitchyEditor.EditWindows
ShowAddComponentButton(entity); ShowAddComponentButton(entity);
} }
public void DrawSceneGUI(Entity entity)
{
DrawComponenSceneGUI<PolygonCollider2DComponent>(entity, => ShowPolygonColliderSceneGUI);
}
static void ShowPolygonColliderSceneGUI(Entity entity, PolygonCollider2DComponent* collider)
{
for (int i < collider.VertexCount)
{
Matrix localToWorld = entity.Transform.WorldTransform * Matrix.Translation(collider.Offset.X, collider.Offset.Y, 0);
Matrix transform = Matrix.Translation(float3(collider.Vertices[i], 0)) * localToWorld;
if (Handles.ShowGizmo(ref transform, .TRANSLATE_X | .TRANSLATE_Y, true, id: (int32)i))
{
transform = localToWorld.Invert() * transform;
collider.Vertices[i] = transform.Translation.XY;
}
}
}
private static void DrawComponenSceneGUI<TComponent>(Entity entity, function void(Entity, TComponent*) showSceneGUI) where TComponent: struct, new
{
if (!entity.HasComponent<TComponent>())
return;
TComponent* component = entity.GetComponent<TComponent>();
showSceneGUI(entity, component);
}
private static void ShowComponentContextMenu<TComponent>(Entity entity, TComponent* component) where TComponent: struct, new private static void ShowComponentContextMenu<TComponent>(Entity entity, TComponent* component) where TComponent: struct, new
{ {
if (ImGui.Selectable("Remove Component")) if (ImGui.Selectable("Remove Component"))
@@ -321,6 +321,8 @@ namespace GlitchyEditor.EditWindows
var view = _editor.CurrentCamera.View; var view = _editor.CurrentCamera.View;
var projection = _editor.CurrentCamera.Projection; var projection = _editor.CurrentCamera.Projection;
Handles.SetViewProjection(view, projection);
if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0) if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0)
return false; return false;
@@ -337,11 +339,15 @@ namespace GlitchyEditor.EditWindows
parentView = parentTransformCmp.WorldTransform.Invert(); parentView = parentTransformCmp.WorldTransform.Invert();
} }
_editor.ComponentEditWindow.DrawSceneGUI(entity);
float3 snap = (float3)_snap; float3 snap = (float3)_snap;
if (_gizmoType.HasFlag(.ROTATE)) if (_gizmoType.HasFlag(.ROTATE))
snap = (float3)_angleSnap; snap = (float3)_angleSnap;
if (ImGuizmo.Manipulate((.)&view, (.)&projection, _gizmoType, _gizmoMode, (.)&worldTransform, null, _doSnap ? (.)&snap : null)) Handles.SetSnap(_doSnap ? (float3?)snap : null);
if (Handles.ShowGizmo(ref worldTransform, _gizmoType, _gizmoMode case .WORLD, id: 1337))
{ {
// TODO: Fix when parent is scaled // TODO: Fix when parent is scaled
// Seems to work fine for parent rotation and translation but scaled parent ruins everything // Seems to work fine for parent rotation and translation but scaled parent ruins everything
@@ -349,7 +355,7 @@ namespace GlitchyEditor.EditWindows
transformCmp.LocalTransform = parentView * worldTransform; transformCmp.LocalTransform = parentView * worldTransform;
} }
return ImGuizmo.IsUsing(); return Handles.UsedGizmo;
} }
} }
} }
+40
View File
@@ -0,0 +1,40 @@
using GlitchyEngine.Math;
using ImGuizmo;
using System;
namespace GlitchyEditor;
static class Handles
{
private static float3? _snap;
public static Matrix View {get; private set;}
public static Matrix Projection {get; private set;}
private static bool _usedGizmo;
public static bool UsedGizmo => _usedGizmo;
public static void SetViewProjection(Matrix view, Matrix projection)
{
View = view;
Projection = projection;
}
public static void SetSnap(float3? snap)
{
_snap = snap;
}
public static bool ShowGizmo(ref Matrix transform, ImGuizmo.OPERATION gizmoType, bool globalGizmo = true, int32? id = null)
{
if (id != null)
ImGuizmo.SetID(id.Value);
#unwarn
bool result = ImGuizmo.Manipulate((.)&View, (.)&Projection, gizmoType, globalGizmo ? .WORLD : .LOCAL, (.)&transform, null, _snap.HasValue ? (.)&_snap : null);
_usedGizmo |= ImGuizmo.IsUsing();
return result;
}
}