From 8d41315c473cb56286cda7babe34e09cf10b105b Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 20 Nov 2017 21:25:18 +0300 Subject: [PATCH] Added proportional resizing with resize node support --- ShareX.ScreenCaptureLib/Shapes/BaseShape.cs | 58 +++++++++++++++---- .../Shapes/Drawing/ImageDrawingShape.cs | 1 + .../Shapes/Drawing/ImageScreenDrawingShape.cs | 4 ++ .../Shapes/Drawing/TextDrawingShape.cs | 1 + .../Shapes/Effect/BaseEffectShape.cs | 1 + 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs b/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs index a67c37a25..242f34e2c 100644 --- a/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/BaseShape.cs @@ -100,6 +100,7 @@ private set protected AnnotationOptions AnnotationOptions => Manager.Options.AnnotationOptions; private Point tempNodePos, tempStartPos, tempEndPos; + private Rectangle tempRectangle; public virtual bool Intersects(Point position) { @@ -167,7 +168,10 @@ public virtual void OnCreating() } } - public virtual void OnCreated() { } + public virtual void OnCreated() + { + InitialSize = Rectangle.Size; + } public virtual void OnMoving() { } @@ -256,8 +260,9 @@ public virtual void OnNodeUpdate() if (!InputManager.IsBeforeMouseDown(MouseButtons.Left)) { tempNodePos = node.Position; - tempStartPos = Rectangle.Location; - tempEndPos = new Point(Rectangle.X + Rectangle.Width - 1, Rectangle.Y + Rectangle.Height - 1); + tempStartPos = StartPosition; + tempEndPos = EndPosition; + tempRectangle = Rectangle; OnResizing(); } @@ -267,6 +272,7 @@ public virtual void OnNodeUpdate() tempStartPos.Offset(InputManager.MouseVelocity); tempEndPos.Offset(InputManager.MouseVelocity); tempNodePos.Offset(InputManager.MouseVelocity); + tempRectangle.LocationOffset(InputManager.MouseVelocity); } Point pos = InputManager.ClientMousePosition; @@ -313,24 +319,54 @@ public virtual void OnNodeUpdate() if (Manager.IsProportionalResizing) { double ratio = Math.Min(Rectangle.Width / (double)InitialSize.Width, Rectangle.Height / (double)InitialSize.Height); - int width = (int)Math.Round(ratio * InitialSize.Width); - int height = (int)Math.Round(ratio * InitialSize.Height); + int newWidth = (int)Math.Round(InitialSize.Width * ratio); + int newHeight = (int)Math.Round(InitialSize.Height * ratio); + + Point anchor = new Point(); + + switch (nodePosition) + { + case NodePosition.TopLeft: + case NodePosition.Left: + case NodePosition.BottomLeft: + anchor.X = tempRectangle.Right - 1; + break; + case NodePosition.TopRight: + case NodePosition.Right: + case NodePosition.BottomRight: + anchor.X = tempRectangle.X; + break; + } + + switch (nodePosition) + { + case NodePosition.TopLeft: + case NodePosition.Top: + case NodePosition.TopRight: + anchor.Y = tempRectangle.Bottom - 1; + break; + case NodePosition.BottomLeft: + case NodePosition.Bottom: + case NodePosition.BottomRight: + anchor.Y = tempRectangle.Y; + break; + } Rectangle newRect = Rectangle; - if (pos.X < tempStartPos.X) + if (pos.X < anchor.X) { - newRect.X = newRect.Right - width; + newRect.X = newRect.Right - newWidth; } - newRect.Width = width; + newRect.Width = newWidth; - if (pos.Y < tempStartPos.Y) + if (pos.Y < anchor.Y) { - newRect.Y = newRect.Bottom - height; + newRect.Y = newRect.Bottom - newHeight; } - newRect.Height = height; + newRect.Height = newHeight; Rectangle = newRect; } diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageDrawingShape.cs index eea7ba78f..4ff6f949b 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageDrawingShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageDrawingShape.cs @@ -115,6 +115,7 @@ public override void OnCreating() } else { + OnCreated(); ShowNodes(); } } diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageScreenDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageScreenDrawingShape.cs index d7e8f1c4a..14bfefa91 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageScreenDrawingShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/ImageScreenDrawingShape.cs @@ -80,6 +80,10 @@ public override void OnCreated() { Remove(); } + else + { + base.OnCreated(); + } } public override void Dispose() diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs index f2ec48e43..ac0a18316 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs @@ -111,6 +111,7 @@ public override void OnCreating() public override void OnCreated() { AutoSize(true); + base.OnCreated(); ShowNodes(); } diff --git a/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs b/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs index 92480f468..68791f386 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Effect/BaseEffectShape.cs @@ -113,6 +113,7 @@ public virtual void OnDrawFinal(Graphics g, Bitmap bmp) public override void OnCreated() { + base.OnCreated(); CacheEffect(); }