public static CAShapeLayer ToShape(this CirclePath element) { var shape = new CAShapeLayer(); var bezierPath = NSBezierPath.FromOvalInRect(new CGRect(0, 0, element.Radio * 2, element.Radio * 2)); shape.Path = bezierPath.ToCGPath(); if (!string.IsNullOrEmpty(element.Stroke)) { shape.StrokeColor = XExtensions.ConvertToNSColor(element.Stroke).CGColor; } shape.LineWidth = element.StrokeWidth; if (!string.IsNullOrEmpty(element.Fill)) { shape.FillColor = XExtensions.ConvertToNSColor(element.Fill).CGColor; } var diameter = element.Radio * 2; shape.Bounds = new CGRect(0, 0, diameter, diameter); return(shape); }
public override void DrawRect(CGRect dirtyRect) { if (EffectiveAppearance.AllowsVibrancy) { // Vibrant drawing codepath. NSColor.LabelColor.Set(); NSBezierPath path = NSBezierPath.FromOvalInRect(Bounds.Inset(5, 5)); path.LineWidth = 5; path.Stroke(); NSColor.SecondaryLabelColor.Set(); path = NSBezierPath.FromOvalInRect(Bounds.Inset(10, 10)); path.Fill(); } else { NSColor.Red.Set(); NSBezierPath path = NSBezierPath.FromOvalInRect(Bounds.Inset(5f, 5f)); path.LineWidth = 5; path.Stroke(); NSColor.Purple.Set(); path = NSBezierPath.FromOvalInRect(Bounds.Inset(10f, 10f)); path.Fill(); } }
public override void DrawRect(CGRect dirtyRect) { //TODO: uncomment code https://bugzilla.xamarin.com/show_bug.cgi?id=24144 // if (EffectiveAppearance.AllowsVibrancy) { // // Vibrant drawing codepath. // NSColor.LabelColor.Set (); // NSBezierPath path = NSBezierPath.FromOvalInRect (Bounds.Inset (5, 5)); // path.LineWidth = 5; // path.Stroke (); // // NSColor.SecondaryLabelColor.Set (); // path = NSBezierPath.FromOvalInRect (Bounds.Inset (10, 10)); // path.Fill (); // } else { NSColor.Red.Set(); NSBezierPath path = NSBezierPath.FromOvalInRect(Bounds.Inset(5f, 5f)); path.LineWidth = 5; path.Stroke(); NSColor.Purple.Set(); path = NSBezierPath.FromOvalInRect(Bounds.Inset(10f, 10f)); path.Fill(); // } }
public void CreateMember(FigmaNode currentNode, IView view) { var elipseView = (NSView)view.NativeObject; elipseView.Configure(currentNode); var elipseNode = (FigmaElipse)currentNode; var circleLayer = new CAShapeLayer(); elipseView.Layer.AddSublayer(circleLayer); var bezierPath = NSBezierPath.FromOvalInRect( new CGRect(elipseNode.strokeWeight, elipseNode.strokeWeight, elipseNode.absoluteBoundingBox.Width - (elipseNode.strokeWeight * 2), elipseNode.absoluteBoundingBox.Height - ((elipseNode.strokeWeight * 2)))); circleLayer.Path = bezierPath.ToCGPath(); }
public override void DrawRect(CGRect dirtyRect) { /* * Things look good no matter where the view is located; either a vibrant appearance or * a non-vibrant appearance. Since the view says YES to allowsVibrancy, everything drawn in * drawRect will be vibrant; all colors, images, etc. */ NSColor.FromDeviceWhite(0f, 0.85f).Set(); NSBezierPath path = NSBezierPath.FromOvalInRect(Bounds.Inset(5f, 5f)); path.LineWidth = 5; path.Stroke(); NSColor.FromDeviceWhite(0f, 0.48f).Set(); path = NSBezierPath.FromOvalInRect(Bounds.Inset(10f, 10f)); path.Fill(); }
void Button_Activated(object sender, EventArgs e) { var image = new NSImage(new CGSize(300, 100)); image.LockFocus(); var path = NSBezierPath.FromOvalInRect(new CGRect(new CGPoint(0, 0), new CGSize(300, 100))); NSColor.Red.Set(); path.Fill(); image.UnlockFocus(); var bmprep = new NSBitmapImageRep(image.CGImage); var data = bmprep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Bmp); var bitmap = new Bitmap(data.AsStream()); var folder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); var filename = System.IO.Path.Combine(folder, "test.bmp"); bitmap.Save(filename); }
public override void DrawRect(CGRect dirtyRect) { base.DrawRect(dirtyRect); FillColor.SetFill(); NSBezierPath.FromOvalInRect(Bounds).Fill(); if (LineThickness > 0) { StrokeColor.SetStroke(); var halfLineThickness = LineThickness * 0.5f; var path = NSBezierPath.FromOvalInRect(CGRect.Inflate(Bounds, -halfLineThickness, -halfLineThickness)); path.LineWidth = LineThickness; if (LineDash.Length > 0) // The API doesn't expect an empty line dash array (for some reason) { path.SetLineDash(LineDash.Select(w => w * LineThickness).ToArray(), 0); } path.Stroke(); } }
public static UIBezierPath FromOval(CGRect rect) { return(new UIBezierPath(NSBezierPath.FromOvalInRect(rect))); }
//TODO: This public static NSView ToNSView(this FigmaNode parent, NSView parentView, FigmaNode child, List <FigmaImageView> figmaImageViews = null) { Console.WriteLine("[{0}({1})] Processing {2}..", child.id, child.name, child.GetType()); if (child is IFigmaDocumentContainer instance && child is IConstraints instanceConstrains) { var absolute = instance.absoluteBoundingBox; var parentFrame = (IAbsoluteBoundingBox)parent; if (child.name == "button" || child.name == "button default") { var button = new NSButton() { TranslatesAutoresizingMaskIntoConstraints = false }; parentView.AddSubview(button); button.Hidden = !child.visible; button.WantsLayer = true; var figmaText = instance.children.OfType <FigmaText>().FirstOrDefault(); if (figmaText != null) { button.Font = ToNSFont(figmaText.style); } button.WidthAnchor.ConstraintEqualToConstant(absolute.width).Active = true; button.HeightAnchor.ConstraintEqualToConstant(absolute.height).Active = true; CreateConstraints(button, parentView, instanceConstrains.constraints, absolute, parentFrame.absoluteBoundingBox); if (instance.children.OfType <FigmaGroup>().Any()) { //button.Bordered false; //button.SetButtonType(NSButtonType.MomentaryPushIn); button.Title = ""; //button.Transparent = true; button.AlphaValue = 0.15f; button.BezelStyle = NSBezelStyle.TexturedSquare; } else { if (figmaText != null) { button.AlphaValue = figmaText.opacity; button.Title = figmaText.characters; } button.BezelStyle = NSBezelStyle.Rounded; button.Layer.BackgroundColor = ToNSColor(instance.backgroundColor).CGColor; return(null); } } if (child.name == "text field" || child.name == "Field") { var textField = new NSTextField() { TranslatesAutoresizingMaskIntoConstraints = false }; parentView.AddSubview(textField); textField.Hidden = !child.visible; var figmaText = instance.children.OfType <FigmaText>() .FirstOrDefault(); textField.AlphaValue = figmaText.opacity; textField.StringValue = figmaText.characters; textField.Font = ToNSFont(figmaText.style); textField.WidthAnchor.ConstraintEqualToConstant(absolute.width).Active = true; textField.HeightAnchor.ConstraintEqualToConstant(absolute.height).Active = true; CreateConstraints(textField, parentView, instanceConstrains.constraints, absolute, parentFrame.absoluteBoundingBox); //return null; } } if (child.GetType() == typeof(FigmaVector)) { var vector = ((FigmaVector)child); Console.WriteLine(vector); } else if (child.GetType() == typeof(FigmaInstance)) { Console.WriteLine("Not implemented {0}", child.name); if (child is IFigmaNodeContainer nodeContainer) { foreach (var item in nodeContainer.children) { ToNSView(parent, parentView, item, figmaImageViews); } } } else if (child is FigmaFrameEntity figmaFrameEntity) { var absolute = figmaFrameEntity.absoluteBoundingBox; var parentFrame = (IAbsoluteBoundingBox)parent; var currengroupView = new NSView() { TranslatesAutoresizingMaskIntoConstraints = false }; currengroupView.WantsLayer = true; currengroupView.Hidden = !child.visible; currengroupView.AlphaValue = figmaFrameEntity.opacity; currengroupView.Layer.BackgroundColor = ToNSColor(figmaFrameEntity.backgroundColor).CGColor; parentView.AddSubview(currengroupView); var constraintWidth = currengroupView.WidthAnchor.ConstraintEqualToConstant(absolute.width); constraintWidth.Priority = (uint)NSLayoutPriority.DefaultLow; constraintWidth.Active = true; var constraintHeight = currengroupView.HeightAnchor.ConstraintEqualToConstant(absolute.height); constraintHeight.Priority = (uint)NSLayoutPriority.DefaultLow; constraintHeight.Active = true; if (parentView?.Superview is NSClipView) { parentView.Frame = new CGRect( parentFrame.absoluteBoundingBox.x, parentFrame.absoluteBoundingBox.y, parentFrame.absoluteBoundingBox.width, parentFrame.absoluteBoundingBox.height); } var constraints = figmaFrameEntity.constraints; if (parent is FigmaCanvas canvas) { CreateConstraints(currengroupView, parentView, constraints, absolute, canvas.absoluteBoundingBox); } else if (parent is FigmaFrameEntity parentFigmaFrameEntity) { CreateConstraints(currengroupView, parentView, constraints, absolute, parentFigmaFrameEntity.absoluteBoundingBox ?? FigmaRectangle.Zero); } foreach (var item in figmaFrameEntity.children) { ToNSView(figmaFrameEntity, currengroupView, item, figmaImageViews); } Console.WriteLine(figmaFrameEntity); } else if (child.GetType() == typeof(FigmaText)) { var text = ((FigmaText)child); var absolute = text.absoluteBoundingBox; var parentFrame = (FigmaFrameEntity)parent; var position = GetRelativePosition(parentFrame, text); var constraints = text.constraints; var font = ToNSFont(text.style); var label = FigmaViewsHelper.CreateLabel(text.characters, font); label.Alignment = text.style.textAlignHorizontal == "CENTER" ? NSTextAlignment.Center : text.style.textAlignHorizontal == "LEFT" ? NSTextAlignment.Left : NSTextAlignment.Right; label.AlphaValue = text.opacity; label.Hidden = !child.visible; //label.LineBreakMode = NSLineBreakMode.ByWordWrapping; //label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal); var fills = text.fills.FirstOrDefault(); if (fills != null) { label.TextColor = ToNSColor(fills.color); } if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0) { var attributedText = new NSMutableAttributedString(label.AttributedStringValue); for (int i = 0; i < text.characterStyleOverrides.Length; i++) { var key = text.characterStyleOverrides[i].ToString(); if (!text.styleOverrideTable.ContainsKey(key)) { continue; } var element = text.styleOverrideTable[key]; if (element.fontFamily == null) { continue; } var localFont = ToNSFont(element); var range = new NSRange(i, 1); attributedText.AddAttribute(NSStringAttributeKey.Font, localFont, range); attributedText.AddAttribute(NSStringAttributeKey.ForegroundColor, label.TextColor, range); } label.AttributedStringValue = attributedText; } parentView.AddSubview(label); label.WidthAnchor.ConstraintGreaterThanOrEqualToConstant(absolute.width).Active = true; //label.HeightAnchor.ConstraintEqualToConstant (absolute.height).Active = true; CreateConstraints(label, parentView, constraints, absolute, parentFrame.absoluteBoundingBox); Console.WriteLine(text); } else if (child.GetType() == typeof(FigmaVectorEntity)) { var vector = ((FigmaVectorEntity)child); var absolute = vector.absoluteBoundingBox; var parentEntityFrame = (IAbsoluteBoundingBox)parent; var constraints = vector.constraints; var currengroupView = new NSView() { TranslatesAutoresizingMaskIntoConstraints = false }; currengroupView.WantsLayer = true; currengroupView.AlphaValue = vector.opacity; currengroupView.Hidden = !child.visible; var fills = vector.fills.FirstOrDefault(); if (fills != null && fills.color != null) { currengroupView.Layer.BackgroundColor = ToNSColor(fills.color).CGColor; } parentView.AddSubview(currengroupView); var constraintWidth = currengroupView.WidthAnchor.ConstraintEqualToConstant(absolute.width); constraintWidth.Priority = (uint)NSLayoutPriority.DefaultLow; constraintWidth.Active = true; var constraintHeight = currengroupView.HeightAnchor.ConstraintEqualToConstant(absolute.height); constraintHeight.Priority = (uint)NSLayoutPriority.DefaultLow; constraintHeight.Active = true; CreateConstraints(currengroupView, parentView, constraints, absolute, parentEntityFrame.absoluteBoundingBox); } else if (child.GetType() == typeof(FigmaRectangleVector)) { var rectangleVector = ((FigmaVectorEntity)child); var absolute = rectangleVector.absoluteBoundingBox; var parentEntityFrame = (IAbsoluteBoundingBox)parent; var position = GetRelativePosition(parentEntityFrame, rectangleVector); var constraints = rectangleVector.constraints; NSView currengroupView = null; // = new NSView () { TranslatesAutoresizingMaskIntoConstraints = false }; var fills = rectangleVector.fills.FirstOrDefault(); if (fills?.type == "IMAGE" && fills is FigmaPaint figmaPaint) { figmaPaint.ID = child.id; FigmaImageView figmaImageView; currengroupView = figmaImageView = new FigmaImageView() { Data = figmaPaint }; figmaImageViews?.Add(figmaImageView); } else { currengroupView = new NSView() { TranslatesAutoresizingMaskIntoConstraints = false }; } currengroupView.WantsLayer = true; currengroupView.Hidden = !child.visible; currengroupView.AlphaValue = rectangleVector.opacity; if (child is FigmaRectangleVector vector) { currengroupView.Layer.CornerRadius = vector.cornerRadius; } if (fills?.color != null) { currengroupView.Layer.BackgroundColor = ToNSColor(fills.color).CGColor; } var strokes = rectangleVector.strokes.FirstOrDefault(); if (strokes != null) { if (strokes.color != null) { currengroupView.Layer.BorderColor = ToNSColor(strokes.color).CGColor; } currengroupView.Layer.BorderWidth = rectangleVector.strokeWeight; } parentView.AddSubview(currengroupView); var constraintWidth = currengroupView.WidthAnchor.ConstraintEqualToConstant(absolute.width); constraintWidth.Priority = (uint)NSLayoutPriority.DefaultLow; constraintWidth.Active = true; var constraintHeight = currengroupView.HeightAnchor.ConstraintEqualToConstant(absolute.height); constraintHeight.Priority = (uint)NSLayoutPriority.DefaultLow; constraintHeight.Active = true; CreateConstraints(currengroupView, parentView, constraints, absolute, parentEntityFrame.absoluteBoundingBox); } else if (child.GetType() == typeof(FigmaElipse)) { var elipse = ((FigmaElipse)child); var absolute = elipse.absoluteBoundingBox; var parentFrame = (IAbsoluteBoundingBox)parent; var currentElipse = new NSView() { TranslatesAutoresizingMaskIntoConstraints = false }; currentElipse.WantsLayer = true; currentElipse.AlphaValue = elipse.opacity; currentElipse.Hidden = !child.visible; parentView.AddSubview(currentElipse); currentElipse.WidthAnchor.ConstraintEqualToConstant(absolute.width).Active = true; currentElipse.HeightAnchor.ConstraintEqualToConstant(absolute.height).Active = true; var circleLayer = new CAShapeLayer(); var bezierPath = NSBezierPath.FromOvalInRect(new CGRect(0, 0, absolute.width, absolute.height)); circleLayer.Path = bezierPath.ToGCPath(); currentElipse.Layer.AddSublayer(circleLayer); var fills = elipse.fills.OfType <FigmaPaint>().FirstOrDefault(); if (fills != null) { circleLayer.FillColor = ToNSColor(fills.color).CGColor; } var strokes = elipse.strokes.FirstOrDefault(); if (strokes != null) { if (strokes.color != null) { circleLayer.BorderColor = ToNSColor(strokes.color).CGColor; } } CreateConstraints(currentElipse, parentView, elipse.constraints, absolute, parentFrame.absoluteBoundingBox); } else if (child.GetType() == typeof(FigmaLine)) { var figmaLine = ((FigmaLine)child); var absolute = figmaLine.absoluteBoundingBox; var parentFrame = (IAbsoluteBoundingBox)parent; var figmaLineView = new NSView() { TranslatesAutoresizingMaskIntoConstraints = false }; figmaLineView.WantsLayer = true; figmaLineView.AlphaValue = figmaLine.opacity; figmaLineView.Hidden = !child.visible; var fills = figmaLine.fills.OfType <FigmaPaint>().FirstOrDefault(); if (fills != null) { figmaLineView.Layer.BackgroundColor = ToNSColor(fills.color).CGColor; } var strokes = figmaLine.strokes.FirstOrDefault(); if (strokes != null) { if (strokes.color != null) { figmaLineView.Layer.BackgroundColor = ToNSColor(strokes.color).CGColor; } } parentView.AddSubview(figmaLineView); var lineWidth = absolute.width == 0 ? figmaLine.strokeWeight : absolute.width; var constraintWidth = figmaLineView.WidthAnchor.ConstraintEqualToConstant(lineWidth); constraintWidth.Priority = (uint)NSLayoutPriority.DefaultLow; constraintWidth.Active = true; var lineHeight = absolute.height == 0 ? figmaLine.strokeWeight : absolute.height; var constraintHeight = figmaLineView.HeightAnchor.ConstraintEqualToConstant(lineHeight); constraintHeight.Priority = (uint)NSLayoutPriority.DefaultLow; constraintHeight.Active = true; CreateConstraints(figmaLineView, parentView, figmaLine.constraints, absolute, parentFrame.absoluteBoundingBox); } else { Console.WriteLine("[{1}({2})] Not implemented: {0}", child.GetType(), child.id, child.name); if (child is IFigmaNodeContainer nodeContainer) { foreach (var item in nodeContainer.children) { ToNSView(parent, parentView, item, figmaImageViews); } } } return(null); }