From a4875cfeb3b8f289ad89a5c2e7658cfb35836447 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Tue, 11 Jun 2019 21:44:32 +0300 Subject: [PATCH] (776291eb7) Create linked gaps when the door is selected. Move the gaps with the doors. --- .../BarotraumaClient/Source/Items/Item.cs | 2 +- .../Source/Map/ItemAssemblyPrefab.cs | 2 +- .../BarotraumaClient/Source/Map/MapEntity.cs | 80 ++++++++++++++++--- .../Source/Screens/SubEditorScreen.cs | 9 +-- .../Source/Items/Components/Door.cs | 41 +++++----- .../Source/Map/ItemAssemblyPrefab.cs | 2 +- 6 files changed, 96 insertions(+), 40 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/Item.cs b/Barotrauma/BarotraumaClient/Source/Items/Item.cs index abc2b548f..f6e4cb04a 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Item.cs @@ -451,7 +451,7 @@ namespace Barotrauma public override void UpdateEditing(Camera cam) { - if (editingHUD == null || editingHUD.UserData as Item != this) + if (editingHUD == null || editingHUD.UserData == null) { editingHUD = CreateEditingHUD(Screen.Selected != GameMain.SubEditorScreen); } diff --git a/Barotrauma/BarotraumaClient/Source/Map/ItemAssemblyPrefab.cs b/Barotrauma/BarotraumaClient/Source/Map/ItemAssemblyPrefab.cs index 00eb633c9..0fcff3a6d 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/ItemAssemblyPrefab.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/ItemAssemblyPrefab.cs @@ -82,7 +82,7 @@ namespace Barotrauma center.Y -= center.Y % Submarine.GridSize.Y; MapEntity.SelectedList.Clear(); - MapEntity.SelectedList.AddRange(assemblyEntities); + assemblyEntities.ForEach(e => MapEntity.AddSelection(e)); foreach (MapEntity mapEntity in assemblyEntities) { diff --git a/Barotrauma/BarotraumaClient/Source/Map/MapEntity.cs b/Barotrauma/BarotraumaClient/Source/Map/MapEntity.cs index cfa1a5486..91dd8e015 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/MapEntity.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/MapEntity.cs @@ -367,14 +367,19 @@ namespace Barotrauma foreach (MapEntity e in newSelection) { if (selectedList.Contains(e)) - selectedList.Remove(e); + { + RemoveSelection(e); + } else - selectedList.Add(e); + { + AddSelection(e); + } } } else { - selectedList = newSelection; + selectedList.Clear(); + newSelection.ForEach(e => AddSelection(e)); } //select wire if both items it's connected to are selected @@ -457,9 +462,13 @@ namespace Barotrauma PlayerInput.KeyDown(Keys.RightControl)) { if (selectedList.Contains(entity)) - selectedList.Remove(entity); + { + RemoveSelection(entity); + } else - selectedList.Add(entity); + { + AddSelection(entity); + } } else { @@ -469,6 +478,45 @@ namespace Barotrauma return true; }; } + + public static void AddSelection(MapEntity entity) + { + if (selectedList.Contains(entity)) { return; } + selectedList.Add(entity); + if (entity is Item i) + { + var door = i.GetComponent(); + if (door != null) + { + var gap = door.LinkedGap; + if (gap != null) + { + door.RefreshLinkedGap(); + if (!selectedList.Contains(gap)) + { + selectedList.Add(gap); + } + } + } + } + } + + public static void RemoveSelection(MapEntity entity) + { + selectedList.Remove(entity); + if (entity is Item i) + { + var door = i.GetComponent(); + if (door != null) + { + var gap = door.LinkedGap; + if (gap != null) + { + selectedList.Remove(gap); + } + } + } + } static partial void UpdateAllProjSpecific(float deltaTime) { @@ -533,8 +581,11 @@ namespace Barotrauma } } + public static List FilteredSelectedList { get; private set; } = new List(); + public static void UpdateEditor(Camera cam) { + FilteredSelectedList.Clear(); if (highlightedListBox != null) highlightedListBox.UpdateManually((float)Timing.Step); if (editingHUD != null) @@ -552,13 +603,18 @@ namespace Barotrauma } if (selectedList.Count == 0) return; - - if (selectedList.Count == 1) + foreach (var e in selectedList) { - selectedList[0].UpdateEditing(cam); - if (selectedList[0].ResizeHorizontal || selectedList[0].ResizeVertical) + if (e is Gap) { continue; } + FilteredSelectedList.Add(e); + } + var first = FilteredSelectedList.FirstOrDefault(); + if (first != null) + { + first.UpdateEditing(cam); + if (first.ResizeHorizontal || first.ResizeVertical) { - selectedList[0].UpdateResizing(cam); + first.UpdateResizing(cam); } } @@ -622,12 +678,10 @@ namespace Barotrauma selectedList.Clear(); } - public static void SelectEntity(MapEntity entity) { DeselectAll(); - - selectedList.Add(entity); + AddSelection(entity); } /// diff --git a/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs b/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs index a45f872e6..e69d4a663 100644 --- a/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs +++ b/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs @@ -2109,10 +2109,7 @@ namespace Barotrauma public override void AddToGUIUpdateList() { - if (MapEntity.SelectedList.Count == 1) - { - MapEntity.SelectedList[0].AddToGUIUpdateList(); - } + MapEntity.FilteredSelectedList.FirstOrDefault()?.AddToGUIUpdateList(); if (MapEntity.HighlightedListBox != null) { MapEntity.HighlightedListBox.AddToGUIUpdateList(); @@ -2293,9 +2290,9 @@ namespace Barotrauma dummyCharacter.SelectedConstruction = null; }*/ } - else if (MapEntity.SelectedList.Count == 1) + else if (MapEntity.FilteredSelectedList.Count == 1) { - (MapEntity.SelectedList[0] as Item)?.UpdateHUD(cam, dummyCharacter, (float)deltaTime); + (MapEntity.FilteredSelectedList[0] as Item)?.UpdateHUD(cam, dummyCharacter, (float)deltaTime); } CharacterHUD.Update((float)deltaTime, dummyCharacter, cam); diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Door.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Door.cs index b42997efe..7ac15962c 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Door.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Door.cs @@ -22,7 +22,6 @@ namespace Barotrauma.Items.Components private float openState; private Sprite doorSprite, weldedSprite, brokenSprite; private bool scaleBrokenSprite, fadeBrokenSprite; - private bool createdNewGap; private bool autoOrientGap; private bool isStuck; @@ -87,17 +86,19 @@ namespace Barotrauma.Items.Components { get { - if (linkedGap != null) return linkedGap; - - foreach (MapEntity e in item.linkedTo) + if (linkedGap == null) { - linkedGap = e as Gap; - if (linkedGap != null) - { - linkedGap.PassAmbientLight = Window != Rectangle.Empty; - return linkedGap; - } + GetLinkedGap(); } + return linkedGap; + } + } + + private void GetLinkedGap() + { + linkedGap = item.linkedTo.FirstOrDefault(e => e is Gap) as Gap; + if (linkedGap == null) + { Rectangle rect = item.Rect; if (IsHorizontal) { @@ -109,17 +110,13 @@ namespace Barotrauma.Items.Components rect.X -= 5; rect.Width += 10; } - linkedGap = new Gap(rect, !IsHorizontal, Item.Submarine) { - Submarine = item.Submarine, - PassAmbientLight = Window != Rectangle.Empty, - Open = openState + Submarine = item.Submarine }; item.linkedTo.Add(linkedGap); - createdNewGap = true; - return linkedGap; } + RefreshLinkedGap(); } public bool IsHorizontal { get; private set; } @@ -370,12 +367,20 @@ namespace Barotrauma.Items.Components #endif } - public override void OnMapLoaded() + public void RefreshLinkedGap() { LinkedGap.ConnectedDoor = this; + if (autoOrientGap) + { + LinkedGap.AutoOrient(); + } LinkedGap.Open = openState; - if (createdNewGap && autoOrientGap) linkedGap.AutoOrient(); + LinkedGap.PassAmbientLight = Window != Rectangle.Empty; + } + public override void OnMapLoaded() + { + RefreshLinkedGap(); #if CLIENT Vector2[] corners = GetConvexHullCorners(Rectangle.Empty); diff --git a/Barotrauma/BarotraumaShared/Source/Map/ItemAssemblyPrefab.cs b/Barotrauma/BarotraumaShared/Source/Map/ItemAssemblyPrefab.cs index 736acb775..3e1024dfa 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/ItemAssemblyPrefab.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/ItemAssemblyPrefab.cs @@ -107,7 +107,7 @@ namespace Barotrauma if (Screen.Selected == GameMain.SubEditorScreen) { MapEntity.SelectedList.Clear(); - MapEntity.SelectedList.AddRange(entities); + entities.ForEach(e => MapEntity.AddSelection(e)); } #endif return entities;