Ejemplo n.º 1
0
        /// <summary>
        /// Decode an XML DOM and create blocks on the workspace.
        /// </summary>
        /// <param name="dom">XML DOM</param>
        /// <param name="workspace">The WOrkspace</param>
        public static List <string> DomToWorkspace(XmlNode xml, Workspace workspace)
        {
            List <string> newBlockIds    = new List <string>(); // A list of block ids added by this call.
            var           childCount     = xml.ChildNodes.Count;
            int           width          = 0;                   // Not used in LTR.
            var           variablesFirst = true;

            for (var i = 0; i < childCount; i++)
            {
                var xmlChild = xml.ChildNodes[i];

                var name = xmlChild.Name.ToLower();
                if (string.Equals(name, "block") ||
                    string.Equals(name, "shadow"))
                {
                    // Allow top-level shadow blocks if recordUndo is disabled since
                    // that meas an undo is in progress. Such a block is expected
                    // to be moved to a nested destination in the next operation.
                    var block = Xml.DomToBlock(xmlChild, workspace);
                    newBlockIds.Add(block.ID);
                    int  blockX             = 10;
                    int  blockY             = 10;
                    bool blockXParseSucceed = int.TryParse(xmlChild.GetAttribute("x"), out blockX);
                    bool blockYParseSucceed = int.TryParse(xmlChild.GetAttribute("y"), out blockY);

                    if (blockXParseSucceed && blockYParseSucceed)
                    {
                        block.XY = new Vector2(workspace.RTL ? width - blockX : blockX, blockY);
                    }
                    variablesFirst = false;
                }
                else if (string.Equals(name, "shadow"))
                {
                    variablesFirst = false;
                }
                else if (string.Equals(name, "variables"))
                {
                    if (variablesFirst)
                    {
                        Xml.DomToVariables(xmlChild, workspace);
                    }
                    else
                    {
                        throw new Exception("\'variables\' tag must exist once before block and " +
                                            "shadow tag elements in the workspace XML, but it was found in " +
                                            "another location.");
                    }
                    variablesFirst = false;
                }
            }
            workspace.UpdateVariableStore(false);
            return(newBlockIds);
        }