72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using Core;
|
|
using UI.DragAndDrop.Core;
|
|
using UnityEngine;
|
|
|
|
namespace UI.CardSystem
|
|
{
|
|
/// <summary>
|
|
/// Helper utility for shuffling draggable objects in a SlotContainer.
|
|
/// Moves objects to occupy the first available slots (0, 1, 2, etc.)
|
|
/// </summary>
|
|
public static class SlotContainerHelper
|
|
{
|
|
/// <summary>
|
|
/// Shuffles draggable objects to always occupy the first available slots.
|
|
/// Unassigns all objects from their current slots, then reassigns them starting from slot 0.
|
|
/// </summary>
|
|
/// <param name="container">The slot container holding the slots</param>
|
|
/// <param name="objects">List of draggable objects to shuffle</param>
|
|
/// <param name="animate">Whether to animate the movement</param>
|
|
public static void ShuffleToFront(SlotContainer container, List<DraggableObject> objects, bool animate = true)
|
|
{
|
|
if (container == null || objects == null || objects.Count == 0)
|
|
return;
|
|
|
|
Logging.Debug($"[SlotContainerHelper] Shuffling {objects.Count} objects to front slots");
|
|
|
|
// Unassign all objects from their current slots
|
|
foreach (var obj in objects)
|
|
{
|
|
if (obj.CurrentSlot != null)
|
|
{
|
|
obj.CurrentSlot.Vacate();
|
|
}
|
|
}
|
|
|
|
// Reassign objects to first N slots starting from slot 0
|
|
for (int i = 0; i < objects.Count; i++)
|
|
{
|
|
DraggableSlot targetSlot = FindSlotByIndex(container, i);
|
|
DraggableObject obj = objects[i];
|
|
|
|
if (targetSlot != null)
|
|
{
|
|
Logging.Debug($"[SlotContainerHelper] Assigning object to slot with SlotIndex {i}");
|
|
obj.AssignToSlot(targetSlot, animate);
|
|
}
|
|
else
|
|
{
|
|
Logging.Warning($"[SlotContainerHelper] Could not find slot with SlotIndex {i}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Find a slot by its SlotIndex property (not list position)
|
|
/// </summary>
|
|
private static DraggableSlot FindSlotByIndex(SlotContainer container, int slotIndex)
|
|
{
|
|
foreach (var slot in container.Slots)
|
|
{
|
|
if (slot.SlotIndex == slotIndex)
|
|
{
|
|
return slot;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|