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
@@ -267,7 +267,7 @@ namespace Barotrauma.Items.Components
item.NewComponentEvent(this, true, true); item.NewComponentEvent(this, true, true);
} }
Drawable = Nodes.Any(); Drawable = sections.Count > 0;
} }
public override bool Pick(Character picker) public override bool Pick(Character picker)
@@ -277,6 +277,20 @@ namespace Barotrauma.Items.Components
return true; 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() public void UpdateSections()
{ {
sections.Clear(); sections.Clear();
@@ -285,6 +299,7 @@ namespace Barotrauma.Items.Components
{ {
sections.Add(new WireSection(Nodes[i], Nodes[i + 1])); sections.Add(new WireSection(Nodes[i], Nodes[i + 1]));
} }
Drawable = sections.Count > 0;
} }
private void ClearConnections() private void ClearConnections()
@@ -303,7 +318,7 @@ namespace Barotrauma.Items.Components
connections[i] = null; connections[i] = null;
} }
Drawable = false; Drawable = sections.Count > 0;
} }
private Vector2 RoundNode(Vector2 position, Hull hull) private Vector2 RoundNode(Vector2 position, Hull hull)
@@ -365,7 +380,7 @@ namespace Barotrauma.Items.Components
public void Draw(SpriteBatch spriteBatch, bool editing) public void Draw(SpriteBatch spriteBatch, bool editing)
{ {
if (!Nodes.Any()) if (sections.Count == 0)
{ {
Drawable = false; Drawable = false;
return; return;
@@ -386,6 +401,13 @@ namespace Barotrauma.Items.Components
section.Draw(spriteBatch, Color.Gold, drawOffset, depth, 0.5f); 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) foreach (WireSection section in sections)
{ {
+13 -4
View File
@@ -423,12 +423,21 @@ namespace Barotrauma
clone.properties[property.Key].TrySetValue(property.Value.GetValue()); 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) foreach (Item containedItem in ContainedItems)
{ {
var containedClone = containedItem.Clone(); 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) public override void Draw(SpriteBatch spriteBatch, bool editing, bool back = true)
{ {
if (!Visible) return; if (!Visible) return;
Color color = (isSelected && editing) ? color = Color.Red : spriteColor; Color color = (IsSelected && editing) ? color = Color.Red : spriteColor;
if (isHighlighted) color = Color.Orange; if (isHighlighted) color = Color.Orange;
SpriteEffects oldEffects = prefab.sprite.effects; SpriteEffects oldEffects = prefab.sprite.effects;
@@ -943,7 +952,7 @@ namespace Barotrauma
return; 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)); 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));
+1 -1
View File
@@ -609,7 +609,7 @@ namespace Barotrauma
} }
if ((isSelected || isHighlighted) && editing) if ((IsSelected || isHighlighted) && editing)
{ {
GUI.DrawRectangle(spriteBatch, GUI.DrawRectangle(spriteBatch,
new Vector2(drawRect.X + 5, -drawRect.Y + 5), new Vector2(drawRect.X + 5, -drawRect.Y + 5),
+1 -1
View File
@@ -111,7 +111,7 @@ namespace Barotrauma
if (!editing || wallVertices == null) return; if (!editing || wallVertices == null) return;
Color color = (isHighlighted) ? Color.Orange : Color.Green; Color color = (isHighlighted) ? Color.Orange : Color.Green;
if (isSelected) color = Color.Red; if (IsSelected) color = Color.Red;
Vector2 pos = Position; Vector2 pos = Position;
+64 -26
View File
@@ -8,6 +8,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using Barotrauma.Items.Components;
namespace Barotrauma namespace Barotrauma
{ {
@@ -16,7 +17,7 @@ namespace Barotrauma
public static List<MapEntity> mapEntityList = new List<MapEntity>(); public static List<MapEntity> mapEntityList = new List<MapEntity>();
//which entities have been selected for editing //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>(); private static List<MapEntity> copiedList = new List<MapEntity>();
protected static GUIComponent editingHUD; protected static GUIComponent editingHUD;
@@ -39,7 +40,7 @@ namespace Barotrauma
//is the mouse inside the rect //is the mouse inside the rect
protected bool isHighlighted; protected bool isHighlighted;
protected bool isSelected; //protected bool isSelected;
private static bool disableSelect; private static bool disableSelect;
public static bool DisableSelect public static bool DisableSelect
@@ -175,8 +176,7 @@ namespace Barotrauma
public bool IsSelected public bool IsSelected
{ {
get { return isSelected; } get { return selectedList.Contains(this); }
set { isSelected = value; }
} }
protected bool ResizeHorizontal protected bool ResizeHorizontal
@@ -219,7 +219,43 @@ namespace Barotrauma
List<MapEntity> clones = new List<MapEntity>(); List<MapEntity> clones = new List<MapEntity>();
foreach (MapEntity e in entitiesToClone) foreach (MapEntity e in entitiesToClone)
{ {
Debug.Assert(e != null);
clones.Add(e.Clone()); 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; return clones;
@@ -311,7 +347,6 @@ namespace Barotrauma
foreach (MapEntity e in mapEntityList) foreach (MapEntity e in mapEntityList)
{ {
e.isHighlighted = false; e.isHighlighted = false;
e.isSelected = false;
} }
if (DisableSelect) if (DisableSelect)
@@ -359,9 +394,8 @@ namespace Barotrauma
clones.ForEach(c => center += c.WorldPosition); clones.ForEach(c => center += c.WorldPosition);
center /= clones.Count; center /= clones.Count;
clones.ForEach(c => c.Move(cam.WorldViewCenter - center));
selectedList = new List<MapEntity>(clones); 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; if (e.IsMouseOn(position)) highLightedEntity = e;
} }
e.isSelected = false;
} }
if (highLightedEntity != null) highLightedEntity.isHighlighted = true; if (highLightedEntity != null) highLightedEntity.isHighlighted = true;
} }
foreach (MapEntity e in selectedList)
{
e.isSelected = true;
}
//started moving selected entities //started moving selected entities
if (startMovingPos != Vector2.Zero && PlayerInput.LeftButtonReleased()) if (startMovingPos != Vector2.Zero && PlayerInput.LeftButtonReleased())
{ {
@@ -406,9 +434,8 @@ namespace Barotrauma
if (PlayerInput.KeyDown(Keys.LeftControl) || PlayerInput.KeyDown(Keys.RightControl)) if (PlayerInput.KeyDown(Keys.LeftControl) || PlayerInput.KeyDown(Keys.RightControl))
{ {
var clones = Clone(selectedList); var clones = Clone(selectedList);
clones.ForEach(c => c.Move(moveAmount));
selectedList = clones; selectedList = clones;
selectedList.ForEach(c => c.Move(moveAmount));
} }
else // move else // move
{ {
@@ -463,6 +490,25 @@ namespace Barotrauma
{ {
selectedList = newSelection; 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; selectionPos = Vector2.Zero;
selectionSize = Vector2.Zero; selectionSize = Vector2.Zero;
@@ -471,7 +517,6 @@ namespace Barotrauma
//default, not doing anything specific yet //default, not doing anything specific yet
else else
{ {
if (PlayerInput.LeftButtonHeld() && if (PlayerInput.LeftButtonHeld() &&
PlayerInput.KeyUp(Keys.Space)) PlayerInput.KeyUp(Keys.Space))
{ {
@@ -483,12 +528,10 @@ namespace Barotrauma
selectionPos = position; selectionPos = position;
} }
}
}
} }
/// <summary> /// <summary>
/// Draw the "selection rectangle" and outlines of entities that are being dragged (if any) /// Draw the "selection rectangle" and outlines of entities that are being dragged (if any)
/// </summary> /// </summary>
@@ -565,10 +608,6 @@ namespace Barotrauma
public static void DeselectAll() public static void DeselectAll()
{ {
foreach (MapEntity e in selectedList)
{
e.isSelected = false;
}
selectedList.Clear(); selectedList.Clear();
} }
@@ -577,7 +616,6 @@ namespace Barotrauma
{ {
DeselectAll(); DeselectAll();
entity.isSelected = true;
selectedList.Add(entity); selectedList.Add(entity);
} }
+1 -1
View File
@@ -495,7 +495,7 @@ namespace Barotrauma
if (prefab.sprite == null) return; if (prefab.sprite == null) return;
Color color = (isHighlighted) ? Color.Orange : Color.White; Color color = (isHighlighted) ? Color.Orange : Color.White;
if (isSelected && editing) if (IsSelected && editing)
{ {
color = Color.Red; color = Color.Red;
+1 -7
View File
@@ -1080,13 +1080,7 @@ namespace Barotrauma
if (item.Submarine != this) continue; if (item.Submarine != this) continue;
var wire = item.GetComponent<Items.Components.Wire>(); var wire = item.GetComponent<Items.Components.Wire>();
if (wire == null) continue; if (wire != null) wire.MoveNodes(-center);
for (int i = 0; i < wire.Nodes.Count; i++)
{
wire.Nodes[i] -= center;
}
wire.UpdateSections();
} }
for (int i = 0; i < MapEntity.mapEntityList.Count; i++) for (int i = 0; i < MapEntity.mapEntityList.Count; i++)
+1 -1
View File
@@ -144,7 +144,7 @@ namespace Barotrauma
drawPos.Y = -drawPos.Y; drawPos.Y = -drawPos.Y;
Color clr = currentHull == null ? Color.Blue : Color.White; Color clr = currentHull == null ? Color.Blue : Color.White;
if (isSelected) clr = Color.Red; if (IsSelected) clr = Color.Red;
if (isHighlighted) clr = Color.DarkRed; if (isHighlighted) clr = Color.DarkRed;
int iconX = iconIndices[(int)spawnType]*IconSize % iconTexture.Width; int iconX = iconIndices[(int)spawnType]*IconSize % iconTexture.Width;
@@ -602,7 +602,6 @@ namespace Barotrauma
foreach (MapEntity me in MapEntity.mapEntityList) foreach (MapEntity me in MapEntity.mapEntityList)
{ {
me.IsHighlighted = false; me.IsHighlighted = false;
me.IsSelected = false;
} }
return true; return true;