(ba5343957) Button for hiding personal inventory slots

This commit is contained in:
Joonas Rikkonen
2019-04-16 17:08:58 +03:00
parent c361edfb8b
commit f667455659
3 changed files with 142 additions and 30 deletions
@@ -587,7 +587,7 @@ namespace Barotrauma
var affliction = GetAllAfflictions(a => a.Prefab.IndicatorLimb != LimbType.None) var affliction = GetAllAfflictions(a => a.Prefab.IndicatorLimb != LimbType.None)
.OrderByDescending(a => a.DamagePerSecond) .OrderByDescending(a => a.DamagePerSecond)
.ThenByDescending(a => a.Strength).FirstOrDefault(); .ThenByDescending(a => a.Strength).FirstOrDefault();
if (affliction.DamagePerSecond > 0 || affliction.Strength > 0) if (affliction.DamagePerSecond > 0 && affliction.Strength > 0)
{ {
var limbHealth = GetMathingLimbHealth(affliction); var limbHealth = GetMathingLimbHealth(affliction);
if (limbHealth != null) if (limbHealth != null)
@@ -1,4 +1,5 @@
using Barotrauma.Items.Components; using Barotrauma.Extensions;
using Barotrauma.Items.Components;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
@@ -18,7 +19,7 @@ namespace Barotrauma
Right, Right,
Center Center
} }
private enum QuickUseAction private enum QuickUseAction
{ {
None, None,
@@ -33,6 +34,8 @@ namespace Barotrauma
} }
private static Dictionary<InvSlotType, Sprite> limbSlotIcons; private static Dictionary<InvSlotType, Sprite> limbSlotIcons;
const InvSlotType PersonalSlots = InvSlotType.Card | InvSlotType.Headset | InvSlotType.InnerClothes | InvSlotType.OuterClothes | InvSlotType.Head;
private Point screenResolution; private Point screenResolution;
@@ -50,11 +53,42 @@ namespace Barotrauma
} }
} }
public bool Hidden { get; set; } 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) partial void InitProjSpecific(XElement element)
{ {
Hidden = true; 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) if (limbSlotIcons == null)
{ {
limbSlotIcons = new Dictionary<InvSlotType, Sprite>(); limbSlotIcons = new Dictionary<InvSlotType, Sprite>();
@@ -67,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.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))); 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]; SlotPositions = new Vector2[SlotTypes.Length];
CurrentLayout = Layout.Default; CurrentLayout = Layout.Default;
SetSlotPositions(layout); SetSlotPositions(layout);
} }
protected override void PutItem(Item item, int i, Character user, bool removeItem = true, bool createNetworkEvent = true) protected override void PutItem(Item item, int i, Character user, bool removeItem = true, bool createNetworkEvent = true)
{ {
base.PutItem(item, i, user, removeItem, createNetworkEvent); base.PutItem(item, i, user, removeItem, createNetworkEvent);
@@ -158,6 +191,11 @@ namespace Barotrauma
{ {
if (slots[i].Disabled || (hideEmptySlot[i] && Items[i] == null)) return true; 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 //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)) if (Items[i] != null && SlotTypes[i] == InvSlotType.RightHand && IsInLimbSlot(Items[i], InvSlotType.LeftHand))
{ {
@@ -175,7 +213,6 @@ namespace Barotrauma
return false; return false;
} }
private void SetSlotPositions(Layout layout) private void SetSlotPositions(Layout layout)
{ {
int spacing = (int)(10 * UIScale); int spacing = (int)(10 * UIScale);
@@ -184,27 +221,32 @@ namespace Barotrauma
if (slots == null) CreateSlots(); if (slots == null) CreateSlots();
var upperSlots = InvSlotType.Card | InvSlotType.Headset | InvSlotType.InnerClothes | InvSlotType.Head | InvSlotType.OuterClothes; hideButton.Visible = false;
switch (layout) switch (layout)
{ {
case Layout.Default: case Layout.Default:
{ {
int personalSlotCount = SlotTypes.Count(s => upperSlots.HasFlag(s)); int personalSlotCount = SlotTypes.Count(s => PersonalSlots.HasFlag(s));
int normalSlotCount = SlotTypes.Count(s => !upperSlots.HasFlag(s)); int normalSlotCount = SlotTypes.Count(s => !PersonalSlots.HasFlag(s));
int x = GameMain.GraphicsWidth / 2 - normalSlotCount * (slotSize.X + spacing) / 2; 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 //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); 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++) 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); SlotPositions[i] = new Vector2(upperX, GameMain.GraphicsHeight - bottomOffset);
upperX -= slotSize.X + spacing; 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 else
{ {
@@ -212,19 +254,29 @@ namespace Barotrauma
x += slotSize.X + spacing; 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; break;
case Layout.Right: case Layout.Right:
{ {
int extraOffset = 0; int extraOffset = 0;
int x = HUDLayoutSettings.InventoryAreaLower.Right; 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++) for (int i = 0; i < slots.Length; i++)
{ {
if (HideSlot(i)) continue; if (HideSlot(i)) continue;
if (upperSlots.HasFlag(SlotTypes[i])) if (PersonalSlots.HasFlag(SlotTypes[i]))
{ {
upperX -= slotSize.X + spacing; //upperX -= slotSize.X + spacing;
} }
else else
{ {
@@ -236,10 +288,10 @@ namespace Barotrauma
for (int i = 0; i < SlotPositions.Length; i++) for (int i = 0; i < SlotPositions.Length; i++)
{ {
if (HideSlot(i)) continue; 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); SlotPositions[i] = new Vector2(personalSlotX, GameMain.GraphicsHeight - bottomOffset * 2 - extraOffset - spacing * 2);
upperX += slots[i].Rect.Width + spacing; personalSlotX -= slots[i].Rect.Width + spacing;
} }
else else
{ {
@@ -260,14 +312,14 @@ namespace Barotrauma
case Layout.Left: case Layout.Left:
{ {
int x = HUDLayoutSettings.InventoryAreaLower.X; int x = HUDLayoutSettings.InventoryAreaLower.X;
int upperX = x; int personalSlotX = x;
for (int i = 0; i < SlotPositions.Length; i++) for (int i = 0; i < SlotPositions.Length; i++)
{ {
if (HideSlot(i)) continue; 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); SlotPositions[i] = new Vector2(personalSlotX, GameMain.GraphicsHeight - bottomOffset * 2 - spacing * 2);
upperX += slots[i].Rect.Width + spacing; personalSlotX += slots[i].Rect.Width + spacing;
} }
else else
{ {
@@ -353,6 +405,63 @@ namespace Barotrauma
((selectedSlot != null && selectedSlot.IsSubSlot) || (draggingItem != null && (draggingSlot == null || !draggingSlot.MouseOn()))); ((selectedSlot != null && selectedSlot.IsSubSlot) || (draggingItem != null && (draggingSlot == null || !draggingSlot.MouseOn())));
if (CharacterHealth.OpenHealthWindow != null) hoverOnInventory = true; 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);
}
/*var arrowSlot = slots[arrowSlotIndex];
Rectangle arrowRect = new Rectangle(
(int)(arrowSlot.Rect.X + arrowSlot.DrawOffset.X - toggleArrow.size.X / 2),
(int)(arrowSlot.Rect.Y),
(int)toggleArrow.size.X, (int)arrowSlot.Rect.Height);
arrowRect.Inflate(30, 0);
if (arrowRect.Contains(PlayerInput.MousePosition))
{
arrowAlpha = Math.Min(arrowAlpha + deltaTime * 10.0f, 1.0f);
if (PlayerInput.LeftButtonClicked())
{
hidden = !hidden;
HideTimer = 0.0f;
foreach (var highlightedSubInventorySlot in highlightedSubInventorySlots)
{
highlightedSubInventorySlot.Inventory.HideTimer = 0.0f;
}
return;
}
}
else
{
arrowAlpha = Math.Max(arrowAlpha - deltaTime * 10.0f, 0.5f);
}
if (GUI.MouseOn == null &&
(slots[toggleArrowSlotIndex].DrawOffset.Y < 10.0f && PlayerInput.MousePosition.Y > arrowRect.Bottom ||
slots[toggleArrowSlotIndex].DrawOffset.Y > 10.0f && PlayerInput.MousePosition.Y > slots[toggleArrowSlotIndex].EquipButtonRect.Bottom) &&
slots.Any(s => PlayerInput.MousePosition.X > s.InteractRect.X - 10 && PlayerInput.MousePosition.X < s.InteractRect.Right + 10))
{
hoverOnInventory = true;
}*/
}
if (hoverOnInventory) HideTimer = 0.5f; if (hoverOnInventory) HideTimer = 0.5f;
if (HideTimer > 0.0f) HideTimer -= deltaTime; if (HideTimer > 0.0f) HideTimer -= deltaTime;
@@ -702,7 +811,12 @@ namespace Barotrauma
} }
base.Draw(spriteBatch); base.Draw(spriteBatch);
if (hideButton != null && hideButton.Visible)
{
hideButton.DrawManually(spriteBatch, alsoChildren: true);
}
InventorySlot highlightedQuickUseSlot = null; InventorySlot highlightedQuickUseSlot = null;
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{ {
@@ -716,7 +830,7 @@ namespace Barotrauma
if (limbSlotIcons.ContainsKey(SlotTypes[i])) if (limbSlotIcons.ContainsKey(SlotTypes[i]))
{ {
var icon = limbSlotIcons[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; continue;
} }
@@ -726,12 +840,12 @@ namespace Barotrauma
if (IsInLimbSlot(Items[i], InvSlotType.LeftHand)) if (IsInLimbSlot(Items[i], InvSlotType.LeftHand))
{ {
var icon = limbSlotIcons[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)) if (IsInLimbSlot(Items[i], InvSlotType.RightHand))
{ {
var icon = limbSlotIcons[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; Color color = slots[i].EquipButtonState == GUIComponent.ComponentState.Pressed ? Color.Gray : Color.White * 0.8f;
@@ -154,8 +154,6 @@ namespace Barotrauma
public SlotReference(Inventory parentInventory, InventorySlot slot, int slotIndex, bool isSubSlot, Inventory subInventory = null) public SlotReference(Inventory parentInventory, InventorySlot slot, int slotIndex, bool isSubSlot, Inventory subInventory = null)
{ {
ParentInventory = parentInventory; ParentInventory = parentInventory;
Slot = slot; Slot = slot;
SlotIndex = slotIndex; SlotIndex = slotIndex;
@@ -856,7 +854,7 @@ namespace Barotrauma
if (itemContainer.ContainedStateIndicator?.Texture == null) if (itemContainer.ContainedStateIndicator?.Texture == null)
{ {
containedIndicatorArea.Inflate(0, -2); 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, GUI.DrawRectangle(spriteBatch,
new Rectangle(containedIndicatorArea.X, containedIndicatorArea.Y, (int)(containedIndicatorArea.Width * containedState), containedIndicatorArea.Height), new Rectangle(containedIndicatorArea.X, containedIndicatorArea.Y, (int)(containedIndicatorArea.Width * containedState), containedIndicatorArea.Height),
Color.Lerp(Color.Red, Color.Green, containedState) * 0.8f, true); Color.Lerp(Color.Red, Color.Green, containedState) * 0.8f, true);
@@ -870,11 +868,11 @@ namespace Barotrauma
if (containedState > 0.0f && containedState < 0.25f) 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(), indicatorSprite.Draw(spriteBatch, containedIndicatorArea.Center.ToVector2(),
Color.DarkGray * 0.6f, Color.DarkGray * 0.9f,
origin: indicatorSprite.size / 2, origin: indicatorSprite.size / 2,
rotate: 0.0f, rotate: 0.0f,
scale: indicatorScale); scale: indicatorScale);