Wires can be cloned and moved if both items it's connected to are selected

This commit is contained in:
Regalis
2016-11-12 15:51:46 +02:00
parent d403b38440
commit 7fa660d38e
9 changed files with 107 additions and 45 deletions

View File

@@ -267,7 +267,7 @@ namespace Barotrauma.Items.Components
item.NewComponentEvent(this, true, true);
}
Drawable = Nodes.Any();
Drawable = sections.Count > 0;
}
public override bool Pick(Character picker)
@@ -277,6 +277,20 @@ namespace Barotrauma.Items.Components
return true;
}
public override void Move(Vector2 amount)
{
if (item.IsSelected) MoveNodes(amount);
}
public void MoveNodes(Vector2 amount)
{
for (int i = 0; i < Nodes.Count; i++)
{
Nodes[i] += amount;
}
UpdateSections();
}
public void UpdateSections()
{
sections.Clear();
@@ -285,6 +299,7 @@ namespace Barotrauma.Items.Components
{
sections.Add(new WireSection(Nodes[i], Nodes[i + 1]));
}
Drawable = sections.Count > 0;
}
private void ClearConnections()
@@ -303,7 +318,7 @@ namespace Barotrauma.Items.Components
connections[i] = null;
}
Drawable = false;
Drawable = sections.Count > 0;
}
private Vector2 RoundNode(Vector2 position, Hull hull)
@@ -365,7 +380,7 @@ namespace Barotrauma.Items.Components
public void Draw(SpriteBatch spriteBatch, bool editing)
{
if (!Nodes.Any())
if (sections.Count == 0)
{
Drawable = false;
return;
@@ -386,6 +401,13 @@ namespace Barotrauma.Items.Components
section.Draw(spriteBatch, Color.Gold, drawOffset, depth, 0.5f);
}
}
else if (item.IsSelected)
{
foreach (WireSection section in sections)
{
section.Draw(spriteBatch, Color.Red, drawOffset, depth, 0.5f);
}
}
foreach (WireSection section in sections)
{

View File

@@ -423,12 +423,21 @@ namespace Barotrauma
clone.properties[property.Key].TrySetValue(property.Value.GetValue());
}
if (ContainedItems!=null)
for (int i = 0; i < components.Count; i++)
{
foreach (KeyValuePair<string, ObjectProperty> property in components[i].properties)
{
if (!property.Value.Attributes.OfType<Editable>().Any()) continue;
clone.components[i].properties[property.Key].TrySetValue(property.Value.GetValue());
}
}
if (ContainedItems != null)
{
foreach (Item containedItem in ContainedItems)
{
var containedClone = containedItem.Clone();
clone.ownInventory.TryPutItem(containedItem);
clone.ownInventory.TryPutItem(containedClone as Item);
}
}
@@ -880,7 +889,7 @@ namespace Barotrauma
public override void Draw(SpriteBatch spriteBatch, bool editing, bool back = true)
{
if (!Visible) return;
Color color = (isSelected && editing) ? color = Color.Red : spriteColor;
Color color = (IsSelected && editing) ? color = Color.Red : spriteColor;
if (isHighlighted) color = Color.Orange;
SpriteEffects oldEffects = prefab.sprite.effects;
@@ -943,7 +952,7 @@ namespace Barotrauma
return;
}
if (isSelected || isHighlighted)
if (IsSelected || isHighlighted)
{
GUI.DrawRectangle(spriteBatch, new Vector2(DrawPosition.X - rect.Width / 2, -(DrawPosition.Y+rect.Height/2)), new Vector2(rect.Width, rect.Height), Color.Green,false,0,(int)Math.Max((1.5f/GameScreen.Selected.Cam.Zoom),1.0f));

View File

@@ -609,7 +609,7 @@ namespace Barotrauma
}
if ((isSelected || isHighlighted) && editing)
if ((IsSelected || isHighlighted) && editing)
{
GUI.DrawRectangle(spriteBatch,
new Vector2(drawRect.X + 5, -drawRect.Y + 5),

View File

@@ -111,7 +111,7 @@ namespace Barotrauma
if (!editing || wallVertices == null) return;
Color color = (isHighlighted) ? Color.Orange : Color.Green;
if (isSelected) color = Color.Red;
if (IsSelected) color = Color.Red;
Vector2 pos = Position;

View File

@@ -8,6 +8,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.ObjectModel;
using Barotrauma.Items.Components;
namespace Barotrauma
{
@@ -16,7 +17,7 @@ namespace Barotrauma
public static List<MapEntity> mapEntityList = new List<MapEntity>();
//which entities have been selected for editing
public static List<MapEntity> selectedList = new List<MapEntity>();
private static List<MapEntity> selectedList = new List<MapEntity>();
private static List<MapEntity> copiedList = new List<MapEntity>();
protected static GUIComponent editingHUD;
@@ -39,7 +40,7 @@ namespace Barotrauma
//is the mouse inside the rect
protected bool isHighlighted;
protected bool isSelected;
//protected bool isSelected;
private static bool disableSelect;
public static bool DisableSelect
@@ -175,8 +176,7 @@ namespace Barotrauma
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; }
get { return selectedList.Contains(this); }
}
protected bool ResizeHorizontal
@@ -219,7 +219,43 @@ namespace Barotrauma
List<MapEntity> clones = new List<MapEntity>();
foreach (MapEntity e in entitiesToClone)
{
Debug.Assert(e != null);
clones.Add(e.Clone());
Debug.Assert(clones.Last() != null);
}
Debug.Assert(clones.Count == entitiesToClone.Count);
//connect clone wires to the clone items
for (int i = 0; i < clones.Count; i++)
{
var cloneItem = clones[i] as Item;
if (cloneItem == null) continue;
var cloneWire = cloneItem.GetComponent<Wire>();
if (cloneWire == null) continue;
var originalWire = ((Item)entitiesToClone[i]).GetComponent<Wire>();
cloneWire.Nodes = new List<Vector2>(originalWire.Nodes);
cloneWire.UpdateSections();
for (int n = 0; n < 2; n++)
{
if (originalWire.Connections[n] == null) continue;
var connectedItem = originalWire.Connections[n].Item;
if (connectedItem == null) continue;
//index of the item the wire is connected to
int itemIndex = entitiesToClone.IndexOf(connectedItem);
//index of the connection in the connectionpanel of the target item
int connectionIndex = connectedItem.Connections.IndexOf(originalWire.Connections[n]);
(clones[itemIndex] as Item).GetComponent<ConnectionPanel>().Connections[connectionIndex].TryAddLink(cloneWire);
cloneWire.Connect((clones[itemIndex] as Item).Connections[connectionIndex], false);
}
}
return clones;
@@ -311,7 +347,6 @@ namespace Barotrauma
foreach (MapEntity e in mapEntityList)
{
e.isHighlighted = false;
e.isSelected = false;
}
if (DisableSelect)
@@ -359,9 +394,8 @@ namespace Barotrauma
clones.ForEach(c => center += c.WorldPosition);
center /= clones.Count;
clones.ForEach(c => c.Move(cam.WorldViewCenter - center));
selectedList = new List<MapEntity>(clones);
selectedList.ForEach(c => c.Move(cam.WorldViewCenter - center));
}
}
@@ -380,18 +414,12 @@ namespace Barotrauma
{
if (e.IsMouseOn(position)) highLightedEntity = e;
}
e.isSelected = false;
}
if (highLightedEntity != null) highLightedEntity.isHighlighted = true;
}
foreach (MapEntity e in selectedList)
{
e.isSelected = true;
}
//started moving selected entities
if (startMovingPos != Vector2.Zero && PlayerInput.LeftButtonReleased())
{
@@ -406,9 +434,8 @@ namespace Barotrauma
if (PlayerInput.KeyDown(Keys.LeftControl) || PlayerInput.KeyDown(Keys.RightControl))
{
var clones = Clone(selectedList);
clones.ForEach(c => c.Move(moveAmount));
selectedList = clones;
selectedList.ForEach(c => c.Move(moveAmount));
}
else // move
{
@@ -463,6 +490,25 @@ namespace Barotrauma
{
selectedList = newSelection;
}
//select wire if both items it's connected to are selected
var selectedItems = selectedList.Where(e => e is Item).Cast<Item>().ToList();
foreach (Item item in selectedItems)
{
if (item.Connections == null) continue;
foreach (Connection c in item.Connections)
{
foreach (Wire w in c.Wires)
{
if (w == null || selectedList.Contains(w.Item)) continue;
if (w.OtherConnection(c) != null && selectedList.Contains(w.OtherConnection(c).Item))
{
selectedList.Add(w.Item);
}
}
}
}
selectionPos = Vector2.Zero;
selectionSize = Vector2.Zero;
@@ -471,7 +517,6 @@ namespace Barotrauma
//default, not doing anything specific yet
else
{
if (PlayerInput.LeftButtonHeld() &&
PlayerInput.KeyUp(Keys.Space))
{
@@ -483,12 +528,10 @@ namespace Barotrauma
selectionPos = position;
}
}
}
}
/// <summary>
/// Draw the "selection rectangle" and outlines of entities that are being dragged (if any)
/// </summary>
@@ -565,10 +608,6 @@ namespace Barotrauma
public static void DeselectAll()
{
foreach (MapEntity e in selectedList)
{
e.isSelected = false;
}
selectedList.Clear();
}
@@ -577,7 +616,6 @@ namespace Barotrauma
{
DeselectAll();
entity.isSelected = true;
selectedList.Add(entity);
}

