use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class ColumnLayout method onDraw.
@Override
public void onDraw(Canvas canvas) {
if (!widgetList.isEmpty()) {
Vector2i availableSize = canvas.size();
int numRows = TeraMath.ceilToInt((float) widgetList.size() / columns);
if (numRows > 0) {
availableSize.y -= verticalSpacing * (numRows - 1);
}
if (columns > 0) {
availableSize.x -= horizontalSpacing * (columns - 1);
}
List<List<UIWidget>> rows = Lists.newArrayList(getRowIterator());
List<RowInfo> rowInfos = Lists.newArrayList();
rowInfos.addAll(rows.stream().map(row -> calculateRowSize(row, canvas, availableSize)).collect(Collectors.toList()));
int[] minWidths = new int[columns];
int minRowWidth = 0;
int rowOffsetX = 0;
if (autoSizeColumns) {
for (RowInfo row : rowInfos) {
for (int column = 0; column < row.widgetSizes.size(); column++) {
minWidths[column] = Math.max(minWidths[column], row.widgetSizes.get(column).getX());
}
}
for (int width : minWidths) {
minRowWidth += width;
}
minRowWidth += (columns - 1) * horizontalSpacing;
rowOffsetX = (canvas.size().x - minRowWidth) / 2;
} else {
minRowWidth = canvas.size().x;
for (int i = 0; i < columns; ++i) {
minWidths[i] = TeraMath.floorToInt((minRowWidth - (columns - 1) * horizontalSpacing) * columnWidths[i]);
}
}
int rowOffsetY = 0;
int usedHeight = 0;
for (RowInfo row : rowInfos) {
usedHeight += row.height;
}
usedHeight += (rowInfos.size() - 1) * verticalSpacing;
int excessHeight = canvas.size().y - usedHeight;
if (fillVerticalSpace) {
if (extendLast && numRows > 0) {
// give all the extra space to the last entry
rowInfos.get(numRows - 1).height += excessHeight;
} else {
// distribute extra height equally
int extraSpacePerRow = excessHeight / rowInfos.size();
for (RowInfo row : rowInfos) {
row.height += extraSpacePerRow;
}
}
} else {
rowOffsetY = excessHeight / 2;
}
for (int rowIndex = 0; rowIndex < rows.size(); ++rowIndex) {
List<UIWidget> row = rows.get(rowIndex);
RowInfo rowInfo = rowInfos.get(rowIndex);
int cellOffsetX = rowOffsetX;
for (int i = 0; i < row.size(); ++i) {
UIWidget widget = row.get(i);
int rowHeight = rowInfo.height;
if (widget != null) {
Rect2i drawRegion = Rect2i.createFromMinAndSize(cellOffsetX, rowOffsetY, minWidths[i], rowHeight);
canvas.drawWidget(widget, drawRegion);
}
cellOffsetX += minWidths[i] + horizontalSpacing;
}
rowOffsetY += rowInfo.height + verticalSpacing;
}
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class FlowLayout method getPreferredContentSize.
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
Vector2i result = new Vector2i();
int filledWidth = 0;
int filledHeight = 0;
for (UIWidget widget : contents) {
Vector2i size = canvas.calculatePreferredSize(widget);
if (filledWidth != 0 && filledWidth + size.x > sizeHint.x) {
result.x = Math.max(result.x, filledWidth);
result.y += filledHeight;
filledWidth = size.x;
filledHeight = size.y;
} else {
filledWidth += size.x;
filledHeight = Math.max(filledHeight, size.y);
}
}
result.x = Math.max(result.x, filledWidth);
result.y += filledHeight;
return result;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class FlowLayout method onDraw.
@Override
public void onDraw(Canvas canvas) {
int filledWidth = 0;
int filledHeight = 0;
int heightOffset = 0;
for (UIWidget widget : contents) {
Vector2i size = canvas.calculatePreferredSize(widget);
if (filledWidth != 0 && filledWidth + size.x > canvas.size().x) {
heightOffset += filledHeight;
filledWidth = 0;
filledHeight = 0;
}
canvas.drawWidget(widget, Rect2i.createFromMinAndSize(filledWidth, heightOffset, size.x, size.y));
filledWidth += size.x;
filledHeight = Math.max(filledHeight, size.y);
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class MultiRowLayout method getMaxContentSize.
@Override
public Vector2i getMaxContentSize(Canvas canvas) {
Iterator<List<UIWidget>> columns = getColumnIterator();
Vector2i size = new Vector2i();
int[] rowSizes = new int[rows];
while (columns.hasNext()) {
List<UIWidget> column = columns.next();
int columnWidth = 0;
for (int i = 0; i < column.size(); ++i) {
Vector2i maxSize = canvas.calculateMaximumSize(column.get(i));
rowSizes[i] = Math.max(rowSizes[i], maxSize.y);
columnWidth = Math.max(columnWidth, maxSize.x);
}
size.x = TeraMath.addClampAtMax(size.x, columnWidth);
if (columns.hasNext()) {
size.x = TeraMath.addClampAtMax(size.x, horizontalSpacing);
}
}
long height = 0;
for (int rowSize : rowSizes) {
height += rowSize;
}
if (!autoSizeRows) {
for (int i = 0; i < rows; ++i) {
height = Math.min(height, TeraMath.floorToInt(rowSizes[i] / rowHeights[i]));
}
}
height += verticalSpacing * (rows - 1);
size.y = (int) Math.min(Integer.MAX_VALUE, height);
return size;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class MultiRowLayout method getPreferredContentSize.
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
Vector2i availableSize = new Vector2i(areaHint);
int numColumns = TeraMath.ceilToInt((float) widgetList.size() / rows);
if (numColumns > 0) {
availableSize.x -= horizontalSpacing * (numColumns - 1);
}
if (rows > 0) {
availableSize.y -= verticalSpacing * (rows - 1);
}
Iterator<List<UIWidget>> columns = getColumnIterator();
Vector2i size = new Vector2i();
int[] rowSizes = new int[rows];
while (columns.hasNext()) {
List<UIWidget> column = columns.next();
ColumnInfo columnInfo = calculateColumnSize(column, canvas, availableSize);
size.x += columnInfo.width;
if (columns.hasNext()) {
size.x += horizontalSpacing;
}
for (int i = 0; i < columnInfo.widgetSizes.size(); ++i) {
rowSizes[i] = Math.max(rowSizes[i], columnInfo.widgetSizes.get(i).getY());
}
}
for (int rowSize : rowSizes) {
size.y += rowSize;
}
if (!autoSizeRows) {
for (int i = 0; i < rows; ++i) {
size.y = Math.max(size.y, TeraMath.floorToInt(rowSizes[i] / rowHeights[i]));
}
}
size.y += verticalSpacing * (rows - 1);
return size;
}
Aggregations