ShareX/ShareX.ScreenCaptureLib/RegionHelpers/AreaManager.cs

807 lines
28 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using System;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.ScreenCaptureLib
2013-11-03 23:53:49 +13:00
{
public class AreaManager
{
public List<BaseShape> Shapes { get; private set; } = new List<BaseShape>();
2013-11-03 23:53:49 +13:00
public BaseShape CurrentShape { get; private set; }
public ShapeType CurrentShapeType { get; private set; } = ShapeType.RegionRectangle;
public Rectangle CurrentRectangle
2013-11-03 23:53:49 +13:00
{
get
{
if (CurrentShape != null)
{
return CurrentShape.Rectangle;
}
return Rectangle.Empty;
}
}
public BaseShape[] Regions
{
get
{
return Shapes.OfType<BaseRegionShape>().ToArray();
2013-11-03 23:53:49 +13:00
}
}
2016-05-03 22:06:58 +12:00
public BaseDrawingShape[] DrawingShapes
{
get
{
return Shapes.OfType<BaseDrawingShape>().ToArray();
}
}
public BaseShape[] ValidRegions
{
get
{
return Regions.Where(x => IsAreaValid(x.Rectangle)).ToArray();
}
}
public bool IsCurrentRegionValid
2013-11-03 23:53:49 +13:00
{
get
{
2016-05-03 22:06:58 +12:00
return IsAreaValid(CurrentRectangle);
2013-11-03 23:53:49 +13:00
}
}
public Rectangle CurrentHoverRectangle { get; private set; }
2013-11-03 23:53:49 +13:00
public bool IsCurrentHoverAreaValid
{
get
{
return !CurrentHoverRectangle.IsEmpty;
2013-11-03 23:53:49 +13:00
}
}
public bool IsCurrentShapeTypeRegion
{
get
{
return CurrentShapeType == ShapeType.RegionRectangle || CurrentShapeType == ShapeType.RegionRoundedRectangle || CurrentShapeType == ShapeType.RegionEllipse;
}
}
public Point CurrentPosition { get; private set; }
public Point PositionOnClick { get; private set; }
2013-11-03 23:53:49 +13:00
public ResizeManager ResizeManager { get; private set; }
public bool IsCreating { get; private set; }
public bool IsMoving { get; private set; }
public bool IsResizing
{
get
{
return ResizeManager.IsResizing;
}
}
public bool IsProportionalResizing { get; private set; }
public bool IsSnapResizing { get; private set; }
public List<SimpleWindowInfo> Windows { get; set; }
2013-11-03 23:53:49 +13:00
public bool WindowCaptureMode { get; set; }
2014-07-10 09:44:08 +12:00
public bool IncludeControls { get; set; }
public int MinimumSize { get; set; } = 3;
2013-11-03 23:53:49 +13:00
2016-05-07 02:38:49 +12:00
public int RoundedRectangleRadiusIncrement { get; } = 3;
2016-05-04 20:28:29 +12:00
private RectangleRegionForm surface;
private SurfaceOptions config;
private ContextMenuStrip cmsContextMenu;
2013-11-03 23:53:49 +13:00
2016-05-04 20:28:29 +12:00
public AreaManager(RectangleRegionForm surface)
2013-11-03 23:53:49 +13:00
{
this.surface = surface;
config = surface.Config;
2013-11-03 23:53:49 +13:00
//CurrentShapeType = config.CurrentShapeType;
ResizeManager = new ResizeManager(surface, this);
2013-11-03 23:53:49 +13:00
surface.MouseDown += surface_MouseDown;
surface.MouseUp += surface_MouseUp;
surface.KeyDown += surface_KeyDown;
surface.KeyUp += surface_KeyUp;
CreateContextMenu();
}
private void CreateContextMenu()
{
cmsContextMenu = new ContextMenuStrip();
ToolStripMenuItem tsmiCancelCapture = new ToolStripMenuItem("Cancel capture");
tsmiCancelCapture.Click += (sender, e) => surface.Close(SurfaceResult.Close);
cmsContextMenu.Items.Add(tsmiCancelCapture);
ToolStripMenuItem tsmiCloseMenu = new ToolStripMenuItem("Close menu");
tsmiCloseMenu.Click += (sender, e) => cmsContextMenu.Close();
cmsContextMenu.Items.Add(tsmiCloseMenu);
cmsContextMenu.Items.Add(new ToolStripSeparator());
foreach (ShapeType shapeType in Helpers.GetEnums<ShapeType>())
{
ToolStripMenuItem tsmiShapeType = new ToolStripMenuItem(shapeType.GetLocalizedDescription());
tsmiShapeType.Checked = shapeType == CurrentShapeType;
tsmiShapeType.Click += (sender, e) =>
{
tsmiShapeType.RadioCheck();
ChangeCurrentShapeType(shapeType);
};
cmsContextMenu.Items.Add(tsmiShapeType);
}
cmsContextMenu.Items.Add(new ToolStripSeparator());
ToolStripMenuItem tsmiChangeBorderColor = new ToolStripMenuItem("Border color...");
tsmiChangeBorderColor.Click += (sender, e) =>
{
surface.Pause();
2016-05-06 07:09:39 +12:00
using (ColorPickerForm dialogColor = new ColorPickerForm(config.ShapeBorderColor))
{
if (dialogColor.ShowDialog() == DialogResult.OK)
{
2016-05-06 07:09:39 +12:00
config.ShapeBorderColor = dialogColor.NewColor;
if (tsmiChangeBorderColor.Image != null) tsmiChangeBorderColor.Image.Dispose();
2016-05-06 07:09:39 +12:00
tsmiChangeBorderColor.Image = ImageHelpers.CreateColorPickerIcon(config.ShapeBorderColor, new Rectangle(0, 0, 16, 16));
UpdateShape();
}
}
surface.Resume();
};
2016-05-06 07:09:39 +12:00
tsmiChangeBorderColor.Image = ImageHelpers.CreateColorPickerIcon(config.ShapeBorderColor, new Rectangle(0, 0, 16, 16));
cmsContextMenu.Items.Add(tsmiChangeBorderColor);
ToolStripLabeledNumericUpDown tslnudBorderSize = new ToolStripLabeledNumericUpDown();
tslnudBorderSize.LabeledNumericUpDownControl.Text = "Border size:";
tslnudBorderSize.LabeledNumericUpDownControl.Minimum = 1;
tslnudBorderSize.LabeledNumericUpDownControl.Maximum = 20;
2016-05-06 07:09:39 +12:00
tslnudBorderSize.LabeledNumericUpDownControl.Value = config.ShapeBorderSize;
tslnudBorderSize.LabeledNumericUpDownControl.ValueChanged = (sender, e) =>
{
config.ShapeBorderSize = (int)tslnudBorderSize.LabeledNumericUpDownControl.Value;
UpdateShape();
};
cmsContextMenu.Items.Add(tslnudBorderSize);
ToolStripMenuItem tsmiChangeFillColor = new ToolStripMenuItem("Fill color...");
tsmiChangeFillColor.Click += (sender, e) =>
{
surface.Pause();
2016-05-06 07:09:39 +12:00
using (ColorPickerForm dialogColor = new ColorPickerForm(config.ShapeFillColor))
{
if (dialogColor.ShowDialog() == DialogResult.OK)
{
2016-05-06 07:09:39 +12:00
config.ShapeFillColor = dialogColor.NewColor;
if (tsmiChangeFillColor.Image != null) tsmiChangeFillColor.Image.Dispose();
2016-05-06 07:09:39 +12:00
tsmiChangeFillColor.Image = ImageHelpers.CreateColorPickerIcon(config.ShapeFillColor, new Rectangle(0, 0, 16, 16));
UpdateShape();
}
}
surface.Resume();
};
2016-05-06 07:09:39 +12:00
tsmiChangeFillColor.Image = ImageHelpers.CreateColorPickerIcon(config.ShapeFillColor, new Rectangle(0, 0, 16, 16));
cmsContextMenu.Items.Add(tsmiChangeFillColor);
cmsContextMenu.Items.Add(new ToolStripSeparator());
ToolStripMenuItem tsmiFullscreenCapture = new ToolStripMenuItem("Capture fullscreen");
tsmiFullscreenCapture.Click += (sender, e) => surface.Close(SurfaceResult.Fullscreen);
cmsContextMenu.Items.Add(tsmiFullscreenCapture);
2016-05-04 21:32:09 +12:00
ToolStripMenuItem tsmiMonitorCapture = new ToolStripMenuItem("Capture monitor");
cmsContextMenu.Items.Add(tsmiMonitorCapture);
2016-05-04 21:32:09 +12:00
tsmiMonitorCapture.DropDownItems.Clear();
Screen[] screens = Screen.AllScreens;
for (int i = 0; i < screens.Length; i++)
{
Screen screen = screens[i];
ToolStripMenuItem tsmi = new ToolStripMenuItem(string.Format("{0}. {1}x{2}", i + 1, screen.Bounds.Width, screen.Bounds.Height));
tsmi.Click += (sender, e) =>
{
surface.MonitorIndex = i;
surface.Close(SurfaceResult.Monitor);
};
tsmiMonitorCapture.DropDownItems.Add(tsmi);
}
ToolStripMenuItem tsmiActiveMonitorCapture = new ToolStripMenuItem("Capture active monitor");
tsmiActiveMonitorCapture.Click += (sender, e) => surface.Close(SurfaceResult.ActiveMonitor);
cmsContextMenu.Items.Add(tsmiActiveMonitorCapture);
cmsContextMenu.Items.Add(new ToolStripSeparator());
ToolStripMenuItem tsmiOptions = new ToolStripMenuItem("Options");
cmsContextMenu.Items.Add(tsmiOptions);
ToolStripMenuItem tsmiQuickCrop = new ToolStripMenuItem("Multi region mode");
tsmiQuickCrop.Checked = !config.QuickCrop;
tsmiQuickCrop.CheckOnClick = true;
tsmiQuickCrop.Click += (sender, e) => config.QuickCrop = !tsmiQuickCrop.Checked;
tsmiOptions.DropDownItems.Add(tsmiQuickCrop);
ToolStripMenuItem tsmiShowInfo = new ToolStripMenuItem("Position and size info");
tsmiShowInfo.Checked = config.ShowInfo;
tsmiShowInfo.CheckOnClick = true;
tsmiShowInfo.Click += (sender, e) => config.ShowInfo = tsmiShowInfo.Checked;
tsmiOptions.DropDownItems.Add(tsmiShowInfo);
ToolStripMenuItem tsmiShowMagnifier = new ToolStripMenuItem("Magnifier");
tsmiShowMagnifier.Checked = config.ShowMagnifier;
tsmiShowMagnifier.CheckOnClick = true;
tsmiShowMagnifier.Click += (sender, e) => config.ShowMagnifier = tsmiShowMagnifier.Checked;
tsmiOptions.DropDownItems.Add(tsmiShowMagnifier);
ToolStripLabeledNumericUpDown tslnudMagnifierPixelCount = new ToolStripLabeledNumericUpDown();
tslnudMagnifierPixelCount.LabeledNumericUpDownControl.Text = "Magnifier pixel count:";
tslnudMagnifierPixelCount.LabeledNumericUpDownControl.Minimum = 1;
tslnudMagnifierPixelCount.LabeledNumericUpDownControl.Maximum = 35;
tslnudMagnifierPixelCount.LabeledNumericUpDownControl.Increment = 2;
tslnudMagnifierPixelCount.LabeledNumericUpDownControl.Value = config.MagnifierPixelCount;
tslnudMagnifierPixelCount.LabeledNumericUpDownControl.ValueChanged = (sender, e) => config.MagnifierPixelCount = (int)tslnudMagnifierPixelCount.LabeledNumericUpDownControl.Value;
tsmiOptions.DropDownItems.Add(tslnudMagnifierPixelCount);
ToolStripLabeledNumericUpDown tslnudMagnifierPixelSize = new ToolStripLabeledNumericUpDown();
tslnudMagnifierPixelSize.LabeledNumericUpDownControl.Text = "Magnifier pixel size:";
tslnudMagnifierPixelSize.LabeledNumericUpDownControl.Minimum = 2;
tslnudMagnifierPixelSize.LabeledNumericUpDownControl.Maximum = 30;
tslnudMagnifierPixelSize.LabeledNumericUpDownControl.Value = config.MagnifierPixelSize;
tslnudMagnifierPixelSize.LabeledNumericUpDownControl.ValueChanged = (sender, e) => config.MagnifierPixelSize = (int)tslnudMagnifierPixelSize.LabeledNumericUpDownControl.Value;
tsmiOptions.DropDownItems.Add(tslnudMagnifierPixelSize);
ToolStripMenuItem tsmiShowCrosshair = new ToolStripMenuItem("Screen wide crosshair");
tsmiShowCrosshair.Checked = config.ShowCrosshair;
tsmiShowCrosshair.CheckOnClick = true;
tsmiShowCrosshair.Click += (sender, e) => config.ShowCrosshair = tsmiShowCrosshair.Checked;
tsmiOptions.DropDownItems.Add(tsmiShowCrosshair);
}
private void surface_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!IsCreating)
{
RegionSelection(e.Location);
}
}
}
private void surface_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (IsMoving || IsCreating)
{
EndRegionSelection();
}
}
else if (e.Button == MouseButtons.Right)
{
if (IsCreating)
{
2016-05-05 01:25:26 +12:00
CancelRegionSelection();
EndRegionSelection();
}
else
{
2016-05-05 01:25:26 +12:00
cmsContextMenu.Show(surface, e.Location.Add(-10, -10));
}
}
2013-11-03 23:53:49 +13:00
}
private void surface_KeyDown(object sender, KeyEventArgs e)
{
2015-08-05 00:40:25 +12:00
switch (e.KeyCode)
2013-11-03 23:53:49 +13:00
{
case Keys.Insert:
if (IsCreating)
{
EndRegionSelection();
}
else
{
2015-08-21 04:29:10 +12:00
if (ResizeManager.Visible)
{
DeselectArea();
}
if (CurrentShape == null || CurrentShape != AreaIntersect())
2015-08-21 04:29:10 +12:00
{
RegionSelection(InputManager.MousePosition);
}
}
break;
case Keys.ShiftKey:
IsProportionalResizing = true;
break;
case Keys.Menu:
IsSnapResizing = true;
break;
case Keys.NumPad1:
ChangeCurrentShapeType(ShapeType.RegionRectangle);
break;
case Keys.NumPad2:
ChangeCurrentShapeType(ShapeType.RegionRoundedRectangle);
break;
case Keys.NumPad3:
ChangeCurrentShapeType(ShapeType.RegionEllipse);
break;
case Keys.NumPad4:
ChangeCurrentShapeType(ShapeType.DrawingRectangle);
break;
case Keys.NumPad5:
ChangeCurrentShapeType(ShapeType.DrawingRoundedRectangle);
break;
case Keys.NumPad6:
ChangeCurrentShapeType(ShapeType.DrawingEllipse);
break;
2016-05-03 22:06:58 +12:00
case Keys.NumPad7:
ChangeCurrentShapeType(ShapeType.DrawingLine);
break;
2016-05-03 22:06:58 +12:00
case Keys.NumPad8:
ChangeCurrentShapeType(ShapeType.DrawingArrow);
2016-05-03 22:06:58 +12:00
break;
case Keys.Add:
switch (CurrentShapeType)
{
case ShapeType.RegionRoundedRectangle:
case ShapeType.DrawingRoundedRectangle:
2016-05-07 02:38:49 +12:00
config.RoundedRectangleRadius += RoundedRectangleRadiusIncrement;
UpdateShape();
break;
}
break;
case Keys.Subtract:
switch (CurrentShapeType)
{
case ShapeType.RegionRoundedRectangle:
case ShapeType.DrawingRoundedRectangle:
2016-05-07 02:38:49 +12:00
config.RoundedRectangleRadius = Math.Max(0, config.RoundedRectangleRadius - RoundedRectangleRadiusIncrement);
UpdateShape();
break;
}
break;
}
}
private void ChangeCurrentShapeType(ShapeType shapeType)
{
CurrentShapeType = shapeType;
2016-05-07 02:38:49 +12:00
config.CurrentShapeType = CurrentShapeType;
DeselectArea();
}
2013-11-03 23:53:49 +13:00
private void surface_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
2013-11-03 23:53:49 +13:00
{
case Keys.ShiftKey:
IsProportionalResizing = false;
break;
case Keys.Menu:
IsSnapResizing = false;
break;
case Keys.Delete:
RemoveCurrentArea();
2016-05-05 02:01:03 +12:00
2015-08-21 04:29:10 +12:00
if (IsCreating)
{
EndRegionSelection();
}
break;
2013-11-03 23:53:49 +13:00
}
}
public void Update()
{
if (CurrentShape != null)
2013-11-03 23:53:49 +13:00
{
if (IsMoving)
2013-11-03 23:53:49 +13:00
{
Rectangle rect = CurrentRectangle;
rect.X += InputManager.MouseVelocity.X;
rect.Y += InputManager.MouseVelocity.Y;
CurrentShape.Rectangle = rect;
2013-11-03 23:53:49 +13:00
}
if (IsCreating && !CurrentRectangle.IsEmpty)
{
CurrentPosition = InputManager.MousePosition0Based;
Point newPosition = CurrentPosition;
if (IsProportionalResizing)
{
newPosition = CaptureHelpers.ProportionalPosition(PositionOnClick, CurrentPosition);
}
if (IsSnapResizing)
{
newPosition = SnapPosition(PositionOnClick, newPosition);
}
CurrentShape.EndPosition = newPosition;
}
2013-11-03 23:53:49 +13:00
}
CheckHover();
ResizeManager.Update();
}
private Point SnapPosition(Point posOnClick, Point posCurrent)
{
Rectangle currentRect = CaptureHelpers.CreateRectangle(posOnClick, posCurrent);
Point newPosition = posCurrent;
foreach (SnapSize size in config.SnapSizes)
{
if (currentRect.Width.IsBetween(size.Width - config.SnapDistance, size.Width + config.SnapDistance) ||
currentRect.Height.IsBetween(size.Height - config.SnapDistance, size.Height + config.SnapDistance))
{
newPosition = CaptureHelpers.CalculateNewPosition(posOnClick, posCurrent, size);
break;
}
}
Rectangle newRect = CaptureHelpers.CreateRectangle(posOnClick, newPosition);
if (surface.ScreenRectangle0Based.Contains(newRect))
{
return newPosition;
}
return posCurrent;
}
2013-11-03 23:53:49 +13:00
private void CheckHover()
{
CurrentHoverRectangle = Rectangle.Empty;
2013-11-03 23:53:49 +13:00
if (!ResizeManager.IsCursorOnNode() && !IsCreating && !IsMoving && !IsResizing)
{
Rectangle hoverArea = GetAreaIntersectWithMouse();
if (!hoverArea.IsEmpty)
{
CurrentHoverRectangle = hoverArea;
2013-11-03 23:53:49 +13:00
}
else
2013-11-03 23:53:49 +13:00
{
SimpleWindowInfo window = FindSelectedWindow();
2013-11-03 23:53:49 +13:00
if (window != null && !window.Rectangle.IsEmpty)
2013-11-03 23:53:49 +13:00
{
hoverArea = CaptureHelpers.ScreenToClient(window.Rectangle);
CurrentHoverRectangle = Rectangle.Intersect(surface.ScreenRectangle0Based, hoverArea);
2013-11-03 23:53:49 +13:00
}
}
}
}
public SimpleWindowInfo FindSelectedWindow()
{
if (Windows != null)
{
return Windows.FirstOrDefault(x => x.Rectangle.Contains(InputManager.MousePosition));
}
return null;
}
public WindowInfo FindSelectedWindowInfo(Point mousePosition)
{
if (Windows != null)
{
SimpleWindowInfo windowInfo = Windows.FirstOrDefault(x => x.IsWindow && x.Rectangle.Contains(mousePosition));
if (windowInfo != null)
{
return windowInfo.WindowInfo;
}
}
return null;
}
private void RegionSelection(Point location)
{
if (ResizeManager.IsCursorOnNode())
{
return;
}
BaseShape shape = AreaIntersect(InputManager.MousePosition0Based);
PositionOnClick = InputManager.MousePosition0Based;
2013-11-03 23:53:49 +13:00
if (shape != null && shape.ShapeType == CurrentShapeType) // Select area
{
IsMoving = true;
CurrentShape = shape;
SelectArea();
}
else if (!IsCreating) // Create new area
2013-11-03 23:53:49 +13:00
{
DeselectArea();
2013-11-03 23:53:49 +13:00
Rectangle rect;
if (config.IsFixedSize)
2013-11-03 23:53:49 +13:00
{
IsMoving = true;
rect = new Rectangle(new Point(location.X - config.FixedSize.Width / 2, location.Y - config.FixedSize.Height / 2), config.FixedSize);
2013-11-03 23:53:49 +13:00
}
else
2013-11-03 23:53:49 +13:00
{
IsCreating = true;
rect = new Rectangle(location, new Size(1, 1));
2013-11-03 23:53:49 +13:00
}
AddRegionShape(rect);
CurrentShape.StartPosition = PositionOnClick;
2013-11-03 23:53:49 +13:00
}
}
private void EndRegionSelection()
2013-11-03 23:53:49 +13:00
{
IsCreating = false;
IsMoving = false;
2013-11-03 23:53:49 +13:00
if (!CurrentRectangle.IsEmpty)
{
if (!IsCurrentRegionValid)
2013-11-03 23:53:49 +13:00
{
RemoveCurrentArea();
CheckHover();
2013-11-03 23:53:49 +13:00
}
else if (config.QuickCrop && IsCurrentShapeTypeRegion)
2013-11-03 23:53:49 +13:00
{
surface.UpdateRegionPath();
surface.Close(SurfaceResult.Region);
}
else
{
SelectArea();
2013-11-03 23:53:49 +13:00
}
}
2016-05-06 05:46:28 +12:00
if (!CurrentHoverRectangle.IsEmpty)
2013-11-03 23:53:49 +13:00
{
AddRegionShape(CurrentHoverRectangle);
2013-11-03 23:53:49 +13:00
if (config.QuickCrop && IsCurrentShapeTypeRegion)
2013-11-03 23:53:49 +13:00
{
surface.UpdateRegionPath();
surface.Close(SurfaceResult.Region);
2013-11-03 23:53:49 +13:00
}
else
{
SelectArea();
2013-11-03 23:53:49 +13:00
}
}
}
private void AddRegionShape(Rectangle rect)
{
BaseShape shape = CreateRegionShape(rect);
Shapes.Add(shape);
CurrentShape = shape;
}
public BaseShape CreateRegionShape(Rectangle rect)
{
BaseShape shape;
2015-07-17 07:51:19 +12:00
switch (CurrentShapeType)
2015-07-17 07:51:19 +12:00
{
default:
case ShapeType.RegionRectangle:
shape = new RectangleRegionShape();
break;
case ShapeType.RegionRoundedRectangle:
2016-05-07 02:38:49 +12:00
shape = new RoundedRectangleRegionShape();
break;
case ShapeType.RegionEllipse:
shape = new EllipseRegionShape();
break;
case ShapeType.DrawingRectangle:
2016-05-06 05:03:57 +12:00
shape = new RectangleDrawingShape();
break;
case ShapeType.DrawingRoundedRectangle:
2016-05-07 02:38:49 +12:00
shape = new RoundedRectangleDrawingShape();
break;
2016-05-06 05:42:56 +12:00
case ShapeType.DrawingEllipse:
shape = new EllipseDrawingShape();
break;
2016-05-06 05:03:57 +12:00
case ShapeType.DrawingLine:
shape = new LineDrawingShape();
break;
2016-05-07 02:38:49 +12:00
case ShapeType.DrawingArrow:
shape = new ArrowDrawingShape();
break;
2015-07-17 07:51:19 +12:00
}
shape.Rectangle = rect;
UpdateShape(shape);
2016-05-06 05:03:57 +12:00
return shape;
}
private void UpdateShape()
{
UpdateShape(CurrentShape);
}
private void UpdateShape(BaseShape shape)
{
if (shape != null)
{
if (shape is BaseDrawingShape)
{
BaseDrawingShape baseDrawingShape = (BaseDrawingShape)shape;
baseDrawingShape.BorderColor = config.ShapeBorderColor;
baseDrawingShape.BorderSize = config.ShapeBorderSize;
baseDrawingShape.FillColor = config.ShapeFillColor;
}
if (shape is IRoundedRectangleShape)
{
IRoundedRectangleShape roundedRectangleShape = (IRoundedRectangleShape)shape;
2016-05-07 02:38:49 +12:00
roundedRectangleShape.Radius = config.RoundedRectangleRadius;
}
}
}
2013-11-03 23:53:49 +13:00
private void SelectArea()
{
if (!CurrentRectangle.IsEmpty && !config.IsFixedSize)
2013-11-03 23:53:49 +13:00
{
ResizeManager.Show();
}
}
private void DeselectArea()
{
CurrentShape = null;
2013-11-03 23:53:49 +13:00
ResizeManager.Hide();
}
2016-05-05 02:01:03 +12:00
private void CancelRegionSelection()
{
BaseShape shape = AreaIntersect();
if (shape != null)
{
Shapes.Remove(shape);
DeselectArea();
}
}
2013-11-03 23:53:49 +13:00
private void RemoveCurrentArea()
{
BaseShape shape = CurrentShape;
if (shape != null)
2013-11-03 23:53:49 +13:00
{
Shapes.Remove(shape);
2013-11-03 23:53:49 +13:00
DeselectArea();
}
}
private bool IsAreaValid(Rectangle rect)
{
return !rect.IsEmpty && rect.Width >= MinimumSize && rect.Height >= MinimumSize;
}
public BaseShape AreaIntersect(Point mousePosition)
2013-11-03 23:53:49 +13:00
{
for (int i = Shapes.Count - 1; i >= 0; i--)
2013-11-03 23:53:49 +13:00
{
BaseShape shape = Shapes[i];
if (shape.ShapeType == CurrentShapeType && shape.Rectangle.Contains(mousePosition))
2013-11-03 23:53:49 +13:00
{
return shape;
2013-11-03 23:53:49 +13:00
}
}
return null;
2013-11-03 23:53:49 +13:00
}
public BaseShape AreaIntersect()
2013-11-03 23:53:49 +13:00
{
return AreaIntersect(InputManager.MousePosition0Based);
}
public Rectangle GetAreaIntersectWithMouse()
{
BaseShape shape = AreaIntersect();
2013-11-03 23:53:49 +13:00
if (shape != null)
2013-11-03 23:53:49 +13:00
{
return shape.Rectangle;
2013-11-03 23:53:49 +13:00
}
return Rectangle.Empty;
}
public bool IsAreaIntersect()
{
return AreaIntersect() != null;
2013-11-03 23:53:49 +13:00
}
public Rectangle CombineAreas()
{
BaseShape[] areas = ValidRegions;
2013-11-03 23:53:49 +13:00
2015-07-17 03:21:02 +12:00
if (areas.Length > 0)
2013-11-03 23:53:49 +13:00
{
Rectangle rect = areas[0].Rectangle;
2013-11-03 23:53:49 +13:00
2015-07-17 03:21:02 +12:00
for (int i = 1; i < areas.Length; i++)
2013-11-03 23:53:49 +13:00
{
rect = Rectangle.Union(rect, areas[i].Rectangle);
2013-11-03 23:53:49 +13:00
}
return rect;
}
return Rectangle.Empty;
}
}
2015-09-01 22:43:03 +12:00
}