Example #1
0
        protected void PickBlockView(BlockView blockView)
        {
            if (!blockView.enabled)
            {
                return;
            }
            if (mPickedBlockView != null)
            {
                Debug.LogError("Toolbox-PickBlockView: Already picked a block view.");
                return;
            }

            // compute the local position of the block view in coding area
            Vector3 localPos = BlocklyUI.WorkspaceView.CodingArea.InverseTransformPoint(blockView.ViewTransform.position);

            // clone a new block view for coding area
            mPickedBlockView = BlocklyUI.WorkspaceView.CloneBlockView(blockView, new Vector2(localPos.x, localPos.y));
            if (mPickedBlockView.InToolbox)
            {
                return;
            }
            mPickedBlockView.OnBeginDrag(null);
            mPickedBlockView.ActivateCountText(false);

            //if the max number of blocks have been used disable the block
            if (Block.blocksAvailable.ContainsKey(blockView.BlockType) && Block.blocksAvailable[blockView.BlockType] > 0)
            {
                Block.blocksAvailable[blockView.BlockType]--;
                if (Block.blocksAvailable[blockView.BlockType] <= 0)
                {
                    blockView.ChangeBgColor(Color.grey);
                    blockView.enabled = false;
                }
            }
            blockView.UpdateCount();

            OnPickBlockView();

            string  id   = GameManager.Instance.GetBlockId(mPickedBlockView.Block);
            XmlNode dom  = Xml.BlockToDomWithXY(mPickedBlockView.Block, false);
            string  text = UBlockly.Xml.DomToText(dom);

            text = GameManager.Instance.ChangeCodeIDs(text);


            TrackerAsset.Instance.setVar("block_type", mPickedBlockView.Block.Type);
            TrackerAsset.Instance.setVar("code", "\r\n" + text);

            TrackerAsset.Instance.setVar("action", "create");
            TrackerAsset.Instance.setVar("level", GameManager.Instance.GetCurrentLevelName().ToLower());
            TrackerAsset.Instance.GameObject.Interacted(id);
        }
Example #2
0
        protected void SetBlockCount(BlockView block)
        {
            //Deactivate the block if it's not in the active list
            bool   active    = false;
            string blockType = block.BlockType;

            if (activeCategories != null && mActiveCategory != null && activeCategories.ContainsKey(mActiveCategory.ToLower()))
            {
                CategoryBlocks info = activeCategories[mActiveCategory.ToLower()];
                active = info.activate == (info.activeBlocks.ContainsKey(blockType));
                int value = (Block.blocksAvailable.ContainsKey(blockType) ? Block.blocksAvailable[blockType] : Int16.MaxValue);
                if (value <= 0)
                {
                    block.enabled = false;
                    block.ChangeBgColor(Color.grey);
                }
            }
            block.gameObject.SetActive(allActive || active);
            block.UpdateCount();
        }
        public static GameObject BuildBlockView(Block block)
        {
            GameObject blockPrefab = BlockViewSettings.Get().PrefabRoot;

            if (block.OutputConnection != null)
            {
                blockPrefab = BlockViewSettings.Get().PrefabRootOutput;
            }
            else if (block.PreviousConnection != null && block.NextConnection != null)
            {
                blockPrefab = BlockViewSettings.Get().PrefabRootPrevNext;
            }
            else if (block.PreviousConnection != null)
            {
                blockPrefab = BlockViewSettings.Get().PrefabRootPrev;
            }

            GameObject blockObj = GameObject.Instantiate(blockPrefab);

            blockObj.name = "Block_" + block.Type;
            RectTransform blockTrans = blockObj.GetComponent <RectTransform>();

            UniformRectTransform(blockTrans);

            //blockview script
            BlockView blockView = AddViewComponent <BlockView>(blockObj);

            //block view's background image
            blockView.AddBgImage(blockObj.GetComponent <Image>());

            //block view's childs: connection, lineGroup
            Transform mutatorEntry = null;

            foreach (Transform child in blockTrans)
            {
                string childName = child.name.ToLower();
                if (childName.StartsWith("connection"))
                {
                    //connection node views
                    ConnectionView conView = AddViewComponent <ConnectionView>(child.gameObject);
                    blockView.AddChild(conView, 0);

                    if (childName.EndsWith("output"))
                    {
                        conView.ConnectionType = Define.EConnection.OutputValue;
                    }
                    else if (childName.EndsWith("prev"))
                    {
                        conView.ConnectionType = Define.EConnection.PrevStatement;
                    }
                    else if (childName.EndsWith("next"))
                    {
                        conView.ConnectionType = Define.EConnection.NextStatement;
                    }

                    //connection node view background color
                    Image image = child.GetComponent <Image>();
                    if (image != null)
                    {
                        blockView.AddBgImage(image);
                    }
                }
                else if (childName.Equals("linegroup"))
                {
                    UniformRectTransform(child as RectTransform);
                    //lineGroup view
                    LineGroupView groupView = AddViewComponent <LineGroupView>(child.gameObject);
                    blockView.AddChild(groupView);
                }
                else if (childName.Equals("mutator_entry"))
                {
                    mutatorEntry = child;
                }
                else if (childName.StartsWith("block_count"))
                {
                    blockView.SetCountText(child.GetComponentInChildren <Text>());
                }
            }

            //check if has mutator entry
            if (mutatorEntry == null)
            {
                throw new Exception("There should be a mutator_entry image under block view prefab");
            }
            if (block.Mutator == null || !block.Mutator.NeedEditor)
            {
                GameObject.DestroyImmediate(mutatorEntry.gameObject);
            }
            else
            {
                blockView.GetLineGroup(0).ReservedStartX = ((RectTransform)mutatorEntry).rect.width + BlockViewSettings.Get().ContentSpace.x;
            }

            //block view's input views, including field's views
            BuildInputViews(block, blockView);

            //block view's layout, build from the very first field
            blockView.BuildLayout();

            //default background color
            blockView.ChangeBgColor(Color.blue);

            return(blockObj);
        }