Separated inventory update & rendering logic
This commit is contained in:
@@ -46,6 +46,19 @@ namespace Barotrauma
|
|||||||
if (suicideButton != null && suicideButton.Visible) suicideButton.Update(deltaTime);
|
if (suicideButton != null && suicideButton.Visible) suicideButton.Update(deltaTime);
|
||||||
|
|
||||||
if (damageOverlayTimer > 0.0f) damageOverlayTimer -= deltaTime;
|
if (damageOverlayTimer > 0.0f) damageOverlayTimer -= deltaTime;
|
||||||
|
|
||||||
|
if (!character.IsUnconscious && character.Stun <= 0.0f)
|
||||||
|
{
|
||||||
|
if (character.Inventory != null && !character.LockHands && character.Stun >= -0.1f)
|
||||||
|
{
|
||||||
|
character.Inventory.Update(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (character.SelectedCharacter != null && character.SelectedCharacter.Inventory != null)
|
||||||
|
{
|
||||||
|
character.SelectedCharacter.Inventory.Update(deltaTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Draw(SpriteBatch spriteBatch, Character character, Camera cam)
|
public static void Draw(SpriteBatch spriteBatch, Character character, Camera cam)
|
||||||
@@ -84,12 +97,15 @@ namespace Barotrauma
|
|||||||
|
|
||||||
if (!character.IsUnconscious && character.Stun <= 0.0f)
|
if (!character.IsUnconscious && character.Stun <= 0.0f)
|
||||||
{
|
{
|
||||||
if (character.Inventory != null && !character.LockHands &&
|
if (character.Inventory != null && !character.LockHands && character.Stun >= -0.1f)
|
||||||
character.Stun >= -0.1f) character.Inventory.DrawOwn(spriteBatch, Vector2.Zero);
|
{
|
||||||
|
character.Inventory.DrawOwn(spriteBatch);
|
||||||
|
}
|
||||||
|
|
||||||
if (character.SelectedCharacter != null && character.SelectedCharacter.Inventory != null)
|
if (character.SelectedCharacter != null && character.SelectedCharacter.Inventory != null)
|
||||||
{
|
{
|
||||||
character.SelectedCharacter.Inventory.DrawOwn(spriteBatch, new Vector2(320.0f, 0.0f));
|
character.SelectedCharacter.Inventory.DrawOffset = new Vector2(320.0f, 0.0f);
|
||||||
|
character.SelectedCharacter.Inventory.DrawOwn(spriteBatch);
|
||||||
|
|
||||||
if (cprButton == null)
|
if (cprButton == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -278,12 +278,43 @@ namespace Barotrauma
|
|||||||
|
|
||||||
return TryPutItem(item, new List<InvSlotType>() {placeToSlots}, createNetworkEvent);
|
return TryPutItem(item, new List<InvSlotType>() {placeToSlots}, createNetworkEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawOwn(SpriteBatch spriteBatch, Vector2 offset)
|
|
||||||
{
|
|
||||||
string toolTip = "";
|
|
||||||
Rectangle highlightedSlot = Rectangle.Empty;
|
|
||||||
|
|
||||||
|
protected override void PutItem(Item item, int i, bool createNetworkEvent, bool removeItem = true)
|
||||||
|
{
|
||||||
|
base.PutItem(item, i, createNetworkEvent, removeItem);
|
||||||
|
CreateSlots();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void RemoveItem(Item item)
|
||||||
|
{
|
||||||
|
base.RemoveItem(item);
|
||||||
|
CreateSlots();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void CreateSlots()
|
||||||
|
{
|
||||||
|
slots = new InventorySlot[capacity];
|
||||||
|
|
||||||
|
int rectWidth = 40, rectHeight = 40;
|
||||||
|
|
||||||
|
Rectangle slotRect = new Rectangle(0, 0, rectWidth, rectHeight);
|
||||||
|
for (int i = 0; i < capacity; i++)
|
||||||
|
{
|
||||||
|
slotRect.X = (int)(SlotPositions[i].X + DrawOffset.X);
|
||||||
|
slotRect.Y = (int)(SlotPositions[i].Y + DrawOffset.Y);
|
||||||
|
|
||||||
|
slots[i] = new InventorySlot(slotRect);
|
||||||
|
|
||||||
|
slots[i].Color = limbSlots[i] == InvSlotType.Any ? Color.White * 0.2f : Color.White * 0.4f;
|
||||||
|
}
|
||||||
|
|
||||||
|
MergeSlots();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(float deltaTime)
|
||||||
|
{
|
||||||
|
base.Update(deltaTime);
|
||||||
|
|
||||||
if (doubleClickedItem != null)
|
if (doubleClickedItem != null)
|
||||||
{
|
{
|
||||||
if (doubleClickedItem.ParentInventory != this)
|
if (doubleClickedItem.ParentInventory != this)
|
||||||
@@ -309,7 +340,7 @@ namespace Barotrauma
|
|||||||
//not equipped -> attempt to equip
|
//not equipped -> attempt to equip
|
||||||
if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any))
|
if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any))
|
||||||
{
|
{
|
||||||
TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true);
|
TryPutItem(doubleClickedItem, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true);
|
||||||
}
|
}
|
||||||
//equipped -> attempt to unequip
|
//equipped -> attempt to unequip
|
||||||
else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any))
|
else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any))
|
||||||
@@ -319,17 +350,77 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
doubleClickedItem = null;
|
||||||
|
}
|
||||||
|
|
||||||
const int rectWidth = 40, rectHeight = 40;
|
private void MergeSlots()
|
||||||
Rectangle slotRect = new Rectangle(0, 0, rectWidth, rectHeight);
|
{
|
||||||
Rectangle draggingItemSlot = slotRect;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawOwn(SpriteBatch spriteBatch)
|
||||||
|
{
|
||||||
|
if (slots == null) CreateSlots();
|
||||||
|
|
||||||
|
Rectangle slotRect = new Rectangle(0, 0, 40, 40);
|
||||||
|
|
||||||
for (int i = 0; i < capacity; i++)
|
for (int i = 0; i < capacity; i++)
|
||||||
{
|
{
|
||||||
slotRect.X = (int)(SlotPositions[i].X + offset.X);
|
slotRect.X = (int)(SlotPositions[i].X + DrawOffset.X);
|
||||||
slotRect.Y = (int)(SlotPositions[i].Y + offset.Y);
|
slotRect.Y = (int)(SlotPositions[i].Y + DrawOffset.Y);
|
||||||
|
|
||||||
if (i==1) //head
|
if (i==1) //head
|
||||||
{
|
{
|
||||||
@@ -353,149 +444,31 @@ namespace Barotrauma
|
|||||||
SpriteEffects.None, 0.1f);
|
SpriteEffects.None, 0.1f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
base.Draw(spriteBatch);
|
||||||
|
|
||||||
for (int i = 0; i < capacity; i++)
|
if (character == Character.Controlled)
|
||||||
{
|
{
|
||||||
slotRect.X = (int)(SlotPositions[i].X + offset.X);
|
for (int i = 0; i < capacity; i++)
|
||||||
slotRect.Y = (int)(SlotPositions[i].Y + offset.Y);
|
|
||||||
|
|
||||||
bool multiSlot = false;
|
|
||||||
//skip if the item is in multiple slots
|
|
||||||
if (Items[i]!=null)
|
|
||||||
{
|
|
||||||
for (int n = 0; n < capacity; n++ )
|
|
||||||
{
|
|
||||||
if (i==n || Items[n] != Items[i]) continue;
|
|
||||||
multiSlot = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (multiSlot) continue;
|
|
||||||
|
|
||||||
if (Items[i] != null && slotRect.Contains(PlayerInput.MousePosition) && (selectedSlot == -1 || selectedSlot == i))
|
|
||||||
{
|
{
|
||||||
if (GameMain.DebugDraw)
|
if (selectedSlot != i &&
|
||||||
|
Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i]))
|
||||||
{
|
{
|
||||||
toolTip = Items[i].ToString();
|
useOnSelfButton[i - 3].Draw(spriteBatch);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
toolTip = string.IsNullOrEmpty(Items[i].Description) ? Items[i].Name : Items[i].Name + '\n' + Items[i].Description;
|
|
||||||
}
|
|
||||||
|
|
||||||
highlightedSlot = slotRect;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedSlot == i) highlightedSlot = slotRect;
|
|
||||||
|
|
||||||
UpdateSlot(spriteBatch, slotRect, i, Items[i], false, i>5 ? 0.2f : 0.4f);
|
|
||||||
|
|
||||||
if (draggingItem!=null && draggingItem == Items[i]) draggingItemSlot = slotRect;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < capacity; i++)
|
|
||||||
{
|
|
||||||
bool multiSlot = false;
|
|
||||||
|
|
||||||
//check if the item is in multiple slots
|
|
||||||
if (Items[i] != null)
|
|
||||||
{
|
|
||||||
slotRect.X = (int)(SlotPositions[i].X + offset.X);
|
|
||||||
slotRect.Y = (int)(SlotPositions[i].Y + offset.Y);
|
|
||||||
slotRect.Width = 40;
|
|
||||||
slotRect.Height = 40;
|
|
||||||
|
|
||||||
for (int n = 0; n < capacity; n++)
|
|
||||||
{
|
|
||||||
if (Items[n] != Items[i]) continue;
|
|
||||||
|
|
||||||
if (!multiSlot && i > n) break;
|
|
||||||
|
|
||||||
if (i!=n)
|
|
||||||
{
|
|
||||||
multiSlot = true;
|
|
||||||
slotRect = Rectangle.Union(
|
|
||||||
new Rectangle((int)(SlotPositions[n].X+offset.X), (int)(SlotPositions[n].Y+offset.Y), rectWidth, rectHeight), slotRect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (multiSlot)
|
|
||||||
{
|
|
||||||
if (Items[i] != null && slotRect.Contains(PlayerInput.MousePosition) && (selectedSlot==-1 || selectedSlot==i))
|
|
||||||
{
|
|
||||||
toolTip = string.IsNullOrEmpty(Items[i].Description) ? Items[i].Name : Items[i].Name + '\n' + Items[i].Description;
|
|
||||||
highlightedSlot = slotRect;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedSlot == i) highlightedSlot = slotRect;
|
|
||||||
|
|
||||||
UpdateSlot(spriteBatch, slotRect, i, Items[i], i > 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (character==Character.Controlled && selectedSlot != i &&
|
|
||||||
Items[i] != null && Items[i].CanUseOnSelf && character.HasSelectedItem(Items[i]))
|
|
||||||
{
|
|
||||||
useOnSelfButton[i - 3].Update(0.016f);
|
|
||||||
useOnSelfButton[i - 3].Draw(spriteBatch);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedSlot > -1)
|
if (selectedSlot > -1)
|
||||||
{
|
{
|
||||||
DrawSubInventory(spriteBatch, highlightedSlot, selectedSlot);
|
DrawSubInventory(spriteBatch, selectedSlot);
|
||||||
}
|
|
||||||
|
|
||||||
slotRect.Width = rectWidth;
|
if (selectedSlot > -1 &&
|
||||||
slotRect.Height = rectHeight;
|
!slots[selectedSlot].IsHighlighted &&
|
||||||
|
(draggingItem == null || draggingItem.Container != Items[selectedSlot]))
|
||||||
if (!string.IsNullOrWhiteSpace(toolTip))
|
|
||||||
{
|
|
||||||
DrawToolTip(spriteBatch, toolTip, highlightedSlot);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (draggingItem == null) return;
|
|
||||||
|
|
||||||
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;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!draggingItemSlot.Contains(PlayerInput.MousePosition))
|
|
||||||
{
|
|
||||||
if (PlayerInput.LeftButtonHeld())
|
|
||||||
{
|
{
|
||||||
slotRect.X = (int)PlayerInput.MousePosition.X - slotRect.Width / 2;
|
selectedSlot = -1;
|
||||||
slotRect.Y = (int)PlayerInput.MousePosition.Y - slotRect.Height / 2;
|
|
||||||
|
|
||||||
DrawSlot(spriteBatch, slotRect, draggingItem, false, false);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
DropItem(draggingItem);
|
|
||||||
|
|
||||||
new NetworkEvent(NetworkEventType.DropItem, draggingItem.ID, true);
|
|
||||||
//draggingItem = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -214,10 +214,13 @@ namespace Barotrauma.Items.Components
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void UpdateHUD(Character character)
|
||||||
|
{
|
||||||
|
Inventory.Update((float)Timing.Step);
|
||||||
|
}
|
||||||
|
|
||||||
public override void DrawHUD(SpriteBatch spriteBatch, Character character)
|
public override void DrawHUD(SpriteBatch spriteBatch, Character character)
|
||||||
{
|
{
|
||||||
if (!drawInventory && false) return;
|
|
||||||
|
|
||||||
Inventory.Draw(spriteBatch);
|
Inventory.Draw(spriteBatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,43 @@ using Microsoft.Xna.Framework.Input;
|
|||||||
using Barotrauma.Networking;
|
using Barotrauma.Networking;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Barotrauma.Items.Components;
|
||||||
|
|
||||||
namespace Barotrauma
|
namespace Barotrauma
|
||||||
{
|
{
|
||||||
|
class InventorySlot
|
||||||
|
{
|
||||||
|
public Rectangle Rect;
|
||||||
|
|
||||||
|
public GUIComponent.ComponentState State;
|
||||||
|
|
||||||
|
public bool Disabled;
|
||||||
|
|
||||||
|
public bool IsHighlighted
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return State == GUIComponent.ComponentState.Hover;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color Color;
|
||||||
|
|
||||||
|
public InventorySlot(Rectangle rect)
|
||||||
|
{
|
||||||
|
Rect = rect;
|
||||||
|
|
||||||
|
State = GUIComponent.ComponentState.None;
|
||||||
|
|
||||||
|
Color = Color.White * 0.4f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Inventory
|
class Inventory
|
||||||
{
|
{
|
||||||
|
public static InventorySlot draggingSlot;
|
||||||
public static Item draggingItem;
|
public static Item draggingItem;
|
||||||
|
|
||||||
public static Item doubleClickedItem;
|
public static Item doubleClickedItem;
|
||||||
|
|
||||||
public readonly Entity Owner;
|
public readonly Entity Owner;
|
||||||
@@ -31,6 +62,8 @@ namespace Barotrauma
|
|||||||
|
|
||||||
protected int selectedSlot = -1;
|
protected int selectedSlot = -1;
|
||||||
|
|
||||||
|
protected InventorySlot[] slots;
|
||||||
|
|
||||||
public Item[] Items;
|
public Item[] Items;
|
||||||
|
|
||||||
public bool Locked;
|
public bool Locked;
|
||||||
@@ -46,6 +79,23 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
public Inventory(Entity owner, int capacity, Vector2? centerPos = null, int slotsPerRow=5)
|
||||||
{
|
{
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
@@ -116,7 +166,7 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PutItem(Item item, int i, bool createNetworkEvent, bool removeItem = true)
|
protected virtual void PutItem(Item item, int i, bool createNetworkEvent, bool removeItem = true)
|
||||||
{
|
{
|
||||||
if (Owner == null) return;
|
if (Owner == null) return;
|
||||||
|
|
||||||
@@ -143,7 +193,7 @@ namespace Barotrauma
|
|||||||
return Items.FirstOrDefault(i => i != null && (i.Name == itemName || i.HasTag(itemName)));
|
return Items.FirstOrDefault(i => i != null && (i.Name == itemName || i.HasTag(itemName)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveItem(Item item)
|
public virtual void RemoveItem(Item item)
|
||||||
{
|
{
|
||||||
if (item == null) return;
|
if (item == null) return;
|
||||||
|
|
||||||
@@ -161,72 +211,90 @@ namespace Barotrauma
|
|||||||
item.Drop(null, false);
|
item.Drop(null, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//public void DropItem(int i)
|
|
||||||
//{
|
protected virtual void CreateSlots()
|
||||||
// items[i].Drop();
|
|
||||||
// items[i] = null;
|
|
||||||
//}
|
|
||||||
|
|
||||||
public virtual void Draw(SpriteBatch spriteBatch)
|
|
||||||
{
|
{
|
||||||
string toolTip = "";
|
slots = new InventorySlot[capacity];
|
||||||
|
|
||||||
int rectWidth = 40, rectHeight = 40;
|
int rectWidth = 40, rectHeight = 40;
|
||||||
|
|
||||||
Rectangle highlightedSlot = Rectangle.Empty;
|
|
||||||
int spacing = 10;
|
int spacing = 10;
|
||||||
|
|
||||||
int rows = (int)Math.Ceiling((double)capacity / slotsPerRow);
|
int rows = (int)Math.Ceiling((double)capacity / slotsPerRow);
|
||||||
|
|
||||||
int startX = (int)centerPos.X - (rectWidth * slotsPerRow + spacing * (slotsPerRow - 1)) / 2;
|
int startX = (int)centerPos.X - (rectWidth * slotsPerRow + spacing * (slotsPerRow - 1)) / 2;
|
||||||
int startY = (int)centerPos.Y - rows * (spacing + rectHeight);
|
int startY = (int)centerPos.Y - rows * (spacing + rectHeight);
|
||||||
|
|
||||||
Rectangle slotRect = new Rectangle(startX, startY, rectWidth, rectHeight);
|
Rectangle slotRect = new Rectangle(startX, startY, rectWidth, rectHeight);
|
||||||
Rectangle draggingItemSlot = slotRect;
|
|
||||||
|
|
||||||
selectedSlot = -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < capacity; i++)
|
for (int i = 0; i < capacity; i++)
|
||||||
{
|
{
|
||||||
slotRect.X = startX + (rectWidth + spacing) * (i % slotsPerRow);
|
slotRect.X = startX + (rectWidth + spacing) * (i % slotsPerRow) + (int)DrawOffset.X;
|
||||||
slotRect.Y = startY + (rectHeight + spacing) * ((int)Math.Floor((double)i / slotsPerRow));
|
slotRect.Y = startY + (rectHeight + spacing) * ((int)Math.Floor((double)i / slotsPerRow)) + (int)DrawOffset.Y;
|
||||||
|
|
||||||
if (draggingItem == Items[i]) draggingItemSlot = slotRect;
|
slots[i] = new InventorySlot(slotRect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UpdateSlot(spriteBatch, slotRect, i, Items[i], false);
|
public virtual void Update(float deltaTime)
|
||||||
if (slotRect.Contains(PlayerInput.MousePosition) && Items[i] != null)
|
{
|
||||||
{
|
if (slots == null) CreateSlots();
|
||||||
highlightedSlot = slotRect;
|
|
||||||
toolTip = GameMain.DebugDraw ? Items[i].ToString() : Items[i].Name;
|
for (int i = 0; i < capacity; i++)
|
||||||
}
|
{
|
||||||
|
if (slots[i].Disabled) continue;
|
||||||
|
UpdateSlot(slots[i], i, Items[i], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (draggingItem != null && !draggingItemSlot.Contains(PlayerInput.MousePosition) && draggingItem.Container == this.Owner)
|
|
||||||
{
|
|
||||||
if (PlayerInput.LeftButtonHeld())
|
|
||||||
{
|
|
||||||
slotRect.X = (int)PlayerInput.MousePosition.X - slotRect.Width / 2;
|
|
||||||
slotRect.Y = (int)PlayerInput.MousePosition.Y - slotRect.Height / 2;
|
|
||||||
//GUI.DrawRectangle(spriteBatch, rect, Color.White, true);
|
|
||||||
//draggingItem.sprite.Draw(spriteBatch, new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), Color.White);
|
|
||||||
|
|
||||||
DrawSlot(spriteBatch, slotRect, draggingItem, false, false);
|
if (draggingItem != null && !draggingSlot.Rect.Contains(PlayerInput.MousePosition) && draggingItem.ParentInventory == this)
|
||||||
}
|
{
|
||||||
else
|
if (!PlayerInput.LeftButtonHeld())
|
||||||
{
|
{
|
||||||
if (Owner!=null)
|
if (Owner != null)
|
||||||
{
|
{
|
||||||
new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true);
|
new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
DropItem(draggingItem);
|
DropItem(draggingItem);
|
||||||
//draggingItem = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(toolTip))
|
}
|
||||||
|
|
||||||
|
public virtual void Draw(SpriteBatch spriteBatch)
|
||||||
|
{
|
||||||
|
string toolTip = "";
|
||||||
|
|
||||||
|
if (slots == null) CreateSlots();
|
||||||
|
|
||||||
|
for (int i = 0; i < capacity; i++)
|
||||||
{
|
{
|
||||||
DrawToolTip(spriteBatch, toolTip, highlightedSlot);
|
if (slots[i].Disabled) continue;
|
||||||
|
|
||||||
|
//don't draw the slot if dragged an item out of it
|
||||||
|
bool drawItem = draggingItem == null || draggingItem != Items[i] || slots[i].IsHighlighted;
|
||||||
|
|
||||||
|
DrawSlot(spriteBatch, slots[i], Items[i], drawItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (draggingItem != null &&
|
||||||
|
!draggingSlot.Rect.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++)
|
||||||
|
{
|
||||||
|
if (slots[i].IsHighlighted && !slots[i].Disabled)
|
||||||
|
{
|
||||||
|
DrawToolTip(spriteBatch, toolTip, slots[i].Rect);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,12 +319,21 @@ namespace Barotrauma
|
|||||||
1.0f, SpriteEffects.None, 0.0f);
|
1.0f, SpriteEffects.None, 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UpdateSlot(SpriteBatch spriteBatch, Rectangle rect, int slotIndex, Item item, bool isSubSlot, float alpha = 0.4f, bool drawItem=true)
|
protected void UpdateSlot(InventorySlot slot, int slotIndex, Item item, bool isSubSlot)
|
||||||
{
|
{
|
||||||
bool mouseOn = rect.Contains(PlayerInput.MousePosition) && !Locked;
|
bool mouseOn = slot.Rect.Contains(PlayerInput.MousePosition) && !Locked;
|
||||||
|
|
||||||
if (mouseOn)
|
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)
|
if (!isSubSlot && selectedSlot == -1)
|
||||||
{
|
{
|
||||||
@@ -265,9 +342,10 @@ namespace Barotrauma
|
|||||||
|
|
||||||
if (draggingItem == null)
|
if (draggingItem == null)
|
||||||
{
|
{
|
||||||
if (PlayerInput.LeftButtonHeld() && selectedSlot == slotIndex)
|
if (PlayerInput.LeftButtonHeld())
|
||||||
{
|
{
|
||||||
draggingItem = item;
|
draggingItem = Items[slotIndex];
|
||||||
|
draggingSlot = slot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (PlayerInput.LeftButtonReleased())
|
else if (PlayerInput.LeftButtonReleased())
|
||||||
@@ -280,22 +358,38 @@ namespace Barotrauma
|
|||||||
//selectedSlot = slotIndex;
|
//selectedSlot = slotIndex;
|
||||||
TryPutItem(draggingItem, slotIndex, true, true);
|
TryPutItem(draggingItem, slotIndex, true, true);
|
||||||
draggingItem = null;
|
draggingItem = null;
|
||||||
|
draggingSlot = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawSlot(spriteBatch, rect, (draggingItem == item && !mouseOn) ? null : item, mouseOn && selectedSlot == slotIndex, isSubSlot, alpha, drawItem);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawSubInventory(SpriteBatch spriteBatch, Rectangle rect, int slotIndex)
|
public void UpdateSubInventory(float deltaTime, int slotIndex)
|
||||||
{
|
{
|
||||||
var item = Items[slotIndex];
|
var item = Items[slotIndex];
|
||||||
|
if (item == null) return;
|
||||||
|
|
||||||
selectedSlot = -1;
|
var container = item.GetComponent<ItemContainer>();
|
||||||
|
if (container == null) return;
|
||||||
|
|
||||||
int itemCapacity = item == null ? 0 : item.Capacity;
|
if (container.Inventory.slots == null) container.Inventory.CreateSlots();
|
||||||
if (itemCapacity == 0) return;
|
|
||||||
|
slots[slotIndex].State = GUIComponent.ComponentState.Hover;
|
||||||
|
|
||||||
|
container.Inventory.Update(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawSubInventory(SpriteBatch spriteBatch, 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;
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
System.Diagnostics.Debug.Assert(slotIndex >= 0 && slotIndex < Items.Length);
|
System.Diagnostics.Debug.Assert(slotIndex >= 0 && slotIndex < Items.Length);
|
||||||
@@ -303,59 +397,37 @@ namespace Barotrauma
|
|||||||
if (slotIndex < 0 || slotIndex >= Items.Length) return;
|
if (slotIndex < 0 || slotIndex >= Items.Length) return;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Rectangle containerRect = new Rectangle(rect.X - 5, rect.Y - (40 + 10) * itemCapacity - 5,
|
var slot = slots[slotIndex];
|
||||||
rect.Width + 10, rect.Height + (40 + 10) * itemCapacity + 10);
|
Rectangle containerRect = 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 = rect;
|
Rectangle subRect = slot.Rect;
|
||||||
subRect.Height = 40;
|
subRect.Height = 40;
|
||||||
|
|
||||||
selectedSlot = containerRect.Contains(PlayerInput.MousePosition) && !Locked ? slotIndex : -1;
|
GUI.DrawRectangle(spriteBatch, new Rectangle(containerRect.X, containerRect.Y, containerRect.Width, containerRect.Height - slot.Rect.Height - 5), Color.Black * 0.8f, true);
|
||||||
|
|
||||||
GUI.DrawRectangle(spriteBatch, new Rectangle(containerRect.X, containerRect.Y, containerRect.Width, containerRect.Height - 50), Color.Black * 0.8f, true);
|
|
||||||
GUI.DrawRectangle(spriteBatch, containerRect, Color.White);
|
GUI.DrawRectangle(spriteBatch, containerRect, Color.White);
|
||||||
|
|
||||||
Item[] containedItems = null;
|
for (int i = 0; i < itemCapacity; i++)
|
||||||
if (Items[slotIndex] != null) containedItems = Items[slotIndex].ContainedItems;
|
|
||||||
|
|
||||||
string toolTip = "";
|
|
||||||
Rectangle highlightedRect = Rectangle.Empty;
|
|
||||||
|
|
||||||
if (containedItems != null)
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < itemCapacity; i++)
|
subRect.Y = subRect.Y - subRect.Height - 10;
|
||||||
{
|
container.Inventory.slots[i].Rect = subRect;
|
||||||
subRect.Y = subRect.Y - subRect.Height - 10;
|
|
||||||
highlightedRect = subRect;
|
|
||||||
UpdateSlot(spriteBatch, subRect, selectedSlot, i < containedItems.Length ? containedItems[i] : null, true);
|
|
||||||
|
|
||||||
if (i >= containedItems.Length || containedItems[i] == null) continue;
|
|
||||||
|
|
||||||
if (highlightedRect.Contains(PlayerInput.MousePosition))
|
|
||||||
{
|
|
||||||
if (GameMain.DebugDraw)
|
|
||||||
{
|
|
||||||
toolTip = containedItems[i].ToString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
toolTip = string.IsNullOrEmpty(containedItems[i].Description) ?
|
|
||||||
containedItems[i].Name :
|
|
||||||
containedItems[i].Name + '\n' + containedItems[i].Description;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(toolTip)) DrawToolTip(spriteBatch, toolTip, highlightedRect);
|
container.Inventory.Draw(spriteBatch);
|
||||||
|
|
||||||
|
if (!containerRect.Contains(PlayerInput.MousePosition))
|
||||||
|
{
|
||||||
|
if (draggingItem == null || draggingItem.Container != item) selectedSlot = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void DrawSlot(SpriteBatch spriteBatch, Rectangle rect, Item item, bool isHighLighted, bool isSubSlot, float alpha=0.4f, bool drawItem=true)
|
protected void DrawSlot(SpriteBatch spriteBatch, InventorySlot slot, Item item, bool drawItem=true)
|
||||||
{
|
{
|
||||||
GUI.DrawRectangle(spriteBatch, rect, (isHighLighted ? Color.Red : Color.White) * alpha*0.75f, true);
|
Rectangle rect = slot.Rect;
|
||||||
|
|
||||||
|
GUI.DrawRectangle(spriteBatch, rect, (slot.IsHighlighted ? Color.Red * 0.4f : slot.Color), true);
|
||||||
|
|
||||||
if (item != null)
|
if (item != null && drawItem)
|
||||||
{
|
{
|
||||||
if (item.Condition < 100.0f)
|
if (item.Condition < 100.0f)
|
||||||
{
|
{
|
||||||
@@ -365,21 +437,17 @@ namespace Barotrauma
|
|||||||
Color.Lerp(Color.Red, Color.Green, item.Condition / 100.0f)*0.8f, true);
|
Color.Lerp(Color.Red, Color.Green, item.Condition / 100.0f)*0.8f, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isHighLighted)
|
var containedItems = item.ContainedItems;
|
||||||
|
if (containedItems != null && containedItems.Length == 1 && containedItems[0].Condition < 100.0f)
|
||||||
{
|
{
|
||||||
var containedItems = item.ContainedItems;
|
GUI.DrawRectangle(spriteBatch, new Rectangle(rect.X, rect.Y, rect.Width, 8), Color.Black*0.8f, true);
|
||||||
if (containedItems != null && containedItems.Length == 1 && containedItems[0].Condition < 100.0f)
|
GUI.DrawRectangle(spriteBatch,
|
||||||
{
|
new Rectangle(rect.X, rect.Y, (int)(rect.Width * containedItems[0].Condition / 100.0f), 8),
|
||||||
GUI.DrawRectangle(spriteBatch, new Rectangle(rect.X, rect.Y, rect.Width, 8), Color.Black*0.8f, true);
|
Color.Lerp(Color.Red, Color.Green, containedItems[0].Condition / 100.0f)*0.8f, true);
|
||||||
GUI.DrawRectangle(spriteBatch,
|
}
|
||||||
new Rectangle(rect.X, rect.Y, (int)(rect.Width * containedItems[0].Condition / 100.0f), 8),
|
|
||||||
Color.Lerp(Color.Red, Color.Green, containedItems[0].Condition / 100.0f)*0.8f, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GUI.DrawRectangle(spriteBatch, rect, (slot.IsHighlighted ? Color.Red * 0.4f : slot.Color), false);
|
||||||
GUI.DrawRectangle(spriteBatch, rect, (isHighLighted ? Color.Red : Color.White) * alpha, false);
|
|
||||||
|
|
||||||
if (item == null || !drawItem) return;
|
if (item == null || !drawItem) return;
|
||||||
|
|
||||||
|
|||||||
@@ -152,6 +152,14 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
|
|
||||||
GameMain.World.Step((float)deltaTime);
|
GameMain.World.Step((float)deltaTime);
|
||||||
|
|
||||||
|
|
||||||
|
if (!PlayerInput.LeftButtonHeld())
|
||||||
|
{
|
||||||
|
Inventory.draggingSlot = null;
|
||||||
|
Inventory.draggingItem = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Draw(double deltaTime, GraphicsDevice graphics, SpriteBatch spriteBatch)
|
public override void Draw(double deltaTime, GraphicsDevice graphics, SpriteBatch spriteBatch)
|
||||||
@@ -189,8 +197,6 @@ namespace Barotrauma
|
|||||||
|
|
||||||
GUI.Draw((float)deltaTime, spriteBatch, cam);
|
GUI.Draw((float)deltaTime, spriteBatch, cam);
|
||||||
|
|
||||||
if (!PlayerInput.LeftButtonHeld()) Inventory.draggingItem = null;
|
|
||||||
|
|
||||||
spriteBatch.End();
|
spriteBatch.End();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user