Moved inventory UI update logic to the client project, sound effects for picking up and dropping items

This commit is contained in:
Joonas Rikkonen
2017-08-10 20:30:29 +03:00
parent fd844fd696
commit f40d244deb
11 changed files with 352 additions and 339 deletions

View File

@@ -12,7 +12,9 @@ namespace Barotrauma
RadioMessage,
DeadMessage,
Click,
Inventory,
PickItem,
PickItemFail,
DropItem
}
public class GUI
@@ -98,7 +100,9 @@ namespace Barotrauma
sounds[(int)GUISoundType.DeadMessage] = Sound.Load("Content/Sounds/UI/deadmsg.ogg", false);
sounds[(int)GUISoundType.Click] = Sound.Load("Content/Sounds/UI/beep-shinymetal.ogg", false);
sounds[(int)GUISoundType.Inventory] = Sound.Load("Content/Sounds/pickItem.ogg", false);
sounds[(int)GUISoundType.PickItem] = Sound.Load("Content/Sounds/pickItem.ogg", false);
sounds[(int)GUISoundType.PickItemFail] = Sound.Load("Content/Sounds/pickItemFail.ogg", false);
sounds[(int)GUISoundType.DropItem] = Sound.Load("Content/Sounds/dropItem.ogg", false);
}

View File

