public void OnEndDrag(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left)
        {
            return;
        }

        GameObject end = null;

        _endPos *= 2;
        _endPos  = new Vector3(Mathf.Round(_endPos.x), Mathf.Round(_endPos.y));
        _endPos /= 2;

        //if gameobject is in desktop, not in toolbox
        if (this.gameObject.transform.parent.tag == "ActiveItem" || this.gameObject.transform.parent.tag == "ActiveNode")
        {
            //browse all connestors in scene
            GameObject[] objs = GameObject.FindGameObjectsWithTag("Connector");
            foreach (GameObject go in objs)
            {
                Vector3 conPos = go.transform.position * 2;
                conPos  = new Vector3(Mathf.Round(conPos.x), Mathf.Round(conPos.y));
                conPos /= 2;

                //searching connector in mouse position in the end of drag
                if (conPos == _endPos)
                {
                    end = go;
                    break;
                }
            }

            //cant connect with himself or with connector belonging to the same component
            if (end != null &&
                end != this.gameObject &&
                !Connected.Contains(end) &&
                end.transform.parent.gameObject != this.gameObject.transform.parent.gameObject &&
                end.transform.parent.gameObject.tag != "ToolboxItemActive")
            {
                //connecting these two object with line
                _line.End = end;
                Connected.Add(_line.End);
                _line.End.SendMessage("AddConnected", this.gameObject);
                GameObject newLine = Instantiate(Obj);
                newLine.tag = "ActiveLine";
                newLine.transform.position = new Vector2((_line.Begin.transform.position.x + _line.EndPos.x) / 2,
                                                         (_line.Begin.transform.position.y + _line.EndPos.y) / 2);

                Connector con1 = _line.End.GetComponent <Connector>();
                Connector con2 = gameObject.GetComponent <Connector>();
                con1.ConnectedConnectors.Add(con2);
                con2.ConnectedConnectors.Add(con1);

                UndoAction undoAction = new UndoAction();

                List <float> prop = new List <float>();
                prop.Add((float)1.0);
                prop.Add((float)_line.End.GetComponent <Connectable>().GetID());
                prop.Add((float)_line.Begin.GetComponent <Connectable>().GetID());

                CreateDeleteLineChange change = new CreateDeleteLineChange();
                change.SetChange(prop);
                undoAction.AddChange(change);
                GUICircuitComponent.globalUndoList.AddUndo(undoAction);
            }

            //destroy all lines which dont connect two connectors except parental Line
            Destroy(_line);
            GameObject[] killEmAll;
            killEmAll = GameObject.FindGameObjectsWithTag("Line");
            foreach (GameObject t in killEmAll)
            {
                if (t.GetComponent <Line>().End == null && t.transform.name != "Line")
                {
                    Destroy(t.gameObject);
                }
                else
                {
                    //set invisible parental Line
                    t.GetComponent <LineRenderer>().SetPosition(1, this.gameObject.transform.position);
                }
            }
        }
    }
