use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.
the class StringTextIconRenderer method draw.
@Override
public void draw(T value, Canvas canvas) {
// Drawing the icon
TextureRegion texture = getTexture(value);
if (texture != null) {
if (marginTop + texture.getHeight() + marginBottom > canvas.size().y) {
// Icon does not fit within the canvas - vertically shrinking it
int iconHeight = canvas.size().y - marginTop - marginBottom;
canvas.drawTexture(texture, Rect2i.createFromMinAndSize(marginLeft, marginTop, texture.getWidth(), iconHeight));
} else {
// Icon fits within the canvas - vertically centering it
int iconVerticalPosition = (canvas.size().y - texture.getHeight()) / 2;
canvas.drawTexture(texture, Rect2i.createFromMinAndSize(marginLeft, iconVerticalPosition, texture.getWidth(), texture.getHeight()));
}
}
// Drawing the text, adjusting for icon width
String text = getString(value);
int iconWidth;
if (texture != null) {
iconWidth = marginLeft + texture.getWidth() + marginRight;
} else {
iconWidth = 0;
}
Rect2i textRegion = Rect2i.createFromMinAndSize(iconWidth, 0, canvas.getRegion().width() - iconWidth, canvas.getRegion().height());
canvas.drawText(text, textRegion);
}
use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.
the class FacetLayerPreview method render.
@Override
public ByteBuffer render(TextureData texData, int scale, ProgressListener progressListener) throws InterruptedException {
int width = texData.getWidth();
int height = texData.getWidth();
final int offX = -width * scale / 2;
final int offY = -height * scale / 2;
// trigger building the World now
worldGenerator.getWorld();
Rect2i worldArea = Rect2i.createFromMinAndSize(offX, offY, width * scale, height * scale);
Rect2i tileArea = worldToTileArea(worldArea);
AtomicInteger tilesComplete = new AtomicInteger(0);
int tileCount = tileArea.area();
int[] masks = colorModel.getMasks();
DataBufferInt imageBuffer = new DataBufferInt(width * height);
WritableRaster raster = Raster.createPackedRaster(imageBuffer, width, height, width, masks, null);
BufferedImage view = new BufferedImage(colorModel, raster, false, null);
Graphics2D g = view.createGraphics();
g.scale(1f / scale, 1f / scale);
g.translate(-offX, -offY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
Map<ImmutableVector2i, Future<BufferedImage>> imageFutures = new HashMap<>(tileCount);
for (int z = tileArea.minY(); z < tileArea.maxY(); z++) {
for (int x = tileArea.minX(); x < tileArea.maxX(); x++) {
ImmutableVector2i pos = new ImmutableVector2i(x, z);
imageFutures.put(pos, threadPool.submit(() -> {
Region createRegion = createRegion(pos);
BufferedImage image = rasterize(createRegion);
if (progressListener != null) {
progressListener.onProgress(tilesComplete.incrementAndGet() / (float) tileCount);
}
return image;
}));
}
}
for (int z = tileArea.minY(); z < tileArea.maxY(); z++) {
for (int x = tileArea.minX(); x < tileArea.maxX(); x++) {
ImmutableVector2i pos = new ImmutableVector2i(x, z);
try {
BufferedImage tileImage = imageFutures.get(pos).get();
g.drawImage(tileImage, x * TILE_SIZE_X, z * TILE_SIZE_Y, null);
} catch (ExecutionException e) {
logger.warn("Could not rasterize tile {}", pos, e);
}
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
}
}
// draw coordinate lines through 0 / 0
g.setColor(Color.GRAY);
g.drawLine(worldArea.minX(), 0, worldArea.maxX(), 0);
g.drawLine(0, worldArea.minY(), 0, worldArea.maxY());
g.dispose();
int[] data = imageBuffer.getData();
ByteBuffer byteBuffer = texData.getBuffers()[0];
byteBuffer.asIntBuffer().put(data);
return byteBuffer;
}
use of org.terasology.math.geom.Rect2i 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.Rect2i in project Terasology by MovingBlocks.
the class RowLayout method onDraw.
/**
* Draws the widgets contained in this layout's widget list,
* according to the widths calculated in {@link #calcWidths(Canvas)}.
* This is called every frame.
*
* @param canvas The {@link Canvas} on which this {@code RowLayout} is drawn
*/
@Override
public void onDraw(Canvas canvas) {
TIntList widths = calcWidths(canvas);
if (!contents.isEmpty()) {
int xOffset = 0;
for (int i = 0; i < contents.size(); ++i) {
int itemWidth = widths.get(i);
Rect2i region = Rect2i.createFromMinAndSize(xOffset, 0, itemWidth, canvas.size().y);
canvas.drawWidget(contents.get(i), region);
xOffset += itemWidth;
xOffset += horizontalSpacing;
}
}
}
use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.
the class ScrollableArea method layoutWithJustHorizontal.
private void layoutWithJustHorizontal(Canvas canvas, Vector2i contentSize, int availableWidth, int fullHeight, int scrollbarHeight) {
int availableHeight = fullHeight - scrollbarHeight;
Rect2i contentRegion = Rect2i.createFromMinAndSize(0, 0, availableWidth, availableHeight);
horizontalBar.setRange(contentSize.x - contentRegion.width());
canvas.addInteractionRegion(scrollListener);
canvas.drawWidget(horizontalBar, Rect2i.createFromMinAndSize(0, availableHeight, availableWidth, scrollbarHeight));
try (SubRegion ignored = canvas.subRegion(contentRegion, true)) {
canvas.drawWidget(content, Rect2i.createFromMinAndSize(-horizontalBar.getValue(), 0, contentSize.x, availableHeight));
}
}
Aggregations