Better Transform component and edit gui

This commit is contained in:
Simon Lübeß
2022-04-09 12:35:36 +02:00
parent c7fde476e8
commit af37154071
18 changed files with 341 additions and 208 deletions
@@ -5,6 +5,8 @@ using GlitchyEngine.Math;
namespace GlitchyEditor.EditWindows
{
using internal GlitchyEngine.World.TransformComponent;
class ComponentEditWindow : EditorWindow
{
public const String s_WindowTitle = "Components";
@@ -77,117 +79,109 @@ namespace GlitchyEditor.EditWindows
}
}
private static bool Vector3Control(StringView label, ref Vector3 value, Vector3 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f)
{
bool changed = false;
ImGui.PushID(label);
defer ImGui.PopID();
ImGui.Columns(2);
defer ImGui.Columns(1);
ImGui.SetColumnWidth(0, columnWidth);
ImGui.TextUnformatted(label);
ImGui.NextColumn();
ImGui.PushMultiItemsWidths(3, ImGui.CalcItemWidth());
ImGui.PushStyleVar(.ItemSpacing, ImGui.Vec2.Zero);
defer ImGui.PopStyleVar();
float lineHeight = ImGui.GetFont().FontSize + ImGui.GetStyle().FramePadding.y * 2.0f;
ImGui.Vec2 buttonSize = .(lineHeight + 3.0f, lineHeight);
ImGui.PushStyleColor(.Button, ImGui.Color(230, 25, 45).Value);
ImGui.PushStyleColor(.ButtonHovered, ImGui.Color(150, 25, 45).Value);
ImGui.PushStyleColor(.ButtonActive, ImGui.Color(230, 120, 130).Value);
if (ImGui.Button("X", buttonSize))
{
value.X = resetValues.X;
changed = true;
}
ImGui.SameLine();
if (ImGui.DragFloat("##X", &value.X, dragSpeed))
changed = true;
ImGui.PopItemWidth();
ImGui.SameLine();
ImGui.PopStyleColor(3);
ImGui.PushStyleColor(.Button, ImGui.Color(50, 190, 15).Value);
ImGui.PushStyleColor(.ButtonHovered, ImGui.Color(50, 120, 15).Value);
ImGui.PushStyleColor(.ButtonActive, ImGui.Color(116, 190, 99).Value);
if (ImGui.Button("Y", buttonSize))
{
value.Y = resetValues.Y;
changed = true;
}
ImGui.SameLine();
if (ImGui.DragFloat("##Y", &value.Y, dragSpeed))
changed = true;
ImGui.PopItemWidth();
ImGui.SameLine();
ImGui.PopStyleColor(3);
ImGui.PushStyleColor(.Button, ImGui.Color(55, 55, 230).Value);
ImGui.PushStyleColor(.ButtonHovered, ImGui.Color(55, 55, 150).Value);
ImGui.PushStyleColor(.ButtonActive, ImGui.Color(90, 90, 230).Value);
if (ImGui.Button("Z", buttonSize))
{
value.Z = resetValues.Z;
changed = true;
}
ImGui.SameLine();
if (ImGui.DragFloat("##Z", &value.Z, dragSpeed))
changed = true;
ImGui.PopItemWidth();
ImGui.PopStyleColor(3);
return changed;
}
private static void ShowTransformComponentEditor(Entity entity)
{
if (!entity.HasComponent<SimpleTransformComponent>())
if (!entity.HasComponent<TransformComponent>())
return;
var transform = entity.GetComponent<SimpleTransformComponent>();
var transform = entity.GetComponent<TransformComponent>();
if(ImGui.TreeNodeEx("Transform", .DefaultOpen))
{
bool ShowValue(String text, ref float value, String id)
{
ImGui.Text(text);
ImGui.SameLine();
ImGui.PushID(id);
bool valueChanged = ImGui.DragFloat(String.Empty, &value, 0.1f);
ImGui.PopID();
return valueChanged;
}
bool ShowTableRow(String name, ref Vector3 value)
{
bool changed = false;
ImGui.TableNextColumn();
ImGui.Text(name);
ImGui.TableNextColumn();
changed |= ShowValue("X: ", ref value.X, scope $"{name}X");
ImGui.TableNextColumn();
changed |= ShowValue("Y: ", ref value.Y, scope $"{name}Y");
ImGui.TableNextColumn();
changed |= ShowValue("Z: ", ref value.Z, scope $"{name}Z");
ImGui.TableNextRow();
return changed;
}
Matrix.Decompose(transform.Transform, var position, var rotation, var scale);
ImGui.BeginTable("posRotScaleTable", 4);
if (ShowTableRow("Position", ref position))
{
transform.Transform.Translation = position;
}
var oldScale = scale;
if (ShowTableRow("Scale", ref scale))
{
Vector3 v = 1.0f / oldScale * scale;
transform.Transform.Scale = (Vector3)transform.Transform.Scale * v;
}
ImGui.EndTable();
/*
Vector3 position = transform.Position;
bool positionChanged = false;
if (Vector3Control("Position", ref position))
transform.Position = position;
Vector3 rotationEuler = MathHelper.ToDegrees(transform.EditorRotationEuler);
if (Vector3Control("Rotation", ref rotationEuler))
transform.EditorRotationEuler = MathHelper.ToRadians(rotationEuler);
Vector3 rotationEuler = MathHelper.ToDegrees(component.RotationEuler);
bool rotationChanged = false;
Vector3 scale = component.Scale;
bool scaleChanged = false;
Vector3 scale = transform.Scale;
if (Vector3Control("Scale", ref scale, .One))
transform.Scale = scale;
void ShowValue(String text, ref float value, ref bool valueChanged, String id)
{
ImGui.Text(text);
ImGui.SameLine();
ImGui.PushID(id);
valueChanged |= ImGui.DragFloat(String.Empty, &value, 0.1f);
ImGui.PopID();
}
void ShowTableRow(String name, ref Vector3 value, ref bool valueChanged)
{
ImGui.TableNextColumn();
ImGui.Text(name);
ImGui.TableNextColumn();
ShowValue("X: ", ref value.X, ref valueChanged, scope $"{name}X");
ImGui.TableNextColumn();
ShowValue("Y: ", ref value.Y, ref valueChanged, scope $"{name}Y");
ImGui.TableNextColumn();
ShowValue("Z: ", ref value.Z, ref valueChanged, scope $"{name}Z");
ImGui.TableNextRow();
}
ImGui.BeginTable("posRotScaleTable", 4);
ShowTableRow("Position", ref position, ref positionChanged);
ShowTableRow("Rotation", ref rotationEuler, ref rotationChanged);
ShowTableRow("Scale", ref scale, ref scaleChanged);
ImGui.EndTable();
if(positionChanged)
component.Position = position;
if(rotationChanged)
component.RotationEuler = MathHelper.ToRadians(rotationEuler);
if(scaleChanged)
component.Scale = scale;
*/
ImGui.TreePop();
}
}
@@ -55,7 +55,7 @@ namespace GlitchyEditor.EditWindows
for(var selectedEntity in _selectedEntities)
{
var transformComponent = selectedEntity.GetComponent<SimpleTransformComponent>();
var transformComponent = selectedEntity.GetComponent<TransformComponent>();
if(parent == .InvalidEntity)
{
@@ -73,7 +73,7 @@ namespace GlitchyEditor.EditWindows
/// Finds all children of the given entity and stores their IDs in the given list.
internal void FindChildren(EcsEntity entity, List<EcsEntity> entities)
{
for(var (child, childTransform) in _scene.[Friend]_ecsWorld.Enumerate<SimpleTransformComponent>())
for(var (child, childTransform) in _scene.[Friend]_ecsWorld.Enumerate<TransformComponent>())
{
if(childTransform.Parent == entity)
{
@@ -124,7 +124,7 @@ namespace GlitchyEditor.EditWindows
{
var newEntity = _scene.CreateEntity();
var transformCmp = newEntity.GetComponent<SimpleTransformComponent>();
var transformCmp = newEntity.GetComponent<TransformComponent>();
// Last entity in list is the entity that has been selected last.
transformCmp.Parent = _selectedEntities.Back.Handle;
}
@@ -134,7 +134,7 @@ namespace GlitchyEditor.EditWindows
if(ImGui.MenuItem("Empty Parent", null, false, !_selectedEntities.IsEmpty && AllSelectionsOnSameLevel()))
{
var commonParent = _selectedEntities.Front.GetComponent<SimpleTransformComponent>();
var commonParent = _selectedEntities.Front.GetComponent<TransformComponent>();
var newEntity = _scene.CreateEntity();
@@ -142,14 +142,14 @@ namespace GlitchyEditor.EditWindows
{
// parent of selected entities is parent of the new entity.
// (which is why this doesn't work if the entities don't have the same parent)
var newEntityTransform = newEntity.GetComponent<SimpleTransformComponent>();
var newEntityTransform = newEntity.GetComponent<TransformComponent>();
newEntityTransform.Parent = commonParent.Parent;
}
// new entity is parent of all selected entities.
for(var selectedEntity in _selectedEntities)
{
var selectedTransform = selectedEntity.GetComponent<SimpleTransformComponent>();
var selectedTransform = selectedEntity.GetComponent<TransformComponent>();
selectedTransform.Parent = newEntity.Handle;
}
}
@@ -163,7 +163,8 @@ namespace GlitchyEditor.EditWindows
if(ImGui.IsItemHovered())
ImGui.SetTooltip("Create a new Entity.");
if(ImGui.MenuItem("Delete", null, false, !_selectedEntities.IsEmpty) || Input.IsKeyPressed(.Delete))
if(ImGui.MenuItem("Delete", null, false, !_selectedEntities.IsEmpty) ||
(Input.IsKeyPressed(.Delete) && ImGui.IsWindowHovered()))
{
DeleteSelectedEntities();
}
@@ -232,7 +233,7 @@ namespace GlitchyEditor.EditWindows
// make sure the dropped entity is not a parent of the entity we dropped it on.
while(true)
{
var parentTransform = walker.GetComponent<SimpleTransformComponent>();
var parentTransform = walker.GetComponent<TransformComponent>();
if(parentTransform.Parent == .InvalidEntity)
{
@@ -250,7 +251,7 @@ namespace GlitchyEditor.EditWindows
if(dropLegal)
{
var movedEntityTransform = movedEntity.GetComponent<SimpleTransformComponent>();
var movedEntityTransform = movedEntity.GetComponent<TransformComponent>();
movedEntityTransform.Parent = tree.Value.Handle;
}
}
@@ -302,7 +303,7 @@ namespace GlitchyEditor.EditWindows
TreeNode<Entity> InsertIntoTree(Entity entity)
{
var transform = entity.GetComponent<SimpleTransformComponent>();
var transform = entity.GetComponent<TransformComponent>();
if(transform == null)
{
@@ -342,7 +343,7 @@ namespace GlitchyEditor.EditWindows
Entity movedEntity = *(Entity*)payload.Data;
// Also mark transform as dirty
var transformComponent = movedEntity.GetComponent<SimpleTransformComponent>();
var transformComponent = movedEntity.GetComponent<TransformComponent>();
transformComponent.Parent = .InvalidEntity;
//transformComponent?.IsDirty = true;
}
@@ -10,7 +10,7 @@ namespace GlitchyEditor.EditWindows
{
class SceneViewportWindow : EditorWindow
{
public OldCamera _camera;
//public OldCamera _camera;
public const String s_WindowTitle = "Scene";
@@ -38,6 +38,8 @@ namespace GlitchyEditor.EditWindows
private ImGui.Vec2 oldViewportSize;
private bool viewPortChanged;
public Entity CameraEntity { get; set; }
protected override void InternalShow()
{
ImGui.PushStyleVar(.WindowPadding, ImGui.Vec2(1, 1));
@@ -87,8 +89,11 @@ namespace GlitchyEditor.EditWindows
topLeft.y += cntMin.y;
ImGuizmo.SetRect(topLeft.x, topLeft.y, viewportSize.x, viewportSize.y);
var view = _camera.View;
var projection = _camera.Projection;
var cameraTransformCmp = CameraEntity.GetComponent<TransformComponent>();
var view = cameraTransformCmp.WorldTransform.Invert();
var cameraCmp = CameraEntity.GetComponent<CameraComponent>();
var projection = cameraCmp.Camera.Projection;
Matrix mat = .Identity;
ImGuizmo.DrawGrid((.)&view, (.)&projection, (.)&mat, 10);
@@ -0,0 +1,63 @@
using GlitchyEngine;
using GlitchyEngine.Math;
using GlitchyEngine.World;
namespace GlitchyEditor
{
class EditorCameraController : ScriptableEntity
{
private float _cameraTranslationSpeed = 2.0f;
private float _cameraRotationSpeedX = 0.001f;
private float _cameraRotationSpeedY = 0.001f;
public bool IsEnabled = false;
protected override void OnUpdate(GameTime gt)
{
if (!IsEnabled)
return;
Debug.Profiler.ProfileFunction!();
Vector3 movement = .();
if(Input.IsKeyPressed(Key.W))
movement.Z += 1;
if(Input.IsKeyPressed(Key.S))
movement.Z -= 1;
if(Input.IsKeyPressed(Key.A))
movement.X -= 1;
if(Input.IsKeyPressed(Key.D))
movement.X += 1;
if(Input.IsKeyPressed(Key.Space))
movement.Y += 1;
if(Input.IsKeyPressed(Key.Control))
movement.Y -= 1;
var transformComponent = transform;
if(movement != .Zero)
{
movement.Normalize();
movement *= (float)(gt.FrameTime.TotalSeconds) * _cameraTranslationSpeed;
Matrix view = transformComponent.WorldTransform.Invert();
Vector4 delta = Vector4(movement, 1.0f) * view;
transformComponent.Position = transformComponent.Position + delta.XYZ;
}
// Camera rotation
var mouseDelta = Input.GetMouseMovement();
float rotY = mouseDelta.X * _cameraRotationSpeedX;
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
transformComponent.RotationEuler = transformComponent.RotationEuler + .(rotX, rotY, 0);
}
}
}
+24 -26
View File
@@ -25,8 +25,6 @@ namespace GlitchyEditor
Editor _editor ~ delete _;
PerspectiveCameraController _cameraController ~ delete _;
RenderTarget2D _viewportTarget ~ _?.ReleaseRef();
Entity _cameraEntity;
@@ -41,24 +39,28 @@ namespace GlitchyEditor
protected override void OnUpdate(GameTime gameTime)
{
var transformCmp = GetComponent<SimpleTransformComponent>();
var transformCmp = GetComponent<TransformComponent>();
Vector3 position = transformCmp.Position;
if (Input.IsKeyPressed(Key.A))
{
transformCmp.Transform.Translation.X -= gameTime.DeltaTime;
position.X -= gameTime.DeltaTime;
}
if (Input.IsKeyPressed(Key.D))
{
transformCmp.Transform.Translation.X += gameTime.DeltaTime;
position.X += gameTime.DeltaTime;
}
if (Input.IsKeyPressed(Key.W))
{
transformCmp.Transform.Translation.Y += gameTime.DeltaTime;
position.Y += gameTime.DeltaTime;
}
if (Input.IsKeyPressed(Key.S))
{
transformCmp.Transform.Translation.Y -= gameTime.DeltaTime;
position.Y -= gameTime.DeltaTime;
}
transformCmp.Position = position;
}
protected override void OnDestroy()
@@ -72,8 +74,6 @@ namespace GlitchyEditor
Application.Get().Window.IsVSync = false;
InitGraphics();
//InitEcs();
InitEditor();
{
_cameraEntity = _scene.CreateEntity("Camera Entity");
@@ -81,10 +81,10 @@ namespace GlitchyEditor
camera.Camera.SetPerspective(MathHelper.ToRadians(75), 0.1f, 10000.0f);
camera.Primary = true;
camera.FixedAspectRatio = false;
let transform = _cameraEntity.GetComponent<SimpleTransformComponent>();
transform.Transform = Matrix.Translation(0, 0, -5);
let transform = _cameraEntity.GetComponent<TransformComponent>();
transform.Position = .(0, 0, -5);
_cameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();
_cameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>();
_cameraEntity.AddComponent<EditorComponent>();
}
@@ -94,12 +94,14 @@ namespace GlitchyEditor
camera.Camera.SetPerspective(MathHelper.ToRadians(45), 0.1f, 1000.0f);
camera.Primary = false;
camera.FixedAspectRatio = false;
let transform = _otherCameraEntity.GetComponent<SimpleTransformComponent>();
transform.Transform = Matrix.Translation(0, 0, -5);
let transform = _otherCameraEntity.GetComponent<TransformComponent>();
transform.Position = .(0, 0, -5);
_otherCameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();
_otherCameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>();
_otherCameraEntity.AddComponent<EditorComponent>();
}
InitEditor();
}
private void InitGraphics()
@@ -129,19 +131,19 @@ namespace GlitchyEditor
_editor = new Editor(_scene);
_editor.SceneViewportWindow.ViewportSizeChangedEvent.Add(new (s, e) => ViewportSizeChanged(s, e));
_cameraController = new .(_context.SwapChain.AspectRatio);
_cameraController.CameraPosition = .(0, 0, -5);
_cameraController.TranslationSpeed = 10;
//_editor.[Friend]CreateEntityWithTransform();
_editor.SceneViewportWindow._camera = _cameraController.Camera;
_editor.SceneViewportWindow.CameraEntity = _cameraEntity;
}
public override void Update(GameTime gameTime)
{
if(_editor.SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton))
_cameraController.Update(gameTime);
var scriptComponent = _cameraEntity.GetComponent<NativeScriptComponent>();
if (var camController = scriptComponent.Instance as EditorCameraController)
{
camController.IsEnabled = (_editor.SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton));
}
//TransformSystem.Update(_world);
@@ -173,8 +175,6 @@ namespace GlitchyEditor
public override void OnEvent(Event event)
{
_cameraController.OnEvent(event);
EventDispatcher dispatcher = EventDispatcher(event);
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
@@ -257,8 +257,6 @@ namespace GlitchyEditor
_viewportTarget.Resize(sizeX, sizeY);
_cameraController.AspectRatio = (float)sizeX / (float)sizeY;
_scene.OnViewportResize(sizeX, sizeY);
}
}