diff --git a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf index 012f03b..b8d4268 100644 --- a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf +++ b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf @@ -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; } } diff --git a/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf b/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf index 186a7a0..33c2a5b 100644 --- a/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf +++ b/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf @@ -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; diff --git a/GlitchyEditor/src/TextureViewer.bf b/GlitchyEditor/src/TextureViewer.bf index 022234b..b0566e5 100644 --- a/GlitchyEditor/src/TextureViewer.bf +++ b/GlitchyEditor/src/TextureViewer.bf @@ -158,7 +158,7 @@ namespace GlitchyEditor if(_moving) { - Point movement = Input.GetMouseMovement(); + Int2 movement = Input.GetMouseMovement(); _position.X += movement.X; _position.Y += movement.Y; diff --git a/GlitchyEngine/src/Input.bf b/GlitchyEngine/src/Input.bf index ae466ee..2d3cf19 100644 --- a/GlitchyEngine/src/Input.bf +++ b/GlitchyEngine/src/Input.bf @@ -27,22 +27,22 @@ namespace GlitchyEngine public static extern bool IsMouseButtonPressed(MouseButton button); public static extern bool IsMouseButtonReleased(MouseButton button); - public static extern Point GetMousePosition(); + public static extern Int2 GetMousePosition(); public static extern int32 GetMouseX(); public static extern int32 GetMouseY(); // TODO: Should Input be able to set mousepos? - public static extern void SetMousePosition(Point pos); + public static extern void SetMousePosition(Int2 pos); public static extern bool WasMouseButtonPressed(MouseButton button); public static extern bool WasMouseButtonReleased(MouseButton button); - public static extern Point GetLastMousePosition(); + public static extern Int2 GetLastMousePosition(); public static extern int32 GetLastMouseX(); public static extern int32 GetLastMouseY(); public static extern bool IsMouseButtonPressing(MouseButton button); public static extern bool IsMouseButtonReleasing(MouseButton button); - public static extern Point GetMouseMovement(); + public static extern Int2 GetMouseMovement(); public static extern void NewFrame(); } diff --git a/GlitchyEngine/src/Math/Point.bf b/GlitchyEngine/src/Math/Point.bf deleted file mode 100644 index 91ae8fb..0000000 --- a/GlitchyEngine/src/Math/Point.bf +++ /dev/null @@ -1,131 +0,0 @@ -using System; - -namespace GlitchyEngine.Math -{ - - /** - * A 2D point represented by two 32bit integers. - */ - public struct Point - { - public int32 X, Y; - - /** - * Creates a new instance of a @Point with both components set to zero. - */ - public this() => this = default; - - /** - * Creates a new instance of a @Point with both components set to the specified value. - * @param value The value for both components. - */ - public this(int32 value) - { - X = value; - Y = value; - } - - /** - * Creates a new instance of a @Point. - * @param x The value for the x-component. - * @param y The value for the y-component. - */ - public this(int32 x, int32 y) - { - X = x; - Y = y; - } - - // - // Unary Operators - // - - public static Point operator +(Point value) => value; - - public static Point operator -(Point value) => .(-value.X, -value.Y); - - // - // Binary Operators - // - - public static Point operator +(Point left, Point right) => .(left.X + right.X, left.Y + right.Y); - public static Point operator +(int32 left, Point right) => .(left + right.X, left + right.Y); - public static Point operator +(Point left, int32 right) => .(left.X + right, left.Y + right); - - public static Point operator -(Point left, Point right) => .(left.X - right.X, left.Y - right.Y); - public static Point operator -(int32 left, Point right) => .(left - right.X, left - right.Y); - public static Point operator -(Point left, int32 right) => .(left.X - right, left.Y - right); - - public static Point operator *(Point left, Point right) => .(left.X * right.X, left.Y * right.Y); - public static Point operator *(int32 left, Point right) => .(left * right.X, left * right.Y); - public static Point operator *(Point left, int32 right) => .(left.X * right, left.Y * right); - - public static Point operator /(Point left, Point right) => .(left.X / right.X, left.Y / right.Y); - public static Point operator /(int32 left, Point right) => .(left / right.X, left / right.Y); - public static Point operator /(Point left, int32 right) => .(left.X / right, left.Y / right); - - // - // Assignment Operators - // - - public void operator +=(Point value) mut - { - X += value.X; - Y += value.Y; - } - - public void operator +=(int32 value) mut - { - X += value; - Y += value; - } - - public void operator -=(Point value) mut - { - X -= value.X; - Y -= value.Y; - } - - public void operator -=(int32 value) mut - { - X -= value; - Y -= value; - } - - public void operator *=(Point value) mut - { - X *= value.X; - Y *= value.Y; - } - - public void operator *=(int32 value) mut - { - X *= value; - Y *= value; - } - - public void operator /=(Point value) mut - { - X /= value.X; - Y /= value.Y; - } - - public void operator /=(int32 value) mut - { - X /= value; - Y /= value; - } - - // - // Equality - // - public static bool operator ==(Point left, Point right) => left.X == right.X && left.Y == right.Y; - public static bool operator !=(Point left, Point right) => left.X != right.X || left.Y != right.Y; - - // - // Misc - // - public override void ToString(String strBuffer) => strBuffer.AppendF("X={0} Y={1}", X, Y); - } - -} diff --git a/GlitchyEngine/src/Platform/Windows/WindowsInput.bf b/GlitchyEngine/src/Platform/Windows/WindowsInput.bf index 6cd3c17..5289330 100644 --- a/GlitchyEngine/src/Platform/Windows/WindowsInput.bf +++ b/GlitchyEngine/src/Platform/Windows/WindowsInput.bf @@ -17,8 +17,8 @@ namespace GlitchyEngine struct WindowsInputState { public int8[256] KeyStates; - public Point CursorPosition; - public Point CursorPositionDifference; + public Int2 CursorPosition; + public Int2 CursorPositionDifference; } static WindowsInputState[2] IputStates; @@ -119,7 +119,7 @@ namespace GlitchyEngine return state >= 0; } - public override static Point GetMousePosition() => CurrentState.CursorPosition; + public override static Int2 GetMousePosition() => CurrentState.CursorPosition; public override static int32 GetMouseX() => CurrentState.CursorPosition.X; @@ -140,7 +140,7 @@ namespace GlitchyEngine return state >= 0; } - public override static Point GetLastMousePosition() => LastState.CursorPosition; + public override static Int2 GetLastMousePosition() => LastState.CursorPosition; public override static int32 GetLastMouseX() => LastState.CursorPosition.X; @@ -153,18 +153,18 @@ namespace GlitchyEngine public override static bool IsMouseButtonReleasing(MouseButton button) => IsMouseButtonReleased(button) && WasMouseButtonPressed(button); - public override static Point GetMouseMovement() => CurrentState.CursorPositionDifference; + public override static Int2 GetMouseMovement() => CurrentState.CursorPositionDifference; - public override static void SetMousePosition(Point pos) => SetCursorPos(pos.X, pos.Y); + public override static void SetMousePosition(Int2 pos) => SetCursorPos(pos.X, pos.Y); //[CLink, CallingConvention(.Stdcall)] //static extern int16 GetKeyState(int32 keycode); [CLink, CallingConvention(.Stdcall)] - static extern IntBool GetCursorPos(out Point p); + static extern IntBool GetCursorPos(out Int2 p); [CLink, CallingConvention(.Stdcall)] static extern IntBool SetCursorPos(c_int x, c_int y); [CLink, CallingConvention(.Stdcall)] - static extern IntBool ScreenToClient(HWnd hWnd, ref Point p); + static extern IntBool ScreenToClient(HWnd hWnd, ref Int2 p); public override static void NewFrame() { diff --git a/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf b/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf index ebf139f..d919d86 100644 --- a/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf +++ b/GlitchyEngine/src/Platform/Windows/WindowsWindow.bf @@ -66,13 +66,13 @@ namespace GlitchyEngine // // Size // - public override Point Size + public override Int2 Size { - get => *(Point*)&_clientRect.Width; + get => *(Int2*)&_clientRect.Width; set { - *(Point*)&_clientRect.Width = value; + *(Int2*)&_clientRect.Width = value; ApplyRectangle(); } } @@ -102,13 +102,13 @@ namespace GlitchyEngine // // Position // - public override Point Position + public override Int2 Position { - get => *(Point*)&_clientRect; + get => *(Int2*)&_clientRect; set { - *(Point*)&_clientRect = value; + *(Int2*)&_clientRect = value; ApplyRectangle(); } } diff --git a/GlitchyEngine/src/Window.bf b/GlitchyEngine/src/Window.bf index 8263533..5972cbf 100644 --- a/GlitchyEngine/src/Window.bf +++ b/GlitchyEngine/src/Window.bf @@ -54,7 +54,7 @@ namespace GlitchyEngine /** * Gets or Sets the width and height of the window. */ - public extern Point Size {get; set;} + public extern Int2 Size {get; set;} /** * Gets or Sets the width of the window. */ @@ -67,7 +67,7 @@ namespace GlitchyEngine /** * Gets or Sets the position of the upper-left corner of the client area of the window. */ - public extern Point Position {get; set;} + public extern Int2 Position {get; set;} /** * Gets or Sets the x-coordinate of the upper-left corner of the client area of the window. diff --git a/GlitchyEngine/src/World/EditorCamera.bf b/GlitchyEngine/src/World/EditorCamera.bf index 351eeb0..da991d4 100644 --- a/GlitchyEngine/src/World/EditorCamera.bf +++ b/GlitchyEngine/src/World/EditorCamera.bf @@ -34,6 +34,9 @@ namespace GlitchyEngine.World public Matrix View => _view; + // Gets whether or not the camera is currently being moved. + public bool InUse => BindMouse; + public RenderTargetGroup RenderTarget { get => _renderTarget; diff --git a/Sandbox/src/TextureViewer.bf b/Sandbox/src/TextureViewer.bf index d5e13bf..4641bff 100644 --- a/Sandbox/src/TextureViewer.bf +++ b/Sandbox/src/TextureViewer.bf @@ -157,7 +157,7 @@ namespace Sandbox if(_moving) { - Point movement = Input.GetMouseMovement(); + Int2 movement = Input.GetMouseMovement(); _position.X += movement.X; _position.Y += movement.Y;