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

        GameObject           propertiesContainer = GameObject.Find("PropertiesWindowContainer");
        EditObjectProperties script = propertiesContainer.GetComponent <EditObjectProperties>();

        // Deselect selected item first.
        if (SelectObject.SelectedObjects.Count != 0 && !SelectObject.SelectedObjects.Contains(this.gameObject))
        {
            //deselect item
            GameObject item = GameObject.Find("Canvas");
            item.GetComponent <SelectObject>().DeselectObject();

            // Deselect line
            GameObject line = GameObject.Find("Line(Clone)");
            if (line != null)
            {
                GameObject.Find("Canvas").GetComponent <SelectObject>().DeselectLine();
            }
        }
        else if (SelectObject.SelectedObjects.Count > 1 && SelectObject.SelectedObjects.Contains(this.gameObject))
        {
            // Setting starting positions for every selected elements
            _mousePos = Camera.main.ScreenToWorldPoint(eventData.position);
            foreach (GameObject objectSelected in SelectObject.SelectedObjects)
            {
                _itemPoss.Add(objectSelected.transform.position);
            }
        }
        //deselect line
        else
        {
            GameObject line = GameObject.Find("Line(Clone)");
            if (line != null)
            {
                GameObject.Find("Canvas").GetComponent <SelectObject>().DeselectLine();
            }
        }

        // Setting starting posiitons.
        _mousePos = Camera.main.ScreenToWorldPoint(eventData.position);
        _itemPos  = this.gameObject.transform.position;

        // ToolboxItemActive tagged GameObjects are used to generate new instances for the working panel.
        if (this.gameObject.tag == "Arrow" && this.gameObject.transform.parent.tag != "ToolboxItemActive")
        {
            _arrow    = this.gameObject;
            _arrowPos = new Vector2(this.gameObject.transform.parent.transform.position.x, _arrow.transform.position.y);
            _connectorPotentiometer    = this.gameObject.transform.parent.gameObject.transform.FindChild("PlusConnector").gameObject;
            _connectorPotentiometerPos = new Vector2(this.gameObject.transform.parent.transform.position.x, _connectorPotentiometer.transform.position.y);
        }
        else if ((this.gameObject.tag == "Arrow" && this.gameObject.transform.parent.tag == "ToolboxItemActive") ||
                 this.gameObject.tag == "ToolboxItemActive")
        {
            // Debug.Log(this.gameObject.transform.parent);
            GameObject item = this.gameObject;
            if (this.gameObject.transform.parent.tag == "ToolboxItemActive")
            {
                item = item.gameObject.transform.parent.gameObject;
            }

            tbitem = true;
            item.gameObject.tag = "ActiveItem";
            // Awake() function of every script is called when GameObject is instatiated. We need it to be instantiated as ActiveItem.
            _draggingItem       = Instantiate(item.gameObject);
            item.gameObject.tag = "ToolboxItemActive";
            _draggingItem.tag   = "ActiveItem";
            _draggingItem.layer = 8; //Name of 8th layer is ActiveItem
            _draggingItem.transform.localScale = new Vector3(1, 1, 0);
            _draggingItem.GetComponent <SpriteRenderer>().enabled          = true;
            _draggingItem.GetComponent <SpriteRenderer>().sortingLayerName = "ActiveItem";

            for (int i = 0; i < _draggingItem.transform.childCount; i++)
            {
                _draggingItem.transform.GetChild(i).GetComponent <SpriteRenderer>().sortingLayerName = "ActiveItem";
                _draggingItem.transform.GetChild(i).GetComponent <SpriteRenderer>().enabled          = true;
                _draggingItem.transform.GetChild(i).gameObject.layer = 8;
            }
            // Newly created component needs to be selected otherwise an error will occur
            SelectObject.AddItemToSelection(_draggingItem);

            UndoAction          undoAction = new UndoAction();
            GUICircuitComponent component  = _draggingItem.GetComponent <GUICircuitComponent>();
            List <float>        prop       = new List <float>();
            prop.Add((float)1.0);
            prop.Add((float)component.GetId());
            prop.Add((float)_draggingItem.gameObject.transform.GetChild(0).GetComponent <Connectable>().GetID());
            prop.Add((float)_draggingItem.gameObject.transform.GetChild(1).GetComponent <Connectable>().GetID());
            CreateDeleteCompChange change = new CreateDeleteCompChange();
            change.SetPosition(_draggingItem.transform.position);
            change.SetChange(prop);
            change.SetType(_draggingItem.gameObject.GetComponent <GUICircuitComponent>().GetType());
            change.RememberConnectorsToFirst(_draggingItem.gameObject.transform.GetChild(0).GetComponent <Connectable>().Connected);
            change.RememberConnectorsToSecond(_draggingItem.gameObject.transform.GetChild(1).GetComponent <Connectable>().Connected);
            undoAction.AddChange(change);
            GUICircuitComponent.globalUndoList.AddUndo(undoAction);
        }
        else if (this.gameObject.tag == "Node")
        {
            _draggingItem       = Instantiate(this.gameObject);
            _draggingItem.tag   = "ActiveNode";
            _draggingItem.layer = 8; //Name of 8th layer is ActiveItem
            _draggingItem.transform.localScale = new Vector3(1, 1, 0);
            _draggingItem.GetComponent <SpriteRenderer>().enabled          = true;
            _draggingItem.GetComponent <SpriteRenderer>().sortingLayerName = "ActiveItem";

            for (int i = 0; i < _draggingItem.transform.childCount; i++)
            {
                _draggingItem.transform.GetChild(i).GetComponent <SpriteRenderer>().sortingLayerName     = "ActiveItem";
                _draggingItem.transform.GetChild(i).GetComponent <SpriteRenderer>().transform.localScale = new Vector3(1, 1, 0);
                _draggingItem.transform.GetChild(i).GetComponent <SpriteRenderer>().enabled = true;
                _draggingItem.transform.GetChild(i).gameObject.layer = 8;
            }

            // Newly created component needs to be selected otherwise an error will occur
            SelectObject.AddItemToSelection(_draggingItem);
        }
        else if (SelectObject.SelectedObjects.Count == 0 || SelectObject.SelectedObjects.Count == 1 && SelectObject.SelectedObjects[0] == this.gameObject)
        {
            _draggingItem = this.gameObject;
            if (SelectObject.SelectedObjects.Count == 0)
            {
                // Select new object.
                SelectObject.SelectedObjects.Add(_draggingItem);
                _draggingItem.GetComponent <SelectObject>().SelectionBox.GetComponent <SpriteRenderer>().enabled = true;
            }

            // Clear the Properties Window
            script.Clear();

            // Call the script from component that fills the Properties Window
            GUICircuitComponent componentScript = _draggingItem.GetComponent <GUICircuitComponent>();
            componentScript.GetProperties();
        }

        _tbu.EnableToolbarButtons();
    }