@@ -1,5 +1,7 @@
using Microsoft.Xna.Framework;
using Barotrauma.Items.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace Barotrauma
{
@@ -74,6 +76,19 @@ namespace Barotrauma
return UseItemOnSelf(slotIndex);
}
protected override void PutItem(Item item, int i, Character user, bool removeItem = true, bool createNetworkEvent = true)
{
base.PutItem(item, i, user, removeItem, createNetworkEvent);
CreateSlots();
}
public override void RemoveItem(Item item)
{
base.RemoveItem(item);
CreateSlots();
}
protected override void CreateSlots()
{
if (slots == null) slots = new InventorySlot[capacity];
@@ -98,6 +113,110 @@ namespace Barotrauma
MergeSlots();
}
public override void Update(float deltaTime, bool subInventory = false)
{
base.Update(deltaTime);
if (doubleClickedItem != null)
{
bool wasPut = false;
if (doubleClickedItem.ParentInventory != this)
{
wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
}
else
{
var selectedContainer = character.SelectedConstruction?.GetComponent<ItemContainer>();
if (selectedContainer != null && selectedContainer.Inventory != null)
{
wasPut = selectedContainer.Inventory.TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
}
else if (character.SelectedCharacter != null && character.SelectedCharacter.Inventory != null)
{
wasPut = character.SelectedCharacter.Inventory.TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
}
else //doubleclicked and no other inventory is selected
{
//not equipped -> attempt to equip
if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any))
{
wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true);
}
//equipped -> attempt to unequip
else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any))
{
wasPut = TryPutItem(doubleClickedItem, Character.Controlled, new List<InvSlotType>() { InvSlotType.Any }, true);
}
}
}
GUI.PlayUISound(wasPut ? GUISoundType.PickItem : GUISoundType.PickItemFail);
}
if (selectedSlot > -1)
{
UpdateSubInventory(deltaTime, selectedSlot);
}
if (character == Character.Controlled)
{
for (int i = 0; i < capacity; i++)
{
if (selectedSlot != i &&
Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i]))
{
//-3 because selected items are in slots 3 and 4 (hands)
useOnSelfButton[i - 3].Update(deltaTime);
}
}
}
//cancel dragging if too far away from the container of the dragged item
if (draggingItem != null)
{
var rootContainer = draggingItem.GetRootContainer();
var rootInventory = draggingItem.ParentInventory;
if (rootContainer != null)
{
rootInventory = rootContainer.ParentInventory != null ?
rootContainer.ParentInventory : rootContainer.GetComponent<Items.Components.ItemContainer>().Inventory;
}
if (rootInventory != null &&
rootInventory.Owner != Character.Controlled &&
rootInventory.Owner != Character.Controlled.SelectedConstruction &&
rootInventory.Owner != Character.Controlled.SelectedCharacter)
{
draggingItem = null;
}
}
doubleClickedItem = null;
}
private void MergeSlots()
{
for (int i = 0; i < capacity - 1; i++)
{
if (slots[i].Disabled || Items[i] == null) continue;
for (int n = i + 1; n < capacity; n++)
{
if (Items[n] == Items[i])
{
slots[i].Rect = Rectangle.Union(slots[i].Rect, slots[n].Rect);
slots[n].Disabled = true;
}
}
}
selectedSlot = -1;
}
public void DrawOwn(SpriteBatch spriteBatch)
{
if (slots == null) CreateSlots();

View File

@@ -1,13 +1,23 @@
using Barotrauma.Items.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace Barotrauma
{
partial class InventorySlot
class InventorySlot
{
public Rectangle Rect;
public bool Disabled;
public GUIComponent.ComponentState State;
public Color Color;
public Color BorderHighlightColor;
private CoroutineHandle BorderHighlightCoroutine;
public bool IsHighlighted
{
@@ -17,10 +27,12 @@ namespace Barotrauma
}
}
public Color Color;
public Color BorderHighlightColor;
private CoroutineHandle BorderHighlightCoroutine;
public InventorySlot(Rectangle rect)
{
Rect = rect;
State = GUIComponent.ComponentState.None;
Color = Color.White * 0.4f;
}
public void ShowBorderHighlight(Color color, float fadeInDuration, float fadeOutDuration)
{
@@ -52,6 +64,195 @@ namespace Barotrauma
partial class Inventory
{
public static InventorySlot draggingSlot;
public static Item draggingItem;
public static Item doubleClickedItem;
private int slotsPerRow;
public int SlotsPerRow
{
set { slotsPerRow = Math.Max(1, value); }
}
protected int selectedSlot = -1;
protected InventorySlot[] slots;
private Vector2 centerPos;
public Vector2 CenterPos
{
get { return centerPos; }
set
{
centerPos = value;
centerPos.X *= GameMain.GraphicsWidth;
centerPos.Y *= GameMain.GraphicsHeight;
}
}
private Vector2 drawOffset;
public Vector2 DrawOffset
{
get
{
return drawOffset;
}
set
{
if (value == drawOffset) return;
drawOffset = value;
CreateSlots();
}
}
protected virtual void CreateSlots()
{
slots = new InventorySlot[capacity];
int rectWidth = 40, rectHeight = 40;
int spacing = 10;
int rows = (int)Math.Ceiling((double)capacity / slotsPerRow);
int startX = (int)centerPos.X - (rectWidth * slotsPerRow + spacing * (slotsPerRow - 1)) / 2;
int startY = (int)centerPos.Y - rows * (spacing + rectHeight);
Rectangle slotRect = new Rectangle(startX, startY, rectWidth, rectHeight);
for (int i = 0; i < capacity; i++)
{
slotRect.X = startX + (rectWidth + spacing) * (i % slotsPerRow) + (int)DrawOffset.X;
slotRect.Y = startY + (rectHeight + spacing) * ((int)Math.Floor((double)i / slotsPerRow)) + (int)DrawOffset.Y;
slots[i] = new InventorySlot(slotRect);
}
}
public virtual void Update(float deltaTime, bool subInventory = false)
{
syncItemsDelay = Math.Max(syncItemsDelay - deltaTime, 0.0f);
if (slots == null || isSubInventory != subInventory)
{
CreateSlots();
isSubInventory = subInventory;
}
for (int i = 0; i < capacity; i++)
{
if (slots[i].Disabled) continue;
UpdateSlot(slots[i], i, Items[i], false);
}
if (draggingItem != null &&
(draggingSlot == null || (!draggingSlot.Rect.Contains(PlayerInput.MousePosition) && draggingItem.ParentInventory == this)))
{
if (!PlayerInput.LeftButtonHeld())
{
CreateNetworkEvent();
draggingItem.Drop();
GUI.PlayUISound(GUISoundType.DropItem);
}
}
}
protected void UpdateSlot(InventorySlot slot, int slotIndex, Item item, bool isSubSlot)
{
bool mouseOn = slot.Rect.Contains(PlayerInput.MousePosition) && !Locked;
slot.State = GUIComponent.ComponentState.None;
if (!(this is CharacterInventory) && !mouseOn && selectedSlot == slotIndex)
{
selectedSlot = -1;
}
if (mouseOn &&
(draggingItem != null || selectedSlot == slotIndex || selectedSlot == -1))
{
slot.State = GUIComponent.ComponentState.Hover;
if (!isSubSlot && selectedSlot == -1)
{
selectedSlot = slotIndex;
}
if (draggingItem == null)
{
if (PlayerInput.LeftButtonHeld())
{
draggingItem = Items[slotIndex];
draggingSlot = slot;
}
}
else if (PlayerInput.LeftButtonReleased())
{
if (PlayerInput.DoubleClicked())
{
doubleClickedItem = item;
}
if (draggingItem != Items[slotIndex])
{
//selectedSlot = slotIndex;
if (TryPutItem(draggingItem, slotIndex, true, Character.Controlled))
{
if (slots != null) slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f);
GUI.PlayUISound(GUISoundType.PickItem);
}
else
{
if (slots != null) slots[slotIndex].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
GUI.PlayUISound(GUISoundType.PickItemFail);
}
draggingItem = null;
draggingSlot = null;
}
}
}
}
public void UpdateSubInventory(float deltaTime, int slotIndex)
{
var item = Items[slotIndex];
if (item == null) return;
var container = item.GetComponent<ItemContainer>();
if (container == null) return;
if (container.Inventory.slots == null) container.Inventory.CreateSlots();
int itemCapacity = container.Capacity;
var slot = slots[slotIndex];
new Rectangle(slot.Rect.X - 5, slot.Rect.Y - (40 + 10) * itemCapacity - 5,
slot.Rect.Width + 10, slot.Rect.Height + (40 + 10) * itemCapacity + 10);
Rectangle subRect = slot.Rect;
subRect.Height = 40;
for (int i = 0; i < itemCapacity; i++)
{
subRect.Y = subRect.Y - subRect.Height - 10;
container.Inventory.slots[i].Rect = subRect;
}
container.Inventory.isSubInventory = true;
slots[slotIndex].State = GUIComponent.ComponentState.Hover;
container.Inventory.Update(deltaTime, true);
}
public virtual void Draw(SpriteBatch spriteBatch, bool subInventory = false)
{
if (slots == null || isSubInventory != subInventory) return;

View File

@@ -1156,6 +1156,9 @@
<None Include="$(MSBuildThisFileDirectory)Content\Sounds\Damage\StructureCrunch3.ogg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)Content\Sounds\dropItem.ogg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)Content\Sounds\fire.ogg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
@@ -1198,6 +1201,9 @@
<None Include="$(MSBuildThisFileDirectory)Content\Sounds\pickItem.ogg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)Content\Sounds\pickItemFail.ogg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)Content\Sounds\startDrone.ogg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

View File

@@ -226,118 +226,5 @@ namespace Barotrauma
return TryPutItem(item, user, new List<InvSlotType>() { placeToSlots }, createNetworkEvent);
}
protected override void PutItem(Item item, int i, Character user, bool removeItem = true, bool createNetworkEvent = true)
{
base.PutItem(item, i, user, removeItem, createNetworkEvent);
CreateSlots();
}
public override void RemoveItem(Item item)
{
base.RemoveItem(item);
CreateSlots();
}
public override void Update(float deltaTime, bool subInventory = false)
{
base.Update(deltaTime);
if (doubleClickedItem != null)
{
if (doubleClickedItem.ParentInventory != this)
{
TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
}
else
{
var selectedContainer = character.SelectedConstruction?.GetComponent<ItemContainer>();
if (selectedContainer != null && selectedContainer.Inventory != null)
{
selectedContainer.Inventory.TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
}
else if (character.SelectedCharacter != null && character.SelectedCharacter.Inventory != null)
{
character.SelectedCharacter.Inventory.TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
}
else //doubleclicked and no other inventory is selected
{
//not equipped -> attempt to equip
if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any))
{
TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true);
}
//equipped -> attempt to unequip
else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any))
{
TryPutItem(doubleClickedItem, Character.Controlled, new List<InvSlotType>() { InvSlotType.Any }, true);
}
}
}
}
if (selectedSlot > -1)
{
UpdateSubInventory(deltaTime, selectedSlot);
}
#if CLIENT
if (character == Character.Controlled)
{
for (int i = 0; i < capacity; i++)
{
if (selectedSlot != i &&
Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i]))
{
//-3 because selected items are in slots 3 and 4 (hands)
useOnSelfButton[i - 3].Update(deltaTime);
}
}
}
#endif
//cancel dragging if too far away from the container of the dragged item
if (draggingItem != null)
{
var rootContainer = draggingItem.GetRootContainer();
var rootInventory = draggingItem.ParentInventory;
if (rootContainer != null)
{
rootInventory = rootContainer.ParentInventory != null ?
rootContainer.ParentInventory : rootContainer.GetComponent<Items.Components.ItemContainer>().Inventory;
}
if (rootInventory != null &&
rootInventory.Owner != Character.Controlled &&
rootInventory.Owner != Character.Controlled.SelectedConstruction &&
rootInventory.Owner != Character.Controlled.SelectedCharacter)
{
draggingItem = null;
}
}
doubleClickedItem = null;
}
private void MergeSlots()
{
for (int i = 0; i < capacity-1; i++)
{
if (slots[i].Disabled || Items[i] == null) continue;
for (int n = i+1; n < capacity; n++)
{
if (Items[n] == Items[i])
{
slots[i].Rect = Rectangle.Union(slots[i].Rect, slots[n].Rect);
slots[n].Disabled = true;
}
}
}
selectedSlot = -1;
}
}
}

