diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index faeeae8..8c9c7d9 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -77,6 +77,37 @@ namespace GlitchyEditor.EditWindows ShowAddComponentButton(entity); } + public void DrawSceneGUI(Entity entity) + { + DrawComponenSceneGUI(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(Entity entity, function void(Entity, TComponent*) showSceneGUI) where TComponent: struct, new + { + if (!entity.HasComponent()) + return; + + TComponent* component = entity.GetComponent(); + + showSceneGUI(entity, component); + } + private static void ShowComponentContextMenu(Entity entity, TComponent* component) where TComponent: struct, new { if (ImGui.Selectable("Remove Component")) diff --git a/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf b/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf index 584d52c..ee7e1bf 100644 --- a/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf +++ b/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf @@ -321,6 +321,8 @@ namespace GlitchyEditor.EditWindows var view = _editor.CurrentCamera.View; var projection = _editor.CurrentCamera.Projection; + Handles.SetViewProjection(view, projection); + if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0) return false; @@ -337,11 +339,15 @@ namespace GlitchyEditor.EditWindows parentView = parentTransformCmp.WorldTransform.Invert(); } + _editor.ComponentEditWindow.DrawSceneGUI(entity); + float3 snap = (float3)_snap; if (_gizmoType.HasFlag(.ROTATE)) 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 // 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; } - return ImGuizmo.IsUsing(); + return Handles.UsedGizmo; } } } diff --git a/GlitchyEditor/src/Handles.bf b/GlitchyEditor/src/Handles.bf new file mode 100644 index 0000000..e781e61 --- /dev/null +++ b/GlitchyEditor/src/Handles.bf @@ -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; + } +}