This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mac] Initial Origin Control implementation
- Loading branch information
1 parent
d54d7ab
commit ea97571
Showing
6 changed files
with
444 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
using System; | ||
using System.Linq; | ||
using AppKit; | ||
using CoreGraphics; | ||
using Xamarin.PropertyEditing.Drawing; | ||
|
||
namespace Xamarin.PropertyEditing.Mac | ||
{ | ||
internal class OriginControl : NSView | ||
{ | ||
public event EventHandler OriginChanged; | ||
|
||
private CommonOrigin origin = CommonOrigin.TopLeft; | ||
private CGPoint[] points; | ||
|
||
private NSColor innerFillColor = NSColor.White; | ||
private NSColor positionPointColor = NSColor.Black; | ||
|
||
private nfloat[] lightBezelGreys = { .41f, .29f, .29f, .29f }; | ||
|
||
protected UnfocusableTextField OriginLabel { get; set; } | ||
|
||
private readonly IHostResourceProvider hostResources; | ||
|
||
internal OriginControl (IHostResourceProvider hostResources) | ||
{ | ||
if (hostResources == null) | ||
throw new ArgumentNullException (nameof (hostResources)); | ||
|
||
this.hostResources = hostResources; | ||
|
||
OriginLabel = new UnfocusableTextField { | ||
Font = NSFont.FromFontName (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultDescriptionLabelFontSize), | ||
StringValue = "ORIGIN", | ||
TranslatesAutoresizingMaskIntoConstraints = false, | ||
}; | ||
|
||
AddSubview (OriginLabel); | ||
|
||
AddConstraints (new[] { | ||
NSLayoutConstraint.Create (OriginLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1f, 1f), | ||
NSLayoutConstraint.Create (OriginLabel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18), | ||
NSLayoutConstraint.Create (OriginLabel, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterX, 1f, 0), | ||
}); | ||
|
||
AppearanceChanged (); | ||
} | ||
|
||
public sealed override void ViewDidChangeEffectiveAppearance () | ||
{ | ||
base.ViewDidChangeEffectiveAppearance (); | ||
|
||
AppearanceChanged (); | ||
} | ||
|
||
private void AppearanceChanged () | ||
{ | ||
if (EffectiveAppearance.Name.ToLower().Contains("dark")) { | ||
this.lightBezelGreys = new nfloat[] { .59f, .71f, .71f, .71f }; | ||
this.positionPointColor = NSColor.DarkGray; | ||
} else { | ||
this.lightBezelGreys = new nfloat[] { .41f, .29f, .29f, .29f }; | ||
this.positionPointColor = NSColor.LightGray; | ||
} | ||
|
||
this.innerFillColor = this.hostResources.GetNamedColor (NamedResources.ControlBackground); | ||
|
||
OriginLabel.TextColor = this.hostResources.GetNamedColor (NamedResources.DescriptionLabelColor); | ||
|
||
NeedsDisplay = true; | ||
} | ||
|
||
public CommonOrigin Origin { | ||
get { | ||
return this.origin; | ||
} | ||
set { | ||
if (this.origin.Equals (value)) | ||
return; | ||
this.origin = value; | ||
|
||
OriginChanged?.Invoke (this, EventArgs.Empty); | ||
NeedsDisplay = true; | ||
} | ||
} | ||
|
||
public override CGSize IntrinsicContentSize { | ||
get { | ||
return new CGSize (70, 70); | ||
} | ||
} | ||
|
||
public override bool IsFlipped { | ||
get { | ||
return true; | ||
} | ||
} | ||
|
||
public override void DrawRect (CGRect dirtyRect) | ||
{ | ||
const int radius = 1; | ||
var rect = Bounds; | ||
rect.Inflate (-(0 + radius), -(6 + radius)); | ||
rect.Y = 1; | ||
this.innerFillColor.Set (); | ||
NSGraphics.RectFill (rect); | ||
NSGraphics.DrawTiledRects (rect, rect, new NSRectEdge[] { | ||
NSRectEdge.MinYEdge, NSRectEdge.MinXEdge, NSRectEdge.MaxXEdge, NSRectEdge.MaxYEdge | ||
}, | ||
this.lightBezelGreys); | ||
|
||
var innerRect = rect; | ||
innerRect.Inflate (-(5 + radius), -(5 + radius)); | ||
// Draw focus points | ||
this.points = new CGPoint[] { | ||
new CGPoint (innerRect.Left, innerRect.Top), | ||
new CGPoint (innerRect.Center ().X, innerRect.Top), | ||
new CGPoint (innerRect.Right, innerRect.Top), | ||
new CGPoint (innerRect.Left, innerRect.Center ().Y), | ||
innerRect.Center (), | ||
new CGPoint (innerRect.Right, innerRect.Center ().Y), | ||
new CGPoint (innerRect.Left, innerRect.Bottom), | ||
new CGPoint (innerRect.Center ().X, innerRect.Bottom), | ||
new CGPoint (innerRect.Right, innerRect.Bottom) | ||
}; | ||
|
||
this.positionPointColor.Set (); | ||
foreach (var point in this.points) { | ||
var path = new NSBezierPath (); | ||
path.AppendPathWithArc (point, radius, 0, 360); | ||
path.Fill (); | ||
} | ||
|
||
// Draw selection arrows | ||
NSColor.Red.Set (); | ||
var center = GetRectCenterForOrigin (Origin, innerRect); | ||
const int ArrowLength = 8; | ||
foreach (var dir in Origin.GetArrowDirections ()) { | ||
switch (dir) { | ||
case CommonOrigin.Direction.Left: | ||
DrawArrow (center, new CGPoint (center.X - ArrowLength, center.Y)); | ||
break; | ||
case CommonOrigin.Direction.Right: | ||
DrawArrow (center, new CGPoint (center.X + ArrowLength, center.Y)); | ||
break; | ||
case CommonOrigin.Direction.Up: | ||
DrawArrow (center, new CGPoint (center.X, center.Y - ArrowLength)); | ||
break; | ||
case CommonOrigin.Direction.Down: | ||
DrawArrow (center, new CGPoint (center.X, center.Y + ArrowLength)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static CGPoint GetRectCenterForOrigin (CommonOrigin origin, CGRect rect) | ||
{ | ||
nfloat x = rect.Left; | ||
nfloat y = rect.Top; | ||
|
||
switch (origin.Horizontal) { | ||
case CommonOrigin.Position.Middle: | ||
x = rect.Center ().X; | ||
break; | ||
|
||
case CommonOrigin.Position.End: | ||
x = rect.Right; | ||
break; | ||
} | ||
|
||
switch (origin.Vertical) { | ||
case CommonOrigin.Position.Middle: | ||
y = rect.Center ().Y; | ||
break; | ||
|
||
case CommonOrigin.Position.End: | ||
y = rect.Bottom; | ||
break; | ||
} | ||
|
||
return new CGPoint (x, y); | ||
} | ||
|
||
void DrawArrow (CGPoint from, CGPoint to, int thickness = 1) | ||
{ | ||
const int HeadOffset = 3; | ||
|
||
var path = new NSBezierPath (); | ||
// Draw body | ||
path.SetLineDash (new nfloat[] { 2, 1 }, 0); | ||
path.MoveTo (from); | ||
path.LineTo (to); | ||
path.Stroke (); | ||
|
||
path = new NSBezierPath (); | ||
// Draw head | ||
if (from.Y == to.Y) { | ||
// horizontal | ||
path.MoveTo (to); | ||
path.LineTo (new CGPoint ((from.X < to.X) ? to.X - HeadOffset : to.X + HeadOffset, to.Y + HeadOffset)); | ||
path.MoveTo (to); | ||
path.LineTo (new CGPoint ((from.X < to.X) ? to.X - HeadOffset : to.X + HeadOffset, to.Y - HeadOffset)); | ||
path.Stroke (); | ||
} else { | ||
// vertical | ||
path.MoveTo (to); | ||
path.LineTo (new CGPoint (to.X + HeadOffset, (from.Y < to.Y) ? to.Y - HeadOffset : to.Y + HeadOffset)); | ||
path.MoveTo (to); | ||
path.LineTo (new CGPoint (to.X - HeadOffset, (from.Y < to.Y) ? to.Y - HeadOffset : to.Y + HeadOffset)); | ||
path.Stroke (); | ||
} | ||
} | ||
|
||
public override void MouseUp (NSEvent theEvent) | ||
{ | ||
base.MouseUp (theEvent); | ||
|
||
if (this.points == null) | ||
return; | ||
|
||
var mousePosition = ConvertPointFromView (Window.MouseLocationOutsideOfEventStream, null); | ||
var x = mousePosition.X; | ||
var y = mousePosition.Y; | ||
|
||
Func<CGPoint, double> distance = p => Math.Sqrt (Math.Pow (y - p.Y, 2) + Math.Pow (x - p.X, 2)); | ||
var closestPoint = points.OrderBy (distance).First (); | ||
|
||
const int MinDistance = 5; | ||
if (distance (closestPoint) < MinDistance) { | ||
int index = Array.IndexOf (points, closestPoint); | ||
Origin = new CommonOrigin { | ||
Vertical = (CommonOrigin.Position)(index / 3), | ||
Horizontal = (CommonOrigin.Position)(index % 3) | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public static class CGRectEx | ||
{ | ||
public static CGPoint Center (this CGRect rect) | ||
{ | ||
return new CGPoint (rect.X + rect.Width / 2, rect.Y + rect.Height / 2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.