public void DestroyCodeLine(CodeLine line)
 {
     if (InstructionHelper.IsJumpInstruction(line.instruction))
     {
         var arrow = line.instruction.GetComponent <JumpInstructionScript>().arrow;
         Destroy(arrow);
     }
     DestroyImmediate(line.instruction);
     DestroyImmediate(line.container);
 }
 public void AddNestObjectComponentIfNeeded()
 {
     if (!InstructionHelper.IsGroupInstruction(instruction))
     {
         return;
     }
     nestObject = GameObject.Instantiate(NestPrefab);
     nestObject.GetComponent <Image>().color = instruction.GetComponent <Image>().color;
     nestObject.transform.SetParent(container.transform);
     nestObject.transform.SetAsFirstSibling();
 }
        public void RearrangePositions(GameObject parentContainer)
        {
            GameObject parentInstruction = null;

            for (int i = 0; i < parentContainer.transform.childCount; i++)
            {
                var child = parentContainer.transform.GetChild(i);
                if (child.tag == "Instruction" || parentContainer.name == "SolutionPanel")
                {
                    parentInstruction = child.gameObject;
                    break;
                }
            }

            if (parentInstruction == null)
            {
                throw new Exception($"Tried to RearrangePositions in null transform. ParentContainer: {parentContainer.name}");
            }

            var currentTopLeft = GetTopLeftForFirst(parentContainer);

            Debug.Log($"Rearranging in {parentInstruction.name}, it has {parentInstruction.transform.childCount} children", parentInstruction);

            for (int i = 0; i < parentInstruction.transform.childCount; i++)
            {
                var currentChildContainer = parentInstruction.transform.GetChild(i).gameObject;
                var rectTransform         = currentChildContainer.GetComponent <RectTransform>();
                var size = rectTransform.sizeDelta;

                var offset = new Vector2(size.x / 2, -size.y / 2);

                var codeLine = GetCorrespondingCodeLine(currentChildContainer);

                if (codeLine == null)
                {
                    continue;                   //FillerInstruction has no codeline
                }
                rectTransform.anchoredPosition = currentTopLeft + offset;

                foreach (var child in codeLine.children)
                {
                    RearrangePositions(currentChildContainer);
                }

                currentTopLeft -= new Vector2(0, codeLine.BlockHeight);
                if (InstructionHelper.IsGroupInstruction(codeLine.instruction))
                {
                    currentTopLeft -= new Vector2(0, 10);
                }
            }
        }
        private void HandleMoveInstructionFirstDrop(PointerEventData eventData)
        {
            if (!InstructionHelper.IsMoveInstruction(eventData.pointerDrag))
            {
                return;
            }
            if (unpinnedCodeline != null)
            {
                return;
            }

            ShowDirectionIndicatorIfNeeded(eventData);
            StartCoroutine(CoroutineExpandDirectionIndicator(eventData, eventData.pointerDrag.transform.Find("DirectionIndicator").GetComponent <DirectionIndicatorScript>()));
        }
        public void RemovePairedJumpInstructionIfNeeded(CodeLine line)
        {
            if (!InstructionHelper.IsJumpInstruction(line.instruction))
            {
                return;
            }
            var paired            = line.instruction.GetComponent <JumpInstructionScript>().bindedInstruction;
            var pairedInstruction = GetCorrespondingCodeLine(paired);

            if (pairedInstruction == null)
            {
                return;
            }
            pairedInstruction.SetParent(null);
            RemoveCodeLineFromSolution(pairedInstruction);
            Destroy(pairedInstruction.instruction);
            Destroy(pairedInstruction.container);
        }
        private void RemoveDeadJumpInstructions()
        {
            var instructions = AllCodeLines;

            for (int i = instructions.Count - 1; i >= 0; i--)
            {
                var current = instructions[i];
                if (InstructionHelper.IsJumpInstruction(current.instruction))
                {
                    var script = current.instruction.GetComponent <JumpInstructionScript>();
                    if (script.bindedInstruction == null)
                    {
                        RemoveCodeLineFromSolution(current);
                        DestroyCodeLine(current);
                    }
                }
            }
        }
        private void HandleConditionalInstructionFirstDrop(PointerEventData eventData)
        {
            if (!InstructionHelper.IsConditionalInstruction(eventData.pointerDrag))
            {
                return;
            }
            if (unpinnedCodeline != null)
            {
                return;
            }

            ShowDirectionIndicatorIfNeeded(eventData);
            ShowComparisonTypeIndicatorIfNeeded(eventData);
            ShowDropDownIfNeeded(eventData);
            StartCoroutine(CoroutineHandleConditionalInstructionDrop(eventData,
                                                                     eventData.pointerDrag.transform.Find("DirectionIndicator").GetComponent <DirectionIndicatorScript>(),
                                                                     eventData.pointerDrag.transform.Find("ComparisonIndicator").GetComponent <ComparisonTypeIndicatorScript>()
                                                                     ));
        }
        private void InsertJumpLabelInstructionIfNeeded(CodeLine insertedLine, int index, CodeLine parentLine)
        {
            if (unpinnedCodeline != null)
            {
                return;
            }
            if (!InstructionHelper.IsJumpInstruction(insertedLine.instruction))
            {
                return;
            }
            if (InstructionHelper.IsJumpInstructionLabel(insertedLine.instruction))
            {
                return;
            }
            var jumpLabel     = insertedLine.instruction.GetComponent <JumpInstructionScript>().CreateBindedLabel();
            var labelCodeLine = CodeLineFactory.GetStandardCodeLine(jumpLabel);

            InsertAtLine(labelCodeLine, index, parentLine);
            insertedLine.instruction.GetComponent <JumpInstructionScript>().AttachArrow();
        }
        public void RearrangePositions(List <CodeLine> lines, Vector2 topLeft)
        {
            if (lines.Count == 0)
            {
                return;
            }
            var parentContainer = lines[0].container.transform.parent.parent;
            var currentTopLeft  = topLeft;// GetTopLeftForFirst(parentContainer.gameObject);

            for (int i = 0; i < lines.Count; i++)
            {
                var currentChildContainer = lines[i].container;
                var rectTransform         = currentChildContainer.GetComponent <RectTransform>();
                var size = rectTransform.sizeDelta;

                var offset = new Vector2(size.x / 2, -size.y / 2);

                var currentLine = lines[i];
                if (InstructionHelper.IsGroupInstruction(currentLine.instruction))
                {
                }

                //var oldPosition = rectTransform.anchoredPosition;

                rectTransform.anchoredPosition = currentTopLeft + offset;

                //var newPosition = rectTransform.anchoredPosition;
                //var instructionRT = lines[i].instruction.GetComponent<RectTransform>();
                //instructionRT.anchoredPosition -= newPosition - oldPosition;

                RearrangePositions(lines[i].children, FirstChildOffset);

                currentTopLeft -= new Vector2(0, currentLine.YSpacing);
                if (InstructionHelper.IsGroupInstruction(currentLine.instruction))
                {
                }
            }
        }
        public CodeLine GetParentBlockUnderMousePosition(List <GameObject> allContainers)
        {
            ClearTemporaryInstructionFlag(allContainers);
            var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            for (int i = allContainers.Count - 1; i >= 0; i--)
            {
                Debug.Log(i);
                GameObject container = allContainers[i];
                if (IsContainerUnpinned(container))
                {
                    continue;
                }
                var correspondingCodeLine = GetCorrespondingCodeLine(container);
                if (correspondingCodeLine == null)
                {
                    Debug.Log("correspondingCodeLine was null");
                    continue;
                }
                if (!InstructionHelper.IsGroupInstruction(correspondingCodeLine.instruction))
                {
                    continue;
                }

                if (correspondingCodeLine.IsInsideNest(mousePosition))
                {
                    if (draggedObject != null)
                    {
                        correspondingCodeLine.TemporaryCodeLine = unpinnedCodeline ?? fakeSingleLine;
                    }
                    Debug.Log($"currentContainerIndex: {correspondingCodeLine.InstructionIndex}, {i}");
                    return(correspondingCodeLine);
                }
            }

            return(null);
        }
 private void RepairJumps()
 {
     for (int i = 0; i < allCommands.Count; i++)
     {
         var currentCommand = allCommands[i];
         if (!(currentCommand is JumpCommand))
         {
             continue;
         }
         var jumpCommand  = currentCommand as JumpCommand;
         var jumpCodeLine = commandToCodeLineMapping[jumpCommand];
         if (jumpCodeLine == null)
         {
             continue;
         }
         if (InstructionHelper.IsJumpInstructionLabel(jumpCodeLine.instruction))
         {
             continue;                                                                     //labels does not need to be repaired
         }
         var pairedLine  = GetPairedLabelCommand(jumpCodeLine);
         var indexToJump = allCommands.IndexOf(pairedLine);
         jumpCommand.NextCommandId = indexToJump;
     }
 }
        public void GenerateCodeForInstruction(CodeLine line)
        {
            if (InstructionHelper.IsMoveInstruction(line.instruction))
            {
                var command = new MoveCommand(InstructionHelper.GetInstructionDirection(line.instruction), currentCodeLineNumber + 1);
                commandToCodeLineMapping.Add(command, line);
                allCommands.Add(command);
            }
            if (InstructionHelper.IsPutInstruction(line.instruction))
            {
                var command = new PutCommand(currentCodeLineNumber + 1);
                commandToCodeLineMapping.Add(command, line);
                allCommands.Add(command);
            }
            if (InstructionHelper.IsPickInstruction(line.instruction))
            {
                var command = new PickCommand(currentCodeLineNumber + 1);
                commandToCodeLineMapping.Add(command, line);
                allCommands.Add(command);
            }
            if (InstructionHelper.IsJumpInstruction(line.instruction))
            {
                ICommand command;

                if (InstructionHelper.IsJumpInstructionLabel(line.instruction))
                {
                    command = new JumpCommand(currentCodeLineNumber + 1);
                    allCommands.Add(command);
                }
                else
                {
                    //this is being set in code later - otherwise forward jumps will not work - see RepairJumps for reference.
                    command = new JumpCommand(currentCodeLineNumber + 1);
                    allCommands.Add(command);
                }
                commandToCodeLineMapping.Add(command, line);
            }

            if (InstructionHelper.IsIfInstruction(line.instruction))
            {
                int trueLineNumber = currentCodeLineNumber + 1;
                int elseLineNumber = currentCodeLineNumber + line.TotalChildrenCount + 1;
                var command        = new IfCommand(trueLineNumber, elseLineNumber, GetConditions(line), GetLogicalOperators(line));
                allCommands.Add(command);
                commandToCodeLineMapping.Add(command, line);
                currentCodeLineNumber++;
                foreach (var child in line.children)
                {
                    GenerateCodeForInstruction(child);
                }
                command.NextCommandId = currentCodeLineNumber;
                currentCodeLineNumber--; //this may seem wrong but actually it is not
            }

            if (InstructionHelper.IsWhileInstruction(line.instruction))
            {
                int trueLineNumber  = currentCodeLineNumber + 1;
                int falseLineNumber = currentCodeLineNumber + line.TotalChildrenCount + 1;
                var command         = new WhileCommand(trueLineNumber, falseLineNumber, GetConditions(line), GetLogicalOperators(line));
                allCommands.Add(command);
                commandToCodeLineMapping.Add(command, line);
                currentCodeLineNumber++;
                foreach (var child in line.children)
                {
                    GenerateCodeForInstruction(child);
                }
                var loopJumpCommand = new JumpCommand(trueLineNumber - 1);
                commandToCodeLineMapping.Add(loopJumpCommand, null);
                command.NextCommandId = currentCodeLineNumber + 1;
                allCommands.Add(loopJumpCommand);
            }

            if (InstructionHelper.IsRepeatInstruction(line.instruction))
            {
                int trueLineNumber  = currentCodeLineNumber + 1;
                int falseLineNumber = currentCodeLineNumber + line.TotalChildrenCount + 1;
                var command         = new RepeatCommand(trueLineNumber, falseLineNumber, InstructionHelper.GetRepeatTimes(line.instruction));
                allCommands.Add(command);
                commandToCodeLineMapping.Add(command, line);
                currentCodeLineNumber++;
                foreach (var child in line.children)
                {
                    GenerateCodeForInstruction(child);
                }
                var loopJumpCommand = new JumpCommand(trueLineNumber - 1);
                commandToCodeLineMapping.Add(loopJumpCommand, null);
                command.NextCommandId = currentCodeLineNumber;
                allCommands.Add(loopJumpCommand);
            }

            currentCodeLineNumber++;
        }