View File

@@ -495,7 +495,7 @@ namespace Barotrauma
if (prefab.sprite == null) return;
Color color = (isHighlighted) ? Color.Orange : Color.White;
if (isSelected && editing)
if (IsSelected && editing)
{
color = Color.Red;

View File

@@ -1080,13 +1080,7 @@ namespace Barotrauma
if (item.Submarine != this) continue;
var wire = item.GetComponent<Items.Components.Wire>();
if (wire == null) continue;
for (int i = 0; i < wire.Nodes.Count; i++)
{
wire.Nodes[i] -= center;
}
wire.UpdateSections();
if (wire != null) wire.MoveNodes(-center);
}
for (int i = 0; i < MapEntity.mapEntityList.Count; i++)

View File

@@ -144,7 +144,7 @@ namespace Barotrauma
drawPos.Y = -drawPos.Y;
Color clr = currentHull == null ? Color.Blue : Color.White;
if (isSelected) clr = Color.Red;
if (IsSelected) clr = Color.Red;
if (isHighlighted) clr = Color.DarkRed;
int iconX = iconIndices[(int)spawnType]*IconSize % iconTexture.Width;

View File

@@ -602,7 +602,6 @@ namespace Barotrauma
foreach (MapEntity me in MapEntity.mapEntityList)
{
me.IsHighlighted = false;
me.IsSelected = false;
}
return true;