Exemple #2
0
    public void DuplicateComponent()
    {
        List <GameObject> cloneComponents = new List <GameObject>();
        List <GameObject> cloneLines      = new List <GameObject>();

        // Get all game objects and find the top-left and bottom-right most components
        foreach (GameObject objectSelected in SelectObject.SelectedObjects)
        {
            if (objectSelected.tag.Equals("ActiveItem") || objectSelected.tag.Equals("ActiveNode"))
            {
                // Instantiate new copy GameObject
                GameObject copy =
                    (GameObject)Instantiate(objectSelected, objectSelected.transform.position, objectSelected.transform.rotation);
                copy.GetComponent <GUICircuitComponent>()
                .CopyValues(objectSelected.GetComponent <GUICircuitComponent>());
                copy.transform.FindChild("SelectionBox").GetComponent <SpriteRenderer>().enabled = false;

                // Clear connections of component's Plus and Minus connectors
                Connectable[] connectableScripts = copy.GetComponentsInChildren <Connectable>();
                foreach (Connectable connectableScript in connectableScripts)
                {
                    connectableScript.Connected.Clear();
                }

                // Add newly created clone to the list of clones
                cloneComponents.Add(copy);
            }
        }

        // Duplicate each line and interconnect clone objects
        foreach (GameObject line in SelectObject.SelectedLines)
        {
            GameObject duplicateLine = Instantiate(line);
            duplicateLine.GetComponent <Line>().KeepColiders();

            // Set the Begin of the duplicated line
            GameObject beginComponent = line.GetComponent <Line>().Begin.transform.parent.gameObject;
            // Get the index of Connector among component children
            int        beginChildConnectorIndex = -1;
            GameObject beginConnector           = line.GetComponent <Line>().Begin;
            for (int i = 0; i < beginComponent.transform.childCount; i++)
            {
                if (beginComponent.transform.GetChild(i).gameObject.GetInstanceID() == beginConnector.GetInstanceID())
                {
                    beginChildConnectorIndex = i;
                }
            }
            // Get index of component in the List of SelectedObjects
            int        beginComponentIndex = SelectObject.SelectedObjects.IndexOf(beginComponent);
            GameObject cloneBegin          = cloneComponents[beginComponentIndex];
            duplicateLine.GetComponent <Line>().Begin = cloneBegin.transform.GetChild(beginChildConnectorIndex).gameObject;

            // Set the End of the duplicated line
            GameObject endComponent = line.GetComponent <Line>().End.transform.parent.gameObject;
            // Get the index of Connector among component children
            int        endChildConnectorIndex = -1;
            GameObject endConnector           = line.GetComponent <Line>().End;
            for (int i = 0; i < endComponent.transform.childCount; i++)
            {
                if (endComponent.transform.GetChild(i).gameObject.GetInstanceID() == endConnector.GetInstanceID())
                {
                    endChildConnectorIndex = i;
                }
            }
            // Get index of component in the List of SelectedObjects
            int        endComponentIndex = SelectObject.SelectedObjects.IndexOf(endComponent);
            GameObject cloneEnd          = cloneComponents[endComponentIndex];
            duplicateLine.GetComponent <Line>().End = cloneEnd.transform.GetChild(endChildConnectorIndex).gameObject;

            // Add references of each other to both newly connected connectors of the duplicated objects
            duplicateLine.GetComponent <Line>().Begin.GetComponent <Connectable>().AddConnected(duplicateLine.GetComponent <Line>().End);
            duplicateLine.GetComponent <Line>().End.GetComponent <Connectable>().AddConnected(duplicateLine.GetComponent <Line>().Begin);

            duplicateLine.GetComponent <Line>().Begin.GetComponent <Connector>().ConnectedConnectors.Add(duplicateLine.GetComponent <Line>().End.GetComponent <Connector>());
            duplicateLine.GetComponent <Line>().End.GetComponent <Connector>().ConnectedConnectors.Add(duplicateLine.GetComponent <Line>().Begin.GetComponent <Connector>());

            // Add new line to the list of duplicated lines
            cloneLines.Add(duplicateLine);
        }

        SelectObject selectionComponent = GameObject.Find("Canvas").GetComponent <SelectObject>();

        selectionComponent.DeselectObject();
        selectionComponent.DeselectLine();
        SelectObject.AddItemsToSelection(cloneComponents);
        SelectObject.AddLinesToSelection(cloneLines);

        // Check for collisions - duplicated are placed on the same position as their originals so there MUST BE a collision
        GetComponent <Draggable>().Colision();

        UndoAction undoAction = new UndoAction();

        foreach (GameObject objectSelected in SelectObject.SelectedObjects)
        {
            if (objectSelected.tag.Equals("ActiveItem") || objectSelected.tag.Equals("ActiveNode"))
            {
                GUICircuitComponent component = objectSelected.GetComponent <GUICircuitComponent>();
                List <float>        prop      = new List <float>();
                prop.Add((float)1.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);
            }
        }
        GUICircuitComponent.globalUndoList.AddUndo(undoAction);
    }
Exemple #3
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);
        }
    }