View File

@@ -86,10 +86,18 @@ namespace Barotrauma.Items.Components
DropConnectedWires(picker);
ApplyStatusEffects(ActionType.OnPicked, 1.0f, picker);
#if CLIENT
GUI.PlayUISound(GUISoundType.PickItem);
#endif
return true;
}
#if CLIENT
GUI.PlayUISound(GUISoundType.PickItemFail);
#endif
return false;
}

View File

@@ -2,52 +2,17 @@
using Barotrauma.Networking;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Barotrauma
{
partial class InventorySlot
{
public Rectangle Rect;
public bool Disabled;
public InventorySlot(Rectangle rect)
{
Rect = rect;
#if CLIENT
State = GUIComponent.ComponentState.None;
Color = Color.White * 0.4f;
#endif
}
}
partial class Inventory : IServerSerializable, IClientSerializable
{
public static InventorySlot draggingSlot;
public static Item draggingItem;
public static Item doubleClickedItem;
public readonly Entity Owner;
private int slotsPerRow;
public int SlotsPerRow
{
set { slotsPerRow = Math.Max(1, value); }
}
protected int capacity;
protected int selectedSlot = -1;
protected InventorySlot[] slots;
public Item[] Items;
private bool isSubInventory;
@@ -58,49 +23,19 @@ namespace Barotrauma
private float syncItemsDelay;
private CoroutineHandle syncItemsCoroutine;
private Vector2 centerPos;
public Vector2 CenterPos
{
get { return centerPos; }
set
{
centerPos = value;
#if CLIENT
centerPos.X *= GameMain.GraphicsWidth;
centerPos.Y *= GameMain.GraphicsHeight;
#endif
}
}
private Vector2 drawOffset;
public Vector2 DrawOffset
{
get
{
return drawOffset;
}
set
{
if (value == drawOffset) return;
drawOffset = value;
CreateSlots();
}
}
public Inventory(Entity owner, int capacity, Vector2? centerPos = null, int slotsPerRow=5)
{
this.capacity = capacity;
this.Owner = owner;
this.slotsPerRow = slotsPerRow;
Items = new Item[capacity];
#if CLIENT
this.slotsPerRow = slotsPerRow;
CenterPos = (centerPos==null) ? new Vector2(0.5f, 0.5f) : (Vector2)centerPos;
#endif
}
public int FindIndex(Item item)
@@ -225,155 +160,7 @@ namespace Barotrauma
item.ParentInventory = null;
}
}
protected virtual void CreateSlots()
{
slots = new InventorySlot[capacity];
int rectWidth = 40, rectHeight = 40;
int spacing = 10;
int rows = (int)Math.Ceiling((double)capacity / slotsPerRow);
int startX = (int)centerPos.X - (rectWidth * slotsPerRow + spacing * (slotsPerRow - 1)) / 2;
int startY = (int)centerPos.Y - rows * (spacing + rectHeight);
Rectangle slotRect = new Rectangle(startX, startY, rectWidth, rectHeight);
for (int i = 0; i < capacity; i++)
{
slotRect.X = startX + (rectWidth + spacing) * (i % slotsPerRow) + (int)DrawOffset.X;
slotRect.Y = startY + (rectHeight + spacing) * ((int)Math.Floor((double)i / slotsPerRow)) + (int)DrawOffset.Y;
slots[i] = new InventorySlot(slotRect);
}
}
public virtual void Update(float deltaTime, bool subInventory = false)
{
syncItemsDelay = Math.Max(syncItemsDelay - deltaTime, 0.0f);
if (slots == null || isSubInventory != subInventory)
{
CreateSlots();
isSubInventory = subInventory;
}
for (int i = 0; i < capacity; i++)
{
if (slots[i].Disabled) continue;
UpdateSlot(slots[i], i, Items[i], false);
}
if (draggingItem != null &&
(draggingSlot == null || (!draggingSlot.Rect.Contains(PlayerInput.MousePosition) && draggingItem.ParentInventory == this)))
{
if (!PlayerInput.LeftButtonHeld())
{
CreateNetworkEvent();
draggingItem.Drop();
}
}
}
protected void UpdateSlot(InventorySlot slot, int slotIndex, Item item, bool isSubSlot)
{
bool mouseOn = slot.Rect.Contains(PlayerInput.MousePosition) && !Locked;
#if CLIENT
slot.State = GUIComponent.ComponentState.None;
#endif
if (!(this is CharacterInventory) && !mouseOn && selectedSlot==slotIndex)
{
selectedSlot = -1;
}
if (mouseOn &&
(draggingItem!=null || selectedSlot==slotIndex || selectedSlot==-1))
{
#if CLIENT
slot.State = GUIComponent.ComponentState.Hover;
#endif
if (!isSubSlot && selectedSlot == -1)
{
selectedSlot = slotIndex;
}
if (draggingItem == null)
{
if (PlayerInput.LeftButtonHeld())
{
draggingItem = Items[slotIndex];
draggingSlot = slot;
}
}
else if (PlayerInput.LeftButtonReleased())
{
if (PlayerInput.DoubleClicked())
{
doubleClickedItem = item;
}
if (draggingItem != Items[slotIndex])
{
//selectedSlot = slotIndex;
if (TryPutItem(draggingItem, slotIndex, true, Character.Controlled))
{
#if CLIENT
if (slots != null) slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f);
#endif
}
else
{
#if CLIENT
if (slots != null) slots[slotIndex].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
#endif
}
draggingItem = null;
draggingSlot = null;
}
}
}
}
public void UpdateSubInventory(float deltaTime, int slotIndex)
{
var item = Items[slotIndex];
if (item == null) return;
var container = item.GetComponent<ItemContainer>();
if (container == null) return;
if (container.Inventory.slots == null) container.Inventory.CreateSlots();
int itemCapacity = container.Capacity;
var slot = slots[slotIndex];
new Rectangle(slot.Rect.X - 5, slot.Rect.Y - (40 + 10) * itemCapacity - 5,
slot.Rect.Width + 10, slot.Rect.Height + (40 + 10) * itemCapacity + 10);
Rectangle subRect = slot.Rect;
subRect.Height = 40;
for (int i = 0; i < itemCapacity; i++)
{
subRect.Y = subRect.Y - subRect.Height - 10;
container.Inventory.slots[i].Rect = subRect;
}
container.Inventory.isSubInventory = true;
#if CLIENT
slots[slotIndex].State = GUIComponent.ComponentState.Hover;
#endif
container.Inventory.Update(deltaTime, true);
}
public void ClientWrite(NetBuffer msg, object[] extraData = null)
{
ServerWrite(msg, null);

View File

@@ -132,12 +132,13 @@ namespace Barotrauma
}
GameMain.World.Step((float)deltaTime);
#if CLIENT
if (!PlayerInput.LeftButtonHeld())
{
Inventory.draggingSlot = null;
Inventory.draggingItem = null;
}
#endif
}
}
}