Exemple #2
0
    public void DeleteSelected()
    {
        if (SelectObject.SelectedObjects.Count != 0)
        {
            UndoAction undoAction = new UndoAction();
            foreach (GameObject objectSelected in SelectObject.SelectedObjects)
            {
                GUICircuitComponent component = objectSelected.GetComponent <GUICircuitComponent>();
                List <float>        prop      = new List <float>();
                prop.Add((float)0.0);
                prop.Add((float)component.GetId());
                prop.Add((float)objectSelected.gameObject.transform.GetChild(0).GetComponent <Connectable>().GetID());
                prop.Add((float)objectSelected.gameObject.transform.GetChild(1).GetComponent <Connectable>().GetID());

                CreateDeleteCompChange change = new CreateDeleteCompChange();
                change.SetPosition(objectSelected.transform.position);
                change.SetChange(prop);
                change.SetType(objectSelected.gameObject.GetComponent <GUICircuitComponent>().GetType());
                change.RememberConnectorsToFirst(objectSelected.gameObject.transform.GetChild(0).GetComponent <Connectable>().Connected);
                change.RememberConnectorsToSecond(objectSelected.gameObject.transform.GetChild(1).GetComponent <Connectable>().Connected);

                undoAction.AddChange(change);

                if (objectSelected.tag.Equals("ActiveItem"))
                {
                    // List connected connectors with plusconnector
                    List <GameObject> connected1 =
                        objectSelected.transform.GetChild(0).GetComponent <Connectable>().Connected;

                    // List connected connectors with minusconnector
                    List <GameObject> connected2 =
                        objectSelected.transform.GetChild(1).GetComponent <Connectable>().Connected;

                    Connector con1 = objectSelected.transform.GetChild(0).GetComponent <Connector>();
                    Connector con2 = objectSelected.transform.GetChild(1).GetComponent <Connector>();

                    // First update list of connected connectors in connected component with this component
                    foreach (GameObject c in connected1)
                    {
                        c.gameObject.GetComponent <Connectable>().Connected.Remove(objectSelected.transform.GetChild(0).gameObject);
                        c.gameObject.GetComponent <Connector>().ConnectedConnectors.Remove(con1);
                    }

                    foreach (GameObject c in connected2)
                    {
                        c.gameObject.GetComponent <Connectable>().Connected.Remove(objectSelected.transform.GetChild(1).gameObject);
                        c.gameObject.GetComponent <Connector>().ConnectedConnectors.Remove(con2);
                    }

                    // For each lines in scene
                    GameObject[] lines = GameObject.FindGameObjectsWithTag("ActiveLine");

                    foreach (GameObject currentLine in lines)
                    {
                        // For every line connected to this component
                        if (objectSelected.transform.GetChild(0).gameObject ==
                            currentLine.GetComponent <Line>().Begin
                            ||
                            objectSelected.transform.GetChild(1).gameObject ==
                            currentLine.GetComponent <Line>().Begin
                            ||
                            objectSelected.transform.GetChild(0).gameObject ==
                            currentLine.GetComponent <Line>().End
                            ||
                            objectSelected.transform.GetChild(1).gameObject ==
                            currentLine.GetComponent <Line>().End)
                        {
                            Destroy(currentLine.gameObject);
                        }
                    }
                    Destroy(objectSelected);
                }
            }
            GUICircuitComponent.globalUndoList.AddUndo(undoAction);
            SelectObject.SelectedLines.Clear();
            GetComponent <SelectObject>().DeselectObject();
        }


        // Destroy selected line when delete key was pressed
        if (SelectObject.SelectedLines.Count != 0 && SelectObject.SelectedLines.Contains(this.gameObject))
        {
            UndoAction undoAction = new UndoAction();

            List <float> prop = new List <float>();
            prop.Add((float)0.0);
            prop.Add(this.gameObject.GetComponent <Line>().Begin.GetComponent <Connectable>().GetID());
            prop.Add(this.gameObject.GetComponent <Line>().End.GetComponent <Connectable>().GetID());

            CreateDeleteLineChange change = new CreateDeleteLineChange();
            change.SetChange(prop);
            undoAction.AddChange(change);
            GUICircuitComponent.globalUndoList.AddUndo(undoAction);

            // Delete connected connectors from lists of connectors
            this.gameObject.GetComponent <Line>().Begin.GetComponent <Connectable>().Connected.Remove(this.gameObject.GetComponent <Line>().End.gameObject);
            this.gameObject.GetComponent <Line>().End.GetComponent <Connectable>().Connected.Remove(this.gameObject.GetComponent <Line>().Begin.gameObject);

            this.gameObject.GetComponent <Line>().Begin.GetComponent <Connector>().ConnectedConnectors.Remove(this.gameObject.GetComponent <Line>().End.GetComponent <Connector>());
            this.gameObject.GetComponent <Line>().End.GetComponent <Connector>().ConnectedConnectors.Remove(this.gameObject.GetComponent <Line>().Begin.GetComponent <Connector>());

            Destroy(this.gameObject);
            SelectObject.SelectedLines.Remove(this.gameObject);
        }
    }