public void SetRegionOfInterestWithProposedRegionOfInterest (CGRect proposedRegionOfInterest)
{
// We standardize to ensure we have positive widths and heights with an origin at the top left.
var videoPreviewRect = VideoPreviewLayer.MapToLayerCoordinates (new CGRect (0, 0, 1, 1)).Standardize ();
// Intersect the video preview view with the view's frame to only get
// the visible portions of the video preview view.
var visibleVideoPreviewRect = CGRect.Intersect (videoPreviewRect, Frame);
var oldRegion = RegionOfInterest;
var newRegion = proposedRegionOfInterest.Standardize ();
// Move the region of interest in bounds.
if (currentControlCorner == ControlCorner.None) {
nfloat xOffset = 0;
nfloat yOffset = 0;
if (!visibleVideoPreviewRect.Contains (newRegion.CornerTopLeft())) {
xOffset = NMath.Max (visibleVideoPreviewRect.GetMinX () - newRegion.GetMinX (), 0);
yOffset = NMath.Max (visibleVideoPreviewRect.GetMinY () - newRegion.GetMinY (), 0);
}
if (!visibleVideoPreviewRect.Contains (visibleVideoPreviewRect.CornerBottomRight())) {
xOffset = NMath.Min (visibleVideoPreviewRect.GetMaxX () - newRegion.GetMaxX (), xOffset);
yOffset = NMath.Min (visibleVideoPreviewRect.GetMaxY () - newRegion.GetMaxY (), yOffset);
}
newRegion.Offset (xOffset, yOffset);
}
// Clamp the size when the region of interest is being resized.
visibleVideoPreviewRect.Intersect(newRegion);
newRegion = visibleVideoPreviewRect;
// Fix a minimum width of the region of interest.
if (proposedRegionOfInterest.Size.Width < MinimumRegionOfInterestSize) {
switch (currentControlCorner) {
case ControlCorner.TopLeft:
case ControlCorner.BottomLeft:
var x = oldRegion.Location.X + oldRegion.Size.Width - MinimumRegionOfInterestSize;
newRegion = newRegion.WithX (x).WithWidth (MinimumRegionOfInterestSize);
break;
case ControlCorner.TopRight:
newRegion = newRegion.WithX (oldRegion.Location.X)
.WithWidth (MinimumRegionOfInterestSize);
break;
default:
newRegion = newRegion.WithLocation (oldRegion.Location)
.WithWidth (MinimumRegionOfInterestSize);
break;
}
}
// Fix a minimum height of the region of interest.
if (proposedRegionOfInterest.Height < MinimumRegionOfInterestSize) {
switch (currentControlCorner) {
case ControlCorner.TopLeft:
case ControlCorner.TopRight:
newRegion = newRegion.WithY (oldRegion.Y + oldRegion.Height - MinimumRegionOfInterestSize)
.WithHeight (MinimumRegionOfInterestSize);
break;
case ControlCorner.BottomLeft:
newRegion = newRegion.WithY (oldRegion.Y)
.WithHeight (MinimumRegionOfInterestSize);
break;
default:
newRegion = newRegion.WithLocation (oldRegion.Location)
.WithHeight (MinimumRegionOfInterestSize);
break;
}
}
RegionOfInterest = newRegion;
SetNeedsLayout ();
}