Highlighting inventory slots when moving items around, red highlight if an item can't be placed in a slot

This commit is contained in:
Regalis
2016-10-12 16:18:32 +03:00
parent c034e3ed00
commit 71d4df6300
3 changed files with 68 additions and 14 deletions

View File

@@ -264,10 +264,11 @@ namespace Barotrauma
}
else
{
sb.Draw(t, new Rectangle(rect.X, rect.Y, rect.Width, thickness), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
sb.Draw(t, new Rectangle(rect.X, rect.Y+rect.Height- thickness, rect.Width, thickness), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
sb.Draw(t, new Rectangle(rect.X + thickness, rect.Y, rect.Width - thickness * 2, thickness), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
sb.Draw(t, new Rectangle(rect.X + thickness, rect.Y + rect.Height - thickness, rect.Width - thickness * 2, thickness), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
sb.Draw(t, new Rectangle(rect.X, rect.Y, thickness, rect.Height), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
sb.Draw(t, new Rectangle(rect.X+rect.Width- thickness, rect.Y, thickness, rect.Height), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
sb.Draw(t, new Rectangle(rect.X + rect.Width - thickness, rect.Y, thickness, rect.Height), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
}
}

View File

@@ -169,7 +169,7 @@ namespace Barotrauma
if (allowedSlot.HasFlag(limbSlots[i]) && Items[i]!=null && Items[i]!=item)
{
free = false;
break;
if (slots != null) slots[i].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
}
}
@@ -238,10 +238,9 @@ namespace Barotrauma
//swapping the items failed -> move them back to where they were
TryPutItem(item, currentIndex, false, false);
TryPutItem(existingItem, index, false, false);
}
}
}
return combined;
}
@@ -293,17 +292,19 @@ namespace Barotrauma
protected override void CreateSlots()
{
slots = new InventorySlot[capacity];
if (slots == null) slots = new InventorySlot[capacity];
int rectWidth = 40, rectHeight = 40;
Rectangle slotRect = new Rectangle(0, 0, rectWidth, rectHeight);
for (int i = 0; i < capacity; i++)
{
slots[i].Disabled = false;
slotRect.X = (int)(SlotPositions[i].X + DrawOffset.X);
slotRect.Y = (int)(SlotPositions[i].Y + DrawOffset.Y);
slots[i] = new InventorySlot(slotRect);
slots[i].Rect = slotRect;
slots[i].Color = limbSlots[i] == InvSlotType.Any ? Color.White * 0.2f : Color.White * 0.4f;
}

View File

@@ -28,6 +28,9 @@ namespace Barotrauma
public Color Color;
public Color BorderHighlightColor;
private CoroutineHandle BorderHighlightCoroutine;
public InventorySlot(Rectangle rect)
{
Rect = rect;
@@ -36,6 +39,33 @@ namespace Barotrauma
Color = Color.White * 0.4f;
}
public void ShowBorderHighlight(Color color, float fadeInDuration, float fadeOutDuration)
{
if (BorderHighlightCoroutine != null)
{
BorderHighlightCoroutine = null;
}
BorderHighlightCoroutine = CoroutineManager.StartCoroutine(UpdateBorderHighlight(color, fadeInDuration, fadeOutDuration));
}
private IEnumerable<object> UpdateBorderHighlight(Color color, float fadeInDuration, float fadeOutDuration)
{
float t = 0.0f;
while (t < fadeInDuration + fadeOutDuration)
{
BorderHighlightColor = (t < fadeInDuration) ?
Color.Lerp(Color.Transparent, color, t / fadeInDuration) :
Color.Lerp(color, Color.Transparent, (t - fadeInDuration) / fadeOutDuration);
t += CoroutineManager.DeltaTime;
yield return CoroutineStatus.Running;
}
yield return CoroutineStatus.Success;
}
}
class Inventory
@@ -163,6 +193,7 @@ namespace Barotrauma
}
else
{
if (slots != null) slots[i].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
return false;
}
}
@@ -179,6 +210,9 @@ namespace Barotrauma
Items[i] = item;
item.ParentInventory = this;
if (slots != null) slots[i].ShowBorderHighlight(Color.White, 0.1f, 0.4f);
if (item.body != null)
{
item.body.Enabled = false;
@@ -323,7 +357,7 @@ namespace Barotrauma
Vector2 rectSize = textSize * 1.2f;
Vector2 pos = new Vector2(highlightedSlot.Right, highlightedSlot.Y-rectSize.Y);
pos.X = (int)pos.X;
pos.X = (int)(pos.X + 3);
pos.Y = (int)pos.Y;
GUI.DrawRectangle(spriteBatch, pos, rectSize, Color.Black * 0.8f, true);
@@ -370,10 +404,20 @@ namespace Barotrauma
doubleClickedItem = item;
}
//selectedSlot = slotIndex;
TryPutItem(draggingItem, slotIndex, true, true);
draggingItem = null;
draggingSlot = null;
if (draggingItem != Items[slotIndex])
{
//selectedSlot = slotIndex;
if (TryPutItem(draggingItem, slotIndex, true, true))
{
if (slots != null) slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f);
}
else
{
if (slots != null) slots[slotIndex].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
}
draggingItem = null;
draggingSlot = null;
}
}
}
}
@@ -471,6 +515,14 @@ namespace Barotrauma
GUI.DrawRectangle(spriteBatch, rect, (slot.IsHighlighted ? Color.Red * 0.4f : slot.Color), false);
if (slot.BorderHighlightColor != Color.Transparent)
{
Rectangle highlightRect = slot.Rect;
highlightRect.Inflate(3,3);
GUI.DrawRectangle(spriteBatch, highlightRect, slot.BorderHighlightColor, false, 0, 5);
}
if (item == null || !drawItem) return;
item.Sprite.Draw(spriteBatch, new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), item.Color);