public void OnDrop(PointerEventData eventData)
 {
     if (BEEventSystem.SelectedBlock != null)
     {
         BEBlock droppedBlock = BEEventSystem.SelectedBlock;
         SetBlockAtIndex(droppedBlock, CalculateIndex());
         BEEventSystem.SetSelectedBlock(null);
     }
 }
Beispiel #2
0
    public void DragBlock()
    {
        BELayoutRebuild.RebuildAll();

        try
        {
            startParent = transform.parent.GetComponent <UIDrop>();
        }
        catch
        {
            startParent = null;
        }
        startIndex = transform.GetSiblingIndex();

        startPosition = transform.position;
        diffPosition  = Input.mousePosition - startPosition;

        BEBlock block = GetComponent <BEBlock>();

        BEEventSystem.SetSelectedBlock(beBlock);

        if (transform.parent.GetComponent <BEBlock>())
        {
            RemoveBlockFromParentChildList(block);
        }
        else
        {
            if (transform.parent.name == "ProgrammingEnv")
            {
                TargetObject.beBlockGroupsList.Remove(GetComponent <BEBlock>());
            }
        }

        var tempIndex = transform.GetSiblingIndex();

        if (inactiveBlockInputs.Count > 0)
        {
            InactiveBlockInput inactiveChild = inactiveBlockInputs.Find(a => a.childIndex == tempIndex);
            inactiveChild.blockInput.SetParent(transform.parent);
            inactiveChild.blockInput.SetSiblingIndex(tempIndex);
            inactiveChild.blockInput.gameObject.SetActive(true);
            inactiveBlockInputs.Remove(inactiveChild);
        }

        transform.SetParent(onDragCanvas);
        transform.SetAsLastSibling();
    }
Beispiel #3
0
    IEnumerator InstantiateNewBlock(PointerEventData eventData)
    {
        newBlock = Instantiate(transform).GetComponent <UIDrag>();

        // wait the update of the block size based on the children
        yield return(new WaitForEndOfFrame());

        if (newBlock != null)
        {
            Canvas mainCanvas = GameObject.FindGameObjectWithTag("GameController").transform.GetChild(1).GetComponent <Canvas>();
            newBlock.transform.localScale = Vector3.one * mainCanvas.scaleFactor;

            newBlock.transform.position = transform.position;
            newBlock.name = transform.name;
            startPosition = newBlock.transform.position;
            BEEventSystem.SetSelectedBlock(newBlock.beBlock);
            newBlock.transform.SetParent(onDragCanvas);
            newBlock.diffPosition = Input.mousePosition - startPosition;
        }
    }
    public IEnumerator TranslateVirtualCodeToBlocks(List <string> tempVirtualCode, Transform programmingEnv, Vector2 positionOffset)
    {
        GameObject blockInstance = programmingEnv.gameObject;
        Transform  parent_       = programmingEnv;
        int        hierarchyCounter;
        int        previousHierarchyCounter = 0;

        if (tempVirtualCode != null)
        {
            foreach (string line in tempVirtualCode)
            {
                string codeLine = line;
                hierarchyCounter = codeLine.Split('\t').Length - 1;

                codeLine = codeLine.Replace("\t", "");

                if (codeLine.Length > 0)
                {
                    if (hierarchyCounter > previousHierarchyCounter)
                    {
                        parent_ = blockInstance.transform;
                    }
                    else if (hierarchyCounter < previousHierarchyCounter)
                    {
                        parent_ = parent_.parent;
                    }
                    if (hierarchyCounter == 0)
                    {
                        parent_ = programmingEnv;
                    }

                    string   blockName   = ParseBlockName(codeLine);
                    string[] inputsArray = ParseBlockInputs(codeLine);

                    GameObject blockFromResources = null;
                    try
                    {
                        blockFromResources = Resources.Load(blocksPrefabsPath + blockName, typeof(GameObject)) as GameObject;
                    }
                    catch
                    {
                        Debug.Log("Block not found in the resources/Blocks folder");
                    }

                    if (blockFromResources != null)
                    {
                        blockInstance = Instantiate(blockFromResources);

                        blockInstance.transform.localScale = Vector3.one * mainCanvas.scaleFactor;

                        BEBlock block = blockInstance.GetComponent <BEBlock>();
                        BEEventSystem.SetSelectedBlock(block, BEEventSystem.EventType.simulated);

                        blockInstance.name = blockName;

                        int siblingIndex = parent_.childCount;

                        if (hierarchyCounter == 0)
                        {
                            string[] positions = ParseBlockPosition(codeLine).Split(',');
                            float    posX      = float.Parse(positions[0], CultureInfo.InvariantCulture);
                            float    posY      = float.Parse(positions[1], CultureInfo.InvariantCulture);
                            blockInstance.transform.position = new Vector2(posX, posY) + positionOffset;
                        }

                        parent_.GetComponent <UIDrop>().SetBlockAtIndex(block, siblingIndex);

                        previousHierarchyCounter = hierarchyCounter;

                        LoadInputs(block, inputsArray);
                    }
                }

                //wait end of the frame to avoid sizing glitch
                yield return(new WaitForEndOfFrame());
            }
        }

        BEEventSystem.SetSelectedBlock(null);
    }