use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.
the class HUDScreenLayer method onDraw.
@Override
public void onDraw(Canvas canvas) {
for (HUDElement element : elementsLookup.values()) {
int minX = TeraMath.floorToInt(element.region.minX() * canvas.size().x);
int minY = TeraMath.floorToInt(element.region.minY() * canvas.size().y);
int sizeX = TeraMath.floorToInt(element.region.width() * canvas.size().x);
int sizeY = TeraMath.floorToInt(element.region.height() * canvas.size().y);
Rect2i region = Rect2i.createFromMinAndSize(minX, minY, sizeX, sizeY);
canvas.drawWidget(element.widget, region);
}
}
use of org.terasology.math.geom.Rect2i 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.Rect2i in project Terasology by MovingBlocks.
the class ScrollableArea method layoutWithJustVertical.
private void layoutWithJustVertical(Canvas canvas, Vector2i contentSize, int fullWidth, int availableHeight, int scrollbarWidth) {
int availableWidth = fullWidth - scrollbarWidth;
boolean atBottom = verticalBar.getRange() == verticalBar.getValue();
Rect2i contentRegion = Rect2i.createFromMinAndSize(0, 0, availableWidth, availableHeight);
verticalBar.setRange(contentSize.y - contentRegion.height());
if ((stickToBottom && atBottom) || moveToBottomPending) {
verticalBar.setValue(verticalBar.getRange());
moveToBottomPending = false;
}
if (moveToTopPending) {
verticalBar.setValue(0);
moveToTopPending = false;
}
canvas.addInteractionRegion(scrollListener);
canvas.drawWidget(verticalBar, Rect2i.createFromMinAndSize(availableWidth, 0, scrollbarWidth, availableHeight));
try (SubRegion ignored = canvas.subRegion(contentRegion, true)) {
canvas.drawWidget(content, Rect2i.createFromMinAndSize(0, -verticalBar.getValue(), availableWidth, contentSize.y));
}
}
use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.
the class TextureUtil method convertToImage.
public static BufferedImage convertToImage(TextureRegion textureRegion) {
final int width = textureRegion.getWidth();
final int height = textureRegion.getHeight();
final Rect2i pixelRegion = textureRegion.getPixelRegion();
final Texture texture = textureRegion.getTexture();
ByteBuffer textureBytes = texture.getData().getBuffers()[0];
int stride = texture.getWidth() * 4;
int posX = pixelRegion.minX();
int posY = pixelRegion.minY();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4));
int g = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 1));
int b = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 2));
int a = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 3));
int argb = (a << 24) + (r << 16) + (g << 8) + b;
image.setRGB(x, y, argb);
}
}
return image;
}
use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.
the class ResettableUIText method onDraw.
@Override
public void onDraw(Canvas canvas) {
Rect2i clearButtonRegion = Rect2i.createFromMinAndSize(0, 0, 30, canvas.size().y);
lastWidth = canvas.size().x - clearButtonRegion.size().x;
if (isEnabled()) {
canvas.addInteractionRegion(interactionListener, Rect2i.createFromMinAndMax(0, 0, canvas.size().x, canvas.size().y));
canvas.addInteractionRegion(clearInteractionListener, Rect2i.createFromMinAndMax(canvas.size().x, 0, canvas.size().x + clearButtonRegion.size().x, canvas.size().y));
}
drawAll(canvas, canvas.size().x - clearButtonRegion.size().x);
}
Aggregations