Removed redundant Point struct and SceneViewport improvements

- Keyboard shortcuts for Gizmo control
- Improved mousepick-blocking
This commit is contained in:
Simon Lübeß
2022-06-02 12:17:57 +02:00
parent e41645e12a
commit a80018e80c
10 changed files with 136 additions and 214 deletions
@@ -31,7 +31,7 @@ namespace GlitchyEditor.EditWindows
public void SetContext(Scene scene)
{
_selectedEntities.Clear();
ClearEntitySelection();
_scene = scene;
}
@@ -45,6 +45,36 @@ namespace GlitchyEditor.EditWindows
_selectedEntities.Add();
}*/
/// Deselects all entities.
public void ClearEntitySelection()
{
_selectedEntities.Clear();
}
/// Selects the given entity.
/// @param entity The entity to select.
/// @param clearOldSelection If true the previously selected entities will be deselected. If false, the given entity will be added to the current selection.
public void SelectEntity(Entity entity, bool clearOldSelection = false)
{
if (clearOldSelection)
ClearEntitySelection();
_selectedEntities.Add(entity);
}
/// Deselects the given entity.
/// @param entity The entity to deselect.
public bool DeselectEntity(Entity entity)
{
return _selectedEntities.Remove(entity);
}
/// Returns whether or not the given entity is currently selected.
public bool IsEntitySelected(Entity entity)
{
return _selectedEntities.Contains(entity);
}
protected override void InternalShow()
{
if(!ImGui.Begin(s_WindowTitle, &_open, .MenuBar))
@@ -64,25 +94,24 @@ namespace GlitchyEditor.EditWindows
if (_editor.SceneViewportWindow.SelectionChanged)
{
if (Input.IsKeyReleased(.Control))
{
_selectedEntities.Clear();
}
var handle = _scene.[Friend]_ecsWorld.GetCurrentVersion(EcsEntity.[Friend]CreateEntityID(_editor.SceneViewportWindow.SelectedEntityId, 0));
if (handle case .Ok(let h))
{
Entity e = .(h, _scene);
_selectedEntities.Add(e);
SelectEntity(e, Input.IsKeyReleased(.Control));
}
else if (Input.IsKeyReleased(.Control))
{
ClearEntitySelection();
}
}
ShowEntityHierarchy();
if ((ImGui.IsMouseDown(.Left) || ImGui.IsMouseDown(.Right)) && !ImGui.IsAnyItemHovered() && !ImGui.GetIO().KeyCtrl && ImGui.IsWindowHovered(.AllowWhenBlockedByPopup))
_selectedEntities.Clear();
ClearEntitySelection();
ImGui.End();
}
@@ -288,7 +317,7 @@ namespace GlitchyEditor.EditWindows
if(tree.Children.Count == 0)
flags |= .Leaf;
bool inSelectedList = _selectedEntities.Contains(tree.Value);
bool inSelectedList = IsEntitySelected(tree.Value);
if(inSelectedList)
flags |= .Selected;
@@ -381,17 +410,12 @@ namespace GlitchyEditor.EditWindows
{
if (inSelectedList && !clickedRight)
{
_selectedEntities.Remove(tree.Value);
DeselectEntity(tree.Value);
inSelectedList = false;
}
else
{
if (!ImGui.GetIO().KeyCtrl && !clickedRight)
{
_selectedEntities.Clear();
}
_selectedEntities.Add(tree.Value);
SelectEntity(tree.Value, !ImGui.GetIO().KeyCtrl && !clickedRight);
inSelectedList = true;
}
}
@@ -62,39 +62,25 @@ namespace GlitchyEditor.EditWindows
_hasFocus = ImGui.IsWindowFocused();
if (_hasFocus && !_editor.CurrentCamera.InUse)
{
if (Input.IsKeyPressing(.Q))
_gizmoType = .TRANSLATE;
if (Input.IsKeyPressing(.W))
_gizmoType = .ROTATE;
if (Input.IsKeyPressing(.E))
_gizmoType = .SCALE;
if (Input.IsKeyPressing(.G))
_gizmoMode = .WORLD;
if (Input.IsKeyPressing(.L))
_gizmoMode = .LOCAL;
}
var viewportSize = ImGui.GetContentRegionAvail();
if (_editor.CurrentCamera.[Friend]BindMouse)
{
var mousePos = ImGui.GetMousePos();
var newMousePos = mousePos;
var winPos = ImGui.GetWindowPos();
var winSize = ImGui.GetWindowSize();
if (mousePos.x < winPos.x)
{
newMousePos.x = winPos.x + winSize.x - 1;
}
else if (mousePos.x > winPos.x + winSize.x)
{
newMousePos.x = winPos.x + 1;
}
if (mousePos.y < winPos.y)
{
newMousePos.y = winPos.y + winSize.y - 1;
}
else if (mousePos.y > winPos.y + winSize.y)
{
newMousePos.y = winPos.y + 1;
}
if (newMousePos != mousePos)
{
Input.SetMousePosition(Point((int32)newMousePos.x, (int32)newMousePos.y));
_editor.CurrentCamera.[Friend]MouseCooldown = 2;
}
}
WrapMouseInViewport();
if(_renderTarget != null)
{
@@ -102,18 +88,67 @@ namespace GlitchyEditor.EditWindows
//ImGui.Image(_editor.CurrentCamera.RenderTarget.GetViewBinding(0), viewportSize);
//ImGui.Image(_editor.CurrentScene.[Friend]_compositeTarget.GetViewBinding(0), viewportSize);
}
bool gizmoUsed = DrawImGuizmo(viewportSize);
MousePicking(viewportSize, gizmoUsed);
ImGui.End();
if(oldViewportSize != viewportSize)
{
ViewportSizeChangedEvent.Invoke(this, (Vector2)viewportSize);
viewPortChanged = true;
oldViewportSize = viewportSize;
}
}
/// Wraps the mouse, so that it always stays in the viewport.
private void WrapMouseInViewport()
{
var mousePos = (Vector2)ImGui.GetMousePos();
var winPos = (Vector2)ImGui.GetWindowPos();
var winSize = (Vector2)ImGui.GetWindowSize();
Vector2 newMousePos = mousePos;
if (mousePos.X < winPos.X)
{
newMousePos.X = winPos.X + winSize.X - 1;
}
else if (mousePos.X > winPos.X + winSize.X)
{
newMousePos.X = winPos.X + 1;
}
if (mousePos.Y < winPos.Y)
{
newMousePos.Y = winPos.Y + winSize.Y - 1;
}
else if (mousePos.Y > winPos.Y + winSize.Y)
{
newMousePos.Y = winPos.Y + 1;
}
if (newMousePos != mousePos)
{
Input.SetMousePosition((Int2)newMousePos);
_editor.CurrentCamera.[Friend]MouseCooldown = 2;
}
}
private void MousePicking(ImGui.Vec2 viewportSize, bool gizmoUsed)
{
Vector2 relativeMouse = (Vector2)ImGui.GetMousePos() - (Vector2)ImGui.GetItemRectMin();
int x = Renderer.[Friend]_gBuffer.Target.Width;
int y = Renderer.[Friend]_gBuffer.Target.Height;
int rtWidth = _editor.CurrentScene.[Friend]_compositeTarget.Width;
int rtHeight = _editor.CurrentScene.[Friend]_compositeTarget.Height;
if (!gizmoUsed && !_editor.CurrentCamera.[Friend]BindMouse && Input.IsMouseButtonPressing(.LeftButton) && relativeMouse.X >= 0 && relativeMouse.Y >= 0 &&
if (Input.IsMouseButtonPressing(.LeftButton) &&
ImGui.IsWindowHovered() && !gizmoUsed && !_editor.CurrentCamera.InUse &&
relativeMouse.X >= 0 && relativeMouse.Y >= 0 &&
relativeMouse.X < viewportSize.x && relativeMouse.Y < viewportSize.y &&
relativeMouse.X < x && relativeMouse.Y < y)
relativeMouse.X < rtWidth && relativeMouse.Y < rtHeight)
{
uint32 id = uint32.MaxValue;
@@ -128,15 +163,6 @@ namespace GlitchyEditor.EditWindows
{
SelectionChanged = false;
}
ImGui.End();
if(oldViewportSize != viewportSize)
{
ViewportSizeChangedEvent.Invoke(this, (Vector2)viewportSize);
viewPortChanged = true;
oldViewportSize = viewportSize;
}
}
private ImGuizmo.OPERATION _gizmoType = .TRANSLATE;
+1 -1
View File
@@ -158,7 +158,7 @@ namespace GlitchyEditor
if(_moving)
{
Point movement = Input.GetMouseMovement();
Int2 movement = Input.GetMouseMovement();
_position.X += movement.X;
_position.Y += movement.Y;