Exemplo n.º 1
0
        public static void UpdateEditing(List <Wire> wires)
        {
            //dragging a node of some wire
            if (draggingWire != null)
            {
                //cancel dragging
                if (!PlayerInput.LeftButtonHeld())
                {
                    draggingWire      = null;
                    selectedNodeIndex = null;
                }
                //update dragging
                else
                {
                    MapEntity.DisableSelect = true;

                    Submarine sub = null;
                    if (draggingWire.connections[0] != null && draggingWire.connections[0].Item.Submarine != null)
                    {
                        sub = draggingWire.connections[0].Item.Submarine;
                    }
                    if (draggingWire.connections[1] != null && draggingWire.connections[1].Item.Submarine != null)
                    {
                        sub = draggingWire.connections[1].Item.Submarine;
                    }

                    Vector2 nodeWorldPos = GameMain.SubEditorScreen.Cam.ScreenToWorld(PlayerInput.MousePosition) - sub.HiddenSubPosition - sub.Position;// Nodes[(int)selectedNodeIndex];

                    nodeWorldPos.X = MathUtils.Round(nodeWorldPos.X, Submarine.GridSize.X / 2.0f);
                    nodeWorldPos.Y = MathUtils.Round(nodeWorldPos.Y, Submarine.GridSize.Y / 2.0f);

                    draggingWire.nodes[(int)selectedNodeIndex] = nodeWorldPos;
                    draggingWire.UpdateSections();

                    MapEntity.SelectEntity(draggingWire.item);
                }

                return;
            }

            //a wire has been selected -> check if we should start dragging one of the nodes
            float nodeSelectDist = 10, sectionSelectDist = 5;

            highlightedNodeIndex = null;
            if (MapEntity.SelectedList.Count == 1 && MapEntity.SelectedList[0] is Item)
            {
                Wire selectedWire = ((Item)MapEntity.SelectedList[0]).GetComponent <Wire>();

                if (selectedWire != null)
                {
                    Vector2 mousePos = GameMain.SubEditorScreen.Cam.ScreenToWorld(PlayerInput.MousePosition);
                    if (selectedWire.item.Submarine != null)
                    {
                        mousePos -= (selectedWire.item.Submarine.Position + selectedWire.item.Submarine.HiddenSubPosition);
                    }

                    //left click while holding ctrl -> check if the cursor is on a wire section,
                    //and add a new node if it is
                    if (PlayerInput.KeyDown(Keys.RightControl) || PlayerInput.KeyDown(Keys.LeftControl))
                    {
                        if (PlayerInput.LeftButtonClicked())
                        {
                            float temp = 0.0f;
                            int   closestSectionIndex = selectedWire.GetClosestSectionIndex(mousePos, sectionSelectDist, out temp);

                            if (closestSectionIndex > -1)
                            {
                                selectedWire.nodes.Insert(closestSectionIndex + 1, mousePos);
                                selectedWire.UpdateSections();
                            }
                        }
                    }
                    else
                    {
                        //check if close enough to a node
                        float temp         = 0.0f;
                        int   closestIndex = selectedWire.GetClosestNodeIndex(mousePos, nodeSelectDist, out temp);
                        if (closestIndex > -1)
                        {
                            highlightedNodeIndex = closestIndex;
                            //start dragging the node
                            if (PlayerInput.LeftButtonHeld())
                            {
                                draggingWire      = selectedWire;
                                selectedNodeIndex = closestIndex;
                            }
                            //remove the node
                            else if (PlayerInput.RightButtonClicked() && closestIndex > 0 && closestIndex < selectedWire.nodes.Count - 1)
                            {
                                selectedWire.nodes.RemoveAt(closestIndex);
                                selectedWire.UpdateSections();
                            }
                        }
                    }
                }
            }

            //check which wire is highlighted with the cursor
            Wire  highlighted = null;
            float closestDist = float.PositiveInfinity;

            foreach (Wire w in wires)
            {
                Vector2 mousePos = GameMain.SubEditorScreen.Cam.ScreenToWorld(PlayerInput.MousePosition);
                if (w.item.Submarine != null)
                {
                    mousePos -= (w.item.Submarine.Position + w.item.Submarine.HiddenSubPosition);
                }

                float dist            = 0.0f;
                int   highlightedNode = w.GetClosestNodeIndex(mousePos, highlighted == null ? nodeSelectDist : closestDist, out dist);
                if (highlightedNode > -1)
                {
                    if (dist < closestDist)
                    {
                        highlightedNodeIndex = highlightedNode;
                        highlighted          = w;
                        closestDist          = dist;
                    }
                }

                if (w.GetClosestSectionIndex(mousePos, highlighted == null ? sectionSelectDist : closestDist, out dist) > -1)
                {
                    //prefer nodes over sections
                    if (dist + nodeSelectDist * 0.5f < closestDist)
                    {
                        highlightedNodeIndex = null;
                        highlighted          = w;
                        closestDist          = dist + nodeSelectDist * 0.5f;
                    }
                }
            }

            if (highlighted != null)
            {
                highlighted.item.IsHighlighted = true;

                if (PlayerInput.LeftButtonClicked())
                {
                    MapEntity.DisableSelect = true;
                    MapEntity.SelectEntity(highlighted.item);
                }
            }
        }