private AlgorithmBlockUI FindNextForBlock(AlgorithmBlockUI block)
        {
            AlgorithmBlockUI currentBlock = block.MainPrevBlock;

            while (currentBlock != null)
            {
                if (currentBlock.TabulationLevel < block.TabulationLevel)
                {
                    if (currentBlock.BlockData.Type == BlockType.While)
                    {
                        break;
                    }
                    else if (currentBlock.BlockData.Type == BlockType.If)
                    {
                        AlgorithmBlockUI prevBlock = currentBlock;
                        currentBlock = currentBlock.NextBlock;

                        if (currentBlock == null)
                        {
                            currentBlock = FindNextForBlock(prevBlock);
                        }

                        break;
                    }
                }

                currentBlock = currentBlock.MainPrevBlock;
            }

            return(currentBlock);
        }
Example #2
0
        public void AddOnBlock(AlgorithmBlockUI sender)
        {
            switch (_addBlockStep)
            {
            case AddBlockSteps.SetTarget:
            {
                _addTarget  = sender;
                _isAddBlock = true;

                _contextMenu.ClearContextMenu();

                if (_addTarget.BlockData.Type == BlockType.If ||
                    _addTarget.BlockData.Type == BlockType.While)
                {
                    SetAddInsertTypeMenuItems();
                    _contextMenu.gameObject.SetActive(true);

                    _addBlockStep = AddBlockSteps.ChoiceInsertType;
                }
                else
                {
                    _addInsertInside = false;

                    SetAddTypeMenuItems();
                    _contextMenu.gameObject.SetActive(true);

                    _addBlockStep = AddBlockSteps.ChoiceBlockType;
                }
            }
            break;
            }
        }
        public void DeleteOnBlock(AlgorithmBlockUI sender)
        {
            if (sender.BlockData.Type != BlockType.Begin)
            {
                _treePanel.DeleteBlock(sender);
            }

            _editPanel.CurrentTool = EditTools.Cursor;
        }
        private void InitAlgorithmBlocks()
        {
            foreach (AlgorithmBlockUI itBlock in _treePanel.Blocks)
            {
                AlgorithmBlockUI nextBlock = itBlock.NextBlock;
                if (nextBlock == null)
                {
                    nextBlock = FindNextForBlock(itBlock);
                }

                switch (itBlock.BlockData)
                {
                case BeginBlock beginBlock:
                {
                    beginBlock.Next = nextBlock?.BlockData;
                }
                break;

                case ActionBlock actionBlock:
                {
                    actionBlock.Next = nextBlock?.BlockData;
                }
                break;

                case SetBlock setBlock:
                {
                    setBlock.Next = nextBlock?.BlockData;

                    setBlock.Value = itBlock.ValueUis[0].Value;
                }
                break;

                case IfBlock ifBlock:
                {
                    ifBlock.ElseBlock = nextBlock?.BlockData;

                    ifBlock.ThenBlock = itBlock.InnerBlock?.BlockData;
                    ifBlock.Condition = itBlock.ValueUis[0].Value as IBoolean;
                }
                break;

                case WhileBlock whileBlock:
                {
                    whileBlock.OuterBlock = nextBlock?.BlockData;

                    whileBlock.InnerBlock = itBlock.InnerBlock?.BlockData;
                    whileBlock.Condition  = itBlock.ValueUis[0].Value as IBoolean;
                }
                break;
                }
            }
        }
        private void BlockClickHandler(AlgorithmBlockUI sender)
        {
            switch (_editPanel.CurrentTool)
            {
            case EditTools.Add:
            {
                _addTool.AddOnBlock(sender);
            }
            break;

            case EditTools.Delete:
            {
                _deleteTool.DeleteOnBlock(sender);
            }
            break;
            }
        }
        private AlgorithmBlockUI CreateBlock(IAlgorithmBlock blockData)
        {
            AlgorithmBlockUI result = null;

            if (blockData.Type == BlockType.Set)
            {
                result = Instantiate(_resourceProvider.SetBlockPrefab, _content);
            }
            else
            {
                result = Instantiate(_resourceProvider.AlgorithmBlockPrefab, _content);
            }

            result.BlockData = blockData;

            return(result);
        }
        private int FindOutsidePosition(AlgorithmBlockUI block)
        {
            int blockIndex = _blocks.IndexOf(block) + 1;

            while (blockIndex < _blocks.Count)
            {
                if (_blocks[blockIndex].TabulationLevel > block.TabulationLevel)
                {
                    blockIndex++;
                }
                else
                {
                    break;
                }
            }

            return(blockIndex);
        }
        public void AddBlock(AlgorithmBlockUI beforeBlock, IAlgorithmBlock newBlockData, bool isInside)
        {
            int index = _blocks.IndexOf(beforeBlock) + 1;

            AlgorithmBlockUI newBlock = CreateBlock(newBlockData);

            if (isInside)
            {
                if (beforeBlock.InnerBlock != null)
                {
                    newBlock.NextBlock = beforeBlock.InnerBlock;
                    beforeBlock.InnerBlock.MainPrevBlock = newBlock;
                }

                beforeBlock.InnerBlock = newBlock;
            }
            else
            {
                if (beforeBlock.NextBlock != null)
                {
                    newBlock.NextBlock = beforeBlock.NextBlock;
                    beforeBlock.NextBlock.MainPrevBlock = newBlock;
                }

                beforeBlock.NextBlock = newBlock;

                index = FindOutsidePosition(beforeBlock);
            }

            _blocks.Insert(index, newBlock);

            newBlock.MainPrevBlock     = beforeBlock;
            newBlock.IsInsidePrevBlock = isInside;
            newBlock.RefreshAnData();

            SetContentSiblings();

            RefreshTreeBlocksListeners();
            OnTreeChanged?.Invoke(_blocks);
        }
        public void DeleteBlock(AlgorithmBlockUI block)
        {
            AlgorithmBlockUI prevBlock = block.MainPrevBlock;
            AlgorithmBlockUI nextBlock = block.NextBlock;

            if (prevBlock.NextBlock == block)
            {
                prevBlock.NextBlock = nextBlock;
            }
            else if (prevBlock.InnerBlock == block)
            {
                prevBlock.InnerBlock = nextBlock;
            }

            while (block.InnerBlock != null)
            {
                DeleteBlock(block.InnerBlock);
            }

            _blocks.Remove(block);

            Destroy(block.gameObject);
        }
Example #10
0
 private void BlockClickHandler(AlgorithmBlockUI block)
 {
     OnBlockClick?.Invoke(block);
 }