예제 #1
0
void SGridPanel::ComputeDesiredCellSizes( TArray<float>& OutColumns, TArray<float>& OutRows ) const
{
	FMemory::Memzero( OutColumns.GetData(), OutColumns.Num() * sizeof(float) );
	FMemory::Memzero( OutRows.GetData(), OutRows.Num() * sizeof(float) );

	for (int32 SlotIndex=0; SlotIndex<Slots.Num(); ++SlotIndex)
	{
		const FSlot& CurSlot = Slots[SlotIndex];
		if (CurSlot.Widget->GetVisibility() != EVisibility::Collapsed)
		{
			// The slots wants to be as big as its content along with the required padding.
			const FVector2D SlotDesiredSize = CurSlot.Widget->GetDesiredSize() + CurSlot.SlotPadding.Get().GetDesiredSize();

			// If the slot has a (colspan,rowspan) of (1,1) it will only affect that slot.
			// For larger spans, the slots size will be evenly distributed across all the affected slots.
			const FVector2D SizeContribution( SlotDesiredSize.X / CurSlot.ColumnSpanParam, SlotDesiredSize.Y / CurSlot.RowSpanParam );

			// Distribute the size contributions over all the columns and rows that this slot spans
			DistributeSizeContributions( SizeContribution.X, OutColumns, CurSlot.ColumnParam, CurSlot.ColumnParam + CurSlot.ColumnSpanParam );
			DistributeSizeContributions( SizeContribution.Y, OutRows, CurSlot.RowParam, CurSlot.RowParam + CurSlot.RowSpanParam );
		}
	}
}
void SResponsiveGridPanel::ComputeDesiredCellSizes(float AvailableWidth, TArray<float>& OutColumns, TArray<float>& OutRows, TArray<float>& OutRowToSlot) const
{
	FMemory::Memzero(OutColumns.GetData(), OutColumns.Num() * sizeof(float));
	FMemory::Memzero(OutRows.GetData(), OutRows.Num() * sizeof(float));

	int32 ColumnsSoFar = 0;
	int32 CurrentRow = INDEX_NONE;
	int32 LastRowParam = INDEX_NONE;
	for (int32 SlotIndex = 0; SlotIndex < Slots.Num(); ++SlotIndex)
	{
		const FSlot& CurSlot = Slots[SlotIndex];
		if (CurSlot.GetWidget()->GetVisibility() != EVisibility::Collapsed)
		{
			// Find the appropriate column layout for the slot
			FSlot::FColumnLayout ColumnLayout;
			ColumnLayout.Span = TotalColumns;
			ColumnLayout.Offset = 0;

			for (int32 Index = CurSlot.ColumnLayouts.Num() - 1; Index >= 0; Index--)
			{
				if (CurSlot.ColumnLayouts[Index].LayoutSize < AvailableWidth)
				{
					ColumnLayout = CurSlot.ColumnLayouts[Index];
					break;
				}
			}

			if (ColumnLayout.Span == 0)
			{
				continue;
			}

			if (CurSlot.RowParam != LastRowParam)
			{
				ColumnsSoFar = 0;
				LastRowParam = CurSlot.RowParam;
				++CurrentRow;

				OutRowToSlot.AddZeroed((CurrentRow + 1) - OutRowToSlot.Num());
				OutRowToSlot[CurrentRow] = CurSlot.RowParam;
			}

			// The slots want to be as big as its content along with the required padding.
			const FVector2D SlotDesiredSize = CurSlot.GetWidget()->GetDesiredSize() + CurSlot.SlotPadding.Get().GetDesiredSize();

			// If the slot has a (colspan,rowspan) of (1,1) it will only affect that cell.
			// For larger spans, the slots size will be evenly distributed across all the affected cells.
			const FVector2D SizeContribution(SlotDesiredSize.X / ColumnLayout.Span, SlotDesiredSize.Y);

			int32 StartColumnIndex = ColumnsSoFar + ColumnLayout.Offset;
			int32 EndColumnIndex = StartColumnIndex + ColumnLayout.Span;
			ColumnsSoFar = FMath::Max(EndColumnIndex, ColumnsSoFar);

			if (ColumnsSoFar > TotalColumns)
			{
				StartColumnIndex = 0;
				EndColumnIndex = StartColumnIndex + ColumnLayout.Span;
				ColumnsSoFar = EndColumnIndex - StartColumnIndex;
				++CurrentRow;

				OutRowToSlot.AddZeroed((CurrentRow + 1) - OutRowToSlot.Num());
				OutRowToSlot[CurrentRow] = CurSlot.RowParam;
			}

			OutColumns.AddZeroed(FMath::Max(0, ColumnsSoFar - OutColumns.Num()));
			OutRows.AddZeroed((CurrentRow + 1) - OutRows.Num());

			// Distribute the size contributions over all the columns and rows that this slot spans
			DistributeSizeContributions(SizeContribution.X, OutColumns, StartColumnIndex, EndColumnIndex);
			DistributeSizeContributions(SizeContribution.Y, OutRows, CurrentRow, CurrentRow + 1);
		}
	}
}