diff --git a/ShareX.ScreenCaptureLib/RegionHelpers/InputManager.cs b/ShareX.ScreenCaptureLib/RegionHelpers/InputManager.cs index a49431375..67eeeec58 100644 --- a/ShareX.ScreenCaptureLib/RegionHelpers/InputManager.cs +++ b/ShareX.ScreenCaptureLib/RegionHelpers/InputManager.cs @@ -30,35 +30,6 @@ namespace ShareX.ScreenCaptureLib { public static class InputManager { - private static MouseState mouseState = new MouseState(); - private static MouseState oldMouseState; - - public static void Update() - { - oldMouseState = mouseState; - mouseState.Update(); - } - - public static bool IsMouseDown(MouseButtons button) - { - return mouseState.Buttons.HasFlag(button); - } - - public static bool IsBeforeMouseDown(MouseButtons button) - { - return oldMouseState.Buttons.HasFlag(button); - } - - public static bool IsMousePressed(MouseButtons button) - { - return IsMouseDown(button) && !IsBeforeMouseDown(button); - } - - public static bool IsMouseReleased(MouseButtons button) - { - return !IsMouseDown(button) && IsBeforeMouseDown(button); - } - public static Point MousePosition { get @@ -106,5 +77,34 @@ public static bool IsMouseMoved return MouseVelocity.X != 0 || MouseVelocity.Y != 0; } } + + private static MouseState mouseState = new MouseState(); + private static MouseState oldMouseState; + + public static void Update() + { + oldMouseState = mouseState; + mouseState.Update(); + } + + public static bool IsMouseDown(MouseButtons button) + { + return mouseState.Buttons.HasFlag(button); + } + + public static bool IsBeforeMouseDown(MouseButtons button) + { + return oldMouseState.Buttons.HasFlag(button); + } + + public static bool IsMousePressed(MouseButtons button) + { + return IsMouseDown(button) && !IsBeforeMouseDown(button); + } + + public static bool IsMouseReleased(MouseButtons button) + { + return !IsMouseDown(button) && IsBeforeMouseDown(button); + } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs b/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs index 55d9ae8ed..147db2da0 100644 --- a/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs @@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License using ShareX.HelpersLib; using System.Drawing; using System.Drawing.Drawing2D; +using System.Windows.Forms; namespace ShareX.ScreenCaptureLib { @@ -89,6 +90,8 @@ public virtual bool IsValidShape internal ShapeManager Manager { get; set; } + private Rectangle tempNodeRect; + public virtual bool Intersects(Point position) { return Rectangle.Contains(position); @@ -182,5 +185,88 @@ public virtual void OnConfigSave() public virtual void OnDoubleClicked() { } + + public virtual void OnNodeVisible() + { + foreach (NodeObject node in Manager.Nodes) + { + node.Shape = NodeShape.Square; + node.Visible = true; + } + } + + public virtual void OnNodeUpdate() + { + for (int i = 0; i < 8; i++) + { + if (Manager.Nodes[i].IsDragging) + { + Manager.IsResizing = true; + + if (!InputManager.IsBeforeMouseDown(MouseButtons.Left)) + { + tempNodeRect = Rectangle; + } + + NodePosition nodePosition = (NodePosition)i; + + int x = InputManager.MouseVelocity.X; + + switch (nodePosition) + { + case NodePosition.TopLeft: + case NodePosition.Left: + case NodePosition.BottomLeft: + tempNodeRect.X += x; + tempNodeRect.Width -= x; + break; + case NodePosition.TopRight: + case NodePosition.Right: + case NodePosition.BottomRight: + tempNodeRect.Width += x; + break; + } + + int y = InputManager.MouseVelocity.Y; + + switch (nodePosition) + { + case NodePosition.TopLeft: + case NodePosition.Top: + case NodePosition.TopRight: + tempNodeRect.Y += y; + tempNodeRect.Height -= y; + break; + case NodePosition.BottomLeft: + case NodePosition.Bottom: + case NodePosition.BottomRight: + tempNodeRect.Height += y; + break; + } + + Rectangle = CaptureHelpers.FixRectangle(tempNodeRect); + } + } + } + + public virtual void OnNodePositionUpdate() + { + int xStart = Rectangle.X; + int xMid = Rectangle.X + Rectangle.Width / 2; + int xEnd = Rectangle.X + Rectangle.Width - 1; + + int yStart = Rectangle.Y; + int yMid = Rectangle.Y + Rectangle.Height / 2; + int yEnd = Rectangle.Y + Rectangle.Height - 1; + + Manager.Nodes[(int)NodePosition.TopLeft].Position = new Point(xStart, yStart); + Manager.Nodes[(int)NodePosition.Top].Position = new Point(xMid, yStart); + Manager.Nodes[(int)NodePosition.TopRight].Position = new Point(xEnd, yStart); + Manager.Nodes[(int)NodePosition.Right].Position = new Point(xEnd, yMid); + Manager.Nodes[(int)NodePosition.BottomRight].Position = new Point(xEnd, yEnd); + Manager.Nodes[(int)NodePosition.Bottom].Position = new Point(xMid, yEnd); + Manager.Nodes[(int)NodePosition.BottomLeft].Position = new Point(xStart, yEnd); + Manager.Nodes[(int)NodePosition.Left].Position = new Point(xStart, yMid); + } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/LineDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/LineDrawingShape.cs index fd5a9091b..fb11f567c 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Drawing/LineDrawingShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/LineDrawingShape.cs @@ -79,5 +79,33 @@ public override void Resize(int x, int y, bool fromBottomRight) EndPosition = EndPosition.Add(x, y); } } + + public override void OnNodeVisible() + { + Manager.Nodes[(int)NodePosition.TopLeft].Shape = Manager.Nodes[(int)NodePosition.BottomRight].Shape = NodeShape.Circle; + Manager.Nodes[(int)NodePosition.TopLeft].Visible = Manager.Nodes[(int)NodePosition.BottomRight].Visible = true; + } + + public override void OnNodeUpdate() + { + if (Manager.Nodes[(int)NodePosition.TopLeft].IsDragging) + { + Manager.IsResizing = true; + + StartPosition = InputManager.MousePosition0Based; + } + else if (Manager.Nodes[(int)NodePosition.BottomRight].IsDragging) + { + Manager.IsResizing = true; + + EndPosition = InputManager.MousePosition0Based; + } + } + + public override void OnNodePositionUpdate() + { + Manager.Nodes[(int)NodePosition.TopLeft].Position = StartPosition; + Manager.Nodes[(int)NodePosition.BottomRight].Position = EndPosition; + } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Shapes/Region/FreehandRegionShape.cs b/ShareX.ScreenCaptureLib/Shapes/Region/FreehandRegionShape.cs index 5f89c86c2..410d1f894 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Region/FreehandRegionShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Region/FreehandRegionShape.cs @@ -124,5 +124,26 @@ public override void Move(int x, int y) public override void Resize(int x, int y, bool fromBottomRight) { } + + public override void OnNodeVisible() + { + Manager.Nodes[(int)NodePosition.TopLeft].Shape = NodeShape.Circle; + Manager.Nodes[(int)NodePosition.TopLeft].Visible = true; + } + + public override void OnNodeUpdate() + { + if (Manager.Nodes[(int)NodePosition.TopLeft].IsDragging) + { + Manager.IsCreating = true; + + Manager.NodesVisible = false; + } + } + + public override void OnNodePositionUpdate() + { + Manager.Nodes[(int)NodePosition.TopLeft].Position = LastPosition; + } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs index 4dfeff18b..02db6858c 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs @@ -168,9 +168,9 @@ public bool IsCurrentShapeTypeRegion } } - public bool IsCreating { get; private set; } + public bool IsCreating { get; set; } public bool IsMoving { get; private set; } - public bool IsResizing { get; private set; } + public bool IsResizing { get; set; } public bool IsCornerMoving { get; private set; } public bool IsProportionalResizing { get; private set; } @@ -217,26 +217,8 @@ public bool NodesVisible if (shape != null) { - UpdateNodePositions(); - - if (shape.NodeType == NodeType.Rectangle) - { - foreach (NodeObject node in Nodes) - { - node.Shape = NodeShape.Square; - node.Visible = nodesVisible; - } - } - else if (shape.NodeType == NodeType.Line) - { - Nodes[(int)NodePosition.TopLeft].Shape = Nodes[(int)NodePosition.BottomRight].Shape = NodeShape.Circle; - Nodes[(int)NodePosition.TopLeft].Visible = Nodes[(int)NodePosition.BottomRight].Visible = true; - } - else if (shape.NodeType == NodeType.Freehand) - { - Nodes[(int)NodePosition.TopLeft].Shape = NodeShape.Circle; - Nodes[(int)NodePosition.TopLeft].Visible = true; - } + shape.OnNodePositionUpdate(); + shape.OnNodeVisible(); } } } @@ -259,7 +241,6 @@ public bool IsCursorOnNode private ToolStripMenuItem tsmiDeleteSelected, tsmiDeleteAll, tsmiBorderColor, tsmiFillColor, tsmiHighlightColor; private ToolStripLabeledNumericUpDown tslnudBorderSize, tslnudRoundedRectangleRadius, tslnudBlurRadius, tslnudPixelateSize; private bool isLeftPressed, isRightPressed, isUpPressed, isDownPressed; - private Rectangle tempNodeRect; public ShapeManager(RectangleRegionForm form) { @@ -1609,132 +1590,14 @@ private void UpdateNodes() { if (InputManager.IsMouseDown(MouseButtons.Left)) { - if (shape.NodeType == NodeType.Rectangle) - { - for (int i = 0; i < 8; i++) - { - if (Nodes[i].IsDragging) - { - IsResizing = true; - - if (!InputManager.IsBeforeMouseDown(MouseButtons.Left)) - { - tempNodeRect = shape.Rectangle; - } - - NodePosition nodePosition = (NodePosition)i; - - int x = InputManager.MouseVelocity.X; - - switch (nodePosition) - { - case NodePosition.TopLeft: - case NodePosition.Left: - case NodePosition.BottomLeft: - tempNodeRect.X += x; - tempNodeRect.Width -= x; - break; - case NodePosition.TopRight: - case NodePosition.Right: - case NodePosition.BottomRight: - tempNodeRect.Width += x; - break; - } - - int y = InputManager.MouseVelocity.Y; - - switch (nodePosition) - { - case NodePosition.TopLeft: - case NodePosition.Top: - case NodePosition.TopRight: - tempNodeRect.Y += y; - tempNodeRect.Height -= y; - break; - case NodePosition.BottomLeft: - case NodePosition.Bottom: - case NodePosition.BottomRight: - tempNodeRect.Height += y; - break; - } - - shape.Rectangle = CaptureHelpers.FixRectangle(tempNodeRect); - - break; - } - } - } - else if (shape.NodeType == NodeType.Line) - { - if (Nodes[(int)NodePosition.TopLeft].IsDragging) - { - IsResizing = true; - - shape.StartPosition = InputManager.MousePosition0Based; - } - else if (Nodes[(int)NodePosition.BottomRight].IsDragging) - { - IsResizing = true; - - shape.EndPosition = InputManager.MousePosition0Based; - } - } - else if (shape.NodeType == NodeType.Freehand) - { - if (Nodes[(int)NodePosition.TopLeft].IsDragging) - { - IsCreating = true; - - NodesVisible = false; - } - } + shape.OnNodeUpdate(); } else { IsResizing = false; } - UpdateNodePositions(); - } - } - - private void UpdateNodePositions() - { - BaseShape shape = CurrentShape; - - if (shape != null) - { - if (shape.NodeType == NodeType.Rectangle) - { - Rectangle rect = shape.Rectangle; - - int xStart = rect.X; - int xMid = rect.X + rect.Width / 2; - int xEnd = rect.X + rect.Width - 1; - - int yStart = rect.Y; - int yMid = rect.Y + rect.Height / 2; - int yEnd = rect.Y + rect.Height - 1; - - Nodes[(int)NodePosition.TopLeft].Position = new Point(xStart, yStart); - Nodes[(int)NodePosition.Top].Position = new Point(xMid, yStart); - Nodes[(int)NodePosition.TopRight].Position = new Point(xEnd, yStart); - Nodes[(int)NodePosition.Right].Position = new Point(xEnd, yMid); - Nodes[(int)NodePosition.BottomRight].Position = new Point(xEnd, yEnd); - Nodes[(int)NodePosition.Bottom].Position = new Point(xMid, yEnd); - Nodes[(int)NodePosition.BottomLeft].Position = new Point(xStart, yEnd); - Nodes[(int)NodePosition.Left].Position = new Point(xStart, yMid); - } - else if (shape.NodeType == NodeType.Line) - { - Nodes[(int)NodePosition.TopLeft].Position = shape.StartPosition; - Nodes[(int)NodePosition.BottomRight].Position = shape.EndPosition; - } - else if (shape.NodeType == NodeType.Freehand) - { - FreehandRegionShape freehandRegionShape = (FreehandRegionShape)shape; - Nodes[(int)NodePosition.TopLeft].Position = freehandRegionShape.LastPosition; - } + shape.OnNodePositionUpdate(); } }