Exemplo n.º 1
0
        public void Start(string figmaName, IView container, FigmaViewRendererServiceOptions options = null)
        {
            if (options == null)
            {
                options = new FigmaViewRendererServiceOptions();
            }

            Console.WriteLine("[FigmaViewRenderer] Starting process..");
            Console.WriteLine($"Reading {figmaName} from resources..");

            this.container = container;

            try
            {
                if (options.LoadFileProvider)
                {
                    fileProvider.Load(figmaName ?? fileProvider.File);
                }

                //we generate all the processed nodes
                Refresh(options);

                //we render only if there is a canvas and GenerateViews is enabled
                var canvas = NodesProcessed.FirstOrDefault(s => s.FigmaNode is FigmaCanvas);
                if (canvas != null && options.ConfigureViews)
                {
                    RecursivelyConfigureViews(canvas, options);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading resource");
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 2
0
        protected void RecursivelyConfigureViews(ProcessedNode parentNode, FigmaViewRendererServiceOptions options)
        {
            var children = NodesProcessed.Where(s => s.ParentView == parentNode);

            foreach (var child in children)
            {
                if (child.View == null)
                {
                    Console.WriteLine("Node {0} has no view to process... skipping", child.FigmaNode);
                    continue;
                }

                if (RendersAddChild(child, parentNode, this))
                {
                    PropertySetter.Configure(CodeProperties.AddChild, child.View, child.FigmaNode, parentNode.View, parentNode.FigmaNode, this);
                }

                if (RendersSize(child, parentNode, this))
                {
                    PropertySetter.Configure(CodeProperties.Frame, child.View, child.FigmaNode, parentNode.View, parentNode.FigmaNode, this);
                }

                if (RendersConstraints(child, parentNode, this))
                {
                    PropertySetter.Configure(CodeProperties.Constraints, child.View, child.FigmaNode, parentNode.View, parentNode.FigmaNode, this);
                }

                RecursivelyConfigureViews(child, options);
            }
        }
 internal ViewNode[] GetProcessedNodes(FigmaNode[] mainNodes)
 {
     ViewNode[] resultNodes = new ViewNode[mainNodes.Length];
     for (int i = 0; i < mainNodes.Length; i++)
     {
         var currentNode = mainNodes[i];
         resultNodes[i] = NodesProcessed.FirstOrDefault(s => s.Node == currentNode);
     }
     return(resultNodes);
 }
Exemplo n.º 4
0
        void Recursively(ProcessedNode parentNode)
        {
            var children = NodesProcessed.Where(s => s.ParentView == parentNode);

            foreach (var child in children)
            {
                if (child.View == null)
                {
                    Console.WriteLine("Node {0} has no view to process... skipping", child.FigmaNode);
                    continue;
                }

                if (child.FigmaNode is IAbsoluteBoundingBox absoluteBounding && parentNode.FigmaNode is IAbsoluteBoundingBox parentAbsoluteBoundingBox)
                {
                    parentNode.View.AddChild(child.View);

                    var   x = Math.Max(absoluteBounding.absoluteBoundingBox.X - parentAbsoluteBoundingBox.absoluteBoundingBox.X, 0);
                    float y;
                    if (AppContext.Current.IsVerticalAxisFlipped)
                    {
                        var parentY = parentAbsoluteBoundingBox.absoluteBoundingBox.Y + parentAbsoluteBoundingBox.absoluteBoundingBox.Height;
                        var actualY = absoluteBounding.absoluteBoundingBox.Y + absoluteBounding.absoluteBoundingBox.Height;
                        y = parentY - actualY;
                    }
                    else
                    {
                        y = absoluteBounding.absoluteBoundingBox.Y - parentAbsoluteBoundingBox.absoluteBoundingBox.Y;
                    }

                    child.View.SetAllocation(x, y,
                                             Math.Max(absoluteBounding.absoluteBoundingBox.Width, 1),
                                             Math.Max(1, absoluteBounding.absoluteBoundingBox.Height)
                                             );

                    //we need to ensure current view is in height
                    //var instrinsic = child.View.IntrinsicContentSize;

                    //child.View.SetAllocation(x, y,
                    //	Math.Max (instrinsic.Width, Math.Max(absoluteBounding.absoluteBoundingBox.Width, 1)),
                    //	Math.Max (instrinsic.Height, Math.Max(1, absoluteBounding.absoluteBoundingBox.Height))
                    //	);
                }

                Recursively(child);
            }
        }
Exemplo n.º 5
0
        public static bool TryParseNode(Genero4glParser parser, out ModuleNode defNode)
        {
            defNode = new ModuleNode();
            if (!string.IsNullOrWhiteSpace(parser.Filename))
            {
                defNode.ProgramName = Path.GetFileName(Path.GetDirectoryName(parser.Filename));
            }

            NodesProcessed processed = NodesProcessed.None;

            while (!parser.PeekToken(TokenKind.EndOfFile))
            {
                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                CompilerOptionsNode compOptionsNode;
                if (CompilerOptionsNode.TryParseNode(parser, out compOptionsNode) && compOptionsNode != null)
                {
                    if (processed == NodesProcessed.None)
                    {
                        defNode.Children.Add(compOptionsNode.StartIndex, compOptionsNode);
                    }
                    else
                    {
                        parser.ReportSyntaxError("Compiler options statement found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.None)
                {
                    processed = NodesProcessed.CompilerOption;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                ImportModuleNode importNode;
                if (ImportModuleNode.TryParseNode(parser, out importNode) && importNode != null)
                {
                    if (processed == NodesProcessed.CompilerOption)
                    {
                        if (!defNode.Children.ContainsKey(importNode.StartIndex))
                        {
                            defNode.Children.Add(importNode.StartIndex, importNode);
                            if (!string.IsNullOrWhiteSpace(importNode.ImportName))
                            {
                                if (importNode.ImportType == ImportModuleType.C)
                                {
                                    defNode.CExtensionImports.Add(importNode.ImportName);
                                }
                                else if (importNode.ImportType == ImportModuleType.Java)
                                {
                                    defNode.JavaImports.Add(importNode.ImportName);
                                }
                                else
                                {
                                    defNode.FglImports.Add(importNode.ImportName);
                                }
                            }
                        }
                        continue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Import statement found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.CompilerOption)
                {
                    processed = NodesProcessed.Imports;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                SchemaSpecificationNode schemaNode;
                if (SchemaSpecificationNode.TryParseDefine(parser, out schemaNode) && schemaNode != null)
                {
                    if (processed == NodesProcessed.Imports)
                    {
                        if (!defNode.Children.ContainsKey(schemaNode.StartIndex))
                        {
                            defNode.Children.Add(schemaNode.StartIndex, schemaNode);
                        }
                    }
                    else
                    {
                        parser.ReportSyntaxError("Schema statement found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.Imports)
                {
                    processed = NodesProcessed.SchemaSpec;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                GlobalsNode globalNode;
                if (GlobalsNode.TryParseNode(parser, out globalNode) && globalNode != null)
                {
                    if (processed == NodesProcessed.SchemaSpec || processed == NodesProcessed.MemberDefinitions)
                    {
                        defNode.Children.Add(globalNode.StartIndex, globalNode);
                        foreach (var cGlobKVP in globalNode.Constants)
                        {
                            if (!defNode.GlobalConstants.ContainsKey(cGlobKVP.Key))
                            {
                                defNode.GlobalConstants.Add(cGlobKVP);
                            }
                            else
                            {
                                parser.ReportSyntaxError(cGlobKVP.Value.LocationIndex, cGlobKVP.Value.LocationIndex + cGlobKVP.Value.Name.Length, string.Format("Global constant {0} defined more than once.", cGlobKVP.Key), Severity.Error);
                            }
                        }
                        foreach (var tGlobKVP in globalNode.Types)
                        {
                            if (!defNode.GlobalTypes.ContainsKey(tGlobKVP.Key))
                            {
                                defNode.GlobalTypes.Add(tGlobKVP);
                            }
                            else
                            {
                                parser.ReportSyntaxError(tGlobKVP.Value.LocationIndex, tGlobKVP.Value.LocationIndex + tGlobKVP.Value.Name.Length, string.Format("Global type {0} defined more than once.", tGlobKVP.Key), Severity.Error);
                            }
                        }
                        foreach (var vGlobKVP in globalNode.Variables)
                        {
                            if (!defNode.GlobalVariables.ContainsKey(vGlobKVP.Key))
                            {
                                defNode.GlobalVariables.Add(vGlobKVP);
                            }
                            else
                            {
                                parser.ReportSyntaxError(vGlobKVP.Value.LocationIndex, vGlobKVP.Value.LocationIndex + vGlobKVP.Value.Name.Length, string.Format("Global variable {0} defined more than once.", vGlobKVP.Key), Severity.Error);
                            }
                        }
                        continue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Globals statement found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.SchemaSpec)
                {
                    processed = NodesProcessed.MemberDefinitions;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                bool                     matchedBreakSequence = false;
                ConstantDefNode          constNode;
                List <List <TokenKind> > breakSequences = new List <List <TokenKind> >()
                {
                    new List <TokenKind> {
                        TokenKind.GlobalsKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.PublicKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.PrivateKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.ConstantKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.DefineKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.TypeKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.FunctionKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.MainKeyword
                    },
                    new List <TokenKind> {
                        TokenKind.ReportKeyword
                    }
                };
                if (ConstantDefNode.TryParseNode(parser, out constNode, out matchedBreakSequence, breakSequences) && constNode != null)
                {
                    if (processed == NodesProcessed.SchemaSpec || processed == NodesProcessed.MemberDefinitions)
                    {
                        defNode.Children.Add(constNode.StartIndex, constNode);
                        foreach (var def in constNode.GetDefinitions())
                        {
                            def.Scope = "module constant";
                            if (!defNode.Constants.ContainsKey(def.Name))
                            {
                                defNode.Constants.Add(def.Name, def);
                            }
                            else
                            {
                                parser.ReportSyntaxError(def.LocationIndex, def.LocationIndex + def.Name.Length, string.Format("Module constant {0} defined more than once.", def.Name), Severity.Error);
                            }
                        }
                        continue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Constant definition found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.SchemaSpec)
                {
                    processed = NodesProcessed.MemberDefinitions;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                TypeDefNode typeNode;
                if (TypeDefNode.TryParseNode(parser, out typeNode, out matchedBreakSequence, breakSequences) && typeNode != null)
                {
                    if (processed == NodesProcessed.SchemaSpec || processed == NodesProcessed.MemberDefinitions)
                    {
                        if (!defNode.Children.ContainsKey(typeNode.StartIndex))
                        {
                            defNode.Children.Add(typeNode.StartIndex, typeNode);
                        }
                        foreach (var def in typeNode.GetDefinitions())
                        {
                            def.Scope = "module type";
                            if (!defNode.Types.ContainsKey(def.Name))
                            {
                                defNode.Types.Add(def.Name, def);
                            }
                            else
                            {
                                parser.ReportSyntaxError(def.LocationIndex, def.LocationIndex + def.Name.Length, string.Format("Module type {0} defined more than once.", def.Name), Severity.Error);
                            }
                        }
                        continue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Type definition found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.SchemaSpec)
                {
                    processed = NodesProcessed.MemberDefinitions;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                DefineNode defineNode;
                if (DefineNode.TryParseDefine(parser, out defineNode, out matchedBreakSequence, breakSequences) && defineNode != null)
                {
                    if (processed == NodesProcessed.SchemaSpec || processed == NodesProcessed.MemberDefinitions)
                    {
                        defNode.Children.Add(defineNode.StartIndex, defineNode);
                        foreach (var def in defineNode.GetDefinitions())
                        {
                            foreach (var vardef in def.VariableDefinitions)
                            {
                                vardef.Scope = "module variable";
                                vardef.SetIsPublic(defineNode.AccessModifier == AccessModifier.Public);
                                if (!defNode.Variables.ContainsKey(vardef.Name))
                                {
                                    defNode.Variables.Add(vardef.Name, vardef);
                                }
                                else
                                {
                                    parser.ReportSyntaxError(vardef.LocationIndex, vardef.LocationIndex + vardef.Name.Length, string.Format("Module variable {0} defined more than once.", vardef.Name), Severity.Error);
                                }
                            }
                        }
                        continue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Variable definition found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.SchemaSpec)
                {
                    processed = NodesProcessed.MemberDefinitions;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                MainBlockNode mainBlock;
                if (MainBlockNode.TryParseNode(parser, out mainBlock, defNode) && mainBlock != null)
                {
                    if (processed == NodesProcessed.MemberDefinitions)
                    {
                        defNode.Children.Add(mainBlock.StartIndex, mainBlock);
                        defNode.Functions.Add(mainBlock.Name, mainBlock);
                        foreach (var cursor in mainBlock.Children.Values.Where(x => x is PrepareStatement || x is DeclareStatement))
                        {
                            IAnalysisResult curRes = cursor as IAnalysisResult;
                            if (!defNode.Cursors.ContainsKey(curRes.Name))
                            {
                                defNode.Cursors.Add(curRes.Name, curRes);
                            }
                        }
                    }
                    else
                    {
                        parser.ReportSyntaxError("Main block found in incorrect position.");
                    }
                }
                if (processed == NodesProcessed.MemberDefinitions)
                {
                    processed = NodesProcessed.Main;
                }

                if (CheckForPreprocessorNode(parser, defNode))
                {
                    continue;
                }

                FunctionBlockNode      funcNode;
                ReportBlockNode        repNode;
                DeclarativeDialogBlock dialogNode;
                int dummy;
                if (FunctionBlockNode.TryParseNode(parser, out funcNode, out dummy, defNode) && funcNode != null)
                {
                    defNode.Children.Add(funcNode.StartIndex, funcNode);
                    funcNode.Scope = "function";

                    if (string.IsNullOrWhiteSpace(funcNode.Name))
                    {
                        parser.ReportSyntaxError(funcNode.LocationIndex, funcNode.LocationIndex, "Invalid function definition found.");
                    }
                    else if (!defNode.Functions.ContainsKey(funcNode.Name))
                    {
                        defNode.Functions.Add(funcNode.Name, funcNode);
                    }
                    else
                    {
                        parser.ReportSyntaxError(funcNode.LocationIndex, funcNode.LocationIndex + funcNode.Name.Length, string.Format("Function {0} defined more than once.", funcNode.Name), Severity.Error);
                    }
                }
                else if (ReportBlockNode.TryParseNode(parser, out repNode, defNode) && repNode != null)
                {
                    defNode.Children.Add(repNode.StartIndex, repNode);

                    repNode.Scope = "report";
                    if (string.IsNullOrWhiteSpace(repNode.Name))
                    {
                        parser.ReportSyntaxError(repNode.LocationIndex, repNode.LocationIndex, "Invalid report definition found.");
                    }
                    else if (!defNode.Functions.ContainsKey(repNode.Name))
                    {
                        defNode.Functions.Add(repNode.Name, repNode);
                    }
                    else
                    {
                        parser.ReportSyntaxError(repNode.LocationIndex, repNode.LocationIndex + repNode.Name.Length, string.Format("Report {0} defined more than once.", repNode.Name), Severity.Error);
                    }
                }
                else if (DeclarativeDialogBlock.TryParseNode(parser, out dialogNode, defNode) && dialogNode != null)
                {
                    defNode.Children.Add(dialogNode.StartIndex, dialogNode);
                    dialogNode.Scope = "dialog";
                    if (string.IsNullOrWhiteSpace(dialogNode.Name))
                    {
                        parser.ReportSyntaxError(dialogNode.LocationIndex, dialogNode.LocationIndex, "Invalid declarative dialog definition found.");
                    }
                    else if (!defNode.Functions.ContainsKey(dialogNode.Name))
                    {
                        defNode.Functions.Add(dialogNode.Name, dialogNode);
                    }
                    else
                    {
                        parser.ReportSyntaxError(dialogNode.LocationIndex, dialogNode.LocationIndex + dialogNode.Name.Length, string.Format("Declarative dialog {0} defined more than once.", dialogNode.Name), Severity.Error);
                    }
                }
                else
                {
                    parser.NextToken();
                }
            }

            if (defNode.Children.Count > 0)
            {
                defNode.StartIndex = defNode.Children[defNode.Children.Keys[0]].StartIndex;
                defNode.EndIndex   = defNode.Children[defNode.Children.Keys[defNode.Children.Count - 1]].EndIndex;
                defNode.IsComplete = true;
            }

            return(true);
        }