use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class MultiRowLayout method onDraw.
@Override
public void onDraw(Canvas canvas) {
if (!widgetList.isEmpty()) {
Vector2i availableSize = canvas.size();
int numColumns = TeraMath.ceilToInt((float) widgetList.size() / rows);
if (numColumns > 0) {
availableSize.x -= horizontalSpacing * (numColumns - 1);
}
if (rows > 0) {
availableSize.y -= verticalSpacing * (rows - 1);
}
List<List<UIWidget>> columns = Lists.newArrayList(getColumnIterator());
List<ColumnInfo> columnInfos = Lists.newArrayList();
columnInfos.addAll(columns.stream().map(column -> calculateColumnSize(column, canvas, availableSize)).collect(Collectors.toList()));
int[] minHeights = new int[rows];
int minColumnHeight = 0;
int columnOffsetY = 0;
if (autoSizeRows) {
for (ColumnInfo column : columnInfos) {
for (int row = 0; row < column.widgetSizes.size(); row++) {
minHeights[row] = Math.max(minHeights[row], column.widgetSizes.get(row).getY());
}
}
for (int height : minHeights) {
minColumnHeight += height;
}
minColumnHeight += (rows - 1) * verticalSpacing;
columnOffsetY = (canvas.size().y - minColumnHeight) / 2;
} else {
minColumnHeight = canvas.size().y;
for (int i = 0; i < rows; ++i) {
minHeights[i] = TeraMath.floorToInt((minColumnHeight - (rows - 1) * verticalSpacing) * rowHeights[i]);
}
}
int columnOffsetX = 0;
int usedWidth = 0;
for (ColumnInfo column : columnInfos) {
usedWidth += column.width;
}
usedWidth += (columnInfos.size() - 1) * horizontalSpacing;
columnOffsetX = (canvas.size().x - usedWidth) / 2;
for (int columnIndex = 0; columnIndex < columns.size(); ++columnIndex) {
List<UIWidget> column = columns.get(columnIndex);
ColumnInfo columnInfo = columnInfos.get(columnIndex);
int cellOffsetY = columnOffsetY;
for (int i = 0; i < column.size(); ++i) {
UIWidget widget = column.get(i);
int columnWidth = columnInfo.width;
if (widget != null) {
Rect2i drawRegion = Rect2i.createFromMinAndSize(columnOffsetX, cellOffsetY, columnWidth, minHeights[i]);
canvas.drawWidget(widget, drawRegion);
}
cellOffsetY += minHeights[i] + verticalSpacing;
}
columnOffsetX += columnInfo.width + horizontalSpacing;
}
}
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class MultiRowLayout method calculateColumnSize.
private ColumnInfo calculateColumnSize(List<UIWidget> column, Canvas canvas, Vector2i areaHint) {
int availableHeight = areaHint.y - verticalSpacing * (rows - 1);
ColumnInfo columnInfo = new ColumnInfo();
for (int i = 0; i < rows && i < column.size(); ++i) {
UIWidget widget = column.get(i);
Vector2i cellSize = new Vector2i(areaHint.x, availableHeight);
if (!autoSizeRows) {
cellSize.y *= rowHeights[i];
}
if (widget != null) {
Vector2i contentSize = canvas.calculateRestrictedSize(widget, cellSize);
columnInfo.widgetSizes.add(contentSize);
columnInfo.width = Math.max(columnInfo.width, contentSize.x);
} else {
columnInfo.widgetSizes.add(new Vector2i(0, 0));
}
}
return columnInfo;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class RowLayout method calcWidths.
/**
* Calculates the widths of each of the widgets in this layout's widget list.
* Widths are first calculated for widgets with a relative width specified,
* followed by widgets which follow their content width.
* The remaining width is then split equally among the remaining widgets.
*
* @param canvas The {@link Canvas} on which this {@code RowLayout} is drawn
* @return A list of the widths of the widgets in this layout's widget list, in pixels
*/
private TIntList calcWidths(Canvas canvas) {
TIntList results = new TIntArrayList(contents.size());
if (contents.size() > 0) {
int width = canvas.size().x - horizontalSpacing * (contents.size() - 1);
int totalWidthUsed = 0;
int unprocessedWidgets = 0;
for (UIWidget widget : contents) {
RowLayoutHint hint = hints.get(widget);
if (hint != null) {
if (!hint.isUseContentWidth() && hint.getRelativeWidth() != 0) {
int elementWidth = TeraMath.floorToInt(hint.getRelativeWidth() * width);
results.add(elementWidth);
totalWidthUsed += elementWidth;
} else {
results.add(0);
unprocessedWidgets++;
}
} else {
results.add(0);
unprocessedWidgets++;
}
}
if (unprocessedWidgets > 0) {
int remainingWidthPerElement = (width - totalWidthUsed) / unprocessedWidgets;
for (int i = 0; i < results.size(); ++i) {
if (results.get(i) == 0) {
RowLayoutHint hint = hints.get(contents.get(i));
if (hint != null) {
if (hint.isUseContentWidth()) {
Vector2i contentSize = contents.get(i).getPreferredContentSize(canvas, new Vector2i(remainingWidthPerElement, canvas.size().y));
results.set(i, contentSize.x);
totalWidthUsed += contentSize.x;
unprocessedWidgets--;
}
}
}
}
}
if (unprocessedWidgets > 0) {
int remainingWidthPerElement = (width - totalWidthUsed) / unprocessedWidgets;
for (int i = 0; i < results.size(); ++i) {
if (results.get(i) == 0) {
results.set(i, remainingWidthPerElement);
}
}
}
}
return results;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class RowLayout method getPreferredContentSize.
/**
* Retrieves the preferred content size of this {@code RowLayout}.
* This is the minimum size this layout will take, given no space restrictions.
*
* @param canvas The {@code Canvas} on which this {@code RowLayout} is drawn
* @param areaHint A {@link Vector2i} representing the space available for widgets to be drawn in this layout
* @return A {@link Vector2i} representing the preferred content size of this {@code RowLayout}
*/
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
TIntList widths = calcWidths(canvas);
Vector2i result = new Vector2i(areaHint.x, 0);
for (int i = 0; i < contents.size(); ++i) {
Vector2i widgetSize = canvas.calculateRestrictedSize(contents.get(i), new Vector2i(TeraMath.floorToInt(widths.get(i)), areaHint.y));
result.y = Math.max(result.y, widgetSize.y);
}
return result;
}
use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.
the class SubSampledNoise2D method getSubset.
private float[] getSubset(float[] fullData, Rect2i fullRegion, Rect2i subRegion) {
if (subRegion.size().x != fullRegion.size().x || subRegion.size().y != fullRegion.size().y) {
float[] result = new float[subRegion.size().x * subRegion.size().y];
Vector2i offset = new Vector2i(subRegion.minX() - fullRegion.minX(), subRegion.minY() - fullRegion.minY());
for (int y = 0; y < subRegion.size().y; ++y) {
System.arraycopy(fullData, offset.getX() + fullRegion.size().x * (y + offset.getY()), result, subRegion.size().x * y, subRegion.size().x);
}
return result;
} else {
return fullData;
}
}
Aggregations