Exemplo n.º 1
0
        /// <summary>
        /// Generate XML for the workspace factory's category from imported block
        /// definitions.
        /// </summary>
        /// <param name="blockLibStorage">Block Library Storage object.</param>
        /// <returns>XML representation of a category.</returns>
        public Element generateCategoryFromBlockLib(BlockLibraryStorage blockLibStorage)
        {
            var allBlockTypes = blockLibStorage.getBlockTypes();
            // Object mapping block type to XML.
            var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes);

            // Define the custom blocks in order to be able to create instances of
            // them in the exporter workspace.
            this.addBlockDefinitions(blockXmlMap);

            // Get array of defined blocks.
            var blocks = new JsArray <Blockly.Block>();

            foreach (var blockType in blockXmlMap.Keys)
            {
                var block = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace);
                blocks.Push(block);
            }

            return(FactoryUtils.generateCategoryXml(blocks, "Block Library"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generate selector dom from block library storage. For each block in the
        /// library, it has a block option, which consists of a checkbox, a label,
        /// and a fixed size preview workspace.
        /// </summary>
        /// <param name="blockLibStorage"> Block Library Storage object.</param>
        /// <param name="blockSelectorId"> ID of the div element that will contain
        /// the block options.</param>
        /// <returns>Map of block type to Block Option object.</returns>
        public Dictionary <string, BlockOption> createBlockSelectorFromLib(BlockLibraryStorage blockLibStorage, string blockSelectorId)
        {
            // Object mapping each stored block type to XML.
            var allBlockTypes = blockLibStorage.getBlockTypes();
            var blockXmlMap   = blockLibStorage.getBlockXmlMap(allBlockTypes);

            // Define the custom blocks in order to be able to create instances of
            // them in the exporter workspace.
            this.addBlockDefinitions(blockXmlMap);

            var blockSelector = Document.GetElementById(blockSelectorId);
            // Clear the block selector.
            Node child;

            while ((child = blockSelector.FirstChild) != null)
            {
                blockSelector.RemoveChild(child);
            }

            // Append each block option's dom to the selector.
            var blockOptions = new Dictionary <string, BlockOption>();

            foreach (var blockType in blockXmlMap.Keys)
            {
                // Get preview block's XML.
                var block           = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace);
                var previewBlockXml = Blockly.Xml.workspaceToDom(this.hiddenWorkspace);

                // Create block option, inject block into preview workspace, and append
                // option to block selector.
                var blockOpt = new BlockOption(blockSelector, blockType, previewBlockXml);
                blockOpt.createDom();
                blockSelector.AppendChild(blockOpt.dom);
                blockOpt.showPreviewBlock();
                blockOptions[blockType] = blockOpt;
            }
            return(blockOptions);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Pulls information about all blocks in the block library to generate XML
        /// for the selector workpace's toolbox.
        /// </summary>
        /// <param name="blockLibStorage"> Block Library Storage object.</param>
        /// <returns>XML representation of the toolbox.</returns>
        Element generateToolboxFromLibrary(BlockLibraryStorage blockLibStorage)
        {
            // Create DOM for XML.
            var xmlDom = goog.dom.createDom("xml", new Dictionary <string, string> {
                { "id", "blockExporterTools_toolbox" },
                { "style", "display:none" }
            });

            var allBlockTypes = blockLibStorage.getBlockTypes();
            // Object mapping block type to XML.
            var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes);

            // Define the custom blocks in order to be able to create instances of
            // them in the exporter workspace.
            this.addBlockDefinitions(blockXmlMap);

            foreach (var blockType in blockXmlMap.Keys)
            {
                // Get block.
                var block    = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace);
                var category = FactoryUtils.generateCategoryXml(new JsArray <Blockly.Block> {
                    block
                }, blockType);
                xmlDom.AppendChild(category);
            }

            // If there are no blocks in library and the map is empty, append dummy
            // category.
            if (blockXmlMap.Count == 0)
            {
                var category = goog.dom.createDom("category");
                category.SetAttribute("name", "Next Saved Block");
                xmlDom.AppendChild(category);
            }
            return(xmlDom);
        }