Example #1
0
 public static void LoadFromLocalImageResources(this List <FigmaImageView> figmaImageViews, Assembly assembly = null)
 {
     for (int i = 0; i < figmaImageViews.Count; i++)
     {
         try
         {
             var image = FigmaViewsHelper.GetManifestImageResource(assembly, string.Format("{0}.png", figmaImageViews[i].Data.imageRef));
             figmaImageViews[i].Image = image;
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex);
         }
     }
 }
Example #2
0
        //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);
        }