This commit is contained in:
Juan Pablo Arce
2017-12-31 17:45:12 -03:00
3 changed files with 141 additions and 83 deletions
@@ -124,6 +124,8 @@ namespace Barotrauma
{ {
character.SelectedCharacter.Inventory.Update(deltaTime); character.SelectedCharacter.Inventory.Update(deltaTime);
} }
Inventory.UpdateDragging();
} }
} }
@@ -234,6 +236,11 @@ namespace Barotrauma
if (grabHoldButton.Visible) grabHoldButton.Draw(spriteBatch); if (grabHoldButton.Visible) grabHoldButton.Draw(spriteBatch);
} }
if (character.Inventory != null && !character.LockHands && character.Stun >= -0.1f)
{
Inventory.DrawDragging(spriteBatch);
}
if (character.FocusedCharacter != null && character.FocusedCharacter.CanBeSelected) if (character.FocusedCharacter != null && character.FocusedCharacter.CanBeSelected)
{ {
Vector2 startPos = character.DrawPosition + (character.FocusedCharacter.DrawPosition - character.DrawPosition) * 0.7f; Vector2 startPos = character.DrawPosition + (character.FocusedCharacter.DrawPosition - character.DrawPosition) * 0.7f;
@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Barotrauma namespace Barotrauma
{ {
@@ -119,7 +120,7 @@ namespace Barotrauma
} }
public override void Update(float deltaTime, bool subInventory = false) public override void Update(float deltaTime, bool isSubInventory = false)
{ {
base.Update(deltaTime); base.Update(deltaTime);
@@ -157,19 +158,39 @@ namespace Barotrauma
} }
} }
draggingItem = null;
GUI.PlayUISound(wasPut ? GUISoundType.PickItem : GUISoundType.PickItemFail); GUI.PlayUISound(wasPut ? GUISoundType.PickItem : GUISoundType.PickItemFail);
} }
if (selectedSlot > -1)
if (highlightedSubInventorySlot != null)
{ {
UpdateSubInventory(deltaTime, selectedSlot); UpdateSubInventory(deltaTime, highlightedSubInventorySlot.SlotIndex);
if (highlightedSubInventory.slots == null ||
(!highlightedSubInventorySlot.Slot.InteractRect.Contains(PlayerInput.MousePosition) && !highlightedSubInventory.slots.Any(s => s.InteractRect.Contains(PlayerInput.MousePosition))))
{
highlightedSubInventory = null;
highlightedSubInventorySlot = null;
}
}
else
{
if (selectedSlot?.Inventory == this)
{
var subInventory = GetSubInventory(selectedSlot.SlotIndex);
if (subInventory != null)
{
highlightedSubInventory = subInventory;
highlightedSubInventorySlot = selectedSlot;
}
}
} }
if (character == Character.Controlled) if (character == Character.Controlled)
{ {
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{ {
if (selectedSlot != i && if ((selectedSlot == null || selectedSlot.SlotIndex != i) &&
Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i])) Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i]))
{ {
//-3 because selected items are in slots 3 and 4 (hands) //-3 because selected items are in slots 3 and 4 (hands)
@@ -220,7 +241,7 @@ namespace Barotrauma
} }
} }
selectedSlot = -1; selectedSlot = null;
} }
public void DrawOwn(SpriteBatch spriteBatch) public void DrawOwn(SpriteBatch spriteBatch)
@@ -270,7 +291,7 @@ namespace Barotrauma
{ {
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{ {
if (selectedSlot != i && if ((selectedSlot == null || selectedSlot.SlotIndex != i) &&
Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i])) Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i]))
{ {
useOnSelfButton[i - 3].Draw(spriteBatch); useOnSelfButton[i - 3].Draw(spriteBatch);
@@ -278,15 +299,11 @@ namespace Barotrauma
} }
} }
if (selectedSlot > -1) for (int i = 0; i < capacity; i++)
{ {
DrawSubInventory(spriteBatch, selectedSlot); if (slots[i].IsHighlighted)
if (selectedSlot > -1 &&
!slots[selectedSlot].IsHighlighted &&
(draggingItem == null || draggingItem.Container != Items[selectedSlot]))
{ {
selectedSlot = -1; DrawSubInventory(spriteBatch, i);
} }
} }
} }
@@ -68,6 +68,22 @@ namespace Barotrauma
partial class Inventory partial class Inventory
{ {
protected class SlotReference
{
public readonly Inventory Inventory;
public readonly InventorySlot Slot;
public readonly int SlotIndex;
public bool IsSubSlot;
public SlotReference(Inventory inventory, InventorySlot slot, int slotIndex, bool isSubSlot)
{
Inventory = inventory;
Slot = slot;
SlotIndex = slotIndex;
IsSubSlot = isSubSlot;
}
}
public static InventorySlot draggingSlot; public static InventorySlot draggingSlot;
public static Item draggingItem; public static Item draggingItem;
@@ -80,9 +96,12 @@ namespace Barotrauma
set { slotsPerRow = Math.Max(1, value); } set { slotsPerRow = Math.Max(1, value); }
} }
protected int selectedSlot = -1; protected static SlotReference highlightedSubInventorySlot;
protected static Inventory highlightedSubInventory;
protected InventorySlot[] slots; protected static SlotReference selectedSlot;
public InventorySlot[] slots;
private Vector2 centerPos; private Vector2 centerPos;
@@ -135,6 +154,11 @@ namespace Barotrauma
slots[i] = new InventorySlot(slotRect); slots[i] = new InventorySlot(slotRect);
} }
if (selectedSlot != null && selectedSlot.Inventory == this)
{
selectedSlot = new SlotReference(this, slots[selectedSlot.SlotIndex], selectedSlot.SlotIndex, selectedSlot.IsSubSlot);
}
} }
public virtual void Update(float deltaTime, bool subInventory = false) public virtual void Update(float deltaTime, bool subInventory = false)
@@ -150,22 +174,8 @@ namespace Barotrauma
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{ {
if (slots[i].Disabled) continue; if (slots[i].Disabled) continue;
UpdateSlot(slots[i], i, Items[i], false); UpdateSlot(slots[i], i, Items[i], subInventory);
} }
if (draggingItem != null &&
(draggingSlot == null || (!draggingSlot.InteractRect.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) protected void UpdateSlot(InventorySlot slot, int slotIndex, Item item, bool isSubSlot)
@@ -174,19 +184,15 @@ namespace Barotrauma
slot.State = GUIComponent.ComponentState.None; slot.State = GUIComponent.ComponentState.None;
if (!(this is CharacterInventory) && !mouseOn && selectedSlot == slotIndex)
{
selectedSlot = -1;
}
if (mouseOn && if (mouseOn &&
(draggingItem != null || selectedSlot == slotIndex || selectedSlot == -1)) (draggingItem != null || selectedSlot == null || selectedSlot.Slot == slot) &&
(highlightedSubInventory == null || highlightedSubInventory == this || highlightedSubInventorySlot?.Slot == slot))
{ {
slot.State = GUIComponent.ComponentState.Hover; slot.State = GUIComponent.ComponentState.Hover;
if (!isSubSlot && selectedSlot == -1) if (selectedSlot == null || (!selectedSlot.IsSubSlot && isSubSlot))
{ {
selectedSlot = slotIndex; selectedSlot = new SlotReference(this, slot, slotIndex, isSubSlot);
} }
if (draggingItem == null) if (draggingItem == null)
@@ -203,38 +209,29 @@ namespace Barotrauma
{ {
doubleClickedItem = item; 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) protected Inventory GetSubInventory(int slotIndex)
{ {
var item = Items[slotIndex]; var item = Items[slotIndex];
if (item == null) return; if (item == null) return null;
var container = item.GetComponent<ItemContainer>(); var container = item.GetComponent<ItemContainer>();
if (container == null) return; if (container == null) return null;
if (container.Inventory.slots == null) container.Inventory.CreateSlots(); return container.Inventory;
}
int itemCapacity = container.Capacity; public void UpdateSubInventory(float deltaTime, int slotIndex)
{
Inventory subInventory = GetSubInventory(slotIndex);
if (subInventory == null) return;
if (subInventory.slots == null) subInventory.CreateSlots();
int itemCapacity = subInventory.Items.Length;
var slot = slots[slotIndex]; var slot = slots[slotIndex];
new Rectangle(slot.Rect.X - 5, slot.Rect.Y - (40 + 10) * itemCapacity - 5, new Rectangle(slot.Rect.X - 5, slot.Rect.Y - (40 + 10) * itemCapacity - 5,
@@ -246,17 +243,17 @@ namespace Barotrauma
for (int i = 0; i < itemCapacity; i++) for (int i = 0; i < itemCapacity; i++)
{ {
subRect.Y = subRect.Y - subRect.Height - 10; subRect.Y = subRect.Y - subRect.Height - 10;
container.Inventory.slots[i].Rect = subRect; subInventory.slots[i].Rect = subRect;
container.Inventory.slots[i].InteractRect = subRect; subInventory.slots[i].InteractRect = subRect;
container.Inventory.slots[i].InteractRect.Inflate(5, 5); subInventory.slots[i].InteractRect.Inflate(5, 5);
} }
container.Inventory.isSubInventory = true; subInventory.isSubInventory = true;
slots[slotIndex].State = GUIComponent.ComponentState.Hover; slots[slotIndex].State = GUIComponent.ComponentState.Hover;
container.Inventory.Update(deltaTime, true); subInventory.Update(deltaTime, true);
} }
@@ -274,17 +271,6 @@ namespace Barotrauma
DrawSlot(spriteBatch, slots[i], Items[i], drawItem); DrawSlot(spriteBatch, slots[i], Items[i], drawItem);
} }
if (draggingItem != null &&
(draggingSlot == null || (!draggingSlot.InteractRect.Contains(PlayerInput.MousePosition) && draggingItem.ParentInventory == this)))
{
Rectangle dragRect = new Rectangle(
(int)PlayerInput.MousePosition.X - 10,
(int)PlayerInput.MousePosition.Y - 10,
40, 40);
DrawSlot(spriteBatch, new InventorySlot(dragRect), draggingItem);
}
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{ {
if (slots[i].InteractRect.Contains(PlayerInput.MousePosition) && !slots[i].Disabled && Items[i] != null) if (slots[i].InteractRect.Contains(PlayerInput.MousePosition) && !slots[i].Disabled && Items[i] != null)
@@ -373,9 +359,57 @@ namespace Barotrauma
container.Inventory.Draw(spriteBatch, true); container.Inventory.Draw(spriteBatch, true);
if (!containerRect.Contains(PlayerInput.MousePosition)) }
public static void UpdateDragging()
{
if (draggingItem != null && PlayerInput.LeftButtonReleased())
{ {
if (draggingItem == null || draggingItem.Container != item) selectedSlot = -1; if (selectedSlot == null)
{
draggingItem.ParentInventory?.CreateNetworkEvent();
draggingItem.Drop();
GUI.PlayUISound(GUISoundType.DropItem);
}
else if (selectedSlot.Inventory.Items[selectedSlot.SlotIndex] != draggingItem)
{
Inventory selectedInventory = selectedSlot.Inventory;
int slotIndex = selectedSlot.SlotIndex;
if (selectedInventory.TryPutItem(draggingItem, slotIndex, true, Character.Controlled))
{
if (selectedInventory.slots != null) selectedInventory.slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f);
GUI.PlayUISound(GUISoundType.PickItem);
}
else
{
if (selectedInventory.slots != null) selectedInventory.slots[slotIndex].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
GUI.PlayUISound(GUISoundType.PickItemFail);
}
draggingItem = null;
draggingSlot = null;
}
draggingItem = null;
}
if (selectedSlot != null && !selectedSlot.Slot.InteractRect.Contains(PlayerInput.MousePosition))
{
selectedSlot = null;
}
}
public static void DrawDragging(SpriteBatch spriteBatch)
{
if (draggingItem == null) return;
if (draggingSlot == null || (!draggingSlot.InteractRect.Contains(PlayerInput.MousePosition)))
{
Rectangle dragRect = new Rectangle(
(int)PlayerInput.MousePosition.X - 10,
(int)PlayerInput.MousePosition.Y - 10,
40, 40);
DrawSlot(spriteBatch, new InventorySlot(dragRect), draggingItem);
} }
} }