Esempio n. 1
0
        public static void DrawConnections(SpriteBatch spriteBatch, ConnectionPanel panel, Character character)
        {
            Rectangle panelRect = panel.GuiFrame.Rect;
            int       x = panelRect.X, y = panelRect.Y;
            int       width = panelRect.Width, height = panelRect.Height;

            Vector2 scale = new Vector2(GUI.Scale);

            if (panel.GuiFrame.RectTransform.MaxSize.X < int.MaxValue)
            {
                scale.X = panel.GuiFrame.RectTransform.MaxSize.X / panel.GuiFrame.Rect.Width;
            }
            if (panel.GuiFrame.RectTransform.MaxSize.Y < int.MaxValue)
            {
                scale.Y = panel.GuiFrame.RectTransform.MaxSize.Y / panel.GuiFrame.Rect.Height;
            }

            bool mouseInRect = panelRect.Contains(PlayerInput.MousePosition);

            int totalWireCount = 0;

            foreach (Connection c in panel.Connections)
            {
                totalWireCount += c.Wires.Count(w => w != null);
            }

            Wire equippedWire = null;

            bool allowRewiring = GameMain.NetworkMember?.ServerSettings == null || GameMain.NetworkMember.ServerSettings.AllowRewiring || panel.AlwaysAllowRewiring;

            if (allowRewiring && (!panel.Locked || Screen.Selected == GameMain.SubEditorScreen))
            {
                //if the Character using the panel has a wire item equipped
                //and the wire hasn't been connected yet, draw it on the panel
                for (int i = 0; i < character.SelectedItems.Length; i++)
                {
                    Item selectedItem = character.SelectedItems[i];

                    if (selectedItem == null)
                    {
                        continue;
                    }

                    Wire wireComponent = selectedItem.GetComponent <Wire>();
                    if (wireComponent != null)
                    {
                        equippedWire = wireComponent;
                    }
                }
            }

            //two passes: first the connector, then the wires to get the wires to render in front
            for (int i = 0; i < 2; i++)
            {
                Vector2 rightPos = new Vector2(x + width - 80 * scale.X, y + 60 * scale.Y);
                Vector2 leftPos  = new Vector2(x + 80 * scale.X, y + 60 * scale.Y);

                Vector2 rightWirePos = new Vector2(x + width - 5 * scale.X, y + 30 * scale.Y);
                Vector2 leftWirePos  = new Vector2(x + 5 * scale.X, y + 30 * scale.Y);

                int wireInterval           = (height - (int)(20 * scale.Y)) / Math.Max(totalWireCount, 1);
                int connectorIntervalLeft  = (height - (int)(100 * scale.Y)) / Math.Max(panel.Connections.Count(c => c.IsOutput), 1);
                int connectorIntervalRight = (height - (int)(100 * scale.Y)) / Math.Max(panel.Connections.Count(c => !c.IsOutput), 1);

                foreach (Connection c in panel.Connections)
                {
                    //if dragging a wire, let the Inventory know so that the wire can be
                    //dropped or dragged from the panel to the players inventory
                    if (DraggingConnected != null && i == 1)
                    {
                        //the wire can only be dragged out if it's not connected to anything at the other end
                        if (Screen.Selected == GameMain.SubEditorScreen ||
                            (DraggingConnected.Connections[0] == null && DraggingConnected.Connections[1] == null) ||
                            (DraggingConnected.Connections.Contains(c) && DraggingConnected.Connections.Contains(null)))
                        {
                            int linkIndex = c.FindWireIndex(DraggingConnected.Item);
                            if (linkIndex > -1 || panel.DisconnectedWires.Contains(DraggingConnected))
                            {
                                Inventory.draggingItem = DraggingConnected.Item;
                            }
                        }
                    }

                    //outputs are drawn at the right side of the panel, inputs at the left
                    if (c.IsOutput)
                    {
                        if (i == 0)
                        {
                            c.DrawConnection(spriteBatch, panel, rightPos,
                                             new Vector2(rightPos.X - GUI.SmallFont.MeasureString(c.DisplayName.ToUpper()).X - 25 * panel.Scale, rightPos.Y + 5 * panel.Scale),
                                             scale);
                        }
                        else
                        {
                            c.DrawWires(spriteBatch, panel, rightPos, rightWirePos, mouseInRect, equippedWire, wireInterval);
                        }

                        rightPos.Y     += connectorIntervalLeft;
                        rightWirePos.Y += c.Wires.Count(w => w != null) * wireInterval;
                    }
                    else
                    {
                        if (i == 0)
                        {
                            c.DrawConnection(spriteBatch, panel, leftPos,
                                             new Vector2(leftPos.X + 25 * panel.Scale, leftPos.Y - 5 * panel.Scale - GUI.SmallFont.MeasureString(c.DisplayName.ToUpper()).Y),
                                             scale);
                        }
                        else
                        {
                            c.DrawWires(spriteBatch, panel, leftPos, leftWirePos, mouseInRect, equippedWire, wireInterval);
                        }

                        leftPos.Y     += connectorIntervalRight;
                        leftWirePos.Y += c.Wires.Count(w => w != null) * wireInterval;
                    }
                }
            }


            if (DraggingConnected != null)
            {
                if (mouseInRect)
                {
                    DrawWire(spriteBatch, DraggingConnected, PlayerInput.MousePosition, new Vector2(x + width / 2, y + height - 10), null, panel, "");
                }
                panel.TriggerRewiringSound();

                if (!PlayerInput.PrimaryMouseButtonHeld())
                {
                    if (GameMain.NetworkMember != null || panel.CheckCharacterSuccess(character))
                    {
                        if (DraggingConnected.Connections[0]?.ConnectionPanel == panel ||
                            DraggingConnected.Connections[1]?.ConnectionPanel == panel)
                        {
                            DraggingConnected.RemoveConnection(panel.Item);
                            if (DraggingConnected.Item.ParentInventory == null)
                            {
                                panel.DisconnectedWires.Add(DraggingConnected);
                            }
                            else if (DraggingConnected.Connections[0] == null && DraggingConnected.Connections[1] == null)
                            {
                                DraggingConnected.ClearConnections(user: Character.Controlled);
                            }
                        }
                    }

                    if (GameMain.Client != null)
                    {
                        panel.Item.CreateClientEvent(panel);
                    }

                    DraggingConnected = null;
                }
            }

            //if the Character using the panel has a wire item equipped
            //and the wire hasn't been connected yet, draw it on the panel
            if (equippedWire != null && (DraggingConnected != equippedWire || !mouseInRect))
            {
                if (panel.Connections.Find(c => c.Wires.Contains(equippedWire)) == null)
                {
                    DrawWire(spriteBatch, equippedWire, new Vector2(x + width / 2, y + height - 150 * GUI.Scale),
                             new Vector2(x + width / 2, y + height),
                             null, panel, "");

                    if (DraggingConnected == equippedWire)
                    {
                        Inventory.draggingItem = equippedWire.Item;
                    }
                }
            }


            float step = (width * 0.75f) / panel.DisconnectedWires.Count();

            x = (int)(x + width / 2 - step * (panel.DisconnectedWires.Count() - 1) / 2);
            foreach (Wire wire in panel.DisconnectedWires)
            {
                if (wire == DraggingConnected && mouseInRect)
                {
                    continue;
                }
                if (wire.HiddenInGame && Screen.Selected == GameMain.GameScreen)
                {
                    continue;
                }

                Connection recipient = wire.OtherConnection(null);
                string     label     = recipient == null ? "" : recipient.item.Name + $" ({recipient.DisplayName})";
                if (wire.Locked)
                {
                    label += "\n" + TextManager.Get("ConnectionLocked");
                }
                DrawWire(spriteBatch, wire, new Vector2(x, y + height - 100 * GUI.Scale),
                         new Vector2(x, y + height),
                         null, panel, label);
                x += (int)step;
            }

            //stop dragging a wire item if the cursor is within any connection panel
            //(so we don't drop the item when dropping the wire on a connection)
            if (mouseInRect || (GUI.MouseOn?.UserData is ConnectionPanel && GUI.MouseOn.MouseRect.Contains(PlayerInput.MousePosition)))
            {
                Inventory.draggingItem = null;
            }
        }
Esempio n. 2
0
        private static void DrawWire(SpriteBatch spriteBatch, Wire wire, Vector2 end, Vector2 start, Wire equippedWire, ConnectionPanel panel, string label)
        {
            int textX = (int)start.X;

            if (start.X < end.X)
            {
                textX -= 10;
            }
            else
            {
                textX += 10;
            }

            bool canDrag = equippedWire == null || equippedWire == wire;

            float alpha = canDrag ? 1.0f : 0.5f;

            bool mouseOn =
                canDrag &&
                ((PlayerInput.MousePosition.X > Math.Min(start.X, end.X) &&
                  PlayerInput.MousePosition.X < Math.Max(start.X, end.X) &&
                  MathUtils.LineToPointDistanceSquared(start, end, PlayerInput.MousePosition) < 36) ||
                 Vector2.Distance(end, PlayerInput.MousePosition) < 20.0f ||
                 new Rectangle((start.X < end.X) ? textX - 100 : textX, (int)start.Y - 5, 100, 14).Contains(PlayerInput.MousePosition));

            if (!string.IsNullOrEmpty(label))
            {
                if (start.Y > panel.GuiFrame.Rect.Bottom - 1.0f)
                {
                    //wire at the bottom of the panel -> draw the text below the panel, tilted 45 degrees
                    GUI.Font.DrawString(spriteBatch, label, start + Vector2.UnitY * 20 * GUI.Scale, Color.White, 45.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                }
                else
                {
                    GUI.DrawString(spriteBatch,
                                   new Vector2(start.X < end.X ? textX - GUI.SmallFont.MeasureString(label).X : textX, start.Y - 5.0f),
                                   label,
                                   wire.Locked ? GUI.Style.TextColorDim : (mouseOn ? Wire.higlightColor : GUI.Style.TextColor), Color.Black * 0.9f,
                                   3, GUI.SmallFont);
                }
            }

            var wireEnd = end + Vector2.Normalize(start - end) * 30.0f * panel.Scale;

            float dist = Vector2.Distance(start, wireEnd);

            float wireWidth = 12 * panel.Scale;
            float highlight = 5 * panel.Scale;

            if (mouseOn)
            {
                spriteBatch.Draw(wireVertical.Texture, new Rectangle(wireEnd.ToPoint(), new Point((int)(wireWidth + highlight), (int)dist)), wireVertical.SourceRect,
                                 Wire.higlightColor,
                                 MathUtils.VectorToAngle(end - start) + MathHelper.PiOver2,
                                 new Vector2(wireVertical.size.X / 2, 0), // point in line about which to rotate
                                 SpriteEffects.None,
                                 0.0f);
            }
            spriteBatch.Draw(wireVertical.Texture, new Rectangle(wireEnd.ToPoint(), new Point((int)wireWidth, (int)dist)), wireVertical.SourceRect,
                             wire.Item.Color * alpha,
                             MathUtils.VectorToAngle(end - start) + MathHelper.PiOver2,
                             new Vector2(wireVertical.size.X / 2, 0), // point in line about which to rotate
                             SpriteEffects.None,
                             0.0f);

            float connectorScale = wireWidth / (float)wireVertical.SourceRect.Width;

            connector.Draw(spriteBatch, end, Color.White, connector.Origin, MathUtils.VectorToAngle(end - start) + MathHelper.PiOver2, scale: connectorScale);

            if (DraggingConnected == null && canDrag)
            {
                if (mouseOn)
                {
                    ConnectionPanel.HighlightedWire = wire;

                    bool allowRewiring = GameMain.NetworkMember?.ServerSettings == null || GameMain.NetworkMember.ServerSettings.AllowRewiring || panel.AlwaysAllowRewiring;
                    if (allowRewiring && (!wire.Locked && !panel.Locked || Screen.Selected == GameMain.SubEditorScreen))
                    {
                        //start dragging the wire
                        if (PlayerInput.PrimaryMouseButtonHeld())
                        {
                            DraggingConnected = wire;
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private void DrawWires(SpriteBatch spriteBatch, ConnectionPanel panel, Vector2 position, Vector2 wirePosition, bool mouseIn, Wire equippedWire, float wireInterval)
        {
            float connectorSpriteScale = (35.0f / connectionSprite.SourceRect.Width) * panel.Scale;

            for (int i = 0; i < MaxLinked; i++)
            {
                if (wires[i] == null || wires[i].Hidden || (DraggingConnected == wires[i] && (mouseIn || Screen.Selected == GameMain.SubEditorScreen)))
                {
                    continue;
                }
                if (wires[i].HiddenInGame && Screen.Selected == GameMain.GameScreen)
                {
                    continue;
                }

                Connection recipient = wires[i].OtherConnection(this);
                string     label     = recipient == null ? "" : recipient.item.Name + $" ({recipient.DisplayName})";
                if (wires[i].Locked)
                {
                    label += "\n" + TextManager.Get("ConnectionLocked");
                }
                DrawWire(spriteBatch, wires[i], position, wirePosition, equippedWire, panel, label);

                wirePosition.Y += wireInterval;
            }

            if (DraggingConnected != null && Vector2.Distance(position, PlayerInput.MousePosition) < (20.0f * GUI.Scale))
            {
                connectionSpriteHighlight.Draw(spriteBatch, position, scale: connectorSpriteScale);

                if (!PlayerInput.PrimaryMouseButtonHeld())
                {
                    if (GameMain.NetworkMember != null || panel.CheckCharacterSuccess(Character.Controlled))
                    {
                        //find an empty cell for the new connection
                        int index = FindEmptyIndex();
                        if (index > -1 && !Wires.Contains(DraggingConnected))
                        {
                            bool alreadyConnected = DraggingConnected.IsConnectedTo(panel.Item);
                            DraggingConnected.RemoveConnection(panel.Item);
                            if (DraggingConnected.Connect(this, !alreadyConnected, true))
                            {
                                var otherConnection = DraggingConnected.OtherConnection(this);
                                SetWire(index, DraggingConnected);
                            }
                        }
                    }

                    if (GameMain.Client != null)
                    {
                        panel.Item.CreateClientEvent(panel);
                    }
                    DraggingConnected = null;
                }
            }

            if (FlashTimer > 0.0f)
            {
                //the number of flashes depends on the duration, 1 flash per 1 full second
                int   flashCycleCount    = (int)Math.Max(flashDuration, 1);
                float flashCycleDuration = flashDuration / flashCycleCount;

                //MathHelper.Pi * 0.8f -> the curve goes from 144 deg to 0,
                //i.e. quickly bumps up from almost full brightness to full and then fades out
                connectionSpriteHighlight.Draw(spriteBatch, position,
                                               flashColor * (float)Math.Sin(FlashTimer % flashCycleDuration / flashCycleDuration * MathHelper.Pi * 0.8f), scale: connectorSpriteScale);
            }

            if (Wires.Any(w => w != null && w != DraggingConnected && !w.Hidden && (!w.HiddenInGame || Screen.Selected != GameMain.GameScreen)))
            {
                int screwIndex = (int)Math.Floor(position.Y / 30.0f) % screwSprites.Count;
                screwSprites[screwIndex].Draw(spriteBatch, position, scale: connectorSpriteScale);
            }
        }
Esempio n. 4
0
        public Connection(XElement element, ConnectionPanel connectionPanel)
        {
#if CLIENT
            if (connector == null)
            {
                connector                 = GUI.Style.GetComponentStyle("ConnectionPanelConnector").Sprites[GUIComponent.ComponentState.None][0].Sprite;
                wireVertical              = GUI.Style.GetComponentStyle("ConnectionPanelWire").Sprites[GUIComponent.ComponentState.None][0].Sprite;
                connectionSprite          = GUI.Style.GetComponentStyle("ConnectionPanelConnection").Sprites[GUIComponent.ComponentState.None][0].Sprite;
                connectionSpriteHighlight = GUI.Style.GetComponentStyle("ConnectionPanelConnection").Sprites[GUIComponent.ComponentState.Hover][0].Sprite;
                screwSprites              = GUI.Style.GetComponentStyle("ConnectionPanelScrew").Sprites[GUIComponent.ComponentState.None].Select(s => s.Sprite).ToList();
            }
#endif
            ConnectionPanel = connectionPanel;
            item            = connectionPanel.Item;

            wires = new Wire[MaxLinked];

            IsOutput = element.Name.ToString() == "output";
            Name     = element.GetAttributeString("name", IsOutput ? "output" : "input");

            string displayNameTag = "", fallbackTag = "";
            //if displayname is not present, attempt to find it from the prefab
            if (element.Attribute("displayname") == null)
            {
                foreach (XElement subElement in item.Prefab.ConfigElement.Elements())
                {
                    if (subElement.Name.ToString().ToLowerInvariant() != "connectionpanel")
                    {
                        continue;
                    }

                    foreach (XElement connectionElement in subElement.Elements())
                    {
                        string prefabConnectionName = element.GetAttributeString("name", null);
                        if (prefabConnectionName == Name)
                        {
                            displayNameTag = connectionElement.GetAttributeString("displayname", "");
                            fallbackTag    = connectionElement.GetAttributeString("fallbackdisplayname", "");
                        }
                    }
                }
            }
            else
            {
                displayNameTag = element.GetAttributeString("displayname", "");
                fallbackTag    = element.GetAttributeString("fallbackdisplayname", null);
            }

            if (!string.IsNullOrEmpty(displayNameTag))
            {
                //extract the tag parts in case the tags contains variables
                string tagWithoutVariables         = displayNameTag?.Split('~')?.FirstOrDefault();
                string fallbackTagWithoutVariables = fallbackTag?.Split('~')?.FirstOrDefault();
                //use displayNameTag if found, otherwise fallBack
                if (TextManager.ContainsTag(tagWithoutVariables))
                {
                    DisplayName = TextManager.GetServerMessage(displayNameTag);
                }
                else if (TextManager.ContainsTag(fallbackTagWithoutVariables))
                {
                    DisplayName = TextManager.GetServerMessage(fallbackTag);
                }
            }

            if (string.IsNullOrEmpty(DisplayName))
            {
#if DEBUG
                DebugConsole.ThrowError("Missing display name in connection " + item.Name + ": " + Name);
#endif
                DisplayName = Name;
            }

            IsPower = Name == "power_in" || Name == "power" || Name == "power_out";

            Effects = new List <StatusEffect>();

            wireId = new ushort[MaxLinked];

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "link":
                    int index = -1;
                    for (int i = 0; i < MaxLinked; i++)
                    {
                        if (wireId[i] < 1)
                        {
                            index = i;
                        }
                    }
                    if (index == -1)
                    {
                        break;
                    }

                    int id = subElement.GetAttributeInt("w", 0);
                    if (id < 0)
                    {
                        id = 0;
                    }
                    wireId[index] = (ushort)id;

                    break;

                case "statuseffect":
                    Effects.Add(StatusEffect.Load(subElement, item.Name + ", connection " + Name));
                    break;
                }
            }
        }
Esempio n. 5
0
 private static int GetConnectorIntervalRight(int height, Vector2 scale, ConnectionPanel panel)
 {
     return((height - (int)(100 * scale.Y)) / Math.Max(panel.Connections.Count(c => !c.IsOutput), 1));
 }
Esempio n. 6
0
 private static Vector2 GetOutputLabelPosition(Vector2 connectorPosition, ConnectionPanel panel, Connection connection)
 {
     return(new Vector2(
                connectorPosition.X - 25 * panel.Scale - GUI.SmallFont.MeasureString(connection.DisplayName.ToUpper()).X,
                connectorPosition.Y + 5 * panel.Scale));
 }
Esempio n. 7
0
        public static bool CheckConnectionLabelOverlap(ConnectionPanel panel, out Point newRectSize)
        {
            Rectangle panelRect = panel.GuiFrame.Rect;
            int       x = panelRect.X, y = panelRect.Y;
            Vector2   scale                  = GetScale(panel.GuiFrame.RectTransform.MaxSize, panel.GuiFrame.Rect.Size);
            Vector2   rightPos               = GetRightPos(x, y, panelRect.Width, scale);
            Vector2   leftPos                = GetLeftPos(x, y, scale);
            int       connectorIntervalLeft  = GetConnectorIntervalLeft(panelRect.Height, scale, panel);
            int       connectorIntervalRight = GetConnectorIntervalRight(panelRect.Height, scale, panel);

            newRectSize = panelRect.Size;
            var labelAreas = new List <Rectangle>();

            for (int i = 0; i < 100; i++)
            {
                labelAreas.Clear();
                foreach (var c in panel.Connections)
                {
                    if (c.IsOutput)
                    {
                        var labelArea = GetLabelArea(GetOutputLabelPosition(rightPos, panel, c), c.DisplayName.ToUpper(), scale);
                        labelAreas.Add(labelArea);
                        rightPos.Y += connectorIntervalLeft;
                    }
                    else
                    {
                        var labelArea = GetLabelArea(GetInputLabelPosition(leftPos, panel, c), c.DisplayName.ToUpper(), scale);
                        labelAreas.Add(labelArea);
                        leftPos.Y += connectorIntervalRight;
                    }
                }
                bool foundOverlap = false;
                for (int j = 0; j < labelAreas.Count; j++)
                {
                    for (int k = 0; k < labelAreas.Count; k++)
                    {
                        if (k == j)
                        {
                            continue;
                        }
                        if (!labelAreas[j].Intersects(labelAreas[k]))
                        {
                            continue;
                        }
                        newRectSize += new Point(10);
                        Point maxSize = new Point(
                            Math.Max(panel.GuiFrame.RectTransform.MaxSize.X, newRectSize.X),
                            Math.Max(panel.GuiFrame.RectTransform.MaxSize.Y, newRectSize.Y));
                        scale    = GetScale(maxSize, newRectSize);
                        rightPos = GetRightPos(x, y, newRectSize.X, scale);
                        leftPos  = GetLeftPos(x, y, scale);
                        connectorIntervalLeft  = GetConnectorIntervalLeft(newRectSize.Y, scale, panel);
                        connectorIntervalRight = GetConnectorIntervalRight(newRectSize.Y, scale, panel);
                        foundOverlap           = true;
                        break;
                    }
                }
                if (!foundOverlap)
                {
                    break;
                }
            }
            return(newRectSize.X != panel.GuiFrame.Rect.Width || newRectSize.Y > panel.GuiFrame.Rect.Height);
        }