From 9e3d13b1c9a290a4b7039f8d07589cfe44dc8616 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Tue, 16 Apr 2019 17:10:10 +0300 Subject: [PATCH] (3730c56ab) Merge branch 'dev' of https://github.com/Regalis11/Barotrauma-development into dev --- .../Source/Items/CharacterInventory.cs | 152 +++++++++++++++--- .../Source/Items/Inventory.cs | 8 +- 2 files changed, 131 insertions(+), 29 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs b/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs index 4be33d829..4b232ad92 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs @@ -19,7 +19,7 @@ namespace Barotrauma Right, Center } - + private enum QuickUseAction { None, @@ -34,6 +34,8 @@ namespace Barotrauma } private static Dictionary limbSlotIcons; + + const InvSlotType PersonalSlots = InvSlotType.Card | InvSlotType.Headset | InvSlotType.InnerClothes | InvSlotType.OuterClothes | InvSlotType.Head; private Point screenResolution; @@ -51,11 +53,42 @@ namespace Barotrauma } } public bool Hidden { get; set; } + + private bool hidePersonalSlots; + private float hidePersonalSlotsState; + private GUIButton hideButton; + private Rectangle personalSlotArea; + public bool HidePersonalSlots + { + get { return hidePersonalSlots; } + } + + public Rectangle PersonalSlotArea + { + get { return personalSlotArea; } + } + partial void InitProjSpecific(XElement element) { Hidden = true; + hideButton = new GUIButton(new RectTransform(new Point((int)(30 * GUI.Scale), (int)(60 * GUI.Scale)), GUI.Canvas) + { AbsoluteOffset = HUDLayoutSettings.CrewArea.Location }, + "", style: "UIToggleButton"); + hideButton.Children.ForEach(c => c.SpriteEffects = SpriteEffects.FlipHorizontally); + hideButton.OnClicked += (GUIButton btn, object userdata) => + { + hidePersonalSlots = !hidePersonalSlots; + foreach (GUIComponent child in btn.Children) + { + child.SpriteEffects = hidePersonalSlots ? SpriteEffects.None : SpriteEffects.FlipHorizontally; + } + return true; + }; + + hidePersonalSlots = false; + if (limbSlotIcons == null) { limbSlotIcons = new Dictionary(); @@ -68,12 +101,11 @@ namespace Barotrauma limbSlotIcons.Add(InvSlotType.LeftHand, new Sprite("Content/UI/IconAtlas.png", new Rectangle(640 + margin, 383 + margin, 128 - margin * 2, 128 - margin * 2))); limbSlotIcons.Add(InvSlotType.RightHand, new Sprite("Content/UI/IconAtlas.png", new Rectangle(768 + margin, 383 + margin, 128 - margin * 2, 128 - margin * 2))); } - SlotPositions = new Vector2[SlotTypes.Length]; CurrentLayout = Layout.Default; SetSlotPositions(layout); } - + protected override void PutItem(Item item, int i, Character user, bool removeItem = true, bool createNetworkEvent = true) { base.PutItem(item, i, user, removeItem, createNetworkEvent); @@ -159,6 +191,11 @@ namespace Barotrauma { if (slots[i].Disabled || (hideEmptySlot[i] && Items[i] == null)) return true; + if (layout == Layout.Default) + { + if (PersonalSlots.HasFlag(SlotTypes[i]) && !personalSlotArea.Contains(slots[i].Rect.Center + slots[i].DrawOffset.ToPoint())) return true; + } + //no need to draw the right hand slot if the item is in both hands if (Items[i] != null && SlotTypes[i] == InvSlotType.RightHand && IsInLimbSlot(Items[i], InvSlotType.LeftHand)) { @@ -176,7 +213,6 @@ namespace Barotrauma return false; } - private void SetSlotPositions(Layout layout) { int spacing = (int)(10 * UIScale); @@ -185,27 +221,32 @@ namespace Barotrauma if (slots == null) CreateSlots(); - var upperSlots = InvSlotType.Card | InvSlotType.Headset | InvSlotType.InnerClothes | InvSlotType.Head | InvSlotType.OuterClothes; + hideButton.Visible = false; switch (layout) { case Layout.Default: { - int personalSlotCount = SlotTypes.Count(s => upperSlots.HasFlag(s)); - int normalSlotCount = SlotTypes.Count(s => !upperSlots.HasFlag(s)); + int personalSlotCount = SlotTypes.Count(s => PersonalSlots.HasFlag(s)); + int normalSlotCount = SlotTypes.Count(s => !PersonalSlots.HasFlag(s)); int x = GameMain.GraphicsWidth / 2 - normalSlotCount * (slotSize.X + spacing) / 2; - int upperX = HUDLayoutSettings.PortraitArea.X - slotSize.X; + int upperX = HUDLayoutSettings.PortraitArea.X - slotSize.X * 2; //make sure the rightmost normal slot doesn't overlap with the personal slots x -= Math.Max((x + normalSlotCount * (slotSize.X + spacing)) - (upperX - personalSlotCount * (slotSize.X + spacing)), 0); + int hideButtonSlotIndex = -1; for (int i = 0; i < SlotPositions.Length; i++) { - if (upperSlots.HasFlag(SlotTypes[i])) + if (PersonalSlots.HasFlag(SlotTypes[i])) { SlotPositions[i] = new Vector2(upperX, GameMain.GraphicsHeight - bottomOffset); upperX -= slotSize.X + spacing; + personalSlotArea = (hideButtonSlotIndex == -1) ? + new Rectangle(SlotPositions[i].ToPoint(), slotSize) : + Rectangle.Union(personalSlotArea, new Rectangle(SlotPositions[i].ToPoint(), slotSize)); + hideButtonSlotIndex = i; } else { @@ -213,19 +254,29 @@ namespace Barotrauma x += slotSize.X + spacing; } } + + if (hideButtonSlotIndex > -1) + { + hideButton.RectTransform.SetPosition(Anchor.TopLeft, Pivot.TopLeft); + hideButton.RectTransform.NonScaledSize = new Point(slotSize.X / 2, slotSize.Y + slots[hideButtonSlotIndex].EquipButtonRect.Height); + hideButton.RectTransform.AbsoluteOffset = new Point( + personalSlotArea.Right + spacing, + personalSlotArea.Y - slots[hideButtonSlotIndex].EquipButtonRect.Height); + hideButton.Visible = true; + } } break; case Layout.Right: { int extraOffset = 0; int x = HUDLayoutSettings.InventoryAreaLower.Right; - int upperX = HUDLayoutSettings.InventoryAreaLower.Right; + int personalSlotX = HUDLayoutSettings.InventoryAreaLower.Right - slotSize.X - spacing; for (int i = 0; i < slots.Length; i++) { if (HideSlot(i)) continue; - if (upperSlots.HasFlag(SlotTypes[i])) + if (PersonalSlots.HasFlag(SlotTypes[i])) { - upperX -= slotSize.X + spacing; + //upperX -= slotSize.X + spacing; } else { @@ -237,10 +288,10 @@ namespace Barotrauma for (int i = 0; i < SlotPositions.Length; i++) { if (HideSlot(i)) continue; - if (upperSlots.HasFlag(SlotTypes[i])) + if (PersonalSlots.HasFlag(SlotTypes[i])) { - SlotPositions[i] = new Vector2(upperX, GameMain.GraphicsHeight - bottomOffset * 2 - extraOffset - spacing * 2); - upperX += slots[i].Rect.Width + spacing; + SlotPositions[i] = new Vector2(personalSlotX, GameMain.GraphicsHeight - bottomOffset * 2 - extraOffset - spacing * 2); + personalSlotX -= slots[i].Rect.Width + spacing; } else { @@ -261,14 +312,14 @@ namespace Barotrauma case Layout.Left: { int x = HUDLayoutSettings.InventoryAreaLower.X; - int upperX = x; + int personalSlotX = x; for (int i = 0; i < SlotPositions.Length; i++) { if (HideSlot(i)) continue; - if (upperSlots.HasFlag(SlotTypes[i])) + if (PersonalSlots.HasFlag(SlotTypes[i])) { - SlotPositions[i] = new Vector2(upperX, GameMain.GraphicsHeight - bottomOffset * 2 - spacing * 2); - upperX += slots[i].Rect.Width + spacing; + SlotPositions[i] = new Vector2(personalSlotX, GameMain.GraphicsHeight - bottomOffset * 2 - spacing * 2); + personalSlotX += slots[i].Rect.Width + spacing; } else { @@ -354,6 +405,27 @@ namespace Barotrauma ((selectedSlot != null && selectedSlot.IsSubSlot) || (draggingItem != null && (draggingSlot == null || !draggingSlot.MouseOn()))); if (CharacterHealth.OpenHealthWindow != null) hoverOnInventory = true; + if (layout == Layout.Default && hideButton.Visible) + { + hideButton.AddToGUIUpdateList(); + hideButton.UpdateManually(deltaTime, alsoChildren: true); + + hidePersonalSlotsState = hidePersonalSlots ? + Math.Min(hidePersonalSlotsState + deltaTime * 5.0f, 1.0f) : + Math.Max(hidePersonalSlotsState - deltaTime * 5.0f, 0.0f); + + for (int i = 0; i < slots.Length; i++) + { + if (!PersonalSlots.HasFlag(SlotTypes[i])) { continue; } + if (HidePersonalSlots) + { + if (selectedSlot?.Slot == slots[i]) { selectedSlot = null; } + highlightedSubInventorySlots.RemoveWhere(s => s.Slot == slots[i]); + } + slots[i].DrawOffset = Vector2.Lerp(Vector2.Zero, new Vector2(personalSlotArea.Width, 0.0f), hidePersonalSlotsState); + } + } + if (hoverOnInventory) HideTimer = 0.5f; if (HideTimer > 0.0f) HideTimer -= deltaTime; @@ -366,7 +438,34 @@ namespace Barotrauma QuickUseItem(Items[i], true, false, true); } } - + } + + public override void Update(float deltaTime, Camera cam, bool isSubInventory = false) + { + if (!AccessibleWhenAlive && !character.IsDead) + { + syncItemsDelay = Math.Max(syncItemsDelay - deltaTime, 0.0f); + return; + } + + base.Update(deltaTime, cam); + + //force personal slots open if an item is running out of battery/fuel/oxygen/etc + if (hidePersonalSlots) + { + for (int i = 0; i < slots.Length; i++) + { + if (Items[i]?.OwnInventory != null && Items[i].OwnInventory.Capacity == 1 && PersonalSlots.HasFlag(SlotTypes[i])) + { + if (Items[i].OwnInventory.Items[0].Condition > 0.0f && + Items[i].OwnInventory.Items[0].Condition / Items[i].OwnInventory.Items[0].MaxCondition < 0.15f) + { + hidePersonalSlots = false; + } + } + } + } + List hideSubInventories = new List(); foreach (var highlightedSubInventorySlot in highlightedSubInventorySlots) { @@ -703,7 +802,12 @@ namespace Barotrauma } base.Draw(spriteBatch); - + + if (hideButton != null && hideButton.Visible) + { + hideButton.DrawManually(spriteBatch, alsoChildren: true); + } + InventorySlot highlightedQuickUseSlot = null; for (int i = 0; i < capacity; i++) { @@ -717,7 +821,7 @@ namespace Barotrauma if (limbSlotIcons.ContainsKey(SlotTypes[i])) { var icon = limbSlotIcons[SlotTypes[i]]; - icon.Draw(spriteBatch, slots[i].Rect.Center.ToVector2(), Color.White * 0.3f, origin: icon.size / 2, scale: slots[i].Rect.Width / icon.size.X); + icon.Draw(spriteBatch, slots[i].Rect.Center.ToVector2() + slots[i].DrawOffset, Color.White * 0.3f, origin: icon.size / 2, scale: slots[i].Rect.Width / icon.size.X); } continue; } @@ -727,12 +831,12 @@ namespace Barotrauma if (IsInLimbSlot(Items[i], InvSlotType.LeftHand)) { var icon = limbSlotIcons[InvSlotType.LeftHand]; - icon.Draw(spriteBatch, new Vector2(slots[i].Rect.X, slots[i].Rect.Bottom), Color.White * 0.6f, origin: new Vector2(icon.size.X * 0.35f, icon.size.Y * 0.75f), scale: slots[i].Rect.Width / icon.size.X * 0.7f); + icon.Draw(spriteBatch, new Vector2(slots[i].Rect.X, slots[i].Rect.Bottom) + slots[i].DrawOffset, Color.White * 0.6f, origin: new Vector2(icon.size.X * 0.35f, icon.size.Y * 0.75f), scale: slots[i].Rect.Width / icon.size.X * 0.7f); } if (IsInLimbSlot(Items[i], InvSlotType.RightHand)) { var icon = limbSlotIcons[InvSlotType.RightHand]; - icon.Draw(spriteBatch, new Vector2(slots[i].Rect.Right, slots[i].Rect.Bottom), Color.White * 0.6f, origin: new Vector2(icon.size.X * 0.65f, icon.size.Y * 0.75f), scale: slots[i].Rect.Width / icon.size.X * 0.7f); + icon.Draw(spriteBatch, new Vector2(slots[i].Rect.Right, slots[i].Rect.Bottom) + slots[i].DrawOffset, Color.White * 0.6f, origin: new Vector2(icon.size.X * 0.65f, icon.size.Y * 0.75f), scale: slots[i].Rect.Width / icon.size.X * 0.7f); } Color color = slots[i].EquipButtonState == GUIComponent.ComponentState.Pressed ? Color.Gray : Color.White * 0.8f; diff --git a/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs b/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs index c4db30e68..30eaf8155 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs @@ -154,8 +154,6 @@ namespace Barotrauma public SlotReference(Inventory parentInventory, InventorySlot slot, int slotIndex, bool isSubSlot, Inventory subInventory = null) { - - ParentInventory = parentInventory; Slot = slot; SlotIndex = slotIndex; @@ -856,7 +854,7 @@ namespace Barotrauma if (itemContainer.ContainedStateIndicator?.Texture == null) { containedIndicatorArea.Inflate(0, -2); - GUI.DrawRectangle(spriteBatch, containedIndicatorArea, Color.DarkGray * 0.8f, true); + GUI.DrawRectangle(spriteBatch, containedIndicatorArea, Color.DarkGray * 0.9f, true); GUI.DrawRectangle(spriteBatch, new Rectangle(containedIndicatorArea.X, containedIndicatorArea.Y, (int)(containedIndicatorArea.Width * containedState), containedIndicatorArea.Height), Color.Lerp(Color.Red, Color.Green, containedState) * 0.8f, true); @@ -870,11 +868,11 @@ namespace Barotrauma if (containedState > 0.0f && containedState < 0.25f) { - indicatorScale += ((float)Math.Sin(Timing.TotalTime * 5.0f) + 1.0f) * 0.1f; + indicatorScale += ((float)Math.Sin(Timing.TotalTime * 5.0f) + 1.0f) * 0.25f; } indicatorSprite.Draw(spriteBatch, containedIndicatorArea.Center.ToVector2(), - Color.DarkGray * 0.6f, + Color.DarkGray * 0.9f, origin: indicatorSprite.size / 2, rotate: 0.0f, scale: indicatorScale);