using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace Subsurface.Items.Components { class Connection { private static Sprite connector; private static Sprite wireCorner, wireVertical, wireHorizontal; //how many wires can be linked to a single connector private const int MaxLinked = 5; public readonly string name; public Wire[] wires; private Item item; public readonly bool isOutput; private static Item draggingConnected; int[] wireId; public List Recipients { get { List recipients = new List(); for (int i = 0; i-1) { Inventory.draggingItem = c.wires[linkIndex].Item; } } //outputs are drawn at the right side of the panel, inputs at the left if (c.isOutput) { c.Draw(spriteBatch, panel.Item, rightPos, new Vector2(rightPos.X + 20, rightPos.Y), new Vector2(rightWireX, y + height), mouseInRect); rightPos.Y += 30; rightWireX += wireInterval; } else { c.Draw(spriteBatch, panel.Item, leftPos, new Vector2(leftPos.X - 100, leftPos.Y), new Vector2(leftWireX, y + height), mouseInRect); leftPos.Y += 30; leftWireX -= wireInterval; } } //draw a wire for all the items that are linked to this item, but not to any of the signal connections //foreach (MapEntity entity in panel.Item.linkedTo) //{ // Item linked = entity as Item; // if (linked == null) continue; // //if the item is already connected, don't draw it again // if (panel.connections.Find(c => c.linked.Contains()) != null) continue; // DrawWire(spriteBatch, false, linked, // new Vector2(leftPos.X + (leftPos.Y - y), y + height- 50), // new Vector2(leftPos.X + (leftPos.Y - y), y + height), mouseInRect); // leftPos.Y += 30.0f; //} //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(); if (wireComponent != null && panel.connections.Find(c => c.wires.Contains(wireComponent)) == null) { DrawWire(spriteBatch, selectedItem, selectedItem, new Vector2(x + width / 2, y + height - 100), new Vector2(x + width / 2, y + height), mouseInRect); if (draggingConnected == selectedItem) Inventory.draggingItem = selectedItem; break; } } //stop dragging a wire item if cursor is outside the panel if (mouseInRect) Inventory.draggingItem = null; if (draggingConnected != null) { if (!PlayerInput.LeftButtonDown()) { panel.Item.NewComponentEvent(panel, true); draggingConnected = null; } } } private void Draw(SpriteBatch spriteBatch, Item item, Vector2 position, Vector2 labelPos, Vector2 wirePosition, bool mouseIn) { spriteBatch.DrawString(GUI.font, name, new Vector2(labelPos.X, labelPos.Y-10), Color.White); GUI.DrawRectangle(spriteBatch, new Rectangle((int)position.X-10, (int)position.Y-10, 20, 20), Color.White); for (int i = 0; i see if the wire can be connected to this connection if (draggingConnected != null && !PlayerInput.LeftButtonDown()) { //close enough to the connector -> make a new connection if (Vector2.Distance(position, PlayerInput.MousePosition) < 10.0f) { //find an empty cell for the new connection int index = FindWireIndex(null); Wire wireComponent = draggingConnected.GetComponent(); if (index>-1 && wireComponent!=null && !wires.Contains(wireComponent)) { wires[index] = wireComponent; wireComponent.Connect(this); } } //far away -> disconnect if the wire is linked to this connector else { int index = FindWireIndex(draggingConnected); if (index>-1) { wires[index].RemoveConnection(this); wires[index] = null; } } } } private static void DrawWire(SpriteBatch spriteBatch, Item wireItem, Item item, Vector2 end, Vector2 start, bool mouseIn) { if (draggingConnected == wireItem) { if (!mouseIn) return; end = PlayerInput.MousePosition; } bool mouseOn = false; int textX = (int)start.X; float connLength = 10.0f; if (Math.Abs(end.X-start.X) start.X) ? -1.0f : 1.0f; wireCorner.Draw(spriteBatch, new Vector2(start.X, end.Y), 0.0f, 1.0f, (end.X > start.X) ? SpriteEffects.None : SpriteEffects.FlipHorizontally); float wireStartX = start.X - wireCorner.size.X / 2 * dir; float wireEndX = end.X + connLength * dir; pos = new Vector2(Math.Min(wireStartX,wireEndX), end.Y - wireVertical.size.Y / 2); size = new Vector2(Math.Abs(wireStartX - wireEndX), wireHorizontal.size.Y); wireHorizontal.DrawTiled(spriteBatch, pos, size, Color.White); rect = new Rectangle((int)pos.X, (int)pos.Y, (int)size.X, (int)size.Y); if (rect.Contains(PlayerInput.MousePosition)) mouseOn = true; connector.Draw(spriteBatch, end, -MathHelper.PiOver2*dir); } if (draggingConnected == null) { if (mouseOn || Vector2.Distance(end, PlayerInput.MousePosition)<20.0f) { item.IsHighlighted = true; //start dragging the wire if (PlayerInput.LeftButtonDown()) draggingConnected = wireItem; } } spriteBatch.DrawString(GUI.font, item.Name, new Vector2(textX, start.Y-30), mouseOn ? Color.Gold : Color.White, MathHelper.PiOver2, GUI.font.MeasureString(item.Name)*0.5f, 1.0f, SpriteEffects.None, 0.0f); } public void Save(XElement parentElement) { XElement newElement = new XElement(isOutput ? "output" : "input", new XAttribute("name", name)); for (int i = 0; i < MaxLinked; i++ ) { if (wires[i] == null) continue; Connection recipient = wires[i].OtherConnection(this); //int connectionIndex = recipient.item.Connections.FindIndex(x => x == recipient); newElement.Add(new XElement("link", new XAttribute("w", (wires[i] == null) ? "-1" : wires[i].Item.ID.ToString()))); } parentElement.Add(newElement); } public void ConnectLinked() { if (wireId == null) return; for (int i = 0; i < MaxLinked; i++) { if (wireId[i] == -1) continue; Item wireItem = MapEntity.FindEntityByID(wireId[i]) as Item; if (wireItem == null) continue; wires[i] = wireItem.GetComponent(); if (wires[i]!=null) { wires[i].Item.body.Enabled = false; wires[i].Connect(this, false); } } wireId = null